Kaggle competition: Titanic - Machine Learning from Disaster
def get_average_word2vec(tokens_list, vector, vec_size, generate_missing=False):
if len(tokens_list)<1:
return np.zeros(vec_size)
 if generate_missing:
vectorized = [vector[word] if word in vector else np.random.rand(vec_size)for word in tokens_list]
else:
vectorized = [vector[word] if word in vector else np.zeros(vec_size)for word in tokens_list]
length = len(vectorized)
 summed = np.sum(vectorized, axis=0)
 averaged = np.divide(summed, length)
 return averaged
def get_word2vec_embeddings(vectors, ser, vec_size, generate_missing=False):
embeddings = ser.apply(
 lambda x: get_average_word2vec(x, vectors, vec_size=vec_size, generate_missing=generate_missing))
return sparse.csr_matrix(list(embeddings))<categorify>
data_train=pd.read_csv(".. /input/train.csv")
 data_test=pd.read_csv(".. /input/test.csv")
 print("Train info:
")
 data_train.info()
print("-"*40)
 print("Test info:
")
 data_test.info()
data_train.head()
train_embeddings = get_word2vec_embeddings(word2vec, df_train.tagged_tokens, vec_size=word2vec_size )<prepare_x_and_y>
Age=pd.concat([data_train[['Title','Age']],data_test[['Title','Age']]],axis=0)
 print(Age.groupby('Title')['Age'].mean())
 data_train.loc[(data_train['Age'].isnull())&(data_train['Title']=='Master'),'Age'] = Age[Age['Title']=='Master'].Age.mean()
data_train.loc[(data_train['Age'].isnull())&(data_train['Title']=='Miss'),'Age'] = Age[Age['Title']=='Miss'].Age.mean()
data_train.loc[(data_train['Age'].isnull())&(data_train['Title']=='Mr'),'Age'] = Age[Age['Title']=='Mr'].Age.mean()
data_train.loc[(data_train['Age'].isnull())&(data_train['Title']=='Mrs'),'Age'] = Age[Age['Title']=='Mrs'].Age.mean()
data_train.loc[(data_train['Age'].isnull())&(data_train['Title']=='Rare'),'Age'] = Age[Age['Title']=='Rare'].Age.mean()
data_test.loc[(data_test['Age'].isnull())&(data_test['Title']=='Master'),'Age'] = Age[Age['Title']=='Master'].Age.mean()
data_test.loc[(data_test['Age'].isnull())&(data_test['Title']=='Miss'),'Age'] = Age[Age['Title']=='Miss'].Age.mean()
data_test.loc[(data_test['Age'].isnull())&(data_test['Title']=='Mr'),'Age'] = Age[Age['Title']=='Mr'].Age.mean()
data_test.loc[(data_test['Age'].isnull())&(data_test['Title']=='Mrs'),'Age'] = Age[Age['Title']=='Mrs'].Age.mean()
data_test.loc[(data_test['Age'].isnull())&(data_test['Title']=='Rare'),'Age'] = Age[Age['Title']=='Rare'].Age.mean()
X_train, y_train = sparse.hstack(( train_embeddings, train_vectors)) , df_train.label<feature_engineering>
data_test[data_test['Fare'].isnull() ]
test_vectors = vectorizer.transform(df_test.spell_norm_text.apply(lambda tr_vect: np.str_(tr_vect)))
 df_test['tagged_tokens']= tagged_tokens(df_test.spell_norm_text)
 test_embeddings = get_word2vec_embeddings(word2vec, df_test.tagged_tokens, vec_size=word2vec_size)
 X_test = sparse.hstack(( test_embeddings, test_vectors))<train_on_grid>
Fare=pd.concat([data_train[['Fare','Pclass','Embarked','Parch','Sex','SibSp','Title']],
data_test[['Fare','Pclass','Embarked','Parch','Sex','SibSp','Title']]],axis=0)
 data_test['Fare'].fillna(Fare[(Fare["Pclass"]==3)&(Fare["Embarked"]=='S')&(Fare["SibSp"]==0)& 
(Fare["Parch"]==0)&(Fare["Sex"]=='male')&(Fare["Title"]=='Mr')].Fare.median() ,inplace=True)
 data_test.iloc[152]
sgd_w2v_clf = Pipeline(
 [('sgdc', SGDClassifier(random_state=RAND_STATE)) ])
 parameters = {
'sgdc__loss': ['hinge'],
'sgdc__penalty': ['l1', 'l2', 'elasticnet'],
'sgdc__alpha': [1.25e-5, 2.5e-5, 5e-5]}
gs_sgd_w2v_clf = GridSearchCV(sgd_w2v_clf, parameters, n_jobs=N_JOBS, verbose=VERB_LEVEL)
 gs_sgd_w2v_clf = gs_sgd_w2v_clf.fit(X_train, y_train)
 print(gs_sgd_w2v_clf.best_score_, gs_sgd_w2v_clf.best_params_ )<predict_on_test>
data_train['Cabin'] = data_train['Cabin'].str[0]
data_test['Cabin'] = data_test['Cabin'].str[0]
Cabin=pd.concat([data_train[['Cabin','Embarked','Pclass','Fare']],data_test[['Cabin','Embarked','Pclass','Fare']]],axis=0)
 Cabin.groupby(['Pclass','Embarked','Cabin'])['Fare'].max()
predict_submit(gs_sgd_w2v_clf, X_test, name_suf='sgd_w2v' )<train_on_grid>
data_train.loc[(data_train.Cabin.isnull())&(data_train.Pclass==1)& 
(data_train.Embarked=='C')&(data_train.Fare<=56.9292),'Cabin']='A'
data_train.loc[(data_train.Cabin.isnull())&(data_train.Pclass==1)& 
(data_train.Embarked=='C')&(data_train.Fare>56.9292)&(data_train.Fare<=113.2750),'Cabin']='D'
data_train.loc[(data_train.Cabin.isnull())&(data_train.Pclass==1)& 
(data_train.Embarked=='C')&(data_train.Fare>113.2750)&(data_train.Fare<=134.5000),'Cabin']='E'
data_train.loc[(data_train.Cabin.isnull())&(data_train.Pclass==1)& 
(data_train.Embarked=='C')&(data_train.Fare>134.5000)&(data_train.Fare<=227.5250),'Cabin']='C'
data_train.loc[(data_train.Cabin.isnull())&(data_train.Pclass==1)& 
(data_train.Embarked=='C')&(data_train.Fare>227.5250),'Cabin']='B'
data_train.loc[(data_train.Cabin.isnull())&(data_train.Pclass==1)& 
(data_train.Embarked=='S')&(data_train.Fare<=35.5000),'Cabin']='T'
data_train.loc[(data_train.Cabin.isnull())&(data_train.Pclass==1)& 
(data_train.Embarked=='S')&(data_train.Fare>35.5000)&(data_train.Fare<=77.9583),'Cabin']='D'
data_train.loc[(data_train.Cabin.isnull())&(data_train.Pclass==1)& 
(data_train.Embarked=='S')&(data_train.Fare>77.9583)&(data_train.Fare<=79.6500),'Cabin']='E'
data_train.loc[(data_train.Cabin.isnull())&(data_train.Pclass==1)& 
(data_train.Embarked=='S')&(data_train.Fare>79.6500)&(data_train.Fare<=81.8583),'Cabin']='A'
data_train.loc[(data_train.Cabin.isnull())&(data_train.Pclass==1)& 
(data_train.Embarked=='S')&(data_train.Fare>81.8583)&(data_train.Fare<=211.3375),'Cabin']='B'
data_train.loc[(data_train.Cabin.isnull())&(data_train.Pclass==1)& 
(data_train.Embarked=='S')&(data_train.Fare>211.3375),'Cabin']='C'
data_train.loc[(data_train.Cabin.isnull())&(data_train.Pclass==1)&(data_train.Embarked=='Q'),'Cabin']='C'
data_train.loc[(data_train.Cabin.isnull())&(data_train.Pclass==2)& 
(data_train.Embarked=='S')&(data_train.Fare<=13.0000),'Cabin']=random.sample(['D','E'],1)
 data_train.loc[(data_train.Cabin.isnull())&(data_train.Pclass==2)& 
(data_train.Embarked=='S')&(data_train.Fare>13.0000),'Cabin']='F'
data_train.loc[(data_train.Cabin.isnull())&(data_train.Pclass==2)&(data_train.Embarked=='C'),'Cabin']='D'
data_train.loc[(data_train.Cabin.isnull())&(data_train.Pclass==2)&(data_train.Embarked=='Q'),'Cabin']='E'
data_train.loc[(data_train.Cabin.isnull())&(data_train.Pclass==3)& 
(data_train.Embarked=='S')&(data_train.Fare<=7.6500),'Cabin']='F'
data_train.loc[(data_train.Cabin.isnull())&(data_train.Pclass==3)& 
(data_train.Embarked=='S')&(data_train.Fare>7.6500)&(data_train.Fare<=12.4750),'Cabin']='E'
data_train.loc[(data_train.Cabin.isnull())&(data_train.Pclass==3)& 
(data_train.Embarked=='S')&(data_train.Fare>12.4750),'Cabin']='G'
data_train.loc[(data_train.Cabin.isnull())&(data_train.Pclass==3)&(data_train.Embarked=='C'),'Cabin']='F'
data_train.loc[(data_train.Cabin.isnull())&(data_train.Pclass==3)&(data_train.Embarked=='Q'),'Cabin']='F'
data_test.loc[(data_test.Cabin.isnull())&(data_test.Pclass==1)& 
(data_test.Embarked=='C')&(data_test.Fare<=56.9292),'Cabin']='A'
data_test.loc[(data_test.Cabin.isnull())&(data_test.Pclass==1)& 
(data_test.Embarked=='C')&(data_test.Fare>56.9292)&(data_test.Fare<=113.2750),'Cabin']='D'
data_test.loc[(data_test.Cabin.isnull())&(data_test.Pclass==1)& 
(data_test.Embarked=='C')&(data_test.Fare>113.2750)&(data_test.Fare<=134.5000),'Cabin']='E'
data_test.loc[(data_test.Cabin.isnull())&(data_test.Pclass==1)& 
(data_test.Embarked=='C')&(data_test.Fare>134.5000)&(data_test.Fare<=227.5250),'Cabin']='C'
data_test.loc[(data_test.Cabin.isnull())&(data_test.Pclass==1)& 
(data_test.Embarked=='C')&(data_test.Fare>227.5250),'Cabin']='B'
data_test.loc[(data_test.Cabin.isnull())&(data_test.Pclass==1)& 
(data_test.Embarked=='S')&(data_test.Fare<=35.5000),'Cabin']='T'
data_test.loc[(data_test.Cabin.isnull())&(data_test.Pclass==1)& 
(data_test.Embarked=='S')&(data_test.Fare>35.5000)&(data_test.Fare<=77.9583),'Cabin']='D'
data_test.loc[(data_test.Cabin.isnull())&(data_test.Pclass==1)& 
(data_test.Embarked=='S')&(data_test.Fare>77.9583)&(data_test.Fare<=79.6500),'Cabin']='E'
data_test.loc[(data_test.Cabin.isnull())&(data_test.Pclass==1)& 
(data_test.Embarked=='S')&(data_test.Fare>79.6500)&(data_test.Fare<=81.8583),'Cabin']='A'
data_test.loc[(data_test.Cabin.isnull())&(data_test.Pclass==1)& 
(data_test.Embarked=='S')&(data_test.Fare>81.8583)&(data_test.Fare<=211.3375),'Cabin']='B'
data_test.loc[(data_test.Cabin.isnull())&(data_test.Pclass==1)& 
(data_test.Embarked=='S')&(data_test.Fare>211.3375),'Cabin']='C'
data_test.loc[(data_test.Cabin.isnull())&(data_test.Pclass==1)&(data_test.Embarked=='Q'),'Cabin']='C'
data_test.loc[(data_test.Cabin.isnull())&(data_test.Pclass==2)& 
(data_test.Embarked=='S')&(data_test.Fare<=13.0000),'Cabin']=random.sample(['D','E'],1)
 data_test.loc[(data_test.Cabin.isnull())&(data_test.Pclass==2)& 
(data_test.Embarked=='S')&(data_test.Fare>13.0000),'Cabin']='F'
data_test.loc[(data_test.Cabin.isnull())&(data_test.Pclass==2)&(data_test.Embarked=='C'),'Cabin']='D'
data_test.loc[(data_test.Cabin.isnull())&(data_test.Pclass==2)&(data_test.Embarked=='Q'),'Cabin']='E'
data_test.loc[(data_test.Cabin.isnull())&(data_test.Pclass==3)& 
(data_test.Embarked=='S')&(data_test.Fare<=7.6500),'Cabin']='F'
data_test.loc[(data_test.Cabin.isnull())&(data_test.Pclass==3)& 
(data_test.Embarked=='S')&(data_test.Fare>7.6500)&(data_test.Fare<=12.4750),'Cabin']='E'
data_test.loc[(data_test.Cabin.isnull())&(data_test.Pclass==3)& 
(data_test.Embarked=='S')&(data_test.Fare>12.4750),'Cabin']='G'
data_test.loc[(data_test.Cabin.isnull())&(data_test.Pclass==3)&(data_test.Embarked=='C'),'Cabin']='F'
data_test.loc[(data_test.Cabin.isnull())&(data_test.Pclass==3)&(data_test.Embarked=='Q'),'Cabin']='F'
print(data_test.Cabin.isnull().any() ,'
')
 print(data_train.Cabin.isnull().any() )
logreg_w2v_clf = Pipeline(
 [('logreg', LogisticRegression(random_state=RAND_STATE)) ,])
 parameters = {
'logreg__C': [11, 14, 17],
'logreg__multi_class': ['ovr', 'multinomial'],
'logreg__solver': ['lbfgs', 'newton-cg']}
gs_logreg_w2v_clf = GridSearchCV(logreg_w2v_clf, parameters, n_jobs=N_JOBS, verbose=VERB_LEVEL)
 gs_logreg_w2v_clf = gs_logreg_w2v_clf.fit(X_train, y_train)
 print(gs_logreg_w2v_clf.best_score_, gs_logreg_w2v_clf.best_params_ )<predict_on_test>
Cabin=pd.concat([data_train[['Cabin','Embarked','Pclass','Fare']],data_test[['Cabin','Embarked','Pclass','Fare']]],axis=0)
 data_train[data_train['Embarked'].isnull() ]
predict_submit(gs_logreg_w2v_clf, X_test, name_suf='logreg_w2v' )<load_from_csv>
print('Train columns with null values:
',data_train.isnull().sum())
 print("-"*40)
 print('Test columns with null values:
',data_test.isnull().sum() )
df_train = pd.read_csv('.. /input/web-club-recruitment-2018/train.csv')
 df_test = pd.read_csv('.. /input/web-club-recruitment-2018/test.csv')
 corr_matrix=df_train.corr()
corr_matrix['Y'].sort_values() 
<drop_column>
data_train['Sex'].replace(['male','female'],[0,1],inplace=True)
 data_train['Embarked'].replace(['C','Q','S'],[0,1,2],inplace=True)
 data_train['Title'].replace(['Master','Miss','Mr','Mrs','Rare'],[0,1,2,3,4],inplace=True)
 data_train['Cabin'].replace(['A','B','C','D','E','F','G','T'],[0,1,2,3,4,5,6,7],inplace=True)
 data_train.loc[data_train['Age']<=16,'Age']=0
data_train.loc[(data_train['Age']>16)&(data_train['Age']<=32),'Age']=1
data_train.loc[(data_train['Age']>32)&(data_train['Age']<=48),'Age']=2
data_train.loc[(data_train['Age']>48)&(data_train['Age']<=64),'Age']=3
data_train.loc[data_train['Age']>64,'Age']=4
data_test['Sex'].replace(['male','female'],[0,1],inplace=True)
 data_test['Embarked'].replace(['C','Q','S'],[0,1,2],inplace=True)
 data_test['Title'].replace(['Master','Miss','Mr','Mrs','Rare'],[0,1,2,3,4],inplace=True)
 data_test['Cabin'].replace(['A','B','C','D','E','F','G','T'],[0,1,2,3,4,5,6,7],inplace=True)
 data_test.loc[data_test['Age']<=16,'Age']=0
data_test.loc[(data_test['Age']>16)&(data_test['Age']<=32),'Age']=1
data_test.loc[(data_test['Age']>32)&(data_test['Age']<=48),'Age']=2
data_test.loc[(data_test['Age']>48)&(data_test['Age']<=64),'Age']=3
data_test.loc[data_test['Age']>64,'Age']=4
df_train=df_train.drop('X12',axis=1)
 df_test=df_test.drop('X12',axis=1)
 train_ID = df_train['id']
test_ID = df_test['id']
df_train=df_train.drop('id',axis=1)
 df_test=df_test.drop('id',axis=1)
 df_train['X1']=np.log1p(df_train['X1'])
 df_test['X1']=np.log1p(df_test['X1'])
 df_train<create_dataframe>
data_train['Family_Size']=0
data_train['Family_Size']=data_train['Parch']+data_train['SibSp']
data_test['Family_Size']=0
data_test['Family_Size']=data_test['Parch']+data_test['SibSp']
imputer=Imputer(strategy="median")
 col=df_train.columns
cols=df_test.columns
df_train=imputer.fit_transform(df_train)
 df_train=pd.DataFrame(df_train,columns=col)
 df_test=imputer.fit_transform(df_test)
 df_test=pd.DataFrame(df_test,columns=cols)
<prepare_x_and_y>
data_train.loc[data_train['Fare']<=8.0500,'Fare']=0
data_train.loc[(data_train['Fare']>8.0500)&(data_train['Fare']<=15.0458),'Fare']=1
data_train.loc[(data_train['Fare']>15.0458)&(data_train['Fare']<=60.0000),'Fare']=2
data_train.loc[data_train['Fare']>60.0000,'Fare']=3
data_test.loc[data_test['Fare']<=8.0500,'Fare']=0
data_test.loc[(data_test['Fare']>8.0500)&(data_test['Fare']<=15.0458),'Fare']=1
data_test.loc[(data_test['Fare']>15.0458)&(data_test['Fare']<=60.0000),'Fare']=2
data_test.loc[data_test['Fare']>60.0000,'Fare']=3
f,ax=plt.subplots(1,2,figsize=(12,5))
sns.countplot('Fare',data=data_train,hue='Survived',ax=ax[0])
 ax[0].set_title('Survived',color = 'r',fontsize=15)
 sns.barplot(x=data_train.groupby(['Fare'])['Survived'].mean().index,
y=data_train.groupby(['Fare'])['Survived'].mean().values,
ax=ax[1])
 ax[1].set_title('Rate of the Survived by Fare',color = 'r',fontsize=15)
 plt.show()
train_X = df_train.loc[:, 'X1':'X23']
train_y = df_train.loc[:, 'Y']<count_missing_values>
data_train.drop(['Name','Ticket','PassengerId'],axis=1,inplace=True)
 data_test.drop(['Name','Ticket','PassengerId'],axis=1,inplace=True )
col_mask=train_X.isnull().any(axis=0)
 col_mask<split>
y=data_train['Survived']
x=data_train.drop(['Survived'],axis=1 )
dev_X, val_X, dev_y, val_y = train_test_split(train_X, train_y, test_size = 0.2, random_state = 42)
 params = {'objective': 'binary:logistic','eval_metric': 'rmse', 'eta': 0.005, 'max_depth': 10, 'subsample': 0.7, 'colsample_bytree': 0.5, 'alpha':0, 'silent': True, 'random_state':5}
tr_data = xgb.DMatrix(train_X, train_y)
 va_data = xgb.DMatrix(val_X, val_y)
 watchlist = [(tr_data, 'train'),(va_data, 'valid')]
model_xgb = xgb.train(params, tr_data, 2000, watchlist, maximize=False, early_stopping_rounds = 30, verbose_eval=100)
 dft = xgb.DMatrix(df_test)
 xgb_pred_y = np.log1p(model_xgb.predict(dft, ntree_limit=model_xgb.best_ntree_limit))<save_to_csv>
x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=0.2,random_state=1)
 print("x train: ",x_train.shape)
 print("x test: ",x_test.shape)
 print("y train: ",y_train.shape)
 print("y test: ",y_test.shape )
result = pd.DataFrame()
result['id']=test_ID
result['predicted_val']=xgb_pred_y
print(result.head())
 result.to_csv('output.csv',index=False )<set_options>
from sklearn import metrics
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import cross_val_predict
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import confusion_matrix
from sklearn.metrics import roc_curve
%reload_ext autoreload
%autoreload 2
%matplotlib inline<install_modules>
kfold = KFold(n_splits=10, random_state=22)
 mean=[]
accuracy=[]
std=[]
def model(algorithm,x_train_,y_train_,x_test_,y_test_):
algorithm.fit(x_train_,y_train_)
 predicts=algorithm.predict(x_test_)
 prediction=pd.DataFrame(predicts)
 prob=algorithm.predict_proba(x_test_)[:,1]
cross_val=cross_val_score(algorithm,x_train_,y_train_,cv=kfold)
 mean.append(cross_val.mean())
 std.append(cross_val.std())
 accuracy.append(cross_val)
 print(( '{}'.format(algorithm)).split("(")[0].strip() ,'
')
 print("CV std :",cross_val.std() ,"
")
 print("CV scores:",cross_val,"
")
 print("CV mean:",cross_val.mean())
 fpr, tpr, thresholds = roc_curve(y_test_, prob)
 f,ax=plt.subplots(1,2,figsize=(11,4))
plt.plot([0, 1], [0, 1], 'k--')
 plt.plot(fpr, tpr)
 plt.xlabel('False Positive Rate')
 plt.ylabel('True Positive Rate')
 plt.title('ROC')
 y_pred = cross_val_predict(algorithm,x,y,cv=10)
 sns.heatmap(confusion_matrix(y,y_pred),ax=ax[0],annot=True,fmt='2.0f')
 ax[0].set_title(( 'Confusion Matrix for {}'.format(algorithm)).split("(")[0].strip())
 plt.subplots_adjust(wspace=0.3)
 plt.close(0)
 plt.show()
! pip install pretrainedmodels<set_options>
grids = {'n_neighbors': np.arange(1,50)}
grid = GridSearchCV(estimator=KNeighborsClassifier() , param_grid=grids, cv=kfold)
 grid.fit(x_train, y_train)
 print("Tuned hyperparameter k: {}".format(grid.best_params_),'
')
 print("Best score: {}".format(grid.best_score_))
%matplotlib inline
warnings.filterwarnings('always')
 warnings.filterwarnings('ignore')
 style.use('fivethirtyeight')
 sns.set(style='whitegrid',color_codes=True)
<import_modules>
knn = KNeighborsClassifier(n_neighbors = grid.best_estimator_.n_neighbors)
 model(knn,x_train,y_train,x_test,y_test )
from fastai import *
from fastai.vision import *
import pretrainedmodels<define_variables>
Cs = [0.001, 0.01, 0.1, 1, 10]
gammas = [0.001, 0.01, 0.1, 1]
grids = {'C': Cs, 'gamma' : gammas}
grid = GridSearchCV(estimator=svm.SVC(kernel='linear'), param_grid=grids, cv=kfold)
 grid.fit(x_train, y_train)
 print("Tuned hyperparameter k: {}".format(grid.best_params_),'
')
 print("Best score: {}".format(grid.best_score_))
train_dir = '.. /input/ifood-2019-fgvc6/train_set/train_set/'
val_dir = '.. /input/ifood-2019-fgvc6/val_set/val_set/'<load_from_csv>
svm = svm.SVC(kernel='linear',C=grid.best_estimator_.C,gamma=grid.best_estimator_.gamma,probability=True)
 model(svm,x_train,y_train,x_test,y_test )
train_df = pd.read_csv('.. /input/ifood-2019-fgvc6/train_labels.csv')
 train_df['path'] = train_df['img_name'].map(lambda x: os.path.join(train_dir,x))
val_df = pd.read_csv('.. /input/ifood-2019-fgvc6/val_labels.csv')
 val_df['path'] = val_df['img_name'].map(lambda x: os.path.join(val_dir,x))<concatenate>
nb = GaussianNB()
model(nb,x_train,y_train,x_test,y_test )
df = pd.concat([train_df, val_df], ignore_index=True)
 df.head()<define_variables>
grids={'min_samples_split' : range(10,500,20),'max_depth': range(1,20,2)}
grid = GridSearchCV(estimator=DecisionTreeClassifier() , param_grid=grids, cv=kfold)
 grid.fit(x_train, y_train)
 print("Tuned hyperparameter k: {}".format(grid.best_params_),'
')
 print("Best score: {}".format(grid.best_score_))
val_idx = [i for i in range(len(train_df), len(df)) ]<define_variables>
dtc = DecisionTreeClassifier(min_samples_split=grid.best_estimator_.min_samples_split, max_depth=grid.best_estimator_.max_depth)
 model(dtc,x_train,y_train,x_test,y_test )
sz = 256
bs = 32<define_variables>
grids={'n_estimators':range(100,500,100)}
grid = GridSearchCV(estimator=RandomForestClassifier() , param_grid=grids, cv=kfold)
 grid.fit(x_train, y_train)
 print("Tuned hyperparameter k: {}".format(grid.best_params_),'
')
 print("Best score: {}".format(grid.best_score_))
data.show_batch(rows=3, figsize=(12,9))<set_options>
rf = RandomForestClassifier(n_estimators=grid.best_estimator_.n_estimators)
 model(rf,x_train,y_train,x_test,y_test )
gc.collect()<compute_test_metric>
grids = {'C': np.logspace(-3, 3, 7), 'penalty': ['l1', 'l2']}
grid = GridSearchCV(estimator=LogisticRegression() , param_grid=grids, cv=kfold)
 grid.fit(x_train, y_train)
 print("Tuned hyperparameter k: {}".format(grid.best_params_),'
')
 print("Best score: {}".format(grid.best_score_))
def top_3_accuracy(preds, targs):
return top_k_accuracy(preds, targs, 3 )<choose_model_class>
lr = LogisticRegression(C=grid.best_estimator_.C,penalty=grid.best_estimator_.penalty)
 model(lr,x_train,y_train,x_test,y_test )
model_name = 'se_resnext101_32x4d'
def get_cadene_model(pretrained=True, model_name='se_resnext101_32x4d'):
if pretrained:
arch = pretrainedmodels.__dict__[model_name](num_classes=1000, pretrained='imagenet')
 else:
arch = pretrainedmodels.__dict__[model_name](num_classes=1000, pretrained=None)
 return arch<choose_model_class>
classifiers=['KNN','Svm','Naive Bayes','Decision Tree','Random Forest','Logistic Regression']
models=pd.DataFrame({'CV mean':mean,'Std':std},index=classifiers)
 print(models )
<choose_model_class><EOS> 
submission = pd.DataFrame({"PassengerId": pd.read_csv(".. /input/test.csv")["PassengerId"],"Survived": dtc.predict(data_test)})
 submission.to_csv('titanic.csv', index=False )