# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + id="TNXfFd4fPeyZ"
import pandas as pd
from matplotlib import pyplot
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from keras.optimizers import RMSprop

# + colab={"base_uri": "https://localhost:8080/"} id="o74KA3LPPk0e" outputId="90bde47d-f721-4c70-d2e6-323ba8d91b55"
from google.colab import drive
drive.mount("/content/drive")

# + colab={"base_uri": "https://localhost:8080/", "height": 247} id="wbDEGyfBPuSk" outputId="e7456683-3ac3-4cec-f8fc-7a1a30a1f968"
data = pd.read_csv('/content/drive/My Drive/kaggle_compitions/Digit_recognization/train.csv')
data.head()

# + colab={"base_uri": "https://localhost:8080/"} id="fOjmDt4FPz1D" outputId="0f699b90-534a-4a62-b819-00882aea5800"
data.shape

# + colab={"base_uri": "https://localhost:8080/"} id="8amRu_XaP2GE" outputId="40efd25d-c22a-4735-bea2-ec3c7778f857"
#checking unique numbers in train label column
unique = data['label'].unique()
print(unique)

n_classes = len(unique)
print("Number of classes: ",n_classes)

# + colab={"base_uri": "https://localhost:8080/"} id="ehmHf2dDP3Wr" outputId="1add10f4-96cd-4919-9aeb-e29f31380031"
x = data.drop(labels = ["label"], axis=1)
print(x)

# + colab={"base_uri": "https://localhost:8080/"} id="vKK-fJRSP4k7" outputId="ff41845d-d2c6-47cb-ef10-65383bb41e40"
y = data['label']
print(y)

# + id="hccqN-51P54b"
x_train, x_test, y_train, y_test = train_test_split(x,y, test_size=0.20, random_state=42)

# + colab={"base_uri": "https://localhost:8080/"} id="o9KhjsCaP9Fj" outputId="874067bd-2811-4a4a-e5b8-c55a82398b10"
#Training Data and testing data
print("X_train: ", x_train)
print("X_test: ", y_test)
print("Y_train: ", y_train)
print("Y_test: ", y_test)

# + id="0hXL3pOBP-07"
x_train = x_train.values.reshape(-1,28,28,1)
x_test = x_test.values.reshape(-1,28,28,1)

# + colab={"base_uri": "https://localhost:8080/"} id="S9ZxxlmlQAf7" outputId="5c00b6b0-a26c-4de5-b0bc-2b36909033f6"
print("X_train: ",x_train)
print("X_train: ",x_test)

# + colab={"base_uri": "https://localhost:8080/"} id="BwvjVOQjQBeS" outputId="2d1b6c7a-88f5-4234-9939-0c7183610982"
#determine the shape of input images
in_shape = x_train.shape[1:]
print(in_shape)

# + colab={"base_uri": "https://localhost:8080/", "height": 283} id="WHWVEbhxQDxK" outputId="fad7ed98-8db2-4677-8b07-71cbc424f34a"
plt.imshow(x_train[0].reshape([28,28]))

# + id="_qYEsNV9QE3r"
# normalize pixel values
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0

# + id="XJv-Zn8sQJfp"
from numpy import asarray, unique, argmax
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Flatten, Dropout

# + id="6T9khUVGQJt-"
model = Sequential()

model.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', 
                 activation ='relu', input_shape = in_shape))
model.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', 
                 activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.25))


model.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', 
                 activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', 
                 activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
model.add(Dropout(0.25))


model.add(Flatten())
model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))

# + id="30u8YYQCRy4P"
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)

# + id="d5EL4TiMQJw7"
# define loss and optimizer
model.compile(optimizer= optimizer, loss = 'sparse_categorical_crossentropy', metrics=['accuracy'])

# + colab={"base_uri": "https://localhost:8080/"} id="z2VGY9y0QJy9" outputId="8c142fe7-0e3d-4dcb-99fa-093fa5659772"
# fit the model
model.fit(x_train, y_train, validation_split=0.2, epochs=100, batch_size=128, verbose= 1)

# + colab={"base_uri": "https://localhost:8080/"} id="azRuPskQQJ2A" outputId="851cac76-e521-4ba0-e2cf-822c3fccf558"
#Evaluate the model
loss, accuracy = model.evaluate(x_test, y_test, verbose=1)
print('Accuracy: %.3f' %accuracy)
print('Loss: ',loss)

# + colab={"base_uri": "https://localhost:8080/"} id="WtlKCs4DTGDz" outputId="9f953ada-e8c5-41db-ac30-bce96fe2779e"
#make a pridiction
image = x_test[1]
ypred = model.predict(asarray([image]))
print('Prediction: Class =%d' %argmax(ypred))

# + colab={"base_uri": "https://localhost:8080/", "height": 283} id="FU_d2fHxTK-5" outputId="5b7bfb34-32c2-441f-a515-51e66e0584ac"
plt.imshow(x_test[1].reshape([28,28]))

# + colab={"base_uri": "https://localhost:8080/", "height": 247} id="MrqoyrTJTMSu" outputId="ecde3f90-a643-4cb6-c8f0-aa7db820d681"
#Pridict the model on the trained Data
test_data = pd.read_csv('/content/drive/My Drive/kaggle_compitions/Digit_recognization/test.csv')
test_data.head()

# + id="55zxTmQfTNuy"
test = test_data/255.0

# + id="aPMKoEobTPdR"
test_final_Data = test.values.reshape(-1,28,28,1)

# + colab={"base_uri": "https://localhost:8080/"} id="S9EyEr3NTQir" outputId="611eb7e0-e5ee-41d4-cfc5-d3763394f7ab"
label = model.predict(test_final_Data)
print(label)

# + colab={"base_uri": "https://localhost:8080/"} id="x4uz9D1VTRnu" outputId="0c7dd404-47a9-4bf5-91bf-7d8cd85a88cb"
label.shape

# + colab={"base_uri": "https://localhost:8080/"} id="yZgcVH9FTS2K" outputId="d0003b69-90d2-4cde-ee66-478fde24841c"
print(label)

# + id="rOPsEY0mTUBR"
label = np.argmax(label, axis=1)

# + colab={"base_uri": "https://localhost:8080/"} id="_ymfxR2tTbSx" outputId="baf28ae6-11a1-4a18-90ff-eb451c8337e8"
print(label)

# + colab={"base_uri": "https://localhost:8080/"} id="c0Or0RE5TdGR" outputId="e91cdf92-b553-4386-80dd-b19a2f37b03d"
print(label.shape)

# + colab={"base_uri": "https://localhost:8080/", "height": 198} id="xyJQUpnqTeth" outputId="0424cb8f-3dfb-4304-845e-f4f5083960d9"
sample_submission = pd.read_csv('/content/drive/My Drive/kaggle_compitions/Digit_recognization/sample_submission.csv')
sample_submission.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 198} id="rByMHNaaTfpJ" outputId="fd233f16-3362-4857-f2e6-2147db865012"
index = test_data.index.values + 1
data = {'ImageId' : index, "Label" : label}
df = pd.DataFrame(data=data)
df.head()


# + id="c_Ecd8PFTg1p"
submit = pd.DataFrame({'ImageId' : index, "Label" : label.astype(int).ravel()})
submit.to_csv("submission.csv",index = False)

# + colab={"base_uri": "https://localhost:8080/", "height": 407} id="XxrcLzH0TiAC" outputId="647d6421-52ee-4162-fb0b-f52bbd14fb1f"
submit

# + id="SRK-JQSRTkMR"


StarcoderdataJupyterScriptsDedupFiltered