{"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\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import confusion_matrix, accuracy_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-12-07T17:23:55.711140Z","iopub.execute_input":"2022-12-07T17:23:55.711585Z","iopub.status.idle":"2022-12-07T17:23:56.311610Z","shell.execute_reply.started":"2022-12-07T17:23:55.711492Z","shell.execute_reply":"2022-12-07T17:23:56.310375Z"},"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":"2022-12-07T17:23:56.313988Z","iopub.execute_input":"2022-12-07T17:23:56.315174Z","iopub.status.idle":"2022-12-07T17:24:01.990804Z","shell.execute_reply.started":"2022-12-07T17:23:56.315139Z","shell.execute_reply":"2022-12-07T17:24:01.989837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = train.iloc[:, 1:].values.reshape(-1, 28, 28)","metadata":{"execution":{"iopub.status.busy":"2022-12-07T17:24:01.991922Z","iopub.execute_input":"2022-12-07T17:24:01.992301Z","iopub.status.idle":"2022-12-07T17:24:01.997731Z","shell.execute_reply.started":"2022-12-07T17:24:01.992269Z","shell.execute_reply":"2022-12-07T17:24:01.996696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(train_images[50], cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2022-12-07T17:24:01.999084Z","iopub.execute_input":"2022-12-07T17:24:01.999463Z","iopub.status.idle":"2022-12-07T17:24:02.242864Z","shell.execute_reply.started":"2022-12-07T17:24:01.999431Z","shell.execute_reply":"2022-12-07T17:24:02.241628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.iloc[50, 0]","metadata":{"execution":{"iopub.status.busy":"2022-12-07T17:24:02.246916Z","iopub.execute_input":"2022-12-07T17:24:02.247462Z","iopub.status.idle":"2022-12-07T17:24:02.255542Z","shell.execute_reply.started":"2022-12-07T17:24:02.247415Z","shell.execute_reply":"2022-12-07T17:24:02.254241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(train_images, train.iloc[:, 0], test_size=0.3, random_state=0)\n\nprint(X_train.shape)\nprint(X_val.shape)","metadata":{"execution":{"iopub.status.busy":"2022-12-07T17:24:02.257488Z","iopub.execute_input":"2022-12-07T17:24:02.257992Z","iopub.status.idle":"2022-12-07T17:24:02.692639Z","shell.execute_reply.started":"2022-12-07T17:24:02.257930Z","shell.execute_reply":"2022-12-07T17:24:02.691486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc = StandardScaler()\nX_train = sc.fit_transform(X_train.reshape(-1, 28*28)).reshape(-1, 28, 28)\nX_val = sc.transform(X_val.reshape(-1, 28*28)).reshape(-1, 28, 28)","metadata":{"execution":{"iopub.status.busy":"2022-12-07T17:24:02.694766Z","iopub.execute_input":"2022-12-07T17:24:02.695218Z","iopub.status.idle":"2022-12-07T17:24:03.173108Z","shell.execute_reply.started":"2022-12-07T17:24:02.695175Z","shell.execute_reply":"2022-12-07T17:24:03.172207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_train[6], cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2022-12-07T17:24:03.174345Z","iopub.execute_input":"2022-12-07T17:24:03.174693Z","iopub.status.idle":"2022-12-07T17:24:03.378282Z","shell.execute_reply.started":"2022-12-07T17:24:03.174645Z","shell.execute_reply":"2022-12-07T17:24:03.377230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.iloc[6]","metadata":{"execution":{"iopub.status.busy":"2022-12-07T17:24:03.379737Z","iopub.execute_input":"2022-12-07T17:24:03.380310Z","iopub.status.idle":"2022-12-07T17:24:03.387265Z","shell.execute_reply.started":"2022-12-07T17:24:03.380278Z","shell.execute_reply":"2022-12-07T17:24:03.386198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classifier = LogisticRegression(random_state=0, max_iter=1000)\nclassifier.fit(X_train.reshape(-1, 28*28), y_train)\ny_pred = classifier.predict(X_val.reshape(-1, 28*28))","metadata":{"execution":{"iopub.status.busy":"2022-12-07T17:24:03.388790Z","iopub.execute_input":"2022-12-07T17:24:03.389121Z","iopub.status.idle":"2022-12-07T17:26:07.530599Z","shell.execute_reply.started":"2022-12-07T17:24:03.389091Z","shell.execute_reply":"2022-12-07T17:26:07.528860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import metrics\nprint(metrics.classification_report(y_val, y_pred, digits=2))\ncm = confusion_matrix(y_val, y_pred)\nplt.figure(figsize=(10, 7))\nsns.heatmap(cm, annot=True, fmt='d')\nplt.xlabel('Predicted')\nplt.ylabel('Truth')","metadata":{"execution":{"iopub.status.busy":"2022-12-07T17:26:07.533209Z","iopub.execute_input":"2022-12-07T17:26:07.534406Z","iopub.status.idle":"2022-12-07T17:26:08.353789Z","shell.execute_reply.started":"2022-12-07T17:26:07.534342Z","shell.execute_reply":"2022-12-07T17:26:08.352700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = test.values.reshape(-1, 28, 28)\ntest_images = sc.transform(test_images.reshape(-1, 28*28)).reshape(-1, 28, 28)","metadata":{"execution":{"iopub.status.busy":"2022-12-07T17:26:08.355377Z","iopub.execute_input":"2022-12-07T17:26:08.355810Z","iopub.status.idle":"2022-12-07T17:26:08.477714Z","shell.execute_reply.started":"2022-12-07T17:26:08.355769Z","shell.execute_reply":"2022-12-07T17:26:08.476809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(test_images[0], cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2022-12-07T17:26:08.478963Z","iopub.execute_input":"2022-12-07T17:26:08.479299Z","iopub.status.idle":"2022-12-07T17:26:08.666758Z","shell.execute_reply.started":"2022-12-07T17:26:08.479269Z","shell.execute_reply":"2022-12-07T17:26:08.665392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predtest = classifier.predict(test_images.reshape(-1, 28*28))","metadata":{"execution":{"iopub.status.busy":"2022-12-07T17:26:08.670394Z","iopub.execute_input":"2022-12-07T17:26:08.670762Z","iopub.status.idle":"2022-12-07T17:26:08.775980Z","shell.execute_reply.started":"2022-12-07T17:26:08.670730Z","shell.execute_reply":"2022-12-07T17:26:08.774366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predtest[0]","metadata":{"execution":{"iopub.status.busy":"2022-12-07T17:26:08.777613Z","iopub.execute_input":"2022-12-07T17:26:08.778457Z","iopub.status.idle":"2022-12-07T17:26:08.788645Z","shell.execute_reply.started":"2022-12-07T17:26:08.778412Z","shell.execute_reply":"2022-12-07T17:26:08.787248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'ImageId': np.arange(1, len(y_predtest)+1), 'Label': y_predtest})\nsubmission.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-07T17:26:08.791117Z","iopub.execute_input":"2022-12-07T17:26:08.792197Z","iopub.status.idle":"2022-12-07T17:26:08.890922Z","shell.execute_reply.started":"2022-12-07T17:26:08.792135Z","shell.execute_reply":"2022-12-07T17:26:08.890012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-12-07T17:26:08.892307Z","iopub.execute_input":"2022-12-07T17:26:08.892894Z","iopub.status.idle":"2022-12-07T17:26:08.907845Z","shell.execute_reply.started":"2022-12-07T17:26:08.892862Z","shell.execute_reply":"2022-12-07T17:26:08.906564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(5, 5, figsize=(10, 10))\nfor i in range(5):\n    for j in range(5):\n        ax[i, j].imshow(test_images[i*5+j], cmap='gray')\n        ax[i, j].set_title(y_predtest[i*5+j])\n        ax[i, j].axis('off')","metadata":{"execution":{"iopub.status.busy":"2022-12-07T17:26:08.909289Z","iopub.execute_input":"2022-12-07T17:26:08.909930Z","iopub.status.idle":"2022-12-07T17:26:10.043960Z","shell.execute_reply.started":"2022-12-07T17:26:08.909899Z","shell.execute_reply":"2022-12-07T17:26:10.043034Z"},"trusted":true},"execution_count":null,"outputs":[]}]}