Kaggle competition: Digit Recognizer
df_train = pd.read_csv("/kaggle/input/tabular-playground-series-feb-2021/train.csv")
 df_test = pd.read_csv("/kaggle/input/tabular-playground-series-feb-2021/test.csv")
 df_sample = pd.read_csv("/kaggle/input/tabular-playground-series-feb-2021/sample_submission.csv" )<drop_column>
print(os.listdir(".. /input"))
print(os.getcwd())
 X_data = pd.read_csv(".. /input/train.csv")
 T_df = pd.read_csv(".. /input/test.csv")
 Y_df = X_data["label"]
X_df = X_data.drop("label", axis=1)
 X = X_df.values
X = X/255
Y = Y_df.values
np.random.seed(100)
 X_train, X_test, Y_train, Y_test = train_test_split(X, Y)
 Y_train_r = np.ravel(Y_train)
 Y_test_r = np.ravel(Y_test)
 Y_train_c = np_utils.to_categorical(Y_train, 10)
 Y_test_c = np_utils.to_categorical(Y_test, 10 )
train_id = df_train["id"]
test_id = df_test["id"]
df_train.drop("id", axis=1, inplace=True)
 df_test.drop("id", axis=1, inplace=True )<define_variables>
KNN = KNeighborsClassifier(n_neighbors=1)
 KNN.fit(X_train,Y_train)
 KNN.score(X_test, Y_test)
cat_features = [f"cat{i}" for i in range(9 + 1)]<categorify>
KNN_2 = KNeighborsClassifier(n_neighbors=2)
 KNN_2.fit(X_train,Y_train)
 KNN_2.score(X_test, Y_test )
onehot_encoder = ce.one_hot.OneHotEncoder()
onehot_encoder.fit(pd.concat([df_train[cat_features], df_test[cat_features]], axis=0))
train_ohe = onehot_encoder.transform(df_train[cat_features])
 test_ohe = onehot_encoder.transform(df_test[cat_features])
 train_ohe.columns = [f"OHE_{col}" for col in train_ohe]
test_ohe.columns = [f"OHE_{col}" for col in test_ohe]<define_variables>
svclassifier = SVC(kernel="linear")
 svclassifier.fit(X_train, Y_train)
 Y_pred = svclassifier.predict(X_test)
 accuracy_score(Y_test, Y_pred )
numerical_features = [f"cont{i}" for i in range(13 + 1)]<concatenate>
svclassifier_rbf = SVC(kernel="rbf", gamma = "auto")
 svclassifier_rbf.fit(X_train, Y_train)
 Y_pred = svclassifier_rbf.predict(X_test)
 accuracy_score(Y_test, Y_pred )
train_x = pd.concat([
df_train[numerical_features],
train_ohe
], axis=1 )<concatenate>
Lreg = list()
for i in range(-3,4):
c = 10**i
dummy = LogisticRegression(multi_class="multinomial",solver="lbfgs", max_iter = 4000, C=c)
 Lreg.append(dummy.fit(X_train,Y_train_r))
Lreg_scores = list()
for i in range(0,6):
Lreg_scores.append(Lreg[i].score(X_test, Y_test))
print(Lreg_scores )
test_x = pd.concat([
df_test[numerical_features],
test_ohe
], axis=1 )<prepare_x_and_y>
neural1 = Sequential()
neural1.add(Dense(16, input_dim=784 , activation='relu'))
neural1.add(Dense(10, activation='softmax'))
neural2 = Sequential()
neural2.add(Dense(32, input_dim=784 , activation='relu'))
neural2.add(Dense(10, activation='softmax'))
neural3 = Sequential()
neural3.add(Dense(64, input_dim=784 , activation='relu'))
neural3.add(Dense(10, activation='softmax'))
neural4 = Sequential()
neural4.add(Dense(128, input_dim=784 , activation='relu'))
neural4.add(Dense(10, activation='softmax'))
neural5 = Sequential()
neural5.add(Dense(256, input_dim=784 , activation='relu'))
neural5.add(Dense(10, activation='softmax'))
neural6 = Sequential()
neural6.add(Dense(512, input_dim=784 , activation='relu'))
neural6.add(Dense(10, activation='softmax'))
neural7 = Sequential()
neural7.add(Dense(1024, input_dim=784 , activation='relu'))
neural7.add(Dense(10, activation='softmax'))
neural8 = Sequential()
neural8.add(Dense(2048, input_dim=784 , activation='relu'))
neural8.add(Dense(10, activation='softmax'))
neural1.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 neural2.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 neural3.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 neural4.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 neural5.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 neural6.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 neural7.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 neural8.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 neural1.fit(X_train, Y_train_c, epochs=50, batch_size=200)
 neural2.fit(X_train, Y_train_c, epochs=50, batch_size=200)
 neural3.fit(X_train, Y_train_c, epochs=50, batch_size=200)
 neural4.fit(X_train, Y_train_c, epochs=50, batch_size=200)
 neural5.fit(X_train, Y_train_c, epochs=50, batch_size=200)
 neural6.fit(X_train, Y_train_c, epochs=50, batch_size=200)
 neural7.fit(X_train, Y_train_c, epochs=50, batch_size=200)
 neural8.fit(X_train, Y_train_c, epochs=50, batch_size=200)
 scores1 = neural1.evaluate(X_test, Y_test_c)
 scores2 = neural2.evaluate(X_test, Y_test_c)
 scores3 = neural3.evaluate(X_test, Y_test_c)
 scores4 = neural4.evaluate(X_test, Y_test_c)
 scores5 = neural5.evaluate(X_test, Y_test_c)
 scores6 = neural6.evaluate(X_test, Y_test_c)
 scores7 = neural7.evaluate(X_test, Y_test_c)
 scores8 = neural8.evaluate(X_test, Y_test_c)
 plt.plot([1,2,3,4,5,6,7,8],
[scores1[0],scores2[0],scores3[0],scores4[0],scores5[0],scores6[0],
scores7[0],scores8[0]],'-o' )
train_y = df_train["target"]<create_dataframe>
X_train_2d = X_train.reshape(X_train.shape[0], 28, 28,1)
 X_train_2d.shape
