{"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":"2021-11-02T18:02:23.627909Z","iopub.execute_input":"2021-11-02T18:02:23.628288Z","iopub.status.idle":"2021-11-02T18:02:23.63971Z","shell.execute_reply.started":"2021-11-02T18:02:23.628251Z","shell.execute_reply":"2021-11-02T18:02:23.638803Z"},"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":{"execution":{"iopub.status.busy":"2021-11-02T18:02:23.843186Z","iopub.execute_input":"2021-11-02T18:02:23.843589Z","iopub.status.idle":"2021-11-02T18:02:27.411366Z","shell.execute_reply.started":"2021-11-02T18:02:23.843559Z","shell.execute_reply":"2021-11-02T18:02:27.410546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2021-11-02T18:02:27.412956Z","iopub.execute_input":"2021-11-02T18:02:27.413225Z","iopub.status.idle":"2021-11-02T18:02:27.43555Z","shell.execute_reply.started":"2021-11-02T18:02:27.413191Z","shell.execute_reply":"2021-11-02T18:02:27.4349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=np.array(train.iloc[:,1:]).astype(\"float32\")/255.0\nY_train=train.iloc[:,0]\nprint(Y_train.value_counts())\nY_train=np.array(Y_train).reshape(-1,1)\nimport keras\nY_train=keras.utils.np_utils.to_categorical(Y_train)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T18:02:27.43678Z","iopub.execute_input":"2021-11-02T18:02:27.437173Z","iopub.status.idle":"2021-11-02T18:02:27.607266Z","shell.execute_reply.started":"2021-11-02T18:02:27.437123Z","shell.execute_reply":"2021-11-02T18:02:27.606491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import Dense,Conv2D,Input,MaxPool2D,Activation,Flatten\nfrom keras import Sequential","metadata":{"execution":{"iopub.status.busy":"2021-11-02T18:02:27.609282Z","iopub.execute_input":"2021-11-02T18:02:27.609712Z","iopub.status.idle":"2021-11-02T18:02:27.614772Z","shell.execute_reply.started":"2021-11-02T18:02:27.609673Z","shell.execute_reply":"2021-11-02T18:02:27.614069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\nmodel.add(Input(shape=(784,)))\nmodel.add(Dense(256,activation=\"relu\"))\nmodel.add(Dense(128,activation=\"relu\"))\nmodel.add(Dense(64,activation=\"relu\"))\nmodel.add(Dense(10,activation=\"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2021-11-02T18:02:27.617705Z","iopub.execute_input":"2021-11-02T18:02:27.618024Z","iopub.status.idle":"2021-11-02T18:02:27.653088Z","shell.execute_reply.started":"2021-11-02T18:02:27.617997Z","shell.execute_reply":"2021-11-02T18:02:27.652457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"categorical_crossentropy\",metrics=[\"accuracy\"])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-02T18:02:27.654232Z","iopub.execute_input":"2021-11-02T18:02:27.654539Z","iopub.status.idle":"2021-11-02T18:02:27.667399Z","shell.execute_reply.started":"2021-11-02T18:02:27.654503Z","shell.execute_reply":"2021-11-02T18:02:27.666691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train,Y_train,batch_size=100,epochs=20,validation_split=0.3)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T18:02:27.668738Z","iopub.execute_input":"2021-11-02T18:02:27.669039Z","iopub.status.idle":"2021-11-02T18:03:09.268718Z","shell.execute_reply.started":"2021-11-02T18:02:27.669004Z","shell.execute_reply":"2021-11-02T18:03:09.268011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test=np.array(test)\ny_pred=model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T18:03:09.270197Z","iopub.execute_input":"2021-11-02T18:03:09.270452Z","iopub.status.idle":"2021-11-02T18:03:11.026481Z","shell.execute_reply.started":"2021-11-02T18:03:09.270419Z","shell.execute_reply":"2021-11-02T18:03:11.025718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\ny_classes = [np.argmax(y, axis=None, out=None) for y in y_pred]\ndf","metadata":{"execution":{"iopub.status.busy":"2021-11-02T18:03:11.029052Z","iopub.execute_input":"2021-11-02T18:03:11.029371Z","iopub.status.idle":"2021-11-02T18:03:11.122081Z","shell.execute_reply.started":"2021-11-02T18:03:11.029333Z","shell.execute_reply":"2021-11-02T18:03:11.121317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"Label\"]=y_classes\ndf","metadata":{"execution":{"iopub.status.busy":"2021-11-02T18:03:11.124826Z","iopub.execute_input":"2021-11-02T18:03:11.125077Z","iopub.status.idle":"2021-11-02T18:03:11.150609Z","shell.execute_reply.started":"2021-11-02T18:03:11.125042Z","shell.execute_reply":"2021-11-02T18:03:11.149658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T18:03:11.151687Z","iopub.execute_input":"2021-11-02T18:03:11.152124Z","iopub.status.idle":"2021-11-02T18:03:11.201174Z","shell.execute_reply.started":"2021-11-02T18:03:11.152085Z","shell.execute_reply":"2021-11-02T18:03:11.200424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=X_train.reshape(X_train.shape[0],28,28,1)\nmodel=Sequential()\nmodel.add(Conv2D(64,(3,3),input_shape=(28,28,1)))\nmodel.add(Activation(\"relu\"))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Conv2D(32,(3,3)))\nmodel.add(Activation(\"relu\"))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Flatten())\nmodel.add(Dense(64,activation=\"relu\"))\nmodel.add(Dense(10,activation=\"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2021-11-02T18:03:11.202426Z","iopub.execute_input":"2021-11-02T18:03:11.202679Z","iopub.status.idle":"2021-11-02T18:03:11.251304Z","shell.execute_reply.started":"2021-11-02T18:03:11.202648Z","shell.execute_reply":"2021-11-02T18:03:11.250664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"categorical_crossentropy\",metrics=[\"accuracy\"])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-02T18:03:11.252349Z","iopub.execute_input":"2021-11-02T18:03:11.252657Z","iopub.status.idle":"2021-11-02T18:03:11.2677Z","shell.execute_reply.started":"2021-11-02T18:03:11.252622Z","shell.execute_reply":"2021-11-02T18:03:11.266951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train,Y_train,batch_size=100,epochs=20,validation_split=0.3)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T18:03:11.268849Z","iopub.execute_input":"2021-11-02T18:03:11.269074Z","iopub.status.idle":"2021-11-02T18:03:52.925365Z","shell.execute_reply.started":"2021-11-02T18:03:11.269044Z","shell.execute_reply":"2021-11-02T18:03:52.924624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test=X_test.reshape(X_test.shape[0],28,28,1)\ny_pred=model.predict(X_test)\ny_classes = [np.argmax(y, axis=None, out=None) for y in y_pred]","metadata":{"execution":{"iopub.status.busy":"2021-11-02T18:04:57.78243Z","iopub.execute_input":"2021-11-02T18:04:57.782923Z","iopub.status.idle":"2021-11-02T18:04:59.566287Z","shell.execute_reply.started":"2021-11-02T18:04:57.782875Z","shell.execute_reply":"2021-11-02T18:04:59.565519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"Label\"]=y_classes\ndf","metadata":{"execution":{"iopub.status.busy":"2021-11-02T18:05:01.450071Z","iopub.execute_input":"2021-11-02T18:05:01.450664Z","iopub.status.idle":"2021-11-02T18:05:01.478174Z","shell.execute_reply.started":"2021-11-02T18:05:01.450626Z","shell.execute_reply":"2021-11-02T18:05:01.477203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv(\"submission_1.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T18:05:03.352798Z","iopub.execute_input":"2021-11-02T18:05:03.35306Z","iopub.status.idle":"2021-11-02T18:05:03.402728Z","shell.execute_reply.started":"2021-11-02T18:05:03.35303Z","shell.execute_reply":"2021-11-02T18:05:03.402063Z"},"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":[]}]}