Kaggle competition: Don't Get Kicked!
<SOS> metric: Gini Kaggle data source: dont-get-kicked!<find_best_model_class>
import numpy as np
import pandas as pd
from sklearn.pipeline import make_pipeline
from sklearn.compose import make_column_transformer
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import KBinsDiscretizer, OneHotEncoder, StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
svm_grid_clf = SVC(kernel="rbf", gamma=0.1, C=10)
 svm_grid_clf.fit(train_set_prepared, survival_labels)
 svm_grid_cross_scores = cross_val_score(svm_grid_clf, train_set_prepared, survival_labels, cv=skfold, scoring="accuracy")
 svm_pred = svm_grid_clf.predict(validation_set_prepared ).astype(int)
 svm_grid_scores =(svm_grid_cross_scores.mean() + accuracy_score(svm_pred, validation_labels)) /2
svm_grid_scores<train_model>
train = pd.read_csv('/kaggle/input/DontGetKicked/training.csv')
 test = pd.read_csv('/kaggle/input/DontGetKicked/test.csv' )
forest_clf = RandomForestClassifier(random_state=rnd_state, n_estimators=10)
 forest_clf.fit(train_set_prepared, survival_labels )<train_on_grid>
y = train['IsBadBuy']
X = train.drop(['IsBadBuy','RefId'], axis = 1 )
param_grid = [
{'n_estimators': [200, 250], 'max_features': [10, 17]},
{'bootstrap': [False], 'n_estimators': [200, 250], 'max_features': [10, 12, 17]},
]
grid_forest_clf = GridSearchCV(forest_clf, param_grid, cv=skfold,
scoring='accuracy', return_train_score=True)
 grid_forest_clf.fit(train_set_prepared, survival_labels )<find_best_params>
numerical_features = [c for c, dtype in zip(X.columns, X.dtypes)
 if dtype.kind in ['i','f']]
categorical_features = [c for c, dtype in zip(X.columns, X.dtypes)
 if dtype.kind not in ['i','f']]
grid_forest_clf.best_params_<predict_on_test>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0, stratify = y )
grid_forest_pred = grid_forest_clf.predict(validation_set_prepared ).astype(int)
 grid_forest_scores =(grid_forest_clf.best_score_ + accuracy_score(grid_forest_pred, validation_labels)) /2
grid_forest_scores<train_on_grid>
preprocessor = make_column_transformer(
(make_pipeline(
 SimpleImputer(strategy = 'median'),
StandardScaler() ,
KBinsDiscretizer(n_bins=3)) , numerical_features), 
(make_pipeline(
 SimpleImputer(strategy = 'constant', fill_value = 'missing'),
OneHotEncoder(categories = 'auto', handle_unknown = 'ignore')) , categorical_features),
)
param_distribs = {
'n_estimators': randint(low=100, high=300),
'max_features': randint(low=8, high=17),
}
rnd_forest_clf = RandomizedSearchCV(forest_clf, param_distributions=param_distribs,
n_iter=10, cv=skfold, scoring='accuracy', random_state=rnd_state)
 rnd_forest_clf.fit(train_set_prepared, survival_labels )<find_best_params>
from sklearn.ensemble import RandomForestClassifier
rnd_forest_clf.best_params_<predict_on_test>
RF_Model = make_pipeline(preprocessor, RandomForestClassifier(n_estimators = 100))
rnd_forest_pred = rnd_forest_clf.predict(validation_set_prepared ).astype(int)
 rnd_forest_scores =(rnd_forest_clf.best_score_ + accuracy_score(rnd_forest_pred, validation_labels)) /2
rnd_forest_scores<compute_train_metric>
RF_Model.fit(X_train, y_train )
knn_clf = KNeighborsClassifier()
knn_clf.fit(train_set_prepared, survival_labels)
 knn_cross_scores = cross_val_score(knn_clf, train_set_prepared, survival_labels, cv=skfold, scoring="accuracy")
 knn_pred = knn_clf.predict(validation_set_prepared ).astype(int)
 knn_scores =(knn_cross_scores.mean() + accuracy_score(knn_pred, validation_labels)) /2
knn_scores<train_on_grid>
print(f'Train : {RF_Model.score(X_train, y_train):.3f}')
 print(f'Test : {RF_Model.score(X_test, y_test):.3f}' )
param_grid = [{'weights': ["uniform", "distance"], 'n_neighbors': [3, 4, 5]}]
grid_knn_clf = GridSearchCV(knn_clf, param_grid, cv=5, verbose=3, n_jobs=-1)
 grid_knn_clf.fit(train_set_prepared, survival_labels)
 grid_knn_clf.best_params_<compute_train_metric>
sub_test = test.drop(['RefId'], axis = 1 )
grid_knn_cross_scores = cross_val_score(grid_knn_clf, train_set_prepared, survival_labels, cv=skfold, scoring="accuracy")
 grid_knn_pred = grid_knn_clf.predict(validation_set_prepared ).astype(int)
 grid_knn_scores =(grid_knn_cross_scores.mean() + accuracy_score(grid_knn_pred, validation_labels)) /2
grid_knn_scores<choose_model_class>
sub_test_pred = RF_Model.predict(sub_test ).astype(int )
ext_clf = ExtraTreesClassifier()
param_grid = {"max_depth": [None],
"max_features": [10, 17],
"min_samples_split": [2, 3, 10],
"min_samples_leaf": [1, 3, 10],
"bootstrap": [False, True],
"n_estimators" :[50,100,200],
"criterion": ["gini"]}
grid_ext_clf = GridSearchCV(ext_clf,param_grid, cv=skfold, scoring="accuracy", n_jobs= 4, verbose = 1)
 grid_ext_clf.fit(train_set_prepared, survival_labels)
 grid_ext_clf.best_params_<predict_on_test>
AllSub = pd.DataFrame({ 'RefId': test['RefId'],
'IsBadBuy' : sub_test_pred
})
 AllSub.to_csv("DGK_Pipeline_RF.csv", index = False )