{"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":30664,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-03-14T02:00:12.632244Z","iopub.execute_input":"2024-03-14T02:00:12.632720Z","iopub.status.idle":"2024-03-14T02:00:12.638023Z","shell.execute_reply.started":"2024-03-14T02:00:12.632683Z","shell.execute_reply":"2024-03-14T02:00:12.636688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Laoding dataset","metadata":{}},{"cell_type":"code","source":"train_df=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntrain_df.info()","metadata":{"execution":{"iopub.status.busy":"2024-03-14T02:00:12.644004Z","iopub.execute_input":"2024-03-14T02:00:12.644390Z","iopub.status.idle":"2024-03-14T02:00:15.854769Z","shell.execute_reply.started":"2024-03-14T02:00:12.644359Z","shell.execute_reply":"2024-03-14T02:00:15.853540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntest_df.info()","metadata":{"execution":{"iopub.status.busy":"2024-03-14T02:00:15.856784Z","iopub.execute_input":"2024-03-14T02:00:15.857416Z","iopub.status.idle":"2024-03-14T02:00:17.904947Z","shell.execute_reply.started":"2024-03-14T02:00:15.857382Z","shell.execute_reply":"2024-03-14T02:00:17.903766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's draw the first digit of train data using matplotlib ","metadata":{}},{"cell_type":"code","source":"a=train_df.iloc[[3]].drop('label',axis=1).to_numpy().reshape(28,28)\na","metadata":{"execution":{"iopub.status.busy":"2024-03-14T02:00:17.906332Z","iopub.execute_input":"2024-03-14T02:00:17.906678Z","iopub.status.idle":"2024-03-14T02:00:17.917571Z","shell.execute_reply.started":"2024-03-14T02:00:17.906649Z","shell.execute_reply":"2024-03-14T02:00:17.916719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.pyplot.matshow(a)","metadata":{"execution":{"iopub.status.busy":"2024-03-14T02:00:17.920361Z","iopub.execute_input":"2024-03-14T02:00:17.920685Z","iopub.status.idle":"2024-03-14T02:00:18.355188Z","shell.execute_reply.started":"2024-03-14T02:00:17.920658Z","shell.execute_reply":"2024-03-14T02:00:18.353565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Separating feature and target of train data","metadata":{}},{"cell_type":"code","source":"features=train_df.drop('label',axis=1)\nfeatures","metadata":{"execution":{"iopub.status.busy":"2024-03-14T02:00:18.357718Z","iopub.execute_input":"2024-03-14T02:00:18.358776Z","iopub.status.idle":"2024-03-14T02:00:18.502825Z","shell.execute_reply.started":"2024-03-14T02:00:18.358712Z","shell.execute_reply":"2024-03-14T02:00:18.501719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target=train_df[['label']]\ntarget","metadata":{"execution":{"iopub.status.busy":"2024-03-14T02:00:18.504599Z","iopub.execute_input":"2024-03-14T02:00:18.505324Z","iopub.status.idle":"2024-03-14T02:00:18.517095Z","shell.execute_reply.started":"2024-03-14T02:00:18.505289Z","shell.execute_reply":"2024-03-14T02:00:18.515847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Scaling feature of train data and test data","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import MinMaxScaler\nmms=MinMaxScaler()\nfeatures_array=mms.fit_transform(features)\nfeatures_array[0]","metadata":{"execution":{"iopub.status.busy":"2024-03-14T02:00:18.518683Z","iopub.execute_input":"2024-03-14T02:00:18.519127Z","iopub.status.idle":"2024-03-14T02:00:18.946007Z","shell.execute_reply.started":"2024-03-14T02:00:18.519087Z","shell.execute_reply":"2024-03-14T02:00:18.944842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_array=mms.fit_transform(test_df)","metadata":{"execution":{"iopub.status.busy":"2024-03-14T02:00:18.947476Z","iopub.execute_input":"2024-03-14T02:00:18.948559Z","iopub.status.idle":"2024-03-14T02:00:19.250362Z","shell.execute_reply.started":"2024-03-14T02:00:18.948522Z","shell.execute_reply":"2024-03-14T02:00:19.249353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Spliting train dataset to train & test","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train,x_test,y_train,y_test=train_test_split(features_array,\n                                               target,test_size=0.15,\n                                               random_state=1)","metadata":{"execution":{"iopub.status.busy":"2024-03-14T02:00:19.251850Z","iopub.execute_input":"2024-03-14T02:00:19.252173Z","iopub.status.idle":"2024-03-14T02:00:19.720245Z","shell.execute_reply.started":"2024-03-14T02:00:19.252147Z","shell.execute_reply":"2024-03-14T02:00:19.719028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Importing KNN and making a model","metadata":{}},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nknn=KNeighborsClassifier(n_neighbors=5)\nknn.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-03-14T02:00:19.723560Z","iopub.execute_input":"2024-03-14T02:00:19.723921Z","iopub.status.idle":"2024-03-14T02:00:19.763115Z","shell.execute_reply.started":"2024-03-14T02:00:19.723891Z","shell.execute_reply":"2024-03-14T02:00:19.761183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.score(x_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2024-03-14T02:00:19.764835Z","iopub.execute_input":"2024-03-14T02:00:19.766067Z","iopub.status.idle":"2024-03-14T02:00:26.425171Z","shell.execute_reply.started":"2024-03-14T02:00:19.766023Z","shell.execute_reply":"2024-03-14T02:00:26.424336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Predicting for test data","metadata":{}},{"cell_type":"code","source":"prediction=knn.predict(test_array)","metadata":{"execution":{"iopub.status.busy":"2024-03-14T02:00:26.426404Z","iopub.execute_input":"2024-03-14T02:00:26.427050Z","iopub.status.idle":"2024-03-14T02:00:55.218714Z","shell.execute_reply.started":"2024-03-14T02:00:26.427020Z","shell.execute_reply":"2024-03-14T02:00:55.217313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Making output","metadata":{}},{"cell_type":"code","source":"output=pd.DataFrame(np.c_[np.arange(1,28001),prediction],\n                    columns=['ImageId','Label'])\noutput","metadata":{"execution":{"iopub.status.busy":"2024-03-14T02:00:55.220730Z","iopub.execute_input":"2024-03-14T02:00:55.221473Z","iopub.status.idle":"2024-03-14T02:00:55.233642Z","shell.execute_reply.started":"2024-03-14T02:00:55.221436Z","shell.execute_reply":"2024-03-14T02:00:55.232643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output.to_csv('Submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2024-03-14T02:00:55.235306Z","iopub.execute_input":"2024-03-14T02:00:55.235671Z","iopub.status.idle":"2024-03-14T02:00:55.280098Z","shell.execute_reply.started":"2024-03-14T02:00:55.235641Z","shell.execute_reply":"2024-03-14T02:00:55.278981Z"},"trusted":true},"execution_count":null,"outputs":[]}]}