Kaggle competition: Tabular Playground Series - Jan 2021
<SOS> metric: RMSE Kaggle data source: tabular-playground-series-jan-2021<import_modules>
%matplotlib inline
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.layers import Conv2D,MaxPooling2D,Flatten,Dense,Dropout
import matplotlib.pyplot as plt
import keras
from keras.utils.np_utils import to_categorical<load_from_csv>
!pip install --upgrade xgboost
xgb.__version__
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv')
 test1 = test.copy()<prepare_x_and_y>
shap.initjs()
x_train=train.drop(['label'],1)
 y_train=train['label']
x_train=x_train.values.reshape(-1,28,28,1)
 test=test.values.reshape(-1,28,28,1)
 x_train=x_train/255
test=test/255 
<choose_model_class>
train = pd.read_csv('.. /input/tabular-playground-series-jan-2021/train.csv')
 test = pd.read_csv('.. /input/tabular-playground-series-jan-2021/test.csv')
 sub = pd.read_csv('.. /input/tabular-playground-series-jan-2021/sample_submission.csv' )
model=models.Sequential([
Conv2D(32,(5,5), activation='relu' , input_shape=(28,28,1)) ,
MaxPooling2D(pool_size=(2,2)) ,
Conv2D(64,(5,5), activation ='relu'),
MaxPooling2D(pool_size=(2,2)) ,
Dropout(0.25),
Conv2D(64,(3,3), activation ='relu'),
MaxPooling2D(pool_size=(2,2)) ,
Dropout(0.25),
Flatten() ,
Dense(64, activation='relu'),
Dense(10, activation='softmax')
 ] )<train_model>
train_oof = np.zeros(( 300000,))
test_preds = 0
train_oof.shape
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
 model.fit(x_train, y_train, epochs=60, batch_size=64 )<save_to_csv>
Best_trial= {'lambda': 0.0030282073258141168,
'alpha': 0.01563845128469084,
'colsample_bytree': 0.55,
'subsample': 0.7,
'learning_rate': 0.01,
'max_depth': 15,
'random_state': 2020,
'min_child_weight': 257,
'tree_method':'gpu_hist',
'predictor': 'gpu_predictor'}
y_test = model.predict(test)
 y_test = np.argmax(y_test, axis = 1)
 index_list = []
for i in list(test1.index):
index_list.append(i+1)
 submission_df = pd.DataFrame({
"ImageId": index_list,
"Label": y_test
})
 submission_df.to_csv("submission_cnn.csv", index = False )<import_modules>
test = xgb.DMatrix(test[columns] )
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
from keras.utils.np_utils import to_categorical<load_from_csv>
NUM_FOLDS = 8
kf = KFold(n_splits=NUM_FOLDS, shuffle=True, random_state=0)
 for f,(train_ind, val_ind)in tqdm(enumerate(kf.split(train, target))):
train_df, val_df = train.iloc[train_ind][columns], train.iloc[val_ind][columns]
train_target, val_target = target[train_ind], target[val_ind]
train_df = xgb.DMatrix(train_df, label=train_target)
 val_df = xgb.DMatrix(val_df, label=val_target)
 model = xgb.train(Best_trial, train_df, 1500)
 temp_oof = model.predict(val_df)
 temp_test = model.predict(test)
 train_oof[val_ind] = temp_oof
test_preds += temp_test/NUM_FOLDS
print(mean_squared_error(temp_oof, val_target, squared=False))
train=pd.read_csv('.. /input/digit-recognizer/train.csv')
 test=pd.read_csv('.. /input/digit-recognizer/test.csv' )<prepare_x_and_y>
mean_squared_error(train_oof, target, squared=False)
x_train=train.drop(['label'],1)
 y_train=train['label']<prepare_x_and_y>
np.save('train_oof', train_oof)
 np.save('test_preds', test_preds )
x_train=np.array(x_train)
 test=np.array(test )<feature_engineering>
%%time
shap_preds = model.predict(test, pred_contribs=True )
x_train=x_train/255
test=test/255<categorify>
test = pd.read_csv('.. /input/tabular-playground-series-jan-2021/test.csv')
target=x_train.reshape(-1,28,28,1)
 test=test.reshape(-1,28,28,1)
 y_train=np.array(y_train)
 label=to_categorical(y_train)
 label.shape<import_modules>
