{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### IMPORTING LIBRARIES","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow.keras as keras\nfrom keras.utils import to_categorical\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### LOADING THE DATA ","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape, test.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.columns","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train.iloc[:, 1:]\ny_train = train.iloc[:, 0]\nX_test = test.iloc[:]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.info()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.describe()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train/255\ny_train = to_categorical(y_train, 10)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.describe()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_reshaped = X_train.to_numpy().reshape(42000, 28,28)\nX_test_reshaped = X_test.to_numpy().reshape(28000, 28,28)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_train_reshaped[np.random.randint(42000)])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This displays a random image in their original 28x28 pixel form, showing how reshaping helps in visualizing and processing the data as images.","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom keras.layers import Conv2D, Dense, MaxPooling2D, BatchNormalization, Flatten, Dropout","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### MODEL BUILDING","metadata":{}},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(32, (3,3), activation='relu', padding = 'valid', input_shape=(28,28,1)),\n    Conv2D(32, (3,3), activation='relu', padding='valid'),\n    MaxPooling2D(strides=(2,2)),\n    Conv2D(64, (3,3), activation = 'relu', padding='valid'),\n    Conv2D(64, (3,3), activation= 'relu', padding = 'valid'),\n    Flatten(),\n    Dense(512, activation='relu'),\n    Dense(10, activation = 'softmax')\n])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### MODEL TRAINING","metadata":{}},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy', optimizer='adam', metrics = ['accuracy'])\nhistory = model.fit(X_train_reshaped, y_train, batch_size=128, epochs=100)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['loss'])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_test = model.predict(X_test_reshaped)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_labels = np.argmax(pred_test, axis = 1)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_test_reshaped[1])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_labels[1]","metadata":{},"execution_count":null,"outputs":[]}]}