Chapter 71
05 train churn model
NotebookPython 3 (ipykernel)35 cells
In the previous session we trained a model for predicting churn and evaluated it. Now let's deploy it
In [10]python · cell 2
python
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from sklearn.feature_extraction import DictVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_scoreIn [5]python · cell 3
python
df = pd.read_csv('data-week-3.csv')
df.columns = df.columns.str.lower().str.replace(' ', '_')
categorical_columns = list(df.dtypes[df.dtypes == 'object'].index)
for c in categorical_columns:
df[c] = df[c].str.lower().str.replace(' ', '_')
df.totalcharges = pd.to_numeric(df.totalcharges, errors='coerce')
df.totalcharges = df.totalcharges.fillna(0)
df.churn = (df.churn == 'yes').astype(int)In [7]python · cell 4
python
df_full_train, df_test = train_test_split(df, test_size=0.2, random_state=1)In [18]python · cell 5
python
numerical = ['tenure', 'monthlycharges', 'totalcharges']
categorical = [
'gender',
'seniorcitizen',
'partner',
'dependents',
'phoneservice',
'multiplelines',
'internetservice',
'onlinesecurity',
'onlinebackup',
'deviceprotection',
'techsupport',
'streamingtv',
'streamingmovies',
'contract',
'paperlessbilling',
'paymentmethod',
]In [13]python · cell 6
python
def train(df_train, y_train, C=1.0):
dicts = df_train[categorical + numerical].to_dict(orient='records')
dv = DictVectorizer(sparse=False)
X_train = dv.fit_transform(dicts)
model = LogisticRegression(C=C, max_iter=1000)
model.fit(X_train, y_train)
return dv, modelIn [14]python · cell 7
python
def predict(df, dv, model):
dicts = df[categorical + numerical].to_dict(orient='records')
X = dv.transform(dicts)
y_pred = model.predict_proba(X)[:, 1]
return y_predIn [15]python · cell 8
python
C = 1.0
n_splits = 5In [16]python · cell 9
python
kfold = KFold(n_splits=n_splits, shuffle=True, random_state=1)
scores = []
for train_idx, val_idx in kfold.split(df_full_train):
df_train = df_full_train.iloc[train_idx]
df_val = df_full_train.iloc[val_idx]
y_train = df_train.churn.values
y_val = df_val.churn.values
dv, model = train(df_train, y_train, C=C)
y_pred = predict(df_val, dv, model)
auc = roc_auc_score(y_val, y_pred)
scores.append(auc)
print('C=%s %.3f +- %.3f' % (C, np.mean(scores), np.std(scores)))Output
C=1.0 0.841 +- 0.008
In [17]python · cell 10
python
scoresOutput
[0.8423083263338855, 0.8450681201165409, 0.8324061810154525, 0.8319390707936304, 0.8522598914373568]
In [18]python · cell 11
python
dv, model = train(df_full_train, df_full_train.churn.values, C=1.0)
y_pred = predict(df_test, dv, model)
y_test = df_test.churn.values
auc = roc_auc_score(y_test, y_pred)
aucOutput
0.8572386167896259
Save the model
In [8]python · cell 13
python
import pickleIn [9]python · cell 14
python
output_file = f'model_C={C}.bin'In [10]python · cell 15
python
output_fileOutput
'model_C=1.0.bin'
In [16]python · cell 16
python
f_out = open(output_file, 'wb')
pickle.dump((dv, model), f_out)
f_out.close()In [22]python · cell 17
python
!ls -lh *.binOutput
-rwxrwxrwx 1 alexey alexey 2.5K Sep 30 14:10 'model_C=1.0.bin'
In [21]python · cell 18
python
with open(output_file, 'wb') as f_out:
pickle.dump((dv, model), f_out)Load the model
In [1]python · cell 20
python
import pickleIn [2]python · cell 21
python
input_file = 'model_C=1.0.bin'In [4]python · cell 22
python
with open(input_file, 'rb') as f_in:
dv, model = pickle.load(f_in)In [8]python · cell 23
python
modelOutput
LogisticRegression(max_iter=1000)
In [27]python · cell 24
python
customer = {
'gender': 'female',
'seniorcitizen': 0,
'partner': 'yes',
'dependents': 'no',
'phoneservice': 'no',
'multiplelines': 'no_phone_service',
'internetservice': 'dsl',
'onlinesecurity': 'no',
'onlinebackup': 'yes',
'deviceprotection': 'no',
'techsupport': 'no',
'streamingtv': 'no',
'streamingmovies': 'no',
'contract': 'month-to-month',
'paperlessbilling': 'yes',
'paymentmethod': 'electronic_check',
'tenure': 1,
'monthlycharges': 29.85,
'totalcharges': 29.85
}In [28]python · cell 25
python
X = dv.transform([customer])In [29]python · cell 26
python
y_pred = model.predict_proba(X)[0, 1]In [30]python · cell 27
python
print('input:', customer)
print('output:', y_pred)Output
input: {'gender': 'female', 'seniorcitizen': 0, 'partner': 'yes', 'dependents': 'no', 'phoneservice': 'no', 'multiplelines': 'no_phone_service', 'internetservice': 'dsl', 'onlinesecurity': 'no', 'onlinebackup': 'yes', 'deviceprotection': 'no', 'techsupport': 'no', 'streamingtv': 'no', 'streamingmovies': 'no', 'contract': 'month-to-month', 'paperlessbilling': 'yes', 'paymentmethod': 'electronic_check', 'tenure': 1, 'monthlycharges': 29.85, 'totalcharges': 29.85}
output: 0.5912433520805763
Making requests
In [40]python · cell 29
python
import requestsIn [41]python · cell 30
python
url = 'http://localhost:9696/predict'In [42]python · cell 31
python
customer = {
'gender': 'female',
'seniorcitizen': 0,
'partner': 'yes',
'dependents': 'no',
'phoneservice': 'no',
'multiplelines': 'no_phone_service',
'internetservice': 'dsl',
'onlinesecurity': 'no',
'onlinebackup': 'yes',
'deviceprotection': 'no',
'techsupport': 'no',
'streamingtv': 'no',
'streamingmovies': 'no',
'contract': 'two_year',
'paperlessbilling': 'yes',
'paymentmethod': 'electronic_check',
'tenure': 1,
'monthlycharges': 29.85,
'totalcharges': 29.85
}In [ ]python · cell 32
python
response = requests.post(url, json=customer).json()In [39]python · cell 33
python
responseOutput
{'churn': True, 'churn_probability': 0.5133820686195286}In [27]python · cell 34
python
if response['churn']:
print('sending email to', 'asdx-123d')Output
sending email to asdx-123d
In [ ]python · cell 35
python
