Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<prepare_x_and_y>
import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from keras.applications.vgg16 import preprocess_input
from keras.applications.vgg16 import VGG16
from keras.models import Model
from keras.layers import Dense
from keras.layers import Flatten
from keras.models import Sequential
from keras.optimizers import SGD, Adam
import matplotlib.pyplot as plt
from keras import Input
from keras.utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator
X = train_data.drop(['Survived', 'PassengerId'], axis=1)
 y = train_data['Survived']
test_X = test_data.drop(['PassengerId'], axis=1 )<train_on_grid>
train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 target = train['label']
train = train.drop(['label'], axis=1)
best_models = {}
train_X, val_X, train_y, val_y = train_test_split(X, y, random_state=1)
 def print_best_parameters(hyperparameters, best_parameters):
value = "Best parameters: "
for key in hyperparameters:
value += str(key)+ ": " + str(best_parameters[key])+ ", "
if hyperparameters:
print(value[:-2])
 def get_best_model(estimator, hyperparameters):
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
 grid_search = GridSearchCV(estimator=estimator, param_grid=hyperparameters,
n_jobs=-1, cv=cv, scoring="accuracy")
 best_model = grid_search.fit(train_X, train_y)
 best_parameters = best_model.best_estimator_.get_params()
print_best_parameters(hyperparameters, best_parameters)
 return best_model
def evaluate_model(model, name):
print("Accuracy score:", accuracy_score(train_y, model.predict(train_X)))
 best_models[name] = model<train_model>
full = pd.concat([train, test])
 full=full.to_numpy()
full=full.reshape(-1, 28, 28)
 full.shape
class MyXGBClassifier(XGBClassifier):
def fit(self, X, y=None):
return super(XGBClassifier, self ).fit(X, y,
verbose=False,
early_stopping_rounds=40,
eval_metric='logloss',
eval_set=[(val_X, val_y)] )<choose_model_class>
full = np.pad(full,(( 0,0),(2,2),(2,2)) , mode='constant')
 full = stacked_img = np.stack([full, full, full], axis=3)
 full.shape
randomForest = RandomForestClassifier(random_state=1, n_estimators=20, max_features='auto',
criterion='gini', max_depth=4, min_samples_split=2,
min_samples_leaf=3)
 xgbClassifier = MyXGBClassifier(seed=1, tree_method='gpu_hist', predictor='gpu_predictor',
use_label_encoder=False, learning_rate=0.4, gamma=0.4,
max_depth=4, reg_lambda=0, reg_alpha=0.1)
 lgbmClassifier = LGBMClassifier(random_state=1, device='gpu', boosting_type='dart',
num_leaves=8, learning_rate=0.1, n_estimators=100,
reg_alpha=1, reg_lambda=1)
 classifiers = [ 
('randomForest', randomForest), 
('xgbClassifier', xgbClassifier), 
('lgbmClassifier', lgbmClassifier)
 ]<choose_model_class>
full=full.astype("float32")
 full = full/255
train=full[:42000, :, :, :]
test=full[42000:, :, :, :]
hyperparameters = {
'n_jobs' : [-1],
'voting' : ['hard', 'soft'],
'weights' : [(1, 1, 1), 
(2, 1, 1),(1, 2, 1),(1, 1, 2), 
(2, 2, 1),(1, 2, 2),(2, 1, 2), 
(3, 2, 1),(1, 3, 2),(2, 1, 3),(3, 1, 2)]
}
estimator = VotingClassifier(estimators=classifiers)
 best_model_voting = get_best_model(estimator, hyperparameters )<find_best_params>
X_train, X_val, Y_train, Y_val = train_test_split(train, target, test_size = 0.2, random_state = 1 )
evaluate_model(best_model_voting.best_estimator_, 'voting' )<save_to_csv>
Y_val_access = Y_val
for model in best_models:
predictions = best_models[model].predict(test_X)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('submission_' + model + '.csv', index=False )<load_from_csv>
Y_val=to_categorical(Y_val, num_classes=10)
 Y_train=to_categorical(Y_train, num_classes=10 )
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_data = pd.read_csv('/kaggle/input/titanic/test.csv' )<prepare_x_and_y>
def create_model() :
VGG = VGG16(
 input_shape=(32, 32, 3),
weights='imagenet',
include_top=False, 
)
 model =tf.keras.Sequential([VGG,
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
 ])
 return model
features = ['Pclass','Sex','SibSp','Parch','Fare','Age']
x = pd.get_dummies(train_data[features])
 x_test = pd.get_dummies(test_data[features])
 y = train_data["Survived"] 
<data_type_conversions>
im = ImageDataGenerator(zoom_range=0.1, rotation_range=15, height_shift_range= 0.05, width_shift_range= 0.05)
 flow=im.flow(X_train, Y_train, batch_size=32 )
x['Fare'].fillna(x['Fare'].mode() [0], inplace=True)
 x_test['Fare'].fillna(x_test['Fare'].mode() [0], inplace=True)
 x['Age'].fillna(x['Age'].mode() [0], inplace=True)
 x_test['Age'].fillna(x_test['Age'].mode() [0], inplace=True)
<train_on_grid>
mymod=create_model()
mymod.summary()
mymod.compile(optimizer=Adam(lr=0.0001), loss='categorical_crossentropy', metrics=['accuracy'] )
param_grid = {'alpha': sp_rand() }
model = Ridge()
rsearch = RandomizedSearchCV(estimator=model, param_distributions=param_grid, n_iter=100)
 rsearch.fit(x,y)
 print(rsearch )<save_to_csv>
predictions_val = np.zeros(shape=(5, X_val.shape[0], 10))
predictions_test = np.zeros(shape=(5, test.shape[0], 10))
for i in range(5):
print('training model ', i+1, '...')
 perf = mymod.fit_generator(flow, epochs=10, steps_per_epoch= X_train.shape[0]/32, validation_data=(X_val, Y_val), verbose=0)
 pred_val = mymod.predict(X_val)
 pred_test = mymod.predict(test)
 predictions_val[i, :, :] = pred_val
predictions_test[i, :, :] = pred_test
pvall = np.zeros(shape=(X_val.shape[0], 10))
ptall = np.zeros(shape=(test.shape[0], 10))
for i in range(10):
pv1 = np.mean(predictions_val[:, :, i], axis=0)
 pt1 = np.mean(predictions_test[:, :, i], axis=0)
 pvall[:, i] = pv1
ptall[:, i] = pt1
preds_val= np.argmax(pvall,axis = 1)
 preds_test = np.argmax(ptall, axis=1)
 val_acc = np.mean(preds_val == Y_val_access)
 print('Overall validation accuracy is', val_acc)
<load_from_csv><EOS> 
preds= pd.Series(preds_test,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),preds],axis = 1)
 submission.to_csv("MINST.csv",index=False )