image_output_1.png
from keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation, BatchNormalization

from keras.models import Sequential

from sklearn.model_selection import train_test_split

from tensorflow.keras.utils import to_categorical

import matplotlib.pyplot as plt

import numpy as np

import numpy as np # linear algebra

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')

(train.shape, test.shape)

def select_pic(i):
    arr = np.array(test[i:i + 1])
    pix = arr.reshape(arr.shape[0], 28, 28)
    img = pix[0]

for i in range(0, 1):
    r = np.random.randint(i, 10000)
    select_pic(r)

X = train.drop('label', axis=1).values / 255.0
X = X.reshape(-1, 28, 28, 1)
X.shape

Y = train['label'].values
Y.shape

A = test.values / 255.0
A = A.reshape(-1, 28, 28, 1)
A.shape

from tensorflow.keras.utils import to_categorical
Y = to_categorical(Y, num_classes=10)
Y.shape

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2)

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation, BatchNormalization
model = Sequential()
model.add(Conv2D(32, (5, 5), input_shape=(28, 28, 1), activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

history = model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=5, batch_size=200)

predict = [np.argmax(i) for i in model.predict(A)]
len(predict)