【问题标题】:Lifelines boolean index in Python did not match indexed array along dimension 0; dimension is 88 but corresponding boolean dimension is 76Python 中的生命线布尔索引与维度 0 上的索引数组不匹配;维度为 88,但对应的布尔维度为 76
【发布时间】:2018-01-03 11:22:57
【问题描述】:

这段非常简单的代码,

# imports...
from lifelines import CoxPHFitter
import pandas as pd

src_file = "Pred.csv"

df = pd.read_csv(src_file, header=0, delimiter=',')
df = df.drop(columns=['score'])

cph = CoxPHFitter()
cph.fit(df, duration_col='Length', event_col='Status', show_progress=True)

产生错误:

Traceback(最近一次调用最后一次): 文件 “C:/Users/.../predictor.py”,第 11 行, 在 cph.fit(df, duration_col='Length', event_col='Status', show_progress=True)

文件 "C:\Users\...\AppData\Local\conda\conda\envs\hrpred\lib\site-packages\lifelines\fitters\coxph_fitter.py", 第 298 行,合适 self._check_values(df)

文件 "C:\Users\...\AppData\Local\conda\conda\envs\hrpred\lib\site-packages\lifelines\fitters\coxph_fitter.py", 第 323 行,在 _check_values 中 cols = str(list(X.columns[low_var]))

文件 "C:\Users\...\AppData\Local\conda\conda\envs\hrpred\lib\site-packages\pandas\core\indexes\base.py", 第 1754 行,在 _ _ getitem _ _

结果 = getitem(key)

IndexError: 布尔索引与第 0 维的索引数组不匹配;尺寸为 88 但对应 布尔维度为 76

但是,当我打印 df 本身时,一切正常。如您所见,一切都在图书馆内。图书馆的例子工作正常。

【问题讨论】:

  • 我们需要您在此处使用的文件,以找出究竟出了什么问题。您能否提供一个不使用外部文件的示例?
  • 好吧,很遗憾我无法提供该文件,因为它包含专有信息。但是文件是用 pandas 完美处理的,我可以打印和切片数据。问题的根源是 pandas 还是 CoxPHFitter(生命线)?
  • 如果不是数据本身,能否提供以下信息:dataframe中的列,每列的dtype,以及每列中空值和非空值的数量。例如df.columnsdf[column1].dtypesum(df[column1].isnull())sum(df[column1].notnull())(对于每一列)。听起来索引没有对齐,这让我想知道 null/NaN 值。

标签: python pandas cox-regression cox lifelines


【解决方案1】:

在不知道您的数据是什么样子的情况下 - 我遇到了同样的错误,当我从我正在使用的 pandas df 中删除除持续时间、事件和系数之外的所有内容时,该错误得到了解决。也就是说,我在 df 中有很多额外的列使 cox PH fitter 感到困惑,因为您实际上并没有指定要包含哪个 coef 作为 cph.fit() 的参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    • 2020-10-03
    • 1970-01-01
    • 2019-06-18
    • 2018-04-01
    • 2017-11-17
    • 2017-12-31
    相关资源
    最近更新 更多