看起来我的代码和scikit-survival文档的格式一样。
data_y = df[['sensored', 'sensored_2']].to_numpy()
data_x = df.drop(['sensored', 'sensored_2'], axis = 1)
data_y
array([[True, 481],
[True, 424],
[True, 519],
...,
[True, 13],
[True, 96],
[True, 6]], dtype=object)
根据scikit-survival文档,数组是在加载时从数据集创建的。我试图从dataframe创建我的数组,但当我试图将数组适合模型时,继续在标题中得到错误。
sksurv.linear_model import CoxPHSurvivalAnalysis
estimator = CoxPHSurvivalAnalysis()
estimator.fit(df_dummy_3, data_y)
ValueError: y must be a structured array with the first field being a binary
class event indicator and the second field the time of the event/censoring
文件:
from sksurv.datasets import load_veterans_lung_cancer
data_x, data_y = load_veterans_lung_cancer()
data_y
array([( True, 72.), ( True, 411.), ( True, 228.), ( True, 126.),
( True, 118.), ( True, 10.), ( True, 82.), ( True, 110.),
( True, 314.), (False, 100.), ( True, 42.), ( True, 8.),
( True, 144.), (False, 25.), ( True, 11.), ( True, 30.),
( True, 384.), ( True, 4.), ( True, 54.), ( True, 13.),
(False, 123.), (False, 97.), ( True, 153.), ( True, 59.),
( True, 117.), ( True, 16.), ( True, 151.), ( True, 22.),
( True, 56.), ( True, 21.), ( True, 18.), ( True, 139.),
( True, 20.), ( True, 31.), ( True, 52.), ( True, 287.),
( True, 18.), ( True, 51.), ( True, 122.), ( True, 27.),
( True, 54.), ( True, 7.), ( True, 63.), ( True, 392.),
( True, 10.), ( True, 8.), ( True, 92.), ( True, 35.),
( True, 117.), ( True, 132.), ( True, 12.), ( True, 162.),
( True, 3.), ( True, 95.), ( True, 177.), ( True, 162.),
( True, 216.), ( True, 553.), ( True, 278.), ( True, 12.),
( True, 260.), ( True, 200.), ( True, 156.), (False, 182.),
( True, 143.), ( True, 105.), ( True, 103.), ( True, 250.),
( True, 100.), ( True, 999.), ( True, 112.), (False, 87.),
(False, 231.), ( True, 242.), ( True, 991.), ( True, 111.),
( True, 1.), ( True, 587.), ( True, 389.), ( True, 33.),
( True, 25.), ( True, 357.), ( True, 467.), ( True, 201.),
( True, 1.), ( True, 30.), ( True, 44.), ( True, 283.),
( True, 15.), ( True, 25.), (False, 103.), ( True, 21.),
( True, 13.), ( True, 87.), ( True, 2.), ( True, 20.),
( True, 7.), ( True, 24.), ( True, 99.), ( True, 8.),
( True, 99.), ( True, 61.), ( True, 25.), ( True, 95.),
( True, 80.), ( True, 51.), ( True, 29.), ( True, 24.),
( True, 18.), (False, 83.), ( True, 31.), ( True, 51.),
( True, 90.), ( True, 52.), ( True, 73.), ( True, 8.),
( True, 36.), ( True, 48.), ( True, 7.), ( True, 140.),
( True, 186.), ( True, 84.), ( True, 19.), ( True, 45.),
( True, 80.), ( True, 52.), ( True, 164.), ( True, 19.),
( True, 53.), ( True, 15.), ( True, 43.), ( True, 340.),
( True, 133.), ( True, 111.), ( True, 231.), ( True, 378.),
( True, 49.)],
dtype=[('Status', '?'), ('Survival_in_days', '<f8')])
我一直在尝试改变我上面数组的数据类型,但没有运气。任何帮助将不胜感激。
3条答案
按热度按时间l7mqbcuq1#
fit方法期望y数据是结构化数组。在我们的例子中,这是一个元组数组,其中第一个元素是状态,第二个元素是生存天数。
为了将数据以fit方法期望的格式放置,我们需要首先将数组的元素从列表(例如:[True,424])到元组(例如(真,424))。之后,我们可以将元组分组到结构化数组中。下面我将展示一个示例:
假设我们有如下的dataframe:
我们得到的是你的:
将data_y放入我们期望的格式的一种方法是使用其元素创建一个元组列表,然后创建一个结构化数组:
然后,您可以使用这些数据来拟合您的模型:
neskvpey2#
你的数组是2d对象dtype:
假设文档显示了所需的结构化数组dtype:
从文档中可以看到,结构化数组显示(和输入)为元组列表。
使用dtype:
如果有任何不清楚的地方,您可能需要阅读
structured array
文档https://numpy.org/doc/stable/user/basics.rec.html
insrf1ej3#
而不是
.numpy()
使用to_records(index=False)
为我工作