Kaggle competition: Predição de Churn
<SOS> metric: auc Kaggle data source: opt-churn-2019-1<feature_engineering>
print(os.listdir(".. /input"))
print(data[(pd.isnull(data["Fare"])) |(data["Fare"] == 0)])
 data.loc[data["Fare"] == 0, "Fare"] = np.NaN
data["Fare"].fillna(data.groupby(['Embarked', 'Pclass', 'Neighbors'] ).transform('median' ).Fare, inplace=True )<categorify>
data = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv('.. /input/test.csv')
 ids = pd.read_csv('.. /input/sampleSubmission.csv')
 test.head(5 )
encoder = LabelEncoder()
data["Embarked"] = encoder.fit_transform(data["Embarked"] )<define_variables>
data.isnull().any()
FEATURES_1 = ['Sex', 'Pclass', 'Embarked']<groupby>
data.loc[data['TotalCharges'].isna() ] = 0
data['Group_Status'] = 0
ticket_grouping = data.groupby("Ticket")
 for _, group in ticket_grouping:
if(len(group)> 1):
for i, row in group.iterrows() :
s_max = group.drop(i)['Survived'].max()
pass_id = row.name
if s_max == 1.0:
data.loc[pass_id, 'Group_Status'] = 1
elif s_max == 0.0:
data.loc[pass_id, 'Group_Status'] = -1<define_variables>
test.isnull().any()
FEATURES_2 = ['Sex', 'Pclass', 'Embarked', 'Group_Status']<define_variables>
test.loc[test['TotalCharges'].isna() ] = 0
FEATURES_3 = ['Sex', 'Pclass', 'Embarked', 'Group_Status', 'Age_Bin']<feature_engineering>
X = data.drop(['customerID','Churn'], 1)
 x = pd.get_dummies(X)
 x = x.drop(['gender_0','Partner_0','Dependents_0','PaymentMethod_0','MultipleLines_0','PaperlessBilling_0','StreamingMovies_0','PhoneService_0','InternetService_0','DeviceProtection_0','TechSupport_0','OnlineSecurity_0','OnlineBackup_0'],1)
 y = pd.get_dummies(data.Churn)
 y = y.drop([0],1)
 teste = test.drop(['customerID'], 1)
 teste = pd.get_dummies(teste)
 teste = teste.drop(['gender_0','Partner_0','Dependents_0','PaymentMethod_0','MultipleLines_0','PaperlessBilling_0','StreamingMovies_0','PhoneService_0','InternetService_0','DeviceProtection_0','TechSupport_0','OnlineSecurity_0','OnlineBackup_0'],1)
 y.head(2)
data["Fare_per_Person"] = data["Fare"] /(1 + data["Neighbors"])
 bins = pd.qcut(data["Fare_per_Person"], 6, labels=False, retbins=True)
 data["Fare_per_Person_Bin"] = bins[0]<define_variables>
test_np = np.array(teste)
 X=np.array(x)
 Y=np.array(y )
FEATURES_4 = ['Sex', 'Pclass', 'Embarked', 'Group_Status', 'Age_Bin', 'Fare_per_Person_Bin']<feature_engineering>
x_Train,x_Test,y_Train,y_Test=train_test_split(X,Y,test_size=0.30 )
data["Family_Size"] = data["SibSp"] + data["Parch"]
data["Connections"] = data[["Family_Size", "Neighbors"]].max(axis=1)
 bins = np.array([data["Connections"].min() , 1, 4, data["Connections"].max() ])
 data["Connections_Bin"] = np.digitize(data["Connections"], bins )<define_variables>
scaler = StandardScaler()
sdsfds = scaler.fit_transform(x_Train )
FEATURES_5 = ['Sex', 'Pclass', 'Embarked', 'Group_Status', 'Age_Bin', 'Fare_per_Person_Bin', "Connections_Bin"]<train_on_grid>
def score(model,x,y):
prob=model.predict_proba(x)
 prob = prob[:, 1]
