Kaggle competition: Instant Gratification
<SOS> metric: AUC Kaggle data source: instant-gratification<import_modules>
warnings.filterwarnings('ignore' )
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt 
<load_from_csv>
train1 = pd.read_csv('.. /input/train.csv')
 test1 = pd.read_csv('.. /input/test.csv')
 cols = [c for c in train1.columns if c not in ['id', 'target', 'wheezy-copper-turtle-magic']]
titanic=pd.read_csv('.. /input/titanic/train.csv', header = 0, dtype={'Age': np.float64})
 titanicT=pd.read_csv('.. /input/titanic/test.csv', header = 0, dtype={'Age': np.float64})
<count_values>
def instant_model(train, test, col, clf = QuadraticDiscriminantAnalysis(0.5)) :
oof = np.zeros(len(train))
preds = np.zeros(len(test))
for i in tqdm_notebook(range(512)) :
train2 = train[train['wheezy-copper-turtle-magic'] == i]
test2 = test[test['wheezy-copper-turtle-magic'] == i]
idx1 = train2.index
idx2 = test2.index
data = pd.concat([pd.DataFrame(train2[cols]), pd.DataFrame(test2[cols])])
 data2 = VarianceThreshold(threshold = 2 ).fit_transform(data[cols])
 train3 = pd.DataFrame(data2[:train2.shape[0]], index = idx1)
 train3['target'] = train2['target']
test3 = pd.DataFrame(data2[train2.shape[0]:], index = idx2)
 skf = StratifiedKFold(n_splits=11, random_state=42)
 for train_index, test_index in skf.split(train3, train3['target']):
clf = clf
X_train = train3.iloc[train_index, :].drop(["target"], axis = 1)
 X_test = train3.iloc[test_index, :].drop(["target"], axis = 1)
 y_train = train3.iloc[train_index, :]['target']
y_test = train3.iloc[test_index, :]['target']
clf.fit(X_train, y_train)
 train_prob = clf.predict_proba(X_train)[:,1]
test_prob = clf.predict_proba(X_test)[:,1]
oof[idx1[test_index]] = test_prob
preds[idx2] += clf.predict_proba(test3)[:,1] / skf.n_splits
auc = roc_auc_score(train['target'], oof)
 print(f'AUC: {auc:.5}')
 return oof, preds
titanic.Survived.value_counts(normalize=True )<count_unique_values>
def get_newtrain(train, test, preds, oof):
test['target'] = preds
test.loc[test['target'] > 0.985, 'target'] = 1
test.loc[test['target'] < 0.015, 'target'] = 0
usefull_test = test[(test['target'] == 1)|(test['target'] == 0)]
new_train = pd.concat([train, usefull_test] ).reset_index(drop=True)
 new_train.loc[oof > 0.985, 'target'] = 1
new_train.loc[oof < 0.015, 'target'] = 0
return new_train
datadict=pd.DataFrame(titanic.dtypes)
 datadict["MissingVals"]=titanic.isnull().sum()
datadict["UniqueVals"]=titanic.nunique()
datadict["Count"]=titanic.count()
datadict.rename(columns={0:"dtype"} )<categorify>
oof_temp, preds_temp = instant_model(train1, test1, cols, clf = QuadraticDiscriminantAnalysis(0.5))
titanic['Title'] = titanic.Name.str.extract('([a-zA-Z]+)*\.',expand=False)
 titanicT["Title"]= titanicT.Name.str.extract('([a-zA-Z]+)*\.', expand=False)
 titanic['Title'] = titanic['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don',
'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 titanicT['Title'] = titanicT['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don',
'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 titanic['Title'] = titanic['Title'].replace('Mlle', 'Miss')
 titanic['Title'] = titanic['Title'].replace('Ms', 'Miss')
 titanic['Title'] = titanic['Title'].replace('Mme', 'Mrs')
 titanicT['Title'] = titanicT['Title'].replace('Mlle', 'Miss')
 titanicT['Title'] = titanicT['Title'].replace('Ms', 'Miss')
 titanicT['Title'] = titanicT['Title'].replace('Mme', 'Mrs')
<drop_column>
newtrain1 = get_newtrain(train1, test1, preds_temp, oof_temp)
 cols1 = [c for c in newtrain1.columns if c not in ['id', 'target', 'wheezy-copper-turtle-magic']]
oof_qda, preds_qda = instant_model(newtrain1, test1,cols1)
 oof_knn, preds_knn = instant_model(newtrain1, test1,cols1, clf = KNeighborsClassifier(n_neighbors = 7, p = 2, weights = 'distance'))
del titanic["Name"]
del titanic["Ticket"]
del titanic["Cabin"]
del titanic["PassengerId"]
del titanicT["Name"]
del titanicT["Ticket"]
del titanicT["Cabin"]
del titanicT["PassengerId"]<feature_engineering>
log = LogisticRegression()
final_feature = pd.DataFrame({'QDA':oof_qda, 'KNN':oof_knn})
 final_feature_test = pd.DataFrame({'QDA':preds_qda, 'KNN':preds_knn})
 y = newtrain1.target
log.fit(final_feature, y)
 print(roc_auc_score(newtrain1['target'], log.predict_proba(final_feature)[:,1]))
preds = log.predict_proba(final_feature_test)[:,1]
<feature_engineering><EOS> 
sub = pd.read_csv('.. /input/sample_submission.csv')
 sub['target'] = preds
sub.to_csv('submission_stack.csv',index=False )