data-science - 如何避免 Python 的 Lifelines 包的 AalenAdditiveFitter 中的 MemoryError
问题描述
我们正在研究与生存分析相关的问题。我们已经实现了 Cox 比例风险模型和加速故障时间算法。现在我们想看看协变量如何随时间变化。所以我们决定AalenAdditiveFitter
从lifelines
库中实现。这是一个虚拟数据。数据形状为(1341799, 4)
.
Gender Disability_level Time_to_event Event
1 Female Mild 50 0
2 Male Moderate 70 1
3 Male Severe
.
.
.
1341799 Female Mild 45 1
现在,我们面临的问题与内存有关。一个热编码后的数据形状变为(1341799, 15)
。但是,当我们尝试将数据拟合到模型中时,列数只会增加到1904
from15
并且数据会被转置。
这是我们尝试运行的代码。
from lifelines import AalenAdditiveFitter
aaf = AalenAdditiveFitter(coef_penalizer=1.0, fit_intercept=False)
print("DF shape: ", df.shape)
aaf.fit(df, 'Time_to_event', 'Event')
这是我们得到的错误。
DF shape: (1341799, 15)
MemoryError: Unable to allocate 19.0 GiB for an array with shape
(1904, 1341799) and data type float64
当我们减少行数(比如 50k 行)时,代码可以正常工作。但这不符合我们的目的。如何解决这个问题?
另外,任何人都可以解释引擎盖下发生了什么吗?有没有办法解决这个问题?是否有任何其他方法可用于捕获协变量在整个时间内的可变性?
您可以使用以下代码生成我们的数据。
import numpy as np
import pandas as pd
gender = ["Male", "Female", "Others", "Unknown"]
disable = ["Mild", "Moderate", "Severe", "Level1", "Level2", "Level3", "Level4", "Level5", "Level6"]
data = []
for i in range(0, 1341799):
g = np.random.choice(gender)
d = np.random.choice(disable)
t = np.random.randint(30, 1000)
e = np.random.choice([0, 1])
data.append([g, d, t, e])
df = pd.DataFrame(data, columns=['Gender', 'Disability_level', 'Time_to_event', 'Event'])
解决方案
推荐阅读
- arrays - 有人告诉我这个 JSON 有多个根元素,帮助纠正
- keyboard-shortcuts - mc.keymap(午夜指挥官)中的“a1”、“c1”绑定是什么?
- swift - 无法覆盖preferredStatusBarStyle
- ios - 在 swiftUi 中跨视图更改结构内的数据
- azure-data-factory - 将“设置变量活动”的值保存在 Json 文件中
- node.js - 在 nodeJs 中导入 @turf 模块的问题
- parsing - 是否有一种解析器生成器可以处理所有确定性上下文无关文法?
- xml - 仅 PDF 格式文档的 XML 导出
- java - 如果没有可用的元信息,如何使用 Gson 反序列化 JSON 数据(例如:类型信息)
- python - 如何在 Selenium 中抓取数据而不会在 Python 中被检测为机器人?