X_test_2d = X_test.reshape(X_test.shape[0], 28, 28,1)
 X_test_2d.shape
model_1= Sequential()
model_1.add(Conv2D(filters = 32, kernel_size =(5,5),activation ='relu', input_shape=(28,28,1)))
 model_1.add(MaxPooling2D(pool_size=(2, 2)))
 model_1.add(Flatten())
 model_1.add(Dense(512, activation='relu'))
model_1.add(Dense(10, activation='softmax'))
model_2= Sequential()
model_2.add(Conv2D(filters = 32, kernel_size =(5,5),activation ='relu', input_shape=(28,28,1)))
 model_2.add(MaxPooling2D(pool_size=(2, 2)))
 model_2.add(Conv2D(filters = 64, kernel_size =(5,5),activation ='relu'))
model_2.add(MaxPooling2D(pool_size=(2, 2)))
 model_2.add(Flatten())
 model_2.add(Dense(512, activation='relu'))
model_2.add(Dense(10, activation='softmax'))
model_3= Sequential()
model_3.add(Conv2D(filters = 32, kernel_size =(5,5),activation ='relu', input_shape=(28,28,1)))
 model_3.add(MaxPooling2D(pool_size=(2, 2)))
 model_3.add(Conv2D(filters = 64, kernel_size =(5,5),activation ='relu'))
model_3.add(MaxPooling2D(pool_size=(2, 2)))
 model_3.add(Conv2D(filters = 128, kernel_size =(3,3),activation ='relu'))
model_3.add(MaxPooling2D(pool_size=(2, 2)))
 model_3.add(Flatten())
 model_3.add(Dense(512, activation='relu'))
model_3.add(Dense(10, activation='softmax'))
model_1.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 model_1.fit(X_train_2d, Y_train_c, epochs=30, batch_size=200)
 model_2.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 model_2.fit(X_train_2d, Y_train_c, epochs=30, batch_size=200)
 model_3.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 model_3.fit(X_train_2d, Y_train_c, epochs=30, batch_size=200)
 scores1 = model_1.evaluate(X_test_2d, Y_test_c)
 scores2 = model_2.evaluate(X_test_2d, Y_test_c)
 scores3 = model_3.evaluate(X_test_2d, Y_test_c)
oplgb_train_data = oplgb.Dataset(train_x, train_y )<init_hyperparams>
model_2= Sequential()
model_2.add(Conv2D(filters = 32, kernel_size =(5,5),activation ='relu', input_shape=(28,28,1)))
 model_2.add(MaxPooling2D(pool_size=(2, 2)))
 model_2.add(Dropout(0.4))
model_2.add(Conv2D(filters = 64, kernel_size =(5,5),activation ='relu'))
model_2.add(MaxPooling2D(pool_size=(2, 2)))
 model_2.add(Dropout(0.4))
model_2.add(Flatten())
 model_2.add(Dense(128, activation='relu'))
model_2.add(Dropout(0.4))
model_2.add(Dense(10, activation='softmax'))
model_2.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 model_2.fit(X_train_2d, Y_train_c, epochs=30, batch_size=200 )
oplgb_params = {
"objective": "regression",
"metric": "root_mean_squared_error",
"verbosity": -1,
"learning_rate": 0.01
}<choose_model_class>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train_2d )
folds = KFold(n_splits=5, shuffle=True, random_state=2021 )<choose_model_class>
history = model_2.fit_generator(datagen.flow(X_train_2d,Y_train_c, batch_size=200),
epochs = 25, validation_data =(X_test_2d,Y_test_c),
verbose = 2, steps_per_epoch=X_train.shape[0]/200)
tuner_cv = oplgb.LightGBMTunerCV(oplgb_params, oplgb_train_data, num_boost_round=1000, early_stopping_rounds=100, folds=folds, verbose_eval=100, time_budget=21600)
 tuner_cv.run()<find_best_params>
scores2 = model_2.evaluate(X_test_2d, Y_test_c)
 print(scores2)
<train_model><EOS> 
T = T_df.values
T = T/255
T = T.reshape(T.shape[0], 28, 28,1)
 results = model_2.predict(T)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("submission_kaggle_2D.csv",index=False )