{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"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","execution":{"iopub.status.busy":"2022-01-03T10:46:23.169909Z","iopub.execute_input":"2022-01-03T10:46:23.170181Z","iopub.status.idle":"2022-01-03T10:46:23.178853Z","shell.execute_reply.started":"2022-01-03T10:46:23.170152Z","shell.execute_reply":"2022-01-03T10:46:23.177913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### [This](https://machinelearningmastery.com/how-to-develop-a-convolutional-neural-network-from-scratch-for-mnist-handwritten-digit-classification/) will come handy","metadata":{}},{"cell_type":"code","source":"df_train=pd.read_csv(\"../input/digit-recognizer/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:46:23.389814Z","iopub.execute_input":"2022-01-03T10:46:23.390082Z","iopub.status.idle":"2022-01-03T10:46:26.178321Z","shell.execute_reply.started":"2022-01-03T10:46:23.390054Z","shell.execute_reply":"2022-01-03T10:46:26.177583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:46:26.179988Z","iopub.execute_input":"2022-01-03T10:46:26.180276Z","iopub.status.idle":"2022-01-03T10:46:26.199333Z","shell.execute_reply.started":"2022-01-03T10:46:26.180243Z","shell.execute_reply":"2022-01-03T10:46:26.198548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=df_train.drop(columns=['label'])\ny_train=df_train['label']","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:46:26.200266Z","iopub.execute_input":"2022-01-03T10:46:26.200456Z","iopub.status.idle":"2022-01-03T10:46:26.306848Z","shell.execute_reply.started":"2022-01-03T10:46:26.200433Z","shell.execute_reply":"2022-01-03T10:46:26.305929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=x_train.values\ny_train=y_train.values","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:46:26.308617Z","iopub.execute_input":"2022-01-03T10:46:26.30882Z","iopub.status.idle":"2022-01-03T10:46:26.318757Z","shell.execute_reply.started":"2022-01-03T10:46:26.308794Z","shell.execute_reply":"2022-01-03T10:46:26.318172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df_train.shape,x_train.shape,y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:46:26.319632Z","iopub.execute_input":"2022-01-03T10:46:26.319851Z","iopub.status.idle":"2022-01-03T10:46:26.33076Z","shell.execute_reply.started":"2022-01-03T10:46:26.319816Z","shell.execute_reply":"2022-01-03T10:46:26.329944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=np.reshape(x_train,(42000,28,28,1))","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:46:26.331875Z","iopub.execute_input":"2022-01-03T10:46:26.332183Z","iopub.status.idle":"2022-01-03T10:46:26.340293Z","shell.execute_reply.started":"2022-01-03T10:46:26.332141Z","shell.execute_reply":"2022-01-03T10:46:26.339674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape,y_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:46:26.341252Z","iopub.execute_input":"2022-01-03T10:46:26.341485Z","iopub.status.idle":"2022-01-03T10:46:26.352191Z","shell.execute_reply.started":"2022-01-03T10:46:26.341458Z","shell.execute_reply":"2022-01-03T10:46:26.351568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_px = x_train.mean().astype(np.float32)\nstd_px = x_train.std().astype(np.float32)","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:46:26.352922Z","iopub.execute_input":"2022-01-03T10:46:26.353119Z","iopub.status.idle":"2022-01-03T10:46:26.613335Z","shell.execute_reply.started":"2022-01-03T10:46:26.353094Z","shell.execute_reply":"2022-01-03T10:46:26.611108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=(x_train-mean_px)/std_px","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:46:26.614431Z","iopub.execute_input":"2022-01-03T10:46:26.614799Z","iopub.status.idle":"2022-01-03T10:46:26.763572Z","shell.execute_reply.started":"2022-01-03T10:46:26.61475Z","shell.execute_reply":"2022-01-03T10:46:26.762648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:46:26.765517Z","iopub.execute_input":"2022-01-03T10:46:26.765777Z","iopub.status.idle":"2022-01-03T10:46:26.769734Z","shell.execute_reply.started":"2022-01-03T10:46:26.765747Z","shell.execute_reply":"2022-01-03T10:46:26.768941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, Y_train, Y_test = train_test_split(x_train, y_train, test_size=0.13, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:46:26.929137Z","iopub.execute_input":"2022-01-03T10:46:26.929429Z","iopub.status.idle":"2022-01-03T10:46:27.385178Z","shell.execute_reply.started":"2022-01-03T10:46:26.929396Z","shell.execute_reply":"2022-01-03T10:46:27.384411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D,Dropout\nfrom tensorflow.keras.layers import MaxPooling2D\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.layers import Flatten\nfrom tensorflow.keras.optimizers import SGD","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:46:27.386982Z","iopub.execute_input":"2022-01-03T10:46:27.387265Z","iopub.status.idle":"2022-01-03T10:46:27.393183Z","shell.execute_reply.started":"2022-01-03T10:46:27.387228Z","shell.execute_reply":"2022-01-03T10:46:27.392008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:46:27.709451Z","iopub.execute_input":"2022-01-03T10:46:27.709958Z","iopub.status.idle":"2022-01-03T10:46:27.713274Z","shell.execute_reply.started":"2022-01-03T10:46:27.709915Z","shell.execute_reply":"2022-01-03T10:46:27.712609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def define_model():\n    model = Sequential()\n    model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', input_shape=(28, 28, 1)))\n    model.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform'))\n    model.add(MaxPooling2D((2, 2)))\n    model.add(Flatten())\n    model.add(Dense(100, activation='relu', kernel_initializer='he_uniform'))\n    model.add(Dense(10, activation='softmax'))\n    opt = SGD(learning_rate=0.01, momentum=0.9)\n    model.compile(optimizer=opt, loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=['accuracy'])\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:46:28.090348Z","iopub.execute_input":"2022-01-03T10:46:28.090834Z","iopub.status.idle":"2022-01-03T10:46:28.100163Z","shell.execute_reply.started":"2022-01-03T10:46:28.090803Z","shell.execute_reply":"2022-01-03T10:46:28.099536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=define_model()\nhistory = model.fit(X_train, Y_train, epochs=20, batch_size=32, validation_data=(X_test, Y_test))","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:46:57.150041Z","iopub.execute_input":"2022-01-03T10:46:57.151006Z","iopub.status.idle":"2022-01-03T10:52:48.72026Z","shell.execute_reply.started":"2022-01-03T10:46:57.15095Z","shell.execute_reply":"2022-01-03T10:52:48.719462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test=pd.read_csv(\"../input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:52:58.950169Z","iopub.execute_input":"2022-01-03T10:52:58.950643Z","iopub.status.idle":"2022-01-03T10:53:01.052552Z","shell.execute_reply.started":"2022-01-03T10:52:58.950594Z","shell.execute_reply":"2022-01-03T10:53:01.051687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_px.shape,std_px.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:52:54.125697Z","iopub.execute_input":"2022-01-03T10:52:54.125992Z","iopub.status.idle":"2022-01-03T10:52:54.133386Z","shell.execute_reply.started":"2022-01-03T10:52:54.12596Z","shell.execute_reply":"2022-01-03T10:52:54.132427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test=df_test.values","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:53:04.350066Z","iopub.execute_input":"2022-01-03T10:53:04.35068Z","iopub.status.idle":"2022-01-03T10:53:04.355222Z","shell.execute_reply.started":"2022-01-03T10:53:04.350635Z","shell.execute_reply":"2022-01-03T10:53:04.3545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:53:05.535636Z","iopub.execute_input":"2022-01-03T10:53:05.536039Z","iopub.status.idle":"2022-01-03T10:53:05.541948Z","shell.execute_reply.started":"2022-01-03T10:53:05.535986Z","shell.execute_reply":"2022-01-03T10:53:05.540981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test=np.reshape(x_test,(28000,28,28,1))","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:53:06.959374Z","iopub.execute_input":"2022-01-03T10:53:06.959668Z","iopub.status.idle":"2022-01-03T10:53:06.963685Z","shell.execute_reply.started":"2022-01-03T10:53:06.959639Z","shell.execute_reply":"2022-01-03T10:53:06.963043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test=(x_test-mean_px)/std_px","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:53:08.21063Z","iopub.execute_input":"2022-01-03T10:53:08.211185Z","iopub.status.idle":"2022-01-03T10:53:08.324816Z","shell.execute_reply.started":"2022-01-03T10:53:08.211148Z","shell.execute_reply":"2022-01-03T10:53:08.324183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:53:12.209371Z","iopub.execute_input":"2022-01-03T10:53:12.209644Z","iopub.status.idle":"2022-01-03T10:53:19.521655Z","shell.execute_reply.started":"2022-01-03T10:53:12.209615Z","shell.execute_reply":"2022-01-03T10:53:19.5208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=y_pred.argmax(axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:53:19.523663Z","iopub.execute_input":"2022-01-03T10:53:19.523965Z","iopub.status.idle":"2022-01-03T10:53:19.529062Z","shell.execute_reply.started":"2022-01-03T10:53:19.523925Z","shell.execute_reply":"2022-01-03T10:53:19.528202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub=pd.read_csv(\"../input/digit-recognizer/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:53:19.530604Z","iopub.execute_input":"2022-01-03T10:53:19.530899Z","iopub.status.idle":"2022-01-03T10:53:19.556474Z","shell.execute_reply.started":"2022-01-03T10:53:19.53086Z","shell.execute_reply":"2022-01-03T10:53:19.555751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:53:19.558682Z","iopub.execute_input":"2022-01-03T10:53:19.558986Z","iopub.status.idle":"2022-01-03T10:53:19.568696Z","shell.execute_reply.started":"2022-01-03T10:53:19.558947Z","shell.execute_reply":"2022-01-03T10:53:19.568024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['Label']=y_pred","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:53:19.570053Z","iopub.execute_input":"2022-01-03T10:53:19.570838Z","iopub.status.idle":"2022-01-03T10:53:19.579482Z","shell.execute_reply.started":"2022-01-03T10:53:19.570805Z","shell.execute_reply":"2022-01-03T10:53:19.578857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:53:19.580747Z","iopub.execute_input":"2022-01-03T10:53:19.581204Z","iopub.status.idle":"2022-01-03T10:53:19.594086Z","shell.execute_reply.started":"2022-01-03T10:53:19.581162Z","shell.execute_reply":"2022-01-03T10:53:19.593496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv(\"Submmision_final2.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-01-03T10:53:19.595366Z","iopub.execute_input":"2022-01-03T10:53:19.595833Z","iopub.status.idle":"2022-01-03T10:53:19.656561Z","shell.execute_reply.started":"2022-01-03T10:53:19.595799Z","shell.execute_reply":"2022-01-03T10:53:19.655715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}