{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Import Libraries","metadata":{}},{"cell_type":"code","source":"#Import libraries\nimport numpy as np \nimport pandas as pd  \nimport matplotlib.pyplot as plt \nimport seaborn as sns\nfrom sklearn.decomposition import PCA\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.neural_network import MLPClassifier\nfrom sklearn.metrics import confusion_matrix,accuracy_score\nfrom sklearn.metrics import classification_report\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-11T19:56:30.705658Z","iopub.execute_input":"2024-08-11T19:56:30.706080Z","iopub.status.idle":"2024-08-11T19:56:30.712828Z","shell.execute_reply.started":"2024-08-11T19:56:30.706042Z","shell.execute_reply":"2024-08-11T19:56:30.711660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#load train data \ntrain_data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntrain_data.head(5)","metadata":{"execution":{"iopub.status.busy":"2024-08-11T19:51:54.349899Z","iopub.execute_input":"2024-08-11T19:51:54.350435Z","iopub.status.idle":"2024-08-11T19:51:57.117042Z","shell.execute_reply.started":"2024-08-11T19:51:54.350399Z","shell.execute_reply":"2024-08-11T19:51:57.115873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#load test data \ntest_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\nprint(train_data.shape)\nprint(test_data.shape)","metadata":{"execution":{"iopub.status.busy":"2024-08-11T19:51:57.118878Z","iopub.execute_input":"2024-08-11T19:51:57.119210Z","iopub.status.idle":"2024-08-11T19:51:58.637818Z","shell.execute_reply.started":"2024-08-11T19:51:57.119180Z","shell.execute_reply":"2024-08-11T19:51:58.636573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plot histgram to know count nubmber for each digit  \nplt.hist(train_data[\"label\"])\nplt.title(\"Count number of digit \")\nplt.xlabel(\"Digits\")\nplt.ylabel(\"Frequency\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-11T19:51:58.641016Z","iopub.execute_input":"2024-08-11T19:51:58.641515Z","iopub.status.idle":"2024-08-11T19:51:58.897288Z","shell.execute_reply.started":"2024-08-11T19:51:58.641457Z","shell.execute_reply":"2024-08-11T19:51:58.896129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#To show image and label and you can change the variable i \ni=3\nimg = train_data.iloc[i,1:].values\nimg = img.reshape(28,28)\nplt.imshow(img,\"gray\")\nplt.title(train_data.iloc[i,0])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-11T19:51:58.898690Z","iopub.execute_input":"2024-08-11T19:51:58.899109Z","iopub.status.idle":"2024-08-11T19:51:59.158729Z","shell.execute_reply.started":"2024-08-11T19:51:58.899075Z","shell.execute_reply":"2024-08-11T19:51:59.157568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#splitting  data \ny = train_data[\"label\"]\nX = train_data.drop(\"label\",axis=1)\nprint(X.shape)\nprint(y.shape)","metadata":{"execution":{"iopub.status.busy":"2024-08-11T19:51:59.159966Z","iopub.execute_input":"2024-08-11T19:51:59.160301Z","iopub.status.idle":"2024-08-11T19:51:59.263750Z","shell.execute_reply.started":"2024-08-11T19:51:59.160272Z","shell.execute_reply":"2024-08-11T19:51:59.262411Z"},"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.2,shuffle=True,random_state=44)\nprint(\"shape of X_train is :\",X_train.shape)\nprint(\"shape of X_test is :\",X_test.shape)\nprint(\"shape of y_train is :\",y_train.shape)\nprint(\"shape of y_test is :\",y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-08-11T19:51:59.265539Z","iopub.execute_input":"2024-08-11T19:51:59.265957Z","iopub.status.idle":"2024-08-11T19:51:59.521748Z","shell.execute_reply.started":"2024-08-11T19:51:59.265920Z","shell.execute_reply":"2024-08-11T19:51:59.520276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Apply neural netword Model\nMLPClassifierModel = MLPClassifier(hidden_layer_sizes= (100,),activation = \"relu\",solver=\"adam\",alpha=0.001)\nMLPClassifierModel.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-08-11T19:51:59.523381Z","iopub.execute_input":"2024-08-11T19:51:59.523853Z","iopub.status.idle":"2024-08-11T19:52:42.124599Z","shell.execute_reply.started":"2024-08-11T19:51:59.523809Z","shell.execute_reply":"2024-08-11T19:52:42.123353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#calculate predicted value \ny_pred = MLPClassifierModel.predict(X_test)\nprint(\"the predicted value is :\",y_pred[:10])\nprint(\"MLPClassifier Score of train data is :\",MLPClassifierModel.score(X_train,y_train))\nprint(\"MLPClassifier score of test data is : \",MLPClassifierModel.score(X_test,y_test))\nprint(\"number of layer is :\",MLPClassifierModel.n_layers_)\nprint(\"number of iteration is \",MLPClassifierModel.n_iter_)\nprint(\"MLPClassifierModel last activation is :\",MLPClassifierModel.out_activation_)","metadata":{"execution":{"iopub.status.busy":"2024-08-11T19:52:42.126148Z","iopub.execute_input":"2024-08-11T19:52:42.127104Z","iopub.status.idle":"2024-08-11T19:52:42.666392Z","shell.execute_reply.started":"2024-08-11T19:52:42.127060Z","shell.execute_reply":"2024-08-11T19:52:42.664938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"accuracy_score is :\",accuracy_score(y_test,y_pred))\nprint(\"MLPClassifierModel loss is :\",MLPClassifierModel.loss_)\nCM = confusion_matrix(y_test,y_pred)\nsns.heatmap(CM,annot=True,cmap=\"Blues\",fmt=\"d\")\nplt.title(\"confusion Matrix\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-11T19:52:42.671561Z","iopub.execute_input":"2024-08-11T19:52:42.672558Z","iopub.status.idle":"2024-08-11T19:52:43.249701Z","shell.execute_reply.started":"2024-08-11T19:52:42.672503Z","shell.execute_reply":"2024-08-11T19:52:43.248346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Apply normalize data \nX = X / 255","metadata":{"execution":{"iopub.status.busy":"2024-08-11T19:52:43.250993Z","iopub.execute_input":"2024-08-11T19:52:43.251364Z","iopub.status.idle":"2024-08-11T19:52:43.326002Z","shell.execute_reply.started":"2024-08-11T19:52:43.251330Z","shell.execute_reply":"2024-08-11T19:52:43.324884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Apply PCA ","metadata":{}},{"cell_type":"code","source":"#Apply PCA \npca = PCA(n_components=300)\npca.fit(X)\nplt.plot(pca.explained_variance_ratio_)","metadata":{"execution":{"iopub.status.busy":"2024-08-11T19:52:43.327352Z","iopub.execute_input":"2024-08-11T19:52:43.327709Z","iopub.status.idle":"2024-08-11T19:52:51.241275Z","shell.execute_reply.started":"2024-08-11T19:52:43.327677Z","shell.execute_reply":"2024-08-11T19:52:51.239930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Apply optimal value of PCA = 50(up to 100)\npca =PCA(n_components=70)\npca = pca.fit_transform(X)\nX = pd.DataFrame(pca)\n","metadata":{"execution":{"iopub.status.busy":"2024-08-11T19:52:51.242689Z","iopub.execute_input":"2024-08-11T19:52:51.243375Z","iopub.status.idle":"2024-08-11T19:52:56.289442Z","shell.execute_reply.started":"2024-08-11T19:52:51.243322Z","shell.execute_reply":"2024-08-11T19:52:56.288253Z"},"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.2,shuffle=True,random_state=44)\nprint(\"shape of X_train is :\",X_train.shape)\nprint(\"shape of X_test is :\",X_test.shape)\nprint(\"shape of y_train is :\",y_train.shape)\nprint(\"shape of y_test is :\",y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-08-11T19:52:56.290927Z","iopub.execute_input":"2024-08-11T19:52:56.291394Z","iopub.status.idle":"2024-08-11T19:52:56.325667Z","shell.execute_reply.started":"2024-08-11T19:52:56.291361Z","shell.execute_reply":"2024-08-11T19:52:56.324577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Apply neural netword Model\nMLPClassifierModel = MLPClassifier(hidden_layer_sizes= (100,),activation = \"relu\",solver=\"adam\",alpha=0.001)\nMLPClassifierModel.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-08-11T19:52:56.327027Z","iopub.execute_input":"2024-08-11T19:52:56.327385Z","iopub.status.idle":"2024-08-11T19:53:23.493919Z","shell.execute_reply.started":"2024-08-11T19:52:56.327354Z","shell.execute_reply":"2024-08-11T19:53:23.492755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#calculate predicted value \ny_pred = MLPClassifierModel.predict(X_test)\nprint(\"the predicted value is :\",y_pred[:10])\nprint(\"MLPClassifier Score of train data is :\",MLPClassifierModel.score(X_train,y_train))\nprint(\"MLPClassifier score of test data is : \",MLPClassifierModel.score(X_test,y_test))","metadata":{"execution":{"iopub.status.busy":"2024-08-11T19:53:23.495360Z","iopub.execute_input":"2024-08-11T19:53:23.496072Z","iopub.status.idle":"2024-08-11T19:53:23.601052Z","shell.execute_reply.started":"2024-08-11T19:53:23.496028Z","shell.execute_reply":"2024-08-11T19:53:23.599843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Classification report\nprint(classification_report(y_test,y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-08-11T19:56:36.022321Z","iopub.execute_input":"2024-08-11T19:56:36.022775Z","iopub.status.idle":"2024-08-11T19:56:36.058145Z","shell.execute_reply.started":"2024-08-11T19:56:36.022739Z","shell.execute_reply":"2024-08-11T19:56:36.056855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"accuracy_score is :\",accuracy_score(y_test,y_pred))\nCM = confusion_matrix(y_test,y_pred)\nsns.heatmap(CM,annot=True,cmap=\"Blues\",fmt=\"d\")\nplt.title(\"confusion Matrix\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-11T19:53:23.602786Z","iopub.execute_input":"2024-08-11T19:53:23.603507Z","iopub.status.idle":"2024-08-11T19:53:24.145767Z","shell.execute_reply.started":"2024-08-11T19:53:23.603448Z","shell.execute_reply":"2024-08-11T19:53:24.144586Z"},"trusted":true},"execution_count":null,"outputs":[]}]}