Kaggle competition: Don't Overfit! II
clf = RandomForestClassifier(n_estimators=150,criterion='gini',max_features='log2')
 param = {'max_depth':[None,5,7,9,12],
'min_samples_split' : [3,5,7],
'min_weight_fraction_leaf' : [0.0,0.1],
'max_leaf_nodes' : [40,60,80,100]
}
optimzation = GridSearchCV(clf,param_grid = param , cv=5 , scoring='accuracy')
 optimzation.fit(x_train,y)
 print('Best score : ' , optimzation.best_score_)
 print('Best parameter : ' , optimzation.best_params_ )<train_model>
df = pd.read_csv('.. /input/train.csv')
 df.head()
features = ['16', '33', '43', '45', '52', '63', '65', '73', '90', '91', '117', '133', '134', '149', '189', '199', '217', '237', '258', '295']
clf = RandomForestClassifier(n_estimators=150,
criterion='gini',
max_features='log2',
min_samples_split=5,
min_weight_fraction_leaf=0.1,
max_leaf_nodes=100,
max_depth=None)
 clf.fit(x_train,y)
 y_pred = clf.predict(x_test)
 print(clf.classes_)
 print(le.classes_)
 pred = []
for yped in y_pred:
if yped ==0:
pred.append('Ghost')
 elif yped == 1:
pred.append('Ghoul')
 else:
pred.append('Goblin')
 result = pd.DataFrame({'id':test_ids,'type':pred})
 result.to_csv('randomforest_ghost.csv',index=False )<train_model>
X = df[features].values
y = df.values[:,1]
print(X.shape, y.shape )
X_train,X_test,y_train,y_test = train_test_split(x_train,y,test_size=0.2,random_state=42)
 clf = SVC(kernel='rbf',probability=True)
 clf.fit(X_train,y_train)
 y_pred = clf.predict(X_test)
 print('Accuracy %f : ' %(accuracy_score(y_test,y_pred)) )<train_model>
def simple_model(input_shape):

 inp = Input(shape=(input_shape[1],))
x = Dense(3, activation='sigmoid' )(inp)
 x = Dense(1, activation='sigmoid' )(x)
 model = Model(inputs=inp, outputs=x)
 model.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.005), metrics=['accuracy'])
 return model
dtrain = xgb.DMatrix(x_train,label=y)
 dtest = xgb.DMatrix(x_test)
 xgb_params = {
'seed': 0,
'colsample_bytree': 0.7,
'silent': 1,
'subsample': 0.7,
'learning_rate': 0.075,
"objective": "multi:softmax",
'max_depth': 7,
'num_parallel_tree': 1,
'min_child_weight': 1,
'num_class':3
}
clf = xgb.train(xgb_params,dtrain,num_boost_round=350)
 y_pred = clf.predict(dtest)
 pred = []
for yped in y_pred:
if yped ==0:
pred.append('Ghost')
 elif yped == 1:
pred.append('Ghoul')
 else:
pred.append('Goblin')
 result = pd.DataFrame({'id':test_ids,'type':pred})
 result.to_csv('xgb_ghost.csv',index=False )<import_modules>
N_SPLITS = 10
splits = list(StratifiedKFold(n_splits=N_SPLITS, shuffle=True ).split(X, y))
preds_val = []
y_val = []
best_models = []
for idx,(train_idx, val_idx)in enumerate(splits):
print("Beginning fold {}".format(idx+1))
X_train, y_train, X_val, y_val = X[train_idx], y[train_idx], X[val_idx], y[val_idx]
model = simple_model(X_train.shape)
 cb = ModelCheckpoint('weights.h5', monitor='val_acc', mode='max', save_best_only=True, save_weights_only=True)
 model.fit(X_train, y_train, epochs=200, validation_data=(X_val, y_val), callbacks=[cb], verbose=0)
 model.load_weights('weights.h5')
 score = roc_auc_score(y_val, model.predict(X_val))
print(( model, score))
best_models.append(( model, score))
import numpy as np
import pandas as pd
import os
import tensorflow as tf
import tensorflow_addons as tfa
import numpy as np
import pandas as pd
import pathlib
from sklearn.model_selection import GroupKFold
from tqdm import tqdm
from random import choices 
<set_options>
df_test = pd.read_csv('.. /input/test.csv')
 print(len(df_test))
df_test.head()
print("Tensorflow version " + tf.__version__)
 AUTO = tf.data.experimental.AUTOTUNE
MIXED_PRECISION = False
XLA_ACCELERATE = True
if MIXED_PRECISION:
if tpu: policy = tf.keras.mixed_precision.experimental.Policy('mixed_bfloat16')
 else: policy = tf.keras.mixed_precision.experimental.Policy('mixed_float16')
 mixed_precision.set_policy(policy)
 print('Mixed precision enabled')
 if XLA_ACCELERATE:
tf.config.optimizer.set_jit(True)
 print('Accelerated Linear Algebra enabled' )<define_variables>
X_test = df_test[features].values
CV_STRATEGY = 'PurgedGroupTimeSeriesSplit'
SEED = 2021
START_DATE = 86
FOLDS = 5
DEBUG = False<import_modules>
y_preds = []
for mod, score in best_models:
y_preds.append(mod.predict(X_test))
y_preds = np.concatenate(y_preds, axis=1)
 y_preds.shape
<split><EOS> 
subs = pd.read_csv('.. /input/sample_submission.csv')
 mean_preds = y_preds.mean(axis=1)
 subs['target'] = mean_preds
subs.to_csv('submission.csv', index=False )