auc = roc_auc_score(y, prob)
 print('AUC: {}
ROC_AUC: {}
{}'.format(model.score(x,y),auc,prob[:10]))
def model(X, y, features, random_state, parameters=None):
model = RandomForestClassifier(random_state=random_state,
n_estimators=500,
min_samples_split=0.05)
 if parameters is not None:
model.set_params(**parameters)
 model.fit(X[features], y)
 score_cv = cross_val_score(model, X[features], y, cv=cv ).mean()
print(f"Model CV score is {score_cv:.5f}")
 return model<prepare_x_and_y>
knn=KNeighborsClassifier(n_neighbors=3)
 asa = knn.fit(x_Train,y_Train[:,1])
 score(knn,x_Test,y_Test[:,1] )
X_train = data.loc[data_train.index].drop("Survived", axis=1)
 y_train = data.loc[data_train.index, "Survived"]
X_test = data.loc[data_test.index].drop("Survived", axis=1)
 cv=KFold(10, shuffle=True, random_state=random_state )<train_model>
probs = knn.predict_proba(test_np )
MODEL_BASELINE = model(X_train, y_train, FEATURES_BASELINE, random_state)
<train_model>
xgb = XGBClassifier()
xgb.fit(x_Train,y_Train[:,1])
 score(xgb,x_Test,y_Test[:,1] )
MODEL_1 = model(X_train, y_train, FEATURES_1, random_state)
<train_model>
prob=xgb.predict_proba(test_np )
MODEL_2 = model(X_train, y_train, FEATURES_2, random_state)
<train_model>
clf = RandomForestClassifier(n_estimators=10)
 clf.fit(x_Train,y_Train[:,1])
 score(clf, x_Test,y_Test[:,1] )
MODEL_3 = model(X_train, y_train, FEATURES_3, random_state)
<train_model>
ada = AdaBoostClassifier(n_estimators=100, random_state=0)
 ada.fit(x_Train,y_Train[:,1])
 score(ada,x_Test,y_Test[:,1] )
MODEL_4 = model(X_train, y_train, FEATURES_4, random_state)
<train_model>
prob3 = ada.predict_proba(test_np )
MODEL_5 = model(X_train, y_train, FEATURES_5, random_state)
<define_variables>
gbc = GradientBoostingClassifier(n_estimators=100, learning_rate=1.0,
max_depth=1, random_state=0 ).fit(x_Train, y_Train[:,1])
 score(gbc, x_Test, y_Test[:,1])
FEATURES = ['Sex', 'Pclass', 'Embarked', 'Group_Status', 'Age_Bin', 'Fare_per_Person_Bin', "Connections_Bin"]<train_on_grid>
rgl = LogisticRegression(random_state=0, solver='lbfgs',
multi_class='multinomial' ).fit(x_Train, y_Train[:,1])
 score(rgl, x_Test, y_Test[:,1] )
params = {
'n_estimators': [500, 1000],
'min_samples_split': [2, 0.0025, 0.005, 0.01, 0.025, 0.05, 0.10]
}
results = pd.DataFrame()
gs = GridSearchCV(estimator=RandomForestClassifier(random_state), param_grid=params, cv=cv, verbose=1, n_jobs=-1)
 gs.fit(X_train[FEATURES], y_train)
 results = pd.DataFrame(gs.cv_results_)
 table = pd.pivot_table(results, values="mean_test_score", index="param_min_samples_split", columns="param_n_estimators")
 sns.heatmap(table, annot=True, cmap="YlGnBu")
<train_on_grid>
prob4 = rgl.predict_proba(test_np )
params = {
'n_estimators': [500, 1000],
'max_depth': [3, 4, 5, 6, 7, None],
}
results = pd.DataFrame()
gs = GridSearchCV(estimator=RandomForestClassifier(random_state), param_grid=params, cv=cv, verbose=1, n_jobs=-1)
 gs.fit(X_train[FEATURES], y_train)
 results = pd.DataFrame(gs.cv_results_)
 table = pd.pivot_table(results, values="mean_test_score", index="param_max_depth", columns="param_n_estimators")
 sns.heatmap(table, annot=True, cmap="YlGnBu")
<train_on_grid>
BC_model = BaggingClassifier().fit(x_Train, y_Train[:,1])
 score(BC_model, x_Test, y_Test[:,1] )
model = RandomForestClassifier(random_state=random_state)
 pipe = Pipeline([("rfe", RFE(model, verbose=0)) ,("rf", model)])
 params = {
'rfe__n_features_to_select': [1,2,3,4,5,6,7],
'rf__n_estimators': [500],
'rf__min_samples_split': [2, 0.01],
'rf__max_depth': [5, 6, None]
}
grid = GridSearchCV(pipe, param_grid=params, cv=cv, verbose=0, n_jobs=-1)
 grid.fit(X_train[FEATURES], y_train)
 print(gs.best_params_)
 grid.cv_results_<find_best_params>
dtc = DecisionTreeClassifier(random_state=0 ).fit(x_Train, y_Train[:,1])
 score(dtc, x_Test, y_Test[:,1] )
MODEL_6 = grid.best_estimator_
print(f"Model CV score is {grid.best_score_:.5f}")
<train_on_grid>
etc = ExtraTreesClassifier(n_estimators=10, max_depth=None,
min_samples_split=2, random_state=0 ).fit(x_Train, y_Train[:,1])
 score(etc, x_Test, y_Test[:,1] )
MODEL_7 = catboost.CatBoostClassifier(one_hot_max_size=4, iterations=1000, random_seed=random_state, verbose=False)
 MODEL_7.fit(X_train[FEATURES_4], y_train)
 score_cv = cross_val_score(MODEL_7, X_train[FEATURES_4], y_train, cv=cv ).mean()
print(f"Model CV score is {score_cv:.5f}")
<train_on_grid>
sgd = SGDClassifier(loss="log", penalty="elasticnet", max_iter=5 ).fit(x_Train, y_Train[:,1])
 score(sgd, x_Test, y_Test[:,1] )
model = KNeighborsClassifier()
pipe = Pipeline([("scaler", StandardScaler()),("knn", model)])
 params = {
'knn__n_neighbors': [3, 9, 15, 20, 21, 22, 25, 30],
'knn__weights': ['uniform', 'distance']
}
knn_grid = GridSearchCV(pipe, param_grid=params, cv=cv, verbose=0, n_jobs=-1)
 knn_grid.fit(X_train[FEATURES], y_train)
 knn_grid.cv_results_<find_best_params>
submission = pd.DataFrame({
"Id": ids.Id,
"Expected": probs[:,1]
})
 submission.head()
<save_to_csv><EOS> 
submission.to_csv('sampleSubmission.csv', index=False )