使用CoxTimeVaryingFitter遇类型错误:numpy.float64无exp属性
问题:CoxTimeVaryingFitter建模时baseline_cumulative_hazard_类型错误
尝试在数据集上用CoxTimeVaryingFitter建模,但baseline_cumulative_hazard_出现类型相关问题。已尝试缩减特征排查,仍无法拟合数据集,请问问题源于数据还是模型?
代码
from lifelines import CoxTimeVaryingFitter import autograd.numpy as np ctv = CoxTimeVaryingFitter() comp = 'comp_comp1' #start with comp1 event = 'failure_'+comp.split("_")[1] cols = ['start', 'stop', 'machineID', 'age', event, 'volt_24_ma','rotate_24_ma', 'vibration_24_ma', 'pressure_24_ma' ] ctv.fit(df_X_train[cols].dropna(), id_col='machineID', event_col=event, start_col='start', stop_col='stop', show_progress=True, fit_options={'step_size':0.25}) ctv.print_summary() ctv.plot()
数据情况
- 数据类型:各字段类型符合生存分析基本要求
- 时序数据:为时间依赖型的生存数据结构
错误信息
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) AttributeError: 'numpy.float64' object has no attribute 'exp' The above exception was the direct cause of the following exception: TypeError Traceback (most recent call last) <command-1950361299690996> in <module> 23 ] 24 ---> 25 ctv.fit(df_X_train[cols].dropna(), 26 id_col='machineID', 27 event_col=event, /databricks/python/lib/python3.8/site-packages/lifelines/fitters/cox_time_varying_fitter.py in fit(self, df, event_col, start_col, stop_col, weights_col, id_col, show_progress, robust, strata, initial_point, formula, fit_options) 237 self.confidence_intervals_ = self._compute_confidence_intervals() 238 self.baseline_cumulative_hazard_ = self._compute_cumulative_baseline_hazard(df, events, start, stop, weights) ---> 239 self.baseline_survival_ = self._compute_baseline_survival() 240 self.event_observed = events 241 self.start_stop_and_events = pd.DataFrame({"event": events, "start": start, "stop": stop}) /databricks/python/lib/python3.8/site-packages/lifelines/fitters/cox_time_varying_fitter.py in _compute_baseline_survival(self) 815 816 def _compute_baseline_survival(self): ---> 817 survival_df = np.exp(-self.baseline_cumulative_hazard_) 818 survival_df.columns = ["baseline survival"] 819 return survival_df /databricks/python/lib/python3.8/site-packages/pandas/core/generic.py in __array_ufunc__(self, ufunc, method, *inputs, **kwargs) 1934 self, ufunc: Callable, method: str, *inputs: Any, **kwargs: Any 1935 ): -> 1936 return arraylike.array_ufunc(self, ufunc, method, *inputs, **kwargs) 1937 1938 # ideally we would define this to avoid the getattr checks, but /databricks/python/lib/python3.8/site-packages/pandas/core/arraylike.py in array_ufunc(self, ufunc, method, *inputs, **kwargs) 364 # take this path if there are no kwargs 365 mgr = inputs[0]._mgr -> 366 result = mgr.apply(getattr(ufunc, method)) 367 else: 368 # otherwise specific ufunc methods (eg np.<ufunc>.accumulate(..)) /databricks/python/lib/python3.8/site-packages/pandas/core/internals/managers.py in apply(self, f, align_keys, ignore_failures, **kwargs) 423 try: 424 if callable(f): -> 425 applied = b.apply(f, **kwargs) 426 else: 427 applied = getattr(b, f)(**kwargs) /databricks/python/lib/python3.8/site-packages/pandas/core/internals/blocks.py in apply(self, func, **kwargs) 376 """ 377 with np.errstate(all="ignore"): -> 378 result = func(self.values, **kwargs) 379 380 return self._split_op_result(result) TypeError: loop of ufunc does not support argument 0 of type numpy.float64 which has no callable exp method
解答
这个问题源于autograd和numpy的兼容性冲突,不是数据问题。
你导入的是autograd.numpy而非标准numpy,但lifelines内部在计算基线生存函数时,期望用标准numpy的exp方法处理pandas数据结构。autograd的numpy实现和pandas的数组操作逻辑不兼容,导致np.exp无法正确识别pandas中的numpy.float64类型。
解决步骤:
- 替换导入语句,改用标准numpy:
import numpy as np # 移除 import autograd.numpy as np - 如果你的代码中确实需要autograd的功能,可单独导入并使用别名区分:
import numpy as np import autograd.numpy as anp - 重新运行拟合代码,问题即可解决。
内容的提问来源于stack exchange,提问作者Cameron
相关产品推荐
相关产品推荐

