{"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)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\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":"2023-04-05T03:21:07.870707Z","iopub.execute_input":"2023-04-05T03:21:07.871323Z","iopub.status.idle":"2023-04-05T03:21:07.887516Z","shell.execute_reply.started":"2023-04-05T03:21:07.871264Z","shell.execute_reply":"2023-04-05T03:21:07.886130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\ntrain_data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:07.890352Z","iopub.execute_input":"2023-04-05T03:21:07.891172Z","iopub.status.idle":"2023-04-05T03:21:11.873053Z","shell.execute_reply.started":"2023-04-05T03:21:07.891121Z","shell.execute_reply":"2023-04-05T03:21:11.871656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:11.876042Z","iopub.execute_input":"2023-04-05T03:21:11.876852Z","iopub.status.idle":"2023-04-05T03:21:11.903531Z","shell.execute_reply.started":"2023-04-05T03:21:11.876799Z","shell.execute_reply":"2023-04-05T03:21:11.902197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:11.905229Z","iopub.execute_input":"2023-04-05T03:21:11.906207Z","iopub.status.idle":"2023-04-05T03:21:11.930055Z","shell.execute_reply.started":"2023-04-05T03:21:11.906156Z","shell.execute_reply":"2023-04-05T03:21:11.928778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:11.932969Z","iopub.execute_input":"2023-04-05T03:21:11.933537Z","iopub.status.idle":"2023-04-05T03:21:11.946034Z","shell.execute_reply.started":"2023-04-05T03:21:11.933483Z","shell.execute_reply":"2023-04-05T03:21:11.944787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data = train_data, x = \"label\")","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:11.947700Z","iopub.execute_input":"2023-04-05T03:21:11.949092Z","iopub.status.idle":"2023-04-05T03:21:12.144564Z","shell.execute_reply.started":"2023-04-05T03:21:11.949052Z","shell.execute_reply":"2023-04-05T03:21:12.142837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.info()","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:12.146058Z","iopub.execute_input":"2023-04-05T03:21:12.146412Z","iopub.status.idle":"2023-04-05T03:21:12.191878Z","shell.execute_reply.started":"2023-04-05T03:21:12.146376Z","shell.execute_reply":"2023-04-05T03:21:12.190710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_id = sample_sub.ImageId.copy()","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:12.193386Z","iopub.execute_input":"2023-04-05T03:21:12.193706Z","iopub.status.idle":"2023-04-05T03:21:12.198937Z","shell.execute_reply.started":"2023-04-05T03:21:12.193675Z","shell.execute_reply":"2023-04-05T03:21:12.197793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.dtypes","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:12.200348Z","iopub.execute_input":"2023-04-05T03:21:12.200654Z","iopub.status.idle":"2023-04-05T03:21:12.213848Z","shell.execute_reply.started":"2023-04-05T03:21:12.200625Z","shell.execute_reply":"2023-04-05T03:21:12.212361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = train_data.drop([\"label\"], axis = 1)\ny_train = train_data[\"label\"]","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:12.215613Z","iopub.execute_input":"2023-04-05T03:21:12.216010Z","iopub.status.idle":"2023-04-05T03:21:12.346940Z","shell.execute_reply.started":"2023-04-05T03:21:12.215974Z","shell.execute_reply":"2023-04-05T03:21:12.345775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = test_data\ny_test = sample_sub","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:12.348390Z","iopub.execute_input":"2023-04-05T03:21:12.348813Z","iopub.status.idle":"2023-04-05T03:21:12.360021Z","shell.execute_reply.started":"2023-04-05T03:21:12.348746Z","shell.execute_reply":"2023-04-05T03:21:12.359062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_vals = x_train.isnull().sum()\nprint(missing_vals[missing_vals > 0])","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:12.361153Z","iopub.execute_input":"2023-04-05T03:21:12.361633Z","iopub.status.idle":"2023-04-05T03:21:12.418780Z","shell.execute_reply.started":"2023-04-05T03:21:12.361583Z","shell.execute_reply":"2023-04-05T03:21:12.417499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missing_vals = x_test.isnull().sum()\nprint(missing_vals[missing_vals > 0])","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:12.420623Z","iopub.execute_input":"2023-04-05T03:21:12.421392Z","iopub.status.idle":"2023-04-05T03:21:12.460643Z","shell.execute_reply.started":"2023-04-05T03:21:12.421341Z","shell.execute_reply":"2023-04-05T03:21:12.459300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:12.467581Z","iopub.execute_input":"2023-04-05T03:21:12.468059Z","iopub.status.idle":"2023-04-05T03:21:12.475054Z","shell.execute_reply.started":"2023-04-05T03:21:12.468025Z","shell.execute_reply":"2023-04-05T03:21:12.473629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = pd.get_dummies(y_train).values","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:12.476731Z","iopub.execute_input":"2023-04-05T03:21:12.477107Z","iopub.status.idle":"2023-04-05T03:21:12.485521Z","shell.execute_reply.started":"2023-04-05T03:21:12.477072Z","shell.execute_reply":"2023-04-05T03:21:12.483905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nx, x_val, y, y_val = train_test_split(x_train, y_train, test_size = 0.1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:12.486865Z","iopub.execute_input":"2023-04-05T03:21:12.487306Z","iopub.status.idle":"2023-04-05T03:21:12.681934Z","shell.execute_reply.started":"2023-04-05T03:21:12.487259Z","shell.execute_reply":"2023-04-05T03:21:12.680889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:12.683420Z","iopub.execute_input":"2023-04-05T03:21:12.684075Z","iopub.status.idle":"2023-04-05T03:21:12.692469Z","shell.execute_reply.started":"2023-04-05T03:21:12.684015Z","shell.execute_reply":"2023-04-05T03:21:12.691179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:12.693934Z","iopub.execute_input":"2023-04-05T03:21:12.694706Z","iopub.status.idle":"2023-04-05T03:21:12.706647Z","shell.execute_reply.started":"2023-04-05T03:21:12.694655Z","shell.execute_reply":"2023-04-05T03:21:12.705374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:12.708169Z","iopub.execute_input":"2023-04-05T03:21:12.708971Z","iopub.status.idle":"2023-04-05T03:21:12.718168Z","shell.execute_reply.started":"2023-04-05T03:21:12.708931Z","shell.execute_reply":"2023-04-05T03:21:12.717198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import layers\nfrom keras import models\nfrom keras.datasets import mnist\nfrom keras.utils import to_categorical\n\n# Define the CNN model\nmodel = models.Sequential()\n\nmodel.add(layers.Conv2D(32, (5,5), activation='relu', input_shape=(28, 28,1)))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\nmodel.add(layers.Conv2D(64, (5, 5), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(10, activation='softmax'))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:12.719803Z","iopub.execute_input":"2023-04-05T03:21:12.720262Z","iopub.status.idle":"2023-04-05T03:21:12.804047Z","shell.execute_reply.started":"2023-04-05T03:21:12.720214Z","shell.execute_reply":"2023-04-05T03:21:12.802797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = np.array(x).reshape(x.shape[0], 28, 28, 1)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:12.805995Z","iopub.execute_input":"2023-04-05T03:21:12.806495Z","iopub.status.idle":"2023-04-05T03:21:12.899713Z","shell.execute_reply.started":"2023-04-05T03:21:12.806460Z","shell.execute_reply":"2023-04-05T03:21:12.898435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:12.901239Z","iopub.execute_input":"2023-04-05T03:21:12.901621Z","iopub.status.idle":"2023-04-05T03:21:12.909136Z","shell.execute_reply.started":"2023-04-05T03:21:12.901586Z","shell.execute_reply":"2023-04-05T03:21:12.907913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = x\ntrain_images = train_images.astype('float32') / 255","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:12.910620Z","iopub.execute_input":"2023-04-05T03:21:12.911018Z","iopub.status.idle":"2023-04-05T03:21:12.985847Z","shell.execute_reply.started":"2023-04-05T03:21:12.910983Z","shell.execute_reply":"2023-04-05T03:21:12.984462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = np.array(x_test).reshape(x_test.shape[0], 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:12.987461Z","iopub.execute_input":"2023-04-05T03:21:12.987968Z","iopub.status.idle":"2023-04-05T03:21:13.061439Z","shell.execute_reply.started":"2023-04-05T03:21:12.987918Z","shell.execute_reply":"2023-04-05T03:21:13.060112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = x_test\ntest_images = test_images.astype('float32') / 255\n\ntest_images.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:13.063122Z","iopub.execute_input":"2023-04-05T03:21:13.063604Z","iopub.status.idle":"2023-04-05T03:21:13.122959Z","shell.execute_reply.started":"2023-04-05T03:21:13.063551Z","shell.execute_reply":"2023-04-05T03:21:13.121828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_val = np.array(x_val).reshape(x_val.shape[0], 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:13.124569Z","iopub.execute_input":"2023-04-05T03:21:13.125835Z","iopub.status.idle":"2023-04-05T03:21:13.138222Z","shell.execute_reply.started":"2023-04-05T03:21:13.125778Z","shell.execute_reply":"2023-04-05T03:21:13.136694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_val = x_val.astype('float32') / 255\n\nx_val.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:13.139979Z","iopub.execute_input":"2023-04-05T03:21:13.140334Z","iopub.status.idle":"2023-04-05T03:21:13.152375Z","shell.execute_reply.started":"2023-04-05T03:21:13.140302Z","shell.execute_reply":"2023-04-05T03:21:13.151270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:13.154013Z","iopub.execute_input":"2023-04-05T03:21:13.154339Z","iopub.status.idle":"2023-04-05T03:21:13.161080Z","shell.execute_reply.started":"2023-04-05T03:21:13.154309Z","shell.execute_reply":"2023-04-05T03:21:13.159837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:13.162925Z","iopub.execute_input":"2023-04-05T03:21:13.163553Z","iopub.status.idle":"2023-04-05T03:21:13.173226Z","shell.execute_reply.started":"2023-04-05T03:21:13.163518Z","shell.execute_reply":"2023-04-05T03:21:13.171992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:13.174494Z","iopub.execute_input":"2023-04-05T03:21:13.174847Z","iopub.status.idle":"2023-04-05T03:21:13.184345Z","shell.execute_reply.started":"2023-04-05T03:21:13.174804Z","shell.execute_reply":"2023-04-05T03:21:13.183230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',\n              optimizer='rmsprop',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:13.185773Z","iopub.execute_input":"2023-04-05T03:21:13.186135Z","iopub.status.idle":"2023-04-05T03:21:13.201856Z","shell.execute_reply.started":"2023-04-05T03:21:13.186100Z","shell.execute_reply":"2023-04-05T03:21:13.200820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Use the training data to train the model\nmodel.fit(train_images, y,\n          validation_data = (x_val,y_val),\n          batch_size=100,\n          epochs=20,\n          verbose=1)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:21:13.203051Z","iopub.execute_input":"2023-04-05T03:21:13.203677Z","iopub.status.idle":"2023-04-05T03:28:35.918468Z","shell.execute_reply.started":"2023-04-05T03:21:13.203641Z","shell.execute_reply":"2023-04-05T03:28:35.917433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_test = model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:28:35.946334Z","iopub.execute_input":"2023-04-05T03:28:35.946855Z","iopub.status.idle":"2023-04-05T03:28:42.258136Z","shell.execute_reply.started":"2023-04-05T03:28:35.946805Z","shell.execute_reply":"2023-04-05T03:28:42.257046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_test = np.argmax(preds_test,axis = 1)\n\nresults = pd.Series(preds_test,name=\"Label\")","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:28:42.259523Z","iopub.execute_input":"2023-04-05T03:28:42.260280Z","iopub.status.idle":"2023-04-05T03:28:42.266257Z","shell.execute_reply.started":"2023-04-05T03:28:42.260239Z","shell.execute_reply":"2023-04-05T03:28:42.265305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'ImageId': test_id,\n                       'Label': preds_test})\n\noutput.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-05T03:28:42.267634Z","iopub.execute_input":"2023-04-05T03:28:42.268055Z","iopub.status.idle":"2023-04-05T03:28:42.309455Z","shell.execute_reply.started":"2023-04-05T03:28:42.268019Z","shell.execute_reply":"2023-04-05T03:28:42.308245Z"},"trusted":true},"execution_count":null,"outputs":[]}]}