{"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":"markdown","source":"## IMPORT DATA","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:43:13.096525Z","start_time":"2022-08-30T21:43:12.296178Z"},"execution":{"iopub.status.busy":"2022-08-30T22:19:44.111000Z","iopub.execute_input":"2022-08-30T22:19:44.111653Z","iopub.status.idle":"2022-08-30T22:19:44.119182Z","shell.execute_reply.started":"2022-08-30T22:19:44.111601Z","shell.execute_reply":"2022-08-30T22:19:44.117801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_main = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ndf_competetion = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:43:15.194612Z","start_time":"2022-08-30T21:43:13.097727Z"},"execution":{"iopub.status.busy":"2022-08-30T22:19:44.122123Z","iopub.execute_input":"2022-08-30T22:19:44.122929Z","iopub.status.idle":"2022-08-30T22:19:48.699375Z","shell.execute_reply.started":"2022-08-30T22:19:44.122705Z","shell.execute_reply":"2022-08-30T22:19:48.698243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = df_main.iloc[:,0].to_frame()","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:43:15.199059Z","start_time":"2022-08-30T21:43:15.196534Z"},"execution":{"iopub.status.busy":"2022-08-30T22:19:48.700693Z","iopub.execute_input":"2022-08-30T22:19:48.700995Z","iopub.status.idle":"2022-08-30T22:19:48.708597Z","shell.execute_reply.started":"2022-08-30T22:19:48.700968Z","shell.execute_reply":"2022-08-30T22:19:48.707739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['pixels'] = list(df_main.iloc[:,1:].values)","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:43:15.209613Z","start_time":"2022-08-30T21:43:15.200900Z"},"execution":{"iopub.status.busy":"2022-08-30T22:19:48.710832Z","iopub.execute_input":"2022-08-30T22:19:48.711140Z","iopub.status.idle":"2022-08-30T22:19:48.764636Z","shell.execute_reply.started":"2022-08-30T22:19:48.711112Z","shell.execute_reply":"2022-08-30T22:19:48.763507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:43:15.646638Z","start_time":"2022-08-30T21:43:15.210505Z"},"execution":{"iopub.status.busy":"2022-08-30T22:19:48.767806Z","iopub.execute_input":"2022-08-30T22:19:48.768559Z","iopub.status.idle":"2022-08-30T22:19:48.773335Z","shell.execute_reply.started":"2022-08-30T22:19:48.768515Z","shell.execute_reply":"2022-08-30T22:19:48.772206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:43:15.656348Z","start_time":"2022-08-30T21:43:15.647499Z"},"execution":{"iopub.status.busy":"2022-08-30T22:19:48.774796Z","iopub.execute_input":"2022-08-30T22:19:48.775816Z","iopub.status.idle":"2022-08-30T22:19:48.799741Z","shell.execute_reply.started":"2022-08-30T22:19:48.775769Z","shell.execute_reply":"2022-08-30T22:19:48.798715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Image Visualization","metadata":{}},{"cell_type":"code","source":"row = 300\nplt.title(f\"Label == {train.loc[row,'label']}\")\nplt.imshow(train.loc[row,'pixels'].reshape(28,28,1))","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:43:15.719532Z","start_time":"2022-08-30T21:43:15.657367Z"},"execution":{"iopub.status.busy":"2022-08-30T22:19:48.801499Z","iopub.execute_input":"2022-08-30T22:19:48.801939Z","iopub.status.idle":"2022-08-30T22:19:49.049153Z","shell.execute_reply.started":"2022-08-30T22:19:48.801897Z","shell.execute_reply":"2022-08-30T22:19:49.048070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Preprocessing","metadata":{}},{"cell_type":"code","source":"# Normalize the Data\nfrom sklearn.preprocessing import StandardScaler\nstd_scaler = StandardScaler()","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:43:16.006872Z","start_time":"2022-08-30T21:43:15.720416Z"},"execution":{"iopub.status.busy":"2022-08-30T22:19:49.051033Z","iopub.execute_input":"2022-08-30T22:19:49.051456Z","iopub.status.idle":"2022-08-30T22:19:49.546401Z","shell.execute_reply.started":"2022-08-30T22:19:49.051413Z","shell.execute_reply":"2022-08-30T22:19:49.545406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_main_scaled = pd.DataFrame(std_scaler.fit_transform(df_main.iloc[:,1:].values), columns = [f\"pixel_{i}\" for i in range(df_main.shape[1] - 1)])\ndf_main_scaled['Label'] = df_main.iloc[:,0]","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:43:16.523927Z","start_time":"2022-08-30T21:43:16.007843Z"},"execution":{"iopub.status.busy":"2022-08-30T22:19:49.547812Z","iopub.execute_input":"2022-08-30T22:19:49.548156Z","iopub.status.idle":"2022-08-30T22:19:50.154765Z","shell.execute_reply.started":"2022-08-30T22:19:49.548124Z","shell.execute_reply":"2022-08-30T22:19:50.153715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca = PCA(n_components = 0.90)\npca.fit(df_main_scaled.drop(columns = ['Label']))\ndf_main_pca = pca.transform(df_main_scaled.drop(columns = ['Label']))","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:43:20.087900Z","start_time":"2022-08-30T21:43:16.526274Z"},"execution":{"iopub.status.busy":"2022-08-30T22:19:50.159654Z","iopub.execute_input":"2022-08-30T22:19:50.160032Z","iopub.status.idle":"2022-08-30T22:19:56.344598Z","shell.execute_reply.started":"2022-08-30T22:19:50.160002Z","shell.execute_reply":"2022-08-30T22:19:56.343017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_main_pca = pd.DataFrame(df_main_pca, columns = [f'PCA_COMP_{i}' for i in range(df_main_pca.shape[1])])\ndf_main_pca['Label'] = df_main_scaled['Label']","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:43:20.094048Z","start_time":"2022-08-30T21:43:20.090286Z"},"execution":{"iopub.status.busy":"2022-08-30T22:19:56.346500Z","iopub.execute_input":"2022-08-30T22:19:56.347186Z","iopub.status.idle":"2022-08-30T22:19:56.356262Z","shell.execute_reply.started":"2022-08-30T22:19:56.347142Z","shell.execute_reply":"2022-08-30T22:19:56.354349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_to_plot = df_main_pca.Label.value_counts(dropna=False).sort_index().rename('Label_Count').reset_index()\nplt.bar(df_to_plot.index, df_to_plot.Label_Count)\nplt.show()","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:43:20.147966Z","start_time":"2022-08-30T21:43:20.095194Z"},"execution":{"iopub.status.busy":"2022-08-30T22:19:56.358523Z","iopub.execute_input":"2022-08-30T22:19:56.359477Z","iopub.status.idle":"2022-08-30T22:19:56.534475Z","shell.execute_reply.started":"2022-08-30T22:19:56.359357Z","shell.execute_reply":"2022-08-30T22:19:56.533156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:43:20.150822Z","start_time":"2022-08-30T21:43:20.149008Z"},"execution":{"iopub.status.busy":"2022-08-30T22:19:56.536547Z","iopub.execute_input":"2022-08-30T22:19:56.537478Z","iopub.status.idle":"2022-08-30T22:19:56.543109Z","shell.execute_reply.started":"2022-08-30T22:19:56.537425Z","shell.execute_reply":"2022-08-30T22:19:56.542319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df_main_pca.loc[:,df_main_pca.columns != 'Label']\ny = df_main_pca.loc[:,'Label']","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:43:20.172006Z","start_time":"2022-08-30T21:43:20.151536Z"},"execution":{"iopub.status.busy":"2022-08-30T22:19:56.544184Z","iopub.execute_input":"2022-08-30T22:19:56.544739Z","iopub.status.idle":"2022-08-30T22:19:56.600723Z","shell.execute_reply.started":"2022-08-30T22:19:56.544709Z","shell.execute_reply":"2022-08-30T22:19:56.599848Z"},"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, random_state = 30, shuffle = True)","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:43:20.201168Z","start_time":"2022-08-30T21:43:20.173070Z"},"execution":{"iopub.status.busy":"2022-08-30T22:19:56.601907Z","iopub.execute_input":"2022-08-30T22:19:56.602374Z","iopub.status.idle":"2022-08-30T22:19:56.637387Z","shell.execute_reply.started":"2022-08-30T22:19:56.602344Z","shell.execute_reply":"2022-08-30T22:19:56.636424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model Fitting","metadata":{}},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:43:20.217359Z","start_time":"2022-08-30T21:43:20.202610Z"},"execution":{"iopub.status.busy":"2022-08-30T22:19:56.638544Z","iopub.execute_input":"2022-08-30T22:19:56.639202Z","iopub.status.idle":"2022-08-30T22:19:56.668838Z","shell.execute_reply.started":"2022-08-30T22:19:56.639167Z","shell.execute_reply":"2022-08-30T22:19:56.667981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn_classifier = KNeighborsClassifier(n_neighbors=5, metric='euclidean',)","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:43:20.220041Z","start_time":"2022-08-30T21:43:20.218390Z"},"execution":{"iopub.status.busy":"2022-08-30T22:19:56.670527Z","iopub.execute_input":"2022-08-30T22:19:56.671589Z","iopub.status.idle":"2022-08-30T22:19:56.676609Z","shell.execute_reply.started":"2022-08-30T22:19:56.671553Z","shell.execute_reply":"2022-08-30T22:19:56.675641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn_classifier.fit(X_train,y_train)","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:43:20.228681Z","start_time":"2022-08-30T21:43:20.220928Z"},"execution":{"iopub.status.busy":"2022-08-30T22:19:56.677834Z","iopub.execute_input":"2022-08-30T22:19:56.678880Z","iopub.status.idle":"2022-08-30T22:19:56.706191Z","shell.execute_reply.started":"2022-08-30T22:19:56.678836Z","shell.execute_reply":"2022-08-30T22:19:56.704948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_test = knn_classifier.predict(X_test)","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:43:25.478057Z","start_time":"2022-08-30T21:43:20.229670Z"},"execution":{"iopub.status.busy":"2022-08-30T22:19:56.707632Z","iopub.execute_input":"2022-08-30T22:19:56.708060Z","iopub.status.idle":"2022-08-30T22:20:03.870589Z","shell.execute_reply.started":"2022-08-30T22:19:56.708027Z","shell.execute_reply":"2022-08-30T22:20:03.869488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_train = knn_classifier.predict(X_train)","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:43:46.130864Z","start_time":"2022-08-30T21:43:25.480256Z"},"execution":{"iopub.status.busy":"2022-08-30T22:20:03.872035Z","iopub.execute_input":"2022-08-30T22:20:03.872787Z","iopub.status.idle":"2022-08-30T22:20:33.740091Z","shell.execute_reply.started":"2022-08-30T22:20:03.872740Z","shell.execute_reply":"2022-08-30T22:20:33.738620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nfrom sklearn.metrics import f1_score","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:43:46.135540Z","start_time":"2022-08-30T21:43:46.132985Z"},"execution":{"iopub.status.busy":"2022-08-30T22:20:33.742159Z","iopub.execute_input":"2022-08-30T22:20:33.743108Z","iopub.status.idle":"2022-08-30T22:20:33.749007Z","shell.execute_reply.started":"2022-08-30T22:20:33.743070Z","shell.execute_reply":"2022-08-30T22:20:33.747969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"'for train :: f1_score = {f1_score(y_train, y_pred_train, average = 'weighted')}\")\nprint(f\"'for train :: f1_score = {accuracy_score(y_train, y_pred_train)}\")","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:43:46.150877Z","start_time":"2022-08-30T21:43:46.136494Z"},"execution":{"iopub.status.busy":"2022-08-30T22:20:33.750457Z","iopub.execute_input":"2022-08-30T22:20:33.750887Z","iopub.status.idle":"2022-08-30T22:20:33.787335Z","shell.execute_reply.started":"2022-08-30T22:20:33.750847Z","shell.execute_reply":"2022-08-30T22:20:33.786238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"'for test :: f1_score = {f1_score(y_test, y_pred_test, average = 'weighted')}\")\nprint(f\"'for test :: f1_score = {accuracy_score(y_test, y_pred_test)}\")","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:43:46.164014Z","start_time":"2022-08-30T21:43:46.151843Z"},"execution":{"iopub.status.busy":"2022-08-30T22:20:33.788607Z","iopub.execute_input":"2022-08-30T22:20:33.788919Z","iopub.status.idle":"2022-08-30T22:20:33.802145Z","shell.execute_reply.started":"2022-08-30T22:20:33.788889Z","shell.execute_reply":"2022-08-30T22:20:33.800995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## TSNE Plots for Checking the Spread and Separation ","metadata":{}},{"cell_type":"code","source":"from sklearn.manifold import TSNE\nimport plotly.express as px\n\ntsne = TSNE(n_components=2, random_state=0)\nprojections = tsne.fit_transform(df_main_pca.loc[:,df_main_pca.columns != 'Label'])\n\nfig = px.scatter(\n    projections, x=0, y=1,\n    color = df_main_pca['Label'], labels={'color': 'Label'}\n)\nfig.show()","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:45:45.228570Z","start_time":"2022-08-30T21:43:46.165256Z"},"execution":{"iopub.status.busy":"2022-08-30T22:20:33.803559Z","iopub.execute_input":"2022-08-30T22:20:33.804055Z","iopub.status.idle":"2022-08-30T22:25:40.195419Z","shell.execute_reply.started":"2022-08-30T22:20:33.804015Z","shell.execute_reply":"2022-08-30T22:25:40.194497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# tsne_3d = TSNE(n_components=3, random_state=0)\n# projections = tsne_3d.fit_transform(df_main_pca.loc[:,df_main_pca.columns != 'Label'])\n\n# fig = px.scatter_3d(\n#     projections, x=0, y=1, z=2,\n#     color = df_main_pca['Label'], labels={'color': 'Label'}\n# )\n# fig.update_traces(marker_size=8)\n# fig.show()","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:45:45.231832Z","start_time":"2022-08-30T21:45:45.230176Z"},"execution":{"iopub.status.busy":"2022-08-30T22:25:40.196742Z","iopub.execute_input":"2022-08-30T22:25:40.197193Z","iopub.status.idle":"2022-08-30T22:25:40.201058Z","shell.execute_reply.started":"2022-08-30T22:25:40.197162Z","shell.execute_reply":"2022-08-30T22:25:40.200136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Prediction and Submission","metadata":{}},{"cell_type":"code","source":"df_competetion.head()","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:47:26.366127Z","start_time":"2022-08-30T21:47:26.323070Z"},"execution":{"iopub.status.busy":"2022-08-30T22:25:40.202419Z","iopub.execute_input":"2022-08-30T22:25:40.203593Z","iopub.status.idle":"2022-08-30T22:25:40.234400Z","shell.execute_reply.started":"2022-08-30T22:25:40.203550Z","shell.execute_reply":"2022-08-30T22:25:40.233232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_competetion_scaled = pd.DataFrame(std_scaler.transform(df_competetion.iloc[:,0:].values), columns = [f\"pixel_{i}\" for i in range(df_competetion.shape[1])])","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:48:05.571933Z","start_time":"2022-08-30T21:48:05.177746Z"},"execution":{"iopub.status.busy":"2022-08-30T22:25:40.236477Z","iopub.execute_input":"2022-08-30T22:25:40.236951Z","iopub.status.idle":"2022-08-30T22:25:40.359907Z","shell.execute_reply.started":"2022-08-30T22:25:40.236906Z","shell.execute_reply":"2022-08-30T22:25:40.358867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_competetion_pca = pca.transform(df_competetion_scaled)","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:48:07.310859Z","start_time":"2022-08-30T21:48:07.103880Z"},"execution":{"iopub.status.busy":"2022-08-30T22:25:40.364381Z","iopub.execute_input":"2022-08-30T22:25:40.364737Z","iopub.status.idle":"2022-08-30T22:25:40.660873Z","shell.execute_reply.started":"2022-08-30T22:25:40.364706Z","shell.execute_reply":"2022-08-30T22:25:40.659237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_competetion_pca = pd.DataFrame(df_competetion_pca, columns = [f'PCA_COMP_{i}' for i in range(df_competetion_pca.shape[1])])\ndf_competetion_pca['Label'] = knn_classifier.predict(df_competetion_pca)","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:48:26.931677Z","start_time":"2022-08-30T21:48:09.126673Z"},"execution":{"iopub.status.busy":"2022-08-30T22:25:40.663390Z","iopub.execute_input":"2022-08-30T22:25:40.664514Z","iopub.status.idle":"2022-08-30T22:26:03.875538Z","shell.execute_reply.started":"2022-08-30T22:25:40.664450Z","shell.execute_reply":"2022-08-30T22:26:03.874588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission = df_competetion_pca['Label'].rename_axis(index='ImageId').reset_index()","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:59:28.737722Z","start_time":"2022-08-30T21:59:28.730421Z"},"execution":{"iopub.status.busy":"2022-08-30T22:26:03.877251Z","iopub.execute_input":"2022-08-30T22:26:03.877995Z","iopub.status.idle":"2022-08-30T22:26:03.885627Z","shell.execute_reply.started":"2022-08-30T22:26:03.877946Z","shell.execute_reply":"2022-08-30T22:26:03.884386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission['ImageId'] = df_submission['ImageId'] + 1","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission.to_csv('./submission.csv', index = False)","metadata":{"ExecuteTime":{"end_time":"2022-08-30T21:59:30.028506Z","start_time":"2022-08-30T21:59:30.014686Z"},"execution":{"iopub.status.busy":"2022-08-30T22:26:03.887433Z","iopub.execute_input":"2022-08-30T22:26:03.887833Z","iopub.status.idle":"2022-08-30T22:26:03.925944Z","shell.execute_reply.started":"2022-08-30T22:26:03.887792Z","shell.execute_reply":"2022-08-30T22:26:03.924894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}