Kaggle competition: Don't Overfit! II
<SOS> metric: AUC Kaggle data source: dont-overfit!-ii<save_to_csv>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
from sklearn import metrics
import sklearn.preprocessing, sklearn.kernel_ridge, sklearn.model_selection, sklearn.linear_model
import multiprocessing
import seaborn as sns
import scipy.stats
leela_submission=pd.DataFrame({'Id': Id,'Cover_Type':pred})
 leela_submission.to_csv('leela_submision.csv',index=False )<install_modules>
submission = pd.read_csv('.. /input/sample_submission.csv')
 train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 print(f'Shape of the train {train.shape}')
 print(f'Shape of the test {test.shape}')
 test.head()
!pip install seaborn<import_modules>
data = train.iloc[:, 2:].values
ref = train['target'].values
sns.countplot(train['target'])
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
from sklearn.preprocessing import OneHotEncoder, MinMaxScaler, StandardScaler
from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV,RandomizedSearchCV
from sklearn.pipeline import Pipeline
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.dummy import DummyClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier,ExtraTreesClassifier
from xgboost import XGBClassifier<load_from_csv>
model_default = sklearn.linear_model.LogisticRegression()
model_default.fit(data, ref)
 predict_test = model_default.predict_proba(test.iloc[:, 1:].values)
 submission['target'] = predict_test
submission.to_csv('submission_logreg_default.csv', index=False)
 model = sklearn.linear_model.LogisticRegression(class_weight='balanced', penalty='l1', C=0.1, solver='liblinear')
 model.fit(data, ref)
 predict_test = model.predict_proba(test.iloc[:, 1:].values)
 submission['target'] = predict_test
submission.to_csv('submission_logreg_l1.csv', index=False )
submission_sample = pd.read_csv('.. /input/forest-cover-type-prediction/sampleSubmission.csv')
 train = pd.read_csv('.. /input/forest-cover-type-prediction/train.csv')
 test = pd.read_csv('.. /input/forest-cover-type-prediction/test.csv' )<count_unique_values>
def cross_validation(X, y, model, parameters, pname, nfold=10):
cv_method = sklearn.model_selection.KFold(n_splits=nfold, shuffle=True, random_state=13)
 rgr = sklearn.model_selection.GridSearchCV(model, parameters, n_jobs=multiprocessing.cpu_count() -1, cv=cv_method, scoring='roc_auc')
 rgr.fit(X, y)
 plt.semilogx(parameters[pname], rgr.cv_results_['mean_test_score'], 'o-r')
 plt.xlabel(pname)
 plt.ylabel('ROC-AUC')
 plt.title(f'{nfold}-Fold cross validation')
 print(f"The best {pname} parameter is {rgr.best_params_[pname]}")
 return rgr
train.nunique()<count_missing_values>
predict_test = rgr.best_estimator_.predict_proba(test.iloc[:, 1:].values)
 submission['target'] = predict_test
submission.to_csv('submission_logreg_cv_l1.csv', index=False )
train.isna().sum()<define_variables>
def cross_validation_average(X, y, X_test, model, nfold=10):
scores = []
y_test = 0
folds = sklearn.model_selection.StratifiedKFold(n_splits=nfold, shuffle=True, random_state=13)
 for idx,(train_index, valid_index)in enumerate(folds.split(X, y)) :
X_train, X_valid = X[train_index], X[valid_index]
y_train, y_valid = y[train_index], y[valid_index]
print(f'Process fold no {idx}: train/validation={len(y_train)}/{len(y_valid)}')
 model = model
model.fit(X_train, y_train)
 y_pred_valid = model.predict(X_valid ).reshape(-1,)
 score = metrics.roc_auc_score(y_valid, y_pred_valid)
 y_test += model.predict_proba(X_test)[:, 1]
scores.append(score)
 plt.plot(range(nfold), scores, 'o-r')
 plt.xlabel('N-Fold')
 plt.ylabel('ROC-AUC')
 plt.title(f'{nfold}-Fold cross validation scores')
 print(f"Results: Max={np.max(scores)}, Mean={np.mean(scores)}, STD={np.std(scores)}")
 return scores, y_test/nfold
def outlier_function(df, col_name):

 first_quartile = np.percentile(np.array(df[col_name].tolist()), 25)
 third_quartile = np.percentile(np.array(df[col_name].tolist()), 75)
 IQR = third_quartile - first_quartile
upper_limit = third_quartile+(3*IQR)
 lower_limit = first_quartile-(3*IQR)
 outlier_count = 0
for value in df[col_name].tolist() :
if(value < lower_limit)|(value > upper_limit):
outlier_count +=1
return lower_limit, upper_limit, outlier_count 
<filter>
scores, predict_test = cross_validation_average(data, ref, test.iloc[:, 1:].values, rgr.best_estimator_, nfold=10)
 submission['target'] = predict_test
submission.to_csv('submission_logreg_cv_l1_average.csv', index=False)