{"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\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report, confusion_matrix\nfrom sklearn.model_selection import RandomizedSearchCV, GridSearchCV \nfrom sklearn.model_selection import cross_val_score\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-03-12T02:47:25.078029Z","iopub.execute_input":"2022-03-12T02:47:25.079086Z","iopub.status.idle":"2022-03-12T02:47:25.092785Z","shell.execute_reply.started":"2022-03-12T02:47:25.079048Z","shell.execute_reply":"2022-03-12T02:47:25.092079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_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":"2022-03-12T02:47:32.863799Z","iopub.execute_input":"2022-03-12T02:47:32.864386Z","iopub.status.idle":"2022-03-12T02:47:37.360959Z","shell.execute_reply.started":"2022-03-12T02:47:32.864349Z","shell.execute_reply":"2022-03-12T02:47:37.359977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_data.shape[0])\nprint(test_data.shape[0])","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:48:15.913636Z","iopub.execute_input":"2022-03-12T02:48:15.913944Z","iopub.status.idle":"2022-03-12T02:48:15.919980Z","shell.execute_reply.started":"2022-03-12T02:48:15.913913Z","shell.execute_reply":"2022-03-12T02:48:15.919236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:46:28.220319Z","iopub.execute_input":"2022-03-12T02:46:28.220658Z","iopub.status.idle":"2022-03-12T02:46:28.248957Z","shell.execute_reply.started":"2022-03-12T02:46:28.220625Z","shell.execute_reply":"2022-03-12T02:46:28.247661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=train_data.iloc[:,1:]\ny=train_data.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:50:09.214489Z","iopub.execute_input":"2022-03-12T02:50:09.214812Z","iopub.status.idle":"2022-03-12T02:50:09.220650Z","shell.execute_reply.started":"2022-03-12T02:50:09.214780Z","shell.execute_reply":"2022-03-12T02:50:09.219730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nfor i in range(20):\n    plt.subplot(4, 10, i+1)\n    img = X.iloc[i,:].values\n    img = img.reshape(28,28).astype('uint8')\n    plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:55:43.433766Z","iopub.execute_input":"2022-03-12T02:55:43.434604Z","iopub.status.idle":"2022-03-12T02:55:45.341992Z","shell.execute_reply.started":"2022-03-12T02:55:43.434556Z","shell.execute_reply":"2022-03-12T02:55:45.341106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.value_counts().sort_index()","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:53:08.591243Z","iopub.execute_input":"2022-03-12T02:53:08.591509Z","iopub.status.idle":"2022-03-12T02:53:08.601218Z","shell.execute_reply.started":"2022-03-12T02:53:08.591482Z","shell.execute_reply":"2022-03-12T02:53:08.600624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)\nprint(X_train.shape)\nprint(X_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:56:41.692714Z","iopub.execute_input":"2022-03-12T02:56:41.692981Z","iopub.status.idle":"2022-03-12T02:56:41.975273Z","shell.execute_reply.started":"2022-03-12T02:56:41.692951Z","shell.execute_reply":"2022-03-12T02:56:41.974659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = RandomForestClassifier()\nmodel.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:57:08.345442Z","iopub.execute_input":"2022-03-12T02:57:08.345863Z","iopub.status.idle":"2022-03-12T02:57:27.505536Z","shell.execute_reply.started":"2022-03-12T02:57:08.345833Z","shell.execute_reply":"2022-03-12T02:57:27.504669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:58:08.312238Z","iopub.execute_input":"2022-03-12T02:58:08.312516Z","iopub.status.idle":"2022-03-12T02:58:08.893914Z","shell.execute_reply.started":"2022-03-12T02:58:08.312488Z","shell.execute_reply":"2022-03-12T02:58:08.892901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print (\"Classification Report:\\n\", classification_report(y_test, y_pred))\nprint (\"Confusion Report\\n:\", confusion_matrix(y_test, y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:59:36.898718Z","iopub.execute_input":"2022-03-12T02:59:36.899273Z","iopub.status.idle":"2022-03-12T02:59:36.941875Z","shell.execute_reply.started":"2022-03-12T02:59:36.899231Z","shell.execute_reply":"2022-03-12T02:59:36.940788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=model.predict(test_data)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T03:05:30.875958Z","iopub.execute_input":"2022-03-12T03:05:30.876554Z","iopub.status.idle":"2022-03-12T03:05:32.224614Z","shell.execute_reply.started":"2022-03-12T03:05:30.876512Z","shell.execute_reply":"2022-03-12T03:05:32.223435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = pd.Series(y_pred,name=\"Label\")\nsubmission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),y_pred],axis = 1)\nsubmission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T03:15:49.976273Z","iopub.execute_input":"2022-03-12T03:15:49.976618Z","iopub.status.idle":"2022-03-12T03:15:50.033778Z","shell.execute_reply.started":"2022-03-12T03:15:49.976584Z","shell.execute_reply":"2022-03-12T03:15:50.032905Z"},"trusted":true},"execution_count":null,"outputs":[]}]}