{"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\nfrom sklearn.ensemble import RandomForestClassifier\n\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.layers import Dense,Conv2D,Flatten,MaxPooling2D\nfrom tensorflow.keras.models import Sequential\n\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-06-22T11:34:05.344612Z","iopub.execute_input":"2022-06-22T11:34:05.345145Z","iopub.status.idle":"2022-06-22T11:34:05.355301Z","shell.execute_reply.started":"2022-06-22T11:34:05.345101Z","shell.execute_reply":"2022-06-22T11:34:05.354081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\nxtest=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nsample=pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-06-22T11:34:05.473139Z","iopub.execute_input":"2022-06-22T11:34:05.473942Z","iopub.status.idle":"2022-06-22T11:34:10.168544Z","shell.execute_reply.started":"2022-06-22T11:34:05.473893Z","shell.execute_reply":"2022-06-22T11:34:10.167391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Output labels for train dataset\ny=X['label']\n#Input features for the train dataset\nxtrain=X.drop(['label'],axis=1)\nxtrain","metadata":{"execution":{"iopub.status.busy":"2022-06-22T11:34:10.170648Z","iopub.execute_input":"2022-06-22T11:34:10.171046Z","iopub.status.idle":"2022-06-22T11:34:10.361665Z","shell.execute_reply.started":"2022-06-22T11:34:10.171012Z","shell.execute_reply":"2022-06-22T11:34:10.360307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtrain.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-22T11:34:10.363424Z","iopub.execute_input":"2022-06-22T11:34:10.364503Z","iopub.status.idle":"2022-06-22T11:34:10.371529Z","shell.execute_reply.started":"2022-06-22T11:34:10.364464Z","shell.execute_reply":"2022-06-22T11:34:10.370521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf = RandomForestClassifier()\nrf.fit(xtrain, y)\n","metadata":{"execution":{"iopub.status.busy":"2022-06-22T11:34:10.374369Z","iopub.execute_input":"2022-06-22T11:34:10.375353Z","iopub.status.idle":"2022-06-22T11:34:44.460096Z","shell.execute_reply.started":"2022-06-22T11:34:10.375312Z","shell.execute_reply":"2022-06-22T11:34:44.458847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results=rf.predict(xtest) #0.9652 on test results","metadata":{"execution":{"iopub.status.busy":"2022-06-22T11:34:44.461574Z","iopub.execute_input":"2022-06-22T11:34:44.462079Z","iopub.status.idle":"2022-06-22T11:34:45.758832Z","shell.execute_reply.started":"2022-06-22T11:34:44.462043Z","shell.execute_reply":"2022-06-22T11:34:45.757575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=to_categorical(y)\ny.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-22T11:34:45.760550Z","iopub.execute_input":"2022-06-22T11:34:45.761315Z","iopub.status.idle":"2022-06-22T11:34:45.769567Z","shell.execute_reply.started":"2022-06-22T11:34:45.761275Z","shell.execute_reply":"2022-06-22T11:34:45.768421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtrain=xtrain/255\nxtest=xtest/255","metadata":{"execution":{"iopub.status.busy":"2022-06-22T11:34:45.771128Z","iopub.execute_input":"2022-06-22T11:34:45.771471Z","iopub.status.idle":"2022-06-22T11:34:45.938420Z","shell.execute_reply.started":"2022-06-22T11:34:45.771439Z","shell.execute_reply":"2022-06-22T11:34:45.937480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arr_xtrain=xtrain.loc[:,:].values\narr_xtrain=arr_xtrain.reshape(arr_xtrain.shape[0],28,28,1)\narr_xtrain.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-22T11:34:45.939400Z","iopub.execute_input":"2022-06-22T11:34:45.939718Z","iopub.status.idle":"2022-06-22T11:34:45.948194Z","shell.execute_reply.started":"2022-06-22T11:34:45.939690Z","shell.execute_reply":"2022-06-22T11:34:45.946997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arr_xtest=xtest.loc[:,:].values\narr_xtest=arr_xtest.reshape(arr_xtest.shape[0],28,28,1)\narr_xtest.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-22T11:34:45.949834Z","iopub.execute_input":"2022-06-22T11:34:45.950563Z","iopub.status.idle":"2022-06-22T11:34:45.962515Z","shell.execute_reply.started":"2022-06-22T11:34:45.950526Z","shell.execute_reply":"2022-06-22T11:34:45.961395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\nmodel.add(Conv2D(filters=32,kernel_size=(4,4),input_shape=(28,28,1),activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Flatten())\nmodel.add(Dense(128,activation='relu'))\nmodel.add(Dense(10,activation='softmax'))\nmodel.compile(loss='categorical_crossentropy',optimizer='rmsprop',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-06-22T11:34:45.965961Z","iopub.execute_input":"2022-06-22T11:34:45.966309Z","iopub.status.idle":"2022-06-22T11:34:46.024730Z","shell.execute_reply.started":"2022-06-22T11:34:45.966278Z","shell.execute_reply":"2022-06-22T11:34:46.023473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-06-22T11:34:46.026189Z","iopub.execute_input":"2022-06-22T11:34:46.026514Z","iopub.status.idle":"2022-06-22T11:34:46.033820Z","shell.execute_reply.started":"2022-06-22T11:34:46.026484Z","shell.execute_reply":"2022-06-22T11:34:46.032431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(arr_xtrain,y,epochs=15)","metadata":{"execution":{"iopub.status.busy":"2022-06-22T11:34:46.035771Z","iopub.execute_input":"2022-06-22T11:34:46.036848Z","iopub.status.idle":"2022-06-22T11:37:41.493127Z","shell.execute_reply.started":"2022-06-22T11:34:46.036793Z","shell.execute_reply":"2022-06-22T11:37:41.491823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results=np.argmax(model.predict(arr_xtest),axis=-1)\nresults\n","metadata":{"execution":{"iopub.status.busy":"2022-06-22T11:38:33.163347Z","iopub.execute_input":"2022-06-22T11:38:33.163789Z","iopub.status.idle":"2022-06-22T11:38:35.626686Z","shell.execute_reply.started":"2022-06-22T11:38:33.163753Z","shell.execute_reply":"2022-06-22T11:38:35.625386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_df=pd.DataFrame({'ImageId':sample['ImageId'],'Label':results})\nfinal_df.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-06-22T11:38:40.673744Z","iopub.execute_input":"2022-06-22T11:38:40.674171Z","iopub.status.idle":"2022-06-22T11:38:40.728354Z","shell.execute_reply.started":"2022-06-22T11:38:40.674136Z","shell.execute_reply":"2022-06-22T11:38:40.726983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}