首页 > 解决方案 > 为什么具有多个元素的python数组的真值是模棱两可的?

问题描述

我有两个问题。首先,我想绘制预测的生存函数图。代码如下:

from sksurv.preprocessing import OneHotEncoder
from sksurv.datasets import load_veterans_lung_cancer
from sksurv.linear_model import CoxPHSurvivalAnalysis
import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt


data_x, data_y = load_veterans_lung_cancer()
data_y


data_x_numeric = OneHotEncoder().fit_transform(data_x)



estimator = CoxPHSurvivalAnalysis()
estimator.fit(data_x_numeric, data_y)


x_new = pd.DataFrame.from_dict({
    1: [65, 0, 0, 1, 60, 1, 0, 1],
    2: [65, 0, 0, 1, 60, 1, 0, 0],
    3: [65, 0, 1, 0, 60, 1, 0, 0],
    4: [65, 0, 1, 0, 60, 1, 0, 1]},
     columns=data_x_numeric.columns, orient='index')

pred_surv = estimator.predict_survival_function(x_new)

当我想绘制结果时:


time_points = np.arange(1, 1000)
for i, surv_func in enumerate(pred_surv):
    plt.step(time_points, surv_func(time_points), where="post",
             label="Sample %d" % (i + 1))
plt.ylabel("est. probability of survival $\hat{S}(t)$")
plt.xlabel("time $t$")
plt.legend(loc="best")

我收到以下错误:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

我怎么解决这个问题?

第二个问题是,如何将对象pred_surv的结果传输到数据帧?

标签: pythonpandassurvival-analysisscikit-survival

解决方案


推荐阅读