| name | scikit-survival |
| description | Fit Cox proportional hazards model and compute concordance index using scikit-survival. Use when performing survival analysis, fitting Cox models, or evaluating survival predictions with concordance index. |
Cox Proportional Hazards Model
Overview
Fit a Cox proportional hazards model to survival data and compute the concordance index (C-index) to evaluate model performance.
Workflow
Step 1: Load and Prepare Data
The input CSV has columns: time, event, feature1, feature2, feature3.
import pandas as pd
from sksurv.linear_model import CoxPHSurvivalAnalysis
from sksurv.metrics import concordance_index_censored
from sksurv.util import Surv
df = pd.read_csv("/root/input.csv")
Step 2: Create Survival Outcome
The survival outcome combines event indicator (1=event occurred, 0=censored) and time.
y = Surv.from_arrays(
event=df['event'].values,
time=df['time'].values
)
Step 3: Extract Features
X = df[['feature1', 'feature2', 'feature3']].values
Step 4: Fit Cox Model
model = CoxPHSurvivalAnalysis()
model.fit(X, y)
Step 5: Predict and Evaluate
risk_scores = model.predict(X)
c_index = concordance_index_censored(
y['event'],
y['time'],
risk_scores
)[0]
Step 6: Output Result
with open("/root/output.txt", "w") as f:
f.write(f"Concordance index: {c_index:.4f}\n")
Complete Script
import pandas as pd
from sksurv.linear_model import CoxPHSurvivalAnalysis
from sksurv.metrics import concordance_index_censored
from sksurv.util import Surv
df = pd.read_csv("/root/input.csv")
y = Surv.from_arrays(event=df['event'].values, time=df['time'].values)
X = df[['feature1', 'feature2', 'feature3']].values
model = CoxPHSurvivalAnalysis()
model.fit(X, y)
risk_scores = model.predict(X)
c_index = concordance_index_censored(y['event'], y['time'], risk_scores)[0]
with open("/root/output.txt", "w") as f:
f.write(f"Concordance index: {c_index:.4f}\n")
Output Format
Concordance index: X.XXXX
Round to 4 decimal places.
Key Reference
Surv.from_arrays(event=array, time=array) — Create survival outcome from event and time arrays
CoxPHSurvivalAnalysis() — Initialize Cox proportional hazards model
model.fit(X, y) — Fit model on feature matrix and survival outcome
model.predict(X) — Predict risk scores (higher = more risk)
concordance_index_censored(event, time, risk_scores)[0] — Compute Harrell's C-index (range 0-1, 0.5 = random)
About C-index
The concordance index measures how well the model ranks patients by risk:
- C-index = 1.0: perfect prediction
- C-index = 0.5: no better than random
- C-index < 0.5: predictions are worse than random (can be corrected by negating risk scores)