Kaggle competition: Instant Gratification
<SOS> metric: AUC Kaggle data source: instant-gratification<count_missing_values>
warnings.filterwarnings("ignore")
sum(X_test.isnull().sum())
 X.shape<import_modules>
train = pd.read_csv('.. /input/train.csv')
 train_t = train.copy()
test = pd.read_csv('.. /input/test.csv' )
cross_val_score, \
StratifiedKFold, \
learning_curve,\
KFold,\
cross_val_predict;
sns.set(style='white', context='notebook', palette='deep');
warnings.filterwarnings(action='ignore', category=UserWarning)
<train_model>
cols = [c for c in train.columns if c not in ['id', 'target', 'wheezy-copper-turtle-magic']]
reg_best = [0.5,
0.2,
0.3,
0.2,
0.5,
0.1,
0.1,
0.2,
0.3,
0.5,
0.2,
0.4,
0.1,
0.3,
0.1,
0.4,
0.3,
0.2,
0.2,
0.5,
0.1,
0.4,
0.4,
0.1,
0.5,
0.4,
0.1,
0.4,
0.4,
0.1,
0.1,
0.3,
0.4,
0.1,
0.5,
0.2,
0.3,
0.1,
0.1,
0.5,
0.5,
0.5,
0.3,
0.5,
0.4,
0.1,
0.1,
0.1,
0.5,
0.5,
0.5,
0.1,
0.3,
0.1,
0.1,
0.4,
0.2,
0.3,
0.1,
0.1,
0.5,
0.2,
0.4,
0.1,
0.1,
0.1,
0.1,
0.4,
0.2,
0.1,
0.1,
0.5,
0.4,
0.1,
0.3,
0.2,
0.4,
0.1,
0.3,
0.5,
0.1,
0.5,
0.1,
0.5,
0.1,
0.1,
0.4,
0.5,
0.4,
0.2,
0.1,
0.1,
0.4,
0.5,
0.2,
0.5,
0.5,
0.4,
0.1,
0.5,
0.5,
0.3,
0.5,
0.2,
0.4,
0.4,
0.1,
0.4,
0.4,
0.1,
0.1,
0.5,
0.5,
0.5,
0.1,
0.2,
0.4,
0.1,
0.4,
0.5,
0.5,
0.5,
0.2,
0.2,
0.2,
0.5,
0.1,
0.1,
0.3,
0.5,
0.3,
0.1,
0.4,
0.1,
0.3,
0.1,
0.2,
0.5,
0.5,
0.1,
0.1,
0.1,
0.4,
0.1,
0.5,
0.5,
0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0.4,
0.5,
0.1,
0.3,
0.5,
0.5,
0.3,
0.5,
0.1,
0.3,
0.1,
0.4,
0.3,
0.5,
0.5,
0.5,
0.4,
0.2,
0.5,
0.5,
0.5,
0.5,
0.1,
0.1,
0.1,
0.5,
0.4,
0.3,
0.1,
0.5,
0.5,
0.2,
0.3,
0.5,
0.5,
0.1,
0.1,
0.1,
0.5,
0.3,
0.1,
0.4,
0.1,
0.1,
0.5,
0.5,
0.4,
0.1,
0.5,
0.4,
0.2,
0.5,
0.1,
0.4,
0.1,
0.1,
0.1,
0.4,
0.2,
0.1,
0.2,
0.2,
0.5,
0.4,
0.1,
0.1,
0.1,
0.4,
0.5,
0.4,
0.1,
0.1,
0.1,
0.1,
0.1,
0.1,
0.3,
0.5,
0.3,
0.5,
0.5,
0.5,
0.5,
0.5,
0.3,
0.5,
0.5,
0.1,
0.5,
0.1,
0.1,
0.1,
0.1,
0.2,
0.1,
0.5,
0.5,
0.1,
0.1,
0.4,
0.3,
0.1,
0.2,
0.1,
0.1,
0.1,
0.3,
0.5,
0.2,
0.1,
0.3,
0.2,
0.4,
0.4,
0.2,
0.1,
0.3,
0.1,
0.1,
0.4,
0.1,
0.2,
0.4,
0.5,
0.3,
0.1,
0.1,
0.5,
0.5,
0.5,
0.5,
0.5,
0.1,
0.1,
0.1,
0.1,
0.4,
0.1,
0.4,
0.2,
0.1,
0.1,
0.4,
0.1,
0.5,
0.2,
0.1,
0.1,
0.3,
0.5,
0.1,
0.5,
0.5,
0.1,
0.1,
0.2,
0.1,
0.1,
0.1,
0.2,
0.3]
def train_model(estimator=None, X_train=None,y_train=None,X_cv=None,y_cv=None,scoring=None):
m = estimator
if(scoring == 'neg_mean_squared_error'):
m.fit(X_train,np.log1p(y_train))
if(scoring == 'neg_mean_squared_log_error'):
m.fit(X_train,y_train)
 pred=estimator.predict(X_cv)
 if(scoring == 'neg_mean_squared_error'):
