File size: 926 Bytes
da0d126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd

def preprocess_input(data: dict) -> pd.DataFrame:
    """

    Build a single-row DataFrame matching the training schema:

      ['gender','age','hypertension','heart_disease','ever_married',

       'work_type','Residence_type','avg_glucose_level','bmi',

       'smoking_status']

    """
    # Note: 'stroke' column is not included as a feature
    feature_cols = [
        "gender","age","hypertension","heart_disease","ever_married",
        "work_type","Residence_type","avg_glucose_level","bmi",
        "smoking_status"
    ]
    df = pd.DataFrame([{k: data[k] for k in feature_cols}])
    return df

def predict_stroke(model, X: pd.DataFrame):
    """

    Returns human-readable label and probability for the top class.

    """
    proba = model.predict_proba(X)[0]
    idx = proba.argmax()
    label_map = {0: "No Stroke", 1: "Stroke"}
    return label_map[idx], proba[idx]