-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
54 lines (45 loc) · 1.33 KB
/
Copy pathpredict.py
File metadata and controls
54 lines (45 loc) · 1.33 KB
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
import joblib
import numpy as np
import pandas as pd
model = joblib.load("models/churn_model.pkl")
scaler = joblib.load("models/scaler.pkl")
feature_names = joblib.load("models/model_features.pkl")
def preprocess_input(data: dict):
"""
Convert input dict to dataframe, align columns with training features, and scale.
"""
df = pd.DataFrame([data])
# Ensure all expected columns exist
for col in feature_names:
if col not in df.columns:
df[col] = 0
df = df[feature_names] # reorder columns
scaled = scaler.transform(df)
return scaled
def predict_churn(data: dict):
"""
Predict churn probability from user input data.
"""
processed = preprocess_input(data)
prob = model.predict_proba(processed)[0][1]
label = model.predict(processed)[0]
return {
"prediction": int(label),
"churn_probability": float(prob)
}
# For testing
if __name__ == "__main__":
sample = {
"Tenure": 12,
"MonthlyCharges": 70.5,
"TotalCharges": 850,
"SeniorCitizen": 0,
"Gender": 1,
"InternetService_Fiber optic": 1,
"InternetService_No": 0,
"Contract_Month-to-month": 1,
"Contract_One year": 0,
"Contract_Two year": 0
}
result = predict_churn(sample)
print(result)