血小板减少亚型-今日进展
技术名称
在昨天的基础上进一步分析血小板减少的亚型
问题背景
昨天最后进行了简单的聚类,今日将其优化完善
代码实现
对原始数据进行特征工程,将时间标准化,创建目录,计算血小板动态变化特征
特征 | 说明 |
---|---|
baseline_plt | 首次记录的血小板值(基线值) |
min_plt | 血小板最低值 |
plt_decrease_pct | 从基线到最低值的下降百分比 |
hours_to_min_plt | 从ICU入院到血小板最低值的时间(小时) |
plt_slope_24h | 入院后24小时内血小板变化的斜率(反映早期下降速度) |
recovery_ratio | (最终值-最低值)/(基线值-最低值),量化恢复程度 |
is_thrombocytopenia | 是否发生血小板减少症(最低值 < 150×10⁹/L) |
is_severe_thrombocytopenia | 是否发生严重血小板减少症(最低值 < 50×10⁹/L) |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 导入所需库
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from datetime import datetime
import os
# 创建结果目录
os.makedirs('results/figures', exist_ok=True)
os.makedirs('results/tables', exist_ok=True)
os.makedirs('results/models', exist_ok=True)
os.makedirs('data/processed', exist_ok=True)
# 数据加载
sepsis_df = pd.read_csv('data/sepsis_plt_df.csv')
plt_df = pd.read_csv('data/plt_df.csv')
lab_df = pd.read_csv('data/lab_df.csv')
sofa_df = pd.read_csv('data/sofa_df.csv')
# 数据清洗与预处理
# 时间转换
sepsis_df['icu_intime'] = pd.to_datetime(sepsis_df['icu_intime'])
plt_df['charttime'] = pd.to_datetime(plt_df['charttime'])
# 血小板特征计算
plt_features = []
for patient_id in sepsis_df['stay_id'].unique():
# 获取患者血小板数据
patient_plt = plt_df[plt_df['stay_id'] == patient_id].sort_values('charttime')
icu_intime = sepsis_df[sepsis_df['stay_id'] == patient_id]['icu_intime'].iloc[0]
# 计算相对时间
patient_plt['hours_from_admission'] = (patient_plt['charttime'] - icu_intime).dt.total_seconds()/3600
# 提取血小板关键特征
if len(patient_plt) >= 2:
baseline_plt = patient_plt['platelet_count'].iloc[0]
min_plt = patient_plt['platelet_count'].min()
min_plt_time = patient_plt.loc[patient_plt['platelet_count'].idxmin(), 'hours_from_admission']
plt_decrease_pct = ((baseline_plt - min_plt) / baseline_plt * 100) if baseline_plt > 0 else 0
# 计算斜率
plt_24h = patient_plt[patient_plt['hours_from_admission'] <= 24]
if len(plt_24h) >= 2:
time_diff = plt_24h['hours_from_admission'].iloc[-1] - plt_24h['hours_from_admission'].iloc[0]
plt_slope_24h = (plt_24h['platelet_count'].iloc[-1] - plt_24h['platelet_count'].iloc[0]) / time_diff if time_diff > 0 else 0
else:
plt_slope_24h = 0
# 恢复特征
last_plt = patient_plt['platelet_count'].iloc[-1]
recovery_ratio = (last_plt - min_plt) / (baseline_plt - min_plt) if (baseline_plt - min_plt) > 0 else 0
plt_features.append({
'stay_id': patient_id,
'baseline_plt': baseline_plt,
'min_plt': min_plt,
'plt_decrease_pct': plt_decrease_pct,
'hours_to_min_plt': min_plt_time,
'plt_slope_24h': plt_slope_24h,
'recovery_ratio': recovery_ratio,
'is_thrombocytopenia': min_plt < 150,
'is_severe_thrombocytopenia': min_plt < 50
})
# 转换为DataFrame
plt_features_df = pd.DataFrame(plt_features)
# 合并所有特征
enhanced_df = sepsis_df[['stay_id', 'age', 'gender', 'icu_los', 'hospital_expire_flag']].merge(
plt_features_df, on='stay_id', how='inner'
)
# 提取实验室和SOFA特征
# ...类似处理...
# 保存处理后的数据
enhanced_df.to_csv('data/processed/enhanced_features.csv', index=False)
print("特征工程完成,数据已保存")
关键点解析
- 首先需要理解…
- 然后通过…实现…
总结与思考
这个问题的核心是…,通过…方法可以有效解决。