test = xgb.DMatrix(test[columns] )
from keras.models import Sequential
from keras.layers import Conv2D,MaxPooling2D,Flatten,Dense,Dropout<choose_model_class>
%%time
shap_interactions = model.predict(test, pred_interactions=True )
model=Sequential([
Conv2D(32,(5,5), activation='relu' , input_shape=(28,28,1)) ,
MaxPooling2D(pool_size=(2,2)) ,
Conv2D(64,(5,5), activation ='relu'),
MaxPooling2D(pool_size=(2,2)) ,
Dropout(0.25),
Conv2D(64,(3,3), activation ='relu'),
MaxPooling2D(pool_size=(2,2)) ,
Dropout(0.25),
Flatten() ,
Dense(64, activation='relu'),
Dense(10, activation='softmax')
 ] )<choose_model_class>
del shap_interactions, shap_preds
gc.collect()
gc.collect()
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'] )<train_model>
train = pd.read_csv('.. /input/tabular-playground-series-jan-2021/train.csv')
 test = pd.read_csv('.. /input/tabular-playground-series-jan-2021/test.csv')
 sub = pd.read_csv('.. /input/tabular-playground-series-jan-2021/sample_submission.csv' )
model.fit(target,label,epochs=40,batch_size=64 )<predict_on_test>
train['cont13_cont4'] = train['cont13']*train['cont4']
train['cont13_cont11'] = train['cont13']*train['cont11']
train['cont13_cont7'] = train['cont13']*train['cont7']
train['cont13_cont2'] = train['cont13']*train['cont2']
train['cont13_cont10'] = train['cont13']*train['cont10']
test['cont13_cont4'] = test['cont13']*test['cont4']
test['cont13_cont11'] = test['cont13']*test['cont11']
test['cont13_cont7'] = test['cont13']*test['cont7']
test['cont13_cont2'] = test['cont13']*test['cont2']
test['cont13_cont10'] = test['cont13']*test['cont10']
Y_pred = model.predict(test)
 Y_pred_classes = np.argmax(Y_pred,axis = 1 )<load_from_csv>
test = xgb.DMatrix(test[columns] )
submission_data = pd.read_csv('.. /input/digit-recognizer/sample_submission.csv' )<feature_engineering>
Best_trial= {'lambda': 0.0030282073258141168,
'alpha': 0.01563845128469084,
'colsample_bytree': 0.55,
'subsample': 0.7,
'learning_rate': 0.01,
'max_depth': 15,
'random_state': 2020,
'min_child_weight': 257,
'tree_method':'gpu_hist',
'predictor': 'gpu_predictor'}
submission_data['Label']=Y_pred_classes<save_to_csv>
kf = KFold(n_splits=NUM_FOLDS, shuffle=True, random_state=0)
 for f,(train_ind, val_ind)in tqdm(enumerate(kf.split(train, target))):
train_df, val_df = train.iloc[train_ind][columns], train.iloc[val_ind][columns]
train_target, val_target = target[train_ind], target[val_ind]
train_df = xgb.DMatrix(train_df, label=train_target)
 val_df = xgb.DMatrix(val_df, label=val_target)
 model = xgb.train(Best_trial, train_df, 1600)
 temp_oof = model.predict(val_df)
 temp_test = model.predict(test)
 train_oof_2[val_ind] = temp_oof
test_preds_2 += temp_test/NUM_FOLDS
print(mean_squared_error(temp_oof, val_target, squared=False))
submission_data.to_csv('submit.csv' ,index=False )<predict_on_test>
mean_squared_error(train_oof_2, target, squared=False)
def test_output(i):
plt.imshow(x_train[i],cmap='gray')
 predicted=np.argmax(model.predict(target[i].reshape(-1,28,28,1)))
 actual=np.argmax(label[i])
 plt.xlabel(f'predicted= {predicted} Actual= {actual}' )<import_modules>
mean_squared_error(0.6*train_oof+0.4*train_oof_2, target, squared=False)
from PIL import Image, ImageGrab<predict_on_test>
np.save('train_oof_2', train_oof_2)
 np.save('test_preds_2', test_preds_2 )
def predict_digit1(img):
img = Image.open(img)
 plt.imshow(img)
 img = img.convert('L', dither=Image.NONE)
 img = img.resize(( 28,28))
img = np.array(img)
 img=np.invert(img)
 predicted=np.argmax(model.predict(img.reshape(-1,28,28,1)))
 plt.xlabel(f'Predicted= {predicted}' )<randomize_order>
sub['target'] = test_preds
sub.to_csv('submission.csv', index=False )
predict_digit1('.. /input/temporary/Images/images.jfif' )<define_variables>
sub['target'] = test_preds_2
sub.to_csv('submission_2.csv', index=False )
<define_variables><EOS> 
sub['target'] = 0.6*test_preds+0.4*test_preds_2
sub.to_csv('submission_average.csv', index=False )