Kaggle competition: Instant Gratification
train.dropna(inplace=True )<categorify>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
le=LabelEncoder()
for col in cat_features:
le.fit(train[col])
 test[col]=le.transform(test[col].map(str))
train[col]=le.transform(train[col].map(str))<drop_column>
sum(train.isna().any() )
test.drop('PassengerId',axis=1,inplace=True)
 train.drop('PassengerId',axis=1,inplace=True )<drop_column>
df = pd.concat([train, test], sort=False )
feature=train.drop(['Survived'],axis=1)
 label=train.Survived
feature.head()<compute_train_metric>
from sklearn.feature_selection import VarianceThreshold
from sklearn.preprocessing import StandardScaler
dmatrix=xgb.DMatrix(feature,label)
 params={'objective':'binary:logistic','max_depth':5,'colsample_bytree':.6,'eta':.1,'alpha':1}
result=xgb.cv(params,dmatrix,early_stopping_rounds=10,as_pandas=True,num_boost_round=100,nfold=5,metrics='error')
 result<train_model>
def get_best_param(x, y, modelo, params):
grid = GridSearchCV(estimator=modelo, param_grid=params, cv=3, n_jobs=-1)
 grid_result = grid.fit(x, y)
 return(grid_result.best_score_, grid_result.best_params_)
 def testa_tudo(train, alvo):

 start_time = time.time()
melhores = dict()
models = {
'QuadraticDiscriminantAnalysis': QuadraticDiscriminantAnalysis() ,
}
params = {
'QuadraticDiscriminantAnalysis': {'tol': [1,0.1,0.01,0.001,0.0001,0.00001]}
}
for nome, modelo in models.items() :
param = params[nome]
melhores[nome] = get_best_param(train, alvo, modelo, param)
 print("Modelo: %s, melhor classificação: %f usando %s" %(nome, melhores[nome][0], melhores[nome][1]))
print("Tempo rodando: " + str(( time.time() - start_time)) + ' ms')
 return melhores
xgb_cl=xgb.train(params,dmatrix,num_boost_round=19)
 xgb.plot_importance(xgb_cl )<define_variables>
def qual_melhor(melhores):
melhor = {'score': 0, 'nome': '', 'params': None}
for nome, res in melhores.items() :
if res[0] > melhor['score']:
melhor['score'] = res[0]
melhor['nome'] = nome
melhor['params'] = res[1]
return melhor
def roda_melhor(melhor, train, target, test):
models = {
'QuadraticDiscriminantAnalysis': QuadraticDiscriminantAnalysis() ,
}
modelo = models[melhor['nome']].set_params(**melhor['params'])
 modelo.fit(train, target)
 return modelo.predict_proba(test)
feature.index=list(range(len(feature)))
 label.index=list(range(len(label)) )<train_model>
size_x = train.shape[0]
size_y = test.shape[0]
skf=StratifiedKFold(n_splits=5,shuffle=True,random_state=123)
 def validate(estimator,feature,label):
score=[]
for trainind,testind in skf.split(feature,label):
X_train,y_train=feature.loc[trainind],label[trainind]
X_test,y_test=feature.loc[testind],label[testind]
estimator.fit(X_train,y_train)
 score.append(accuracy_score(y_test,estimator.predict(X_test)))
 return score<train_model>
cols = [c for c in df.columns if c not in ['id', 'target', 'wheezy-copper-turtle-magic']]
oof = np.zeros(len(train))
preds = np.zeros(len(test))
for i in range(df['wheezy-copper-turtle-magic'].max() +1):
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)
 df2 = pd.concat([train2[cols], test2[cols]])
 df2 = VarianceThreshold(threshold=1.5 ).fit_transform(df2[cols])
 train3 = df2[:train2.shape[0]]
test3 = df2[train2.shape[0]:]
skf = StratifiedKFold(n_splits=10, random_state=666)
 for train_index, test_index in skf.split(train2, train2['target']):
clf = QuadraticDiscriminantAnalysis(tol=1)
 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}' )
xgb_cl=xgb.XGBClassifier(max_depth=10,colsample_bytree=.6,learning_rate=.01,alpha=1,)
 validate(xgb_cl,feature,label )<train_model>
test['target'] = preds
oof = np.zeros(len(train))
preds = np.zeros(len(test))
for k in range(512):
train2 = train[train['wheezy-copper-turtle-magic']==k]
train2p = train2.copy() ; idx1 = train2.index
test2 = test[test['wheezy-copper-turtle-magic']==k]
test2p = test2[(test2['target']<=0.01)|(test2['target']>=0.99)].copy()
test2p.loc[ test2p['target']>=0.5, 'target' ] = 1
test2p.loc[ test2p['target']<0.5, 'target' ] = 0
train2p = pd.concat([train2p,test2p],axis=0)
 train2p.reset_index(drop=True,inplace=True)
 sel = VarianceThreshold(threshold=1.5 ).fit(train2p[cols])
 train3p = sel.transform(train2p[cols])
 train3 = sel.transform(train2[cols])
 test3 = sel.transform(test2[cols])
 skf = StratifiedKFold(n_splits=11, random_state=42, shuffle=True)
 for train_index, test_index in skf.split(train3p, train2p['target']):
test_index3 = test_index[ test_index<len(train3)]
clf = QuadraticDiscriminantAnalysis(reg_param=0.5)
 clf.fit(train3p[train_index,:],train2p.loc[train_index]['target'])
 oof[idx1[test_index3]] = clf.predict_proba(train3[test_index3,:])[:,1]
preds[test2.index] += clf.predict_proba(test3)[:,1] / skf.n_splits
auc = roc_auc_score(train['target'],oof)
 print('Pseudo Labeled QDA scores CV =',round(auc,5))
<import_modules><EOS> 
sub = pd.read_csv('.. /input/sample_submission.csv')
 sub['target'] = preds
sub.to_csv('submission.csv',index=False )