{"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":"2023-10-27T14:01:33.120725Z","iopub.execute_input":"2023-10-27T14:01:33.121131Z","iopub.status.idle":"2023-10-27T14:01:33.129860Z","shell.execute_reply.started":"2023-10-27T14:01:33.121099Z","shell.execute_reply":"2023-10-27T14:01:33.128497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt \nimport seaborn as sns  \nimport numpy as np\nfrom sklearn.linear_model import LogisticRegression \nfrom sklearn.model_selection import train_test_split\nfrom sklearn import metrics","metadata":{"execution":{"iopub.status.busy":"2023-10-27T14:01:36.296749Z","iopub.execute_input":"2023-10-27T14:01:36.297212Z","iopub.status.idle":"2023-10-27T14:01:36.304727Z","shell.execute_reply.started":"2023-10-27T14:01:36.297180Z","shell.execute_reply":"2023-10-27T14:01:36.303145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntrain=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\nprint(data.shape,\"\\n\\n\\n\",train.shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-27T14:01:39.848343Z","iopub.execute_input":"2023-10-27T14:01:39.848756Z","iopub.status.idle":"2023-10-27T14:01:45.428775Z","shell.execute_reply.started":"2023-10-27T14:01:39.848724Z","shell.execute_reply":"2023-10-27T14:01:45.427680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train[\"label\"]\n\n# Drop 'label' column\nX_train = train.drop(labels = [\"label\"],axis = 1) \n\n \n\ny_train.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-10-27T14:01:45.430631Z","iopub.execute_input":"2023-10-27T14:01:45.430953Z","iopub.status.idle":"2023-10-27T14:01:45.523724Z","shell.execute_reply.started":"2023-10-27T14:01:45.430925Z","shell.execute_reply":"2023-10-27T14:01:45.522556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train[train.columns[1:]].values/255\nY = train.label.values\nX.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-27T14:01:48.745090Z","iopub.execute_input":"2023-10-27T14:01:48.746048Z","iopub.status.idle":"2023-10-27T14:01:49.006600Z","shell.execute_reply.started":"2023-10-27T14:01:48.746015Z","shell.execute_reply":"2023-10-27T14:01:49.004970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y","metadata":{"execution":{"iopub.status.busy":"2023-10-27T14:01:50.990777Z","iopub.execute_input":"2023-10-27T14:01:50.991319Z","iopub.status.idle":"2023-10-27T14:01:51.000548Z","shell.execute_reply.started":"2023-10-27T14:01:50.991149Z","shell.execute_reply":"2023-10-27T14:01:50.999299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_oof = np.zeros((X.shape[0], 10))\ntest_preds = 0\ntrain_oof.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-27T14:01:52.924274Z","iopub.execute_input":"2023-10-27T14:01:52.924684Z","iopub.status.idle":"2023-10-27T14:01:52.932674Z","shell.execute_reply.started":"2023-10-27T14:01:52.924654Z","shell.execute_reply":"2023-10-27T14:01:52.931139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = data.values/255\ntest.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-27T14:01:56.020973Z","iopub.execute_input":"2023-10-27T14:01:56.021482Z","iopub.status.idle":"2023-10-27T14:01:56.114872Z","shell.execute_reply.started":"2023-10-27T14:01:56.021446Z","shell.execute_reply":"2023-10-27T14:01:56.113960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr1=LogisticRegression(solver='lbfgs')\nlr1.fit(X_train,y_train)\npredict= lr1.predict(X_train)\npredict","metadata":{"execution":{"iopub.status.busy":"2023-10-27T14:01:58.449199Z","iopub.execute_input":"2023-10-27T14:01:58.450041Z","iopub.status.idle":"2023-10-27T14:02:19.836872Z","shell.execute_reply.started":"2023-10-27T14:01:58.449998Z","shell.execute_reply":"2023-10-27T14:02:19.835376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm= metrics.confusion_matrix(y_train,lr1.predict(X_train))\ncm","metadata":{"execution":{"iopub.status.busy":"2023-10-27T14:02:20.157954Z","iopub.execute_input":"2023-10-27T14:02:20.158831Z","iopub.status.idle":"2023-10-27T14:02:20.459541Z","shell.execute_reply.started":"2023-10-27T14:02:20.158789Z","shell.execute_reply":"2023-10-27T14:02:20.458481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(9,9))\nplt.imshow(cm,cmap='rainbow_r')\nplt.title(\"Confusion Matrix for MNIST Data\")\nplt.xticks(np.arange(10))\nplt.yticks(np.arange(10))\nplt.ylabel('Actual Label')\nplt.xlabel('Predicted Label')\nplt.colorbar()\nwidth,height = cm.shape\nfor x in range(width):\n    for y in range(height):\n        plt.annotate(str(cm[x][y]),xy=(y,x),horizontalalignment='center',verticalalignment='center')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-27T14:02:20.462474Z","iopub.execute_input":"2023-10-27T14:02:20.463254Z","iopub.status.idle":"2023-10-27T14:02:21.469143Z","shell.execute_reply.started":"2023-10-27T14:02:20.463214Z","shell.execute_reply":"2023-10-27T14:02:21.468227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predict result\nresult = lr1.predict(test)\n\n\nresult = pd.Series(result,name=\"Label\")","metadata":{"execution":{"iopub.status.busy":"2023-10-27T14:02:21.470475Z","iopub.execute_input":"2023-10-27T14:02:21.471365Z","iopub.status.idle":"2023-10-27T14:02:21.581022Z","shell.execute_reply.started":"2023-10-27T14:02:21.471302Z","shell.execute_reply":"2023-10-27T14:02:21.579599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),result],axis = 1)\n\nsubmission.to_csv(\"cnn_mnist_datagen.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-27T14:02:26.757172Z","iopub.execute_input":"2023-10-27T14:02:26.757799Z","iopub.status.idle":"2023-10-27T14:02:26.834383Z","shell.execute_reply.started":"2023-10-27T14:02:26.757756Z","shell.execute_reply":"2023-10-27T14:02:26.833236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\ntrainaccuracy= accuracy_score(y_train,lr1.predict(X_train))\ntrainaccuracy\nprint(\"Train Data Accuracy    :{} %\".format(round((trainaccuracy*100),2)))","metadata":{"execution":{"iopub.status.busy":"2023-10-27T14:02:28.753017Z","iopub.execute_input":"2023-10-27T14:02:28.753520Z","iopub.status.idle":"2023-10-27T14:02:29.116683Z","shell.execute_reply.started":"2023-10-27T14:02:28.753483Z","shell.execute_reply":"2023-10-27T14:02:29.115065Z"},"trusted":true},"execution_count":null,"outputs":[]}]}