-
Notifications
You must be signed in to change notification settings - Fork 0
/
cox-model.py
31 lines (24 loc) · 885 Bytes
/
cox-model.py
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
import pandas as pd
from lifelines import CoxPHFitter
# Load the dataset
df = pd.read_csv('stroke_data.csv')
# Remove the 'id' column if it's present
if 'id' in df:
df = df.drop(columns=['id'])
# Assuming you have already encoded the categorical variables as numbers, and 'status' as 0 or 1
X = df[['age', 'sex', 'dm', 'who', 'gcs', 'nihss', 'mrs']]
T = df['dur_month']
E = df['status']
# Create a Cox Proportional Hazards model
cph = CoxPHFitter()
cph.fit(df, duration_col='dur_month', event_col='status')
# Access the coefficients and their significance
print("Coefficients:")
print(cph.params_)
# Calculate Harrell's C-index for model performance
from lifelines.utils import concordance_index
c_index = concordance_index(T, -cph.predict_partial_hazard(X))
print(f"C-index: {c_index}")
# Save the Cox model to a file using joblib
import joblib
joblib.dump(cph, 'cox.pkl')