python - 为什么具有多个元素的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的结果传输到数据帧?
解决方案
推荐阅读
- java - Spring Elasticsearch 按带空格的字段排序
- flutter - Flutter - 地图中不断闪烁
- .htaccess - 如何编写将“/folder1/folder2/”更改为“/”的.htaccess
- javascript - Google 应用程序脚本:如何在我的自动化电子邮件中添加一些样式?
- javascript - 在js中选择没有选择器的元素
- google-api - 当尝试检索带有 + 值的组织单位时,我得到 404
- python - '错误:命令'gcc'在安装python-fcl时失败,退出状态为1'
- node.js - 如何使用 chai-http 下载文件?
- windows - jstack:docker中的windows上没有安装提供程序
- python - 如何在 Python 中从头开始创建 PDF 文档