{"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":"## import the libraries\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n## read the data\ntrain_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_data  = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-07T18:33:01.547782Z","iopub.execute_input":"2023-11-07T18:33:01.548178Z","iopub.status.idle":"2023-11-07T18:33:06.535060Z","shell.execute_reply.started":"2023-11-07T18:33:01.548148Z","shell.execute_reply":"2023-11-07T18:33:06.533655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## show the count of different values\nplt.hist(train_data['label'])\nplt.title('Frequency of Numbers in Train data')\nplt.xlabel('Number')\nplt.ylabel('Frequency')","metadata":{"execution":{"iopub.status.busy":"2023-11-07T18:33:06.537228Z","iopub.execute_input":"2023-11-07T18:33:06.537581Z","iopub.status.idle":"2023-11-07T18:33:06.859997Z","shell.execute_reply.started":"2023-11-07T18:33:06.537550Z","shell.execute_reply":"2023-11-07T18:33:06.858977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## show 25 image\n## create 25 subplot\nfig, axes = plt.subplots(5, 5)\nplt.subplots_adjust(hspace=0.2, wspace=0.2)\n\n## remove the ticks of x , y\nfor ax in axes.flat:\n    ax.set_xticks([])\n    ax.set_yticks([])\n\n## show the images\nimg_index = 0\nfor i in range(5):\n    for j in range(5):\n        ## convert 1d to 2d\n        axes[i, j].imshow(np.array(train_data.iloc[ img_index , 1:]).reshape((28, 28)))\n        img_index += 1","metadata":{"execution":{"iopub.status.busy":"2023-11-07T18:33:06.861914Z","iopub.execute_input":"2023-11-07T18:33:06.862389Z","iopub.status.idle":"2023-11-07T18:33:08.137420Z","shell.execute_reply.started":"2023-11-07T18:33:06.862351Z","shell.execute_reply":"2023-11-07T18:33:08.136253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Data Normalization\nlabels     = train_data['label']\ntrain_data = train_data.drop('label', axis=1)\n\ntrain_data = train_data / 255.0\ntest_data  = test_data  / 255.0","metadata":{"execution":{"iopub.status.busy":"2023-11-07T18:33:08.140786Z","iopub.execute_input":"2023-11-07T18:33:08.141260Z","iopub.status.idle":"2023-11-07T18:33:08.337041Z","shell.execute_reply.started":"2023-11-07T18:33:08.141216Z","shell.execute_reply":"2023-11-07T18:33:08.336184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## use PCA \nfrom sklearn.decomposition import PCA\npca        = PCA(n_components=200).fit(train_data)\n## plot the variance ratio\nplt.plot(pca.explained_variance_ratio_)\nplt.ylabel('% of variance ratio')","metadata":{"execution":{"iopub.status.busy":"2023-11-07T18:33:08.338184Z","iopub.execute_input":"2023-11-07T18:33:08.338580Z","iopub.status.idle":"2023-11-07T18:33:15.078226Z","shell.execute_reply.started":"2023-11-07T18:33:08.338543Z","shell.execute_reply":"2023-11-07T18:33:15.076830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## so we can use n_components of 50\npca = PCA(n_components=50).fit(train_data)\ntrain_data = pd.DataFrame(pca.transform(train_data))\ntest_data  = pca.transform(test_data)","metadata":{"execution":{"iopub.status.busy":"2023-11-07T18:33:15.079898Z","iopub.execute_input":"2023-11-07T18:33:15.080758Z","iopub.status.idle":"2023-11-07T18:33:20.697734Z","shell.execute_reply.started":"2023-11-07T18:33:15.080713Z","shell.execute_reply":"2023-11-07T18:33:20.696005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## plot 3d for first 3 pca\nfrom mpl_toolkits.mplot3d import Axes3D\n\n## create the 3d figure\nfig = plt.figure()\nax  = fig.add_subplot(111, projection='3d')\n\n## get x, y, z from the first 3 pca\nx = train_data[0]\ny = train_data[1]\nz = train_data[2]\n\n## colors\ncolors = [int(i%9) for i in labels]\n\n## scatter the pca\nax.scatter(x, y, z, c=colors)","metadata":{"execution":{"iopub.status.busy":"2023-11-07T18:33:20.700305Z","iopub.execute_input":"2023-11-07T18:33:20.701449Z","iopub.status.idle":"2023-11-07T18:33:23.346344Z","shell.execute_reply.started":"2023-11-07T18:33:20.701391Z","shell.execute_reply":"2023-11-07T18:33:23.344944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## MLPClassifier\nfrom sklearn.neural_network import MLPClassifier\nfrom sklearn.model_selection import train_test_split\n\n## split the data\nx_train, x_test, y_train, y_test = train_test_split(train_data, labels, test_size=0.2, random_state=42)\n\n## apply MLPClassifier\nmlp_clf = MLPClassifier().fit(x_train, y_train)\nmlp_clf.score(x_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2023-11-07T18:33:23.348389Z","iopub.execute_input":"2023-11-07T18:33:23.349218Z","iopub.status.idle":"2023-11-07T18:34:09.126003Z","shell.execute_reply.started":"2023-11-07T18:33:23.349177Z","shell.execute_reply":"2023-11-07T18:34:09.124441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## get the confusion matrix\nfrom sklearn.metrics import confusion_matrix\ny_pred = mlp_clf.predict(x_test)\n\nCM = confusion_matrix(y_test, y_pred)\nCM","metadata":{"execution":{"iopub.status.busy":"2023-11-07T18:34:09.128052Z","iopub.execute_input":"2023-11-07T18:34:09.129643Z","iopub.status.idle":"2023-11-07T18:34:09.167474Z","shell.execute_reply.started":"2023-11-07T18:34:09.129581Z","shell.execute_reply":"2023-11-07T18:34:09.165884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Create the output\nsubmission = pd.DataFrame()\nsubmission['ImageId'] = range(1, len(test_data) + 1)\nsubmission['Label']   = mlp_clf.predict(test_data)\nsubmission.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-07T18:36:29.323984Z","iopub.execute_input":"2023-11-07T18:36:29.324428Z","iopub.status.idle":"2023-11-07T18:36:29.507331Z","shell.execute_reply.started":"2023-11-07T18:36:29.324395Z","shell.execute_reply":"2023-11-07T18:36:29.506290Z"},"trusted":true},"execution_count":null,"outputs":[]}]}