score = np.sqrt(mean_squared_error(np.log1p(y_cv),pred))
if(scoring == 'neg_mean_squared_log_error'):
score = np.sqrt(mean_squared_log_error(y_cv,pred))
return score,m<save_to_csv>
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
train2.reset_index(drop=True,inplace=True)
 data = pd.concat([pd.DataFrame(train2[cols]), pd.DataFrame(test2[cols])])
 data2 = VarianceThreshold(threshold=2 ).fit_transform(data[cols])
 train3 = data2[:train2.shape[0]]; test3 = data2[train2.shape[0]:]
skf = StratifiedKFold(n_splits=11, random_state=42)
 for train_index, test_index in skf.split(train2, train2['target']):
clf = QuadraticDiscriminantAnalysis(reg_best[i])
 clf.fit(train3[train_index,:],train2.loc[train_index]['target'])
 oof[idx1[test_index]] = clf.predict_proba(train3[test_index,:])[:,1]
preds[idx2] += clf.predict_proba(test3)[:,1] / skf.n_splits
auc = roc_auc_score(train['target'], oof)
 print(f'AUC: {auc:.5}')
def gen_submission(csvname=None,X_test=None,models=None,scoring=None):
if(scoring == 'neg_mean_squared_error'):
pred=np.expm1(models.predict(X_test))
if(scoring == 'neg_mean_squared_log_error'):
pred=models.predict(X_test)
 result = pd.concat([ids,pd.Series(pred ).astype('float64')],axis=1);
result.columns = ['Id','SalePrice']
result.to_csv(csvname+r'.csv',index=False)
 pass<data_type_conversions>
auc = roc_auc_score(train_t['target'], oof)
 print(f'AUC: {auc:.5}' )
X = X.astype('float64' )<count_missing_values>
train.loc[oof > 0.99, 'target'] = 1
train.loc[oof < 0.01, 'target'] = 0
sum(np.isnan(X ).all() )<count_values>
oof_ls = np.zeros(len(train))
pred_te_ls = np.zeros(len(test))
for k in tqdm_notebook(range(512)) :
train2 = train[train['wheezy-copper-turtle-magic']==k]
train2p = train2.copy() ; idx1 = train2.index
test2 = test[test['wheezy-copper-turtle-magic']==k]
test2['target']=-1
train2p = pd.concat([train2,test2],axis=0)
 train2p.reset_index(drop=True,inplace=True)
 sel = VarianceThreshold(threshold=1.5 ).fit(train2p[cols])
 train4p = sel.transform(train2p[cols])
 train4 = sel.transform(train2[cols])
 test4 = sel.transform(test2[cols])
 skf = StratifiedKFold(n_splits=11, random_state=42, shuffle=True)
 for train_index, test_index in skf.split(train4p, train2p['target']):
test_index3 = test_index[ test_index<len(train4)]
clf = LabelSpreading(gamma=0.01,kernel='rbf', max_iter=10,n_jobs=-1)
 clf.fit(train4p[train_index,:],train2p.loc[train_index]['target'])
 oof_ls[idx1[test_index3]] = clf.predict_proba(train4[test_index3,:])[:,1]
pred_te_ls[test2.index] += clf.predict_proba(test4)[:,1] / skf.n_splits
auc = roc_auc_score(train['target'],oof_ls)
 print('CV for LabelSpreading =',round(auc,5))
sum(np.isinf(X ).any() )<drop_column>
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
train2.reset_index(drop=True,inplace=True)
 data = pd.concat([pd.DataFrame(train2[cols]), pd.DataFrame(test2[cols])])
 data2 = VarianceThreshold(threshold=2 ).fit_transform(data[cols])
 train3 = data2[:train2.shape[0]]; test3 = data2[train2.shape[0]:]
train4 = np.hstack([train3,np.array([oof_ls[idx1]] ).T])
 test4 = np.hstack([test3,np.array([pred_te_ls[idx2]] ).T])
 skf = StratifiedKFold(n_splits=11, random_state=42)
 for train_index, test_index in skf.split(train4, train2['target']):
clf = QuadraticDiscriminantAnalysis(reg_best[i])
 clf.fit(train4[train_index,:],train2.loc[train_index]['target'])
 oof[idx1[test_index]] = clf.predict_proba(train4[test_index,:])[:,1]
preds[idx2] += clf.predict_proba(test4)[:,1] / skf.n_splits
auc = roc_auc_score(train_t['target'], oof)
 print(f'AUC: {auc:.5}' )
<split><EOS> 
sub = pd.read_csv('.. /input/sample_submission.csv')
 sub['target'] = preds
sub.to_csv('submission.csv',index=False )