Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<find_best_params>
training_data = pd.read_csv('.. /input/train.csv')
 test_data = pd.read_csv('.. /input/test.csv')
 training_data.head()
study = optuna.create_study(direction='minimize')
 optimize = partial(objective, X=X_train, y=y_train, model=LGBMRegressor)
<init_hyperparams>
x_train = training_data.drop('label', axis = 1)
 y_train = pd.DataFrame(data=training_data['label'])
 display(y_train.head())
 display(x_train.head() )
w1 = 0.2
w2 = 0.8<import_modules>
%matplotlib inline
i= 1
imshow(x_train.iloc[i].values.reshape(( 28, 28)))
 print('This image corresponds to ', y_train.iloc[i] )
import numpy as np
import pandas as pd<load_from_csv>
cnn_model = Sequential()
cnn_model.add(Conv2D(128,(3,3), padding='same', input_shape=(28,28,1), data_format='channels_last', activation='relu'))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
 cnn_model.add(Dropout(0.2))
cnn_model.add(Conv2D(128,(3,3), padding='same', activation='relu'))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
 cnn_model.add(Dropout(0.2))
cnn_model.add(Conv2D(256,(3,3), padding='same', activation='relu'))
cnn_model.add(MaxPooling2D(pool_size=(2, 2)))
 cnn_model.add(Dropout(0.2))
cnn_model.add(Conv2D(256,(3,3), padding='valid', activation='relu'))
cnn_model.add(Dropout(0.2))
cnn_model.add(Flatten())
 cnn_model.add(Dense(units=10, activation='softmax'))
cnn_model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
 print('The model was successfully created and compiled.')
 print('Shape of Layer Outputs:')
 for layer in cnn_model.layers:
print(layer.name,': ',layer.output_shape )
%%time
df1 = pd.read_csv(".. /input/ensembling-starter-tps-feb-2021/submission.csv")
 df2 = pd.read_csv(".. /input/playground-series-february-21/submission_040.csv")
 blended_df = df1.copy(deep=True)
 blended_df['target'] = w1*df1['target'] + w2*df2['target']
print(blended_df.head() )<save_to_csv>
y_train_categorical = to_categorical(y_train, num_classes=10)
 reshaped_x = x_train.values.reshape(x_train.shape[0],28,28,1)/ 255
print(reshaped_x.shape)
 print(y_train_categorical.shape)
 cnn_model.fit(x=reshaped_x, y=y_train_categorical, batch_size=1000, epochs=32, verbose=1, validation_split=0.2 )
blended_df.to_csv("blended_df.csv", index=None )<import_modules>
reshaped_test_data = test_data.values.reshape(test_data.shape[0],28,28,1)/ 255
predictions = cnn_model.predict(reshaped_test_data)
 display(predictions )
import plotly.express as px
import plotly.graph_objects as go
import plotly.figure_factory as ff
from plotly.subplots import make_subplots
import matplotlib.pyplot as plt
from colorama import Fore
from pandas_profiling import ProfileReport
import seaborn as sns
from sklearn import metrics
from scipy import stats
import math
from tqdm.notebook import tqdm
from copy import deepcopy
from sklearn.ensemble import RandomForestRegressor
from xgboost import XGBRegressor
from lightgbm import LGBMRegressor
import lightgbm as lgb
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
import optuna
from optuna import Trial, visualization
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score,confusion_matrix
from sklearn.metrics import accuracy_score, mean_squared_error<load_from_csv>
predictions_formatted = np.argmax(predictions, axis=1)
 display(predictions_formatted )
<concatenate><EOS> 
submission = pd.DataFrame({'ImageId': np.arange(1,28001), 'Label': predictions_formatted})
 submission.to_csv('submission_4.csv', index=False)
 print('Done' )