{"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-31T18:30:27.233323Z","iopub.execute_input":"2022-01-31T18:30:27.233726Z","iopub.status.idle":"2022-01-31T18:30:27.244585Z","shell.execute_reply.started":"2022-01-31T18:30:27.233693Z","shell.execute_reply":"2022-01-31T18:30:27.243792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras import datasets,layers,models\nfrom tensorflow import keras\n\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-01-31T18:30:27.246446Z","iopub.execute_input":"2022-01-31T18:30:27.247005Z","iopub.status.idle":"2022-01-31T18:30:34.566145Z","shell.execute_reply.started":"2022-01-31T18:30:27.246958Z","shell.execute_reply":"2022-01-31T18:30:34.564876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainData = pd.read_csv('../input/digit-recognizer/train.csv')\ntestData = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-01-31T18:30:34.567557Z","iopub.execute_input":"2022-01-31T18:30:34.568017Z","iopub.status.idle":"2022-01-31T18:30:40.521725Z","shell.execute_reply.started":"2022-01-31T18:30:34.567983Z","shell.execute_reply":"2022-01-31T18:30:40.520516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y =trainData['label']\n\nX=trainData.drop(columns = 'label')","metadata":{"execution":{"iopub.status.busy":"2022-01-31T18:30:40.522951Z","iopub.execute_input":"2022-01-31T18:30:40.523796Z","iopub.status.idle":"2022-01-31T18:30:40.645143Z","shell.execute_reply.started":"2022-01-31T18:30:40.523754Z","shell.execute_reply":"2022-01-31T18:30:40.643823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X / 255\nX = np.array(X, dtype=\"float32\")\nX = X.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2022-01-31T18:30:40.647705Z","iopub.execute_input":"2022-01-31T18:30:40.648471Z","iopub.status.idle":"2022-01-31T18:30:40.828970Z","shell.execute_reply.started":"2022-01-31T18:30:40.648409Z","shell.execute_reply":"2022-01-31T18:30:40.827992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y)","metadata":{"execution":{"iopub.status.busy":"2022-01-31T18:30:40.831883Z","iopub.execute_input":"2022-01-31T18:30:40.832231Z","iopub.status.idle":"2022-01-31T18:30:41.198261Z","shell.execute_reply.started":"2022-01-31T18:30:40.832178Z","shell.execute_reply":"2022-01-31T18:30:41.197405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-31T18:30:41.199490Z","iopub.execute_input":"2022-01-31T18:30:41.199710Z","iopub.status.idle":"2022-01-31T18:30:41.207620Z","shell.execute_reply.started":"2022-01-31T18:30:41.199683Z","shell.execute_reply":"2022-01-31T18:30:41.206715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model= tf.keras.Sequential([\n  tf.keras.layers.Conv2D(32, 3, input_shape=(28, 28, 1), activation='relu'),\n  tf.keras.layers.MaxPooling2D(),\n  tf.keras.layers.Flatten(),\n  tf.keras.layers.Dense(128,  activation=tf.keras.layers.LeakyReLU(alpha=0.01)),\n  tf.keras.layers.Dense(10, activation='softmax')\n])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-01-31T18:30:41.208980Z","iopub.execute_input":"2022-01-31T18:30:41.209309Z","iopub.status.idle":"2022-01-31T18:30:41.359562Z","shell.execute_reply.started":"2022-01-31T18:30:41.209279Z","shell.execute_reply":"2022-01-31T18:30:41.358462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-01-31T18:31:06.228817Z","iopub.execute_input":"2022-01-31T18:31:06.229227Z","iopub.status.idle":"2022-01-31T18:31:06.241491Z","shell.execute_reply.started":"2022-01-31T18:31:06.229195Z","shell.execute_reply":"2022-01-31T18:31:06.240885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train,y_train, epochs=15)","metadata":{"execution":{"iopub.status.busy":"2022-01-31T18:31:09.963908Z","iopub.execute_input":"2022-01-31T18:31:09.964329Z","iopub.status.idle":"2022-01-31T18:33:07.813302Z","shell.execute_reply.started":"2022-01-31T18:31:09.964297Z","shell.execute_reply":"2022-01-31T18:33:07.812200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-01-31T18:10:18.163358Z","iopub.execute_input":"2022-01-31T18:10:18.164355Z","iopub.status.idle":"2022-01-31T18:10:18.302789Z","shell.execute_reply.started":"2022-01-31T18:10:18.164305Z","shell.execute_reply":"2022-01-31T18:10:18.301608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-01-31T18:11:34.221822Z","iopub.execute_input":"2022-01-31T18:11:34.222076Z","iopub.status.idle":"2022-01-31T18:11:34.230787Z","shell.execute_reply.started":"2022-01-31T18:11:34.222049Z","shell.execute_reply":"2022-01-31T18:11:34.229856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = np.array(testData, dtype=np.float32)/255\ntest = test.reshape(-1,28,28,1)\nprediction = model.predict(test)\npredict = np.array(np.round(prediction), dtype = np.int32)\npredict = np.argmax(predict , axis=1).reshape(-1, 1)\nsubmission_df = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\nsubmission_df['Label'] = predict\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-01-31T18:33:35.850906Z","iopub.execute_input":"2022-01-31T18:33:35.851206Z","iopub.status.idle":"2022-01-31T18:33:38.010593Z","shell.execute_reply.started":"2022-01-31T18:33:35.851160Z","shell.execute_reply":"2022-01-31T18:33:38.009682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-01-31T18:10:23.625777Z","iopub.execute_input":"2022-01-31T18:10:23.626716Z","iopub.status.idle":"2022-01-31T18:10:23.874564Z","shell.execute_reply.started":"2022-01-31T18:10:23.626647Z","shell.execute_reply":"2022-01-31T18:10:23.873778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-01-31T18:10:25.143805Z","iopub.execute_input":"2022-01-31T18:10:25.145032Z","iopub.status.idle":"2022-01-31T18:10:25.153807Z","shell.execute_reply.started":"2022-01-31T18:10:25.144969Z","shell.execute_reply":"2022-01-31T18:10:25.152063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-01-31T18:11:37.525511Z","iopub.execute_input":"2022-01-31T18:11:37.525808Z","iopub.status.idle":"2022-01-31T18:14:06.353732Z","shell.execute_reply.started":"2022-01-31T18:11:37.525779Z","shell.execute_reply":"2022-01-31T18:14:06.35269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-01-31T18:22:16.510434Z","iopub.execute_input":"2022-01-31T18:22:16.511435Z","iopub.status.idle":"2022-01-31T18:22:17.960256Z","shell.execute_reply.started":"2022-01-31T18:22:16.511408Z","shell.execute_reply":"2022-01-31T18:22:17.95889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}