{"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":"2022-08-09T15:34:32.386334Z","iopub.execute_input":"2022-08-09T15:34:32.386851Z","iopub.status.idle":"2022-08-09T15:34:32.412758Z","shell.execute_reply.started":"2022-08-09T15:34:32.386722Z","shell.execute_reply":"2022-08-09T15:34:32.411902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:35:20.735016Z","iopub.execute_input":"2022-08-09T15:35:20.735414Z","iopub.status.idle":"2022-08-09T15:35:24.040871Z","shell.execute_reply.started":"2022-08-09T15:35:20.735384Z","shell.execute_reply":"2022-08-09T15:35:24.039819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:35:31.704086Z","iopub.execute_input":"2022-08-09T15:35:31.704568Z","iopub.status.idle":"2022-08-09T15:35:31.737935Z","shell.execute_reply.started":"2022-08-09T15:35:31.704529Z","shell.execute_reply":"2022-08-09T15:35:31.736832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape\n# 28x28 =784 + 1 label column","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:36:13.829275Z","iopub.execute_input":"2022-08-09T15:36:13.829828Z","iopub.status.idle":"2022-08-09T15:36:13.838414Z","shell.execute_reply.started":"2022-08-09T15:36:13.829760Z","shell.execute_reply":"2022-08-09T15:36:13.837355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sample()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:36:51.415898Z","iopub.execute_input":"2022-08-09T15:36:51.416324Z","iopub.status.idle":"2022-08-09T15:36:51.439498Z","shell.execute_reply.started":"2022-08-09T15:36:51.416290Z","shell.execute_reply":"2022-08-09T15:36:51.438377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:38:32.591384Z","iopub.execute_input":"2022-08-09T15:38:32.591854Z","iopub.status.idle":"2022-08-09T15:38:32.596785Z","shell.execute_reply.started":"2022-08-09T15:38:32.591768Z","shell.execute_reply":"2022-08-09T15:38:32.595670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(df.iloc[29298,1:].values.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:38:58.650843Z","iopub.execute_input":"2022-08-09T15:38:58.651262Z","iopub.status.idle":"2022-08-09T15:38:59.172308Z","shell.execute_reply.started":"2022-08-09T15:38:58.651230Z","shell.execute_reply":"2022-08-09T15:38:59.171205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#lets get our features and labels\nX= df.iloc[:,1:]\ny=df.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:40:36.645427Z","iopub.execute_input":"2022-08-09T15:40:36.645832Z","iopub.status.idle":"2022-08-09T15:40:36.654121Z","shell.execute_reply.started":"2022-08-09T15:40:36.645785Z","shell.execute_reply":"2022-08-09T15:40:36.652865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:42:27.624937Z","iopub.execute_input":"2022-08-09T15:42:27.625355Z","iopub.status.idle":"2022-08-09T15:42:27.906148Z","shell.execute_reply.started":"2022-08-09T15:42:27.625322Z","shell.execute_reply":"2022-08-09T15:42:27.905065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:42:50.559708Z","iopub.execute_input":"2022-08-09T15:42:50.560143Z","iopub.status.idle":"2022-08-09T15:42:50.567587Z","shell.execute_reply.started":"2022-08-09T15:42:50.560110Z","shell.execute_reply":"2022-08-09T15:42:50.566461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:44:01.574088Z","iopub.execute_input":"2022-08-09T15:44:01.574500Z","iopub.status.idle":"2022-08-09T15:44:01.698125Z","shell.execute_reply.started":"2022-08-09T15:44:01.574466Z","shell.execute_reply":"2022-08-09T15:44:01.696948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:44:14.585767Z","iopub.execute_input":"2022-08-09T15:44:14.586781Z","iopub.status.idle":"2022-08-09T15:44:14.591626Z","shell.execute_reply.started":"2022-08-09T15:44:14.586735Z","shell.execute_reply":"2022-08-09T15:44:14.590446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:44:33.425783Z","iopub.execute_input":"2022-08-09T15:44:33.426852Z","iopub.status.idle":"2022-08-09T15:44:33.449087Z","shell.execute_reply.started":"2022-08-09T15:44:33.426807Z","shell.execute_reply":"2022-08-09T15:44:33.447770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nstart = time.time()\ny_pred = knn.predict(X_test)\nprint (time.time()-start)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:46:25.934344Z","iopub.execute_input":"2022-08-09T15:46:25.934771Z","iopub.status.idle":"2022-08-09T15:46:38.257189Z","shell.execute_reply.started":"2022-08-09T15:46:25.934734Z","shell.execute_reply":"2022-08-09T15:46:38.256352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(y_test,y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:47:22.245312Z","iopub.execute_input":"2022-08-09T15:47:22.245784Z","iopub.status.idle":"2022-08-09T15:47:22.256377Z","shell.execute_reply.started":"2022-08-09T15:47:22.245741Z","shell.execute_reply":"2022-08-09T15:47:22.255521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#first step before PCA is standardizing data\nfrom sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:49:51.336332Z","iopub.execute_input":"2022-08-09T15:49:51.336785Z","iopub.status.idle":"2022-08-09T15:49:51.341408Z","shell.execute_reply.started":"2022-08-09T15:49:51.336748Z","shell.execute_reply":"2022-08-09T15:49:51.340618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_sca = scaler.fit_transform(X_train)\nX_test_sca = scaler.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:53:19.889338Z","iopub.execute_input":"2022-08-09T15:53:19.889817Z","iopub.status.idle":"2022-08-09T15:53:20.392316Z","shell.execute_reply.started":"2022-08-09T15:53:19.889758Z","shell.execute_reply":"2022-08-09T15:53:20.391402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PCA\nfrom sklearn.decomposition import PCA\npca = PCA(n_components=100)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:53:23.670782Z","iopub.execute_input":"2022-08-09T15:53:23.671571Z","iopub.status.idle":"2022-08-09T15:53:23.675937Z","shell.execute_reply.started":"2022-08-09T15:53:23.671534Z","shell.execute_reply":"2022-08-09T15:53:23.675034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_pca = pca.fit_transform(X_train)\nX_test_pca = pca.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:54:00.391506Z","iopub.execute_input":"2022-08-09T15:54:00.392474Z","iopub.status.idle":"2022-08-09T15:54:03.737988Z","shell.execute_reply.started":"2022-08-09T15:54:00.392429Z","shell.execute_reply":"2022-08-09T15:54:03.736784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn_pca = KNeighborsClassifier()\nknn_pca.fit(X_train_pca,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:56:09.165911Z","iopub.execute_input":"2022-08-09T15:56:09.166892Z","iopub.status.idle":"2022-08-09T15:56:09.183667Z","shell.execute_reply.started":"2022-08-09T15:56:09.166839Z","shell.execute_reply":"2022-08-09T15:56:09.182771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nstart = time.time()\ny_pred_pca = knn_pca.predict(X_test_pca)\nprint (time.time()-start)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:57:13.504278Z","iopub.execute_input":"2022-08-09T15:57:13.504687Z","iopub.status.idle":"2022-08-09T15:57:20.370473Z","shell.execute_reply.started":"2022-08-09T15:57:13.504651Z","shell.execute_reply":"2022-08-09T15:57:20.369201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test,y_pred_pca)","metadata":{"execution":{"iopub.status.busy":"2022-08-09T15:58:05.928577Z","iopub.execute_input":"2022-08-09T15:58:05.929014Z","iopub.status.idle":"2022-08-09T15:58:05.938531Z","shell.execute_reply.started":"2022-08-09T15:58:05.928973Z","shell.execute_reply":"2022-08-09T15:58:05.937139Z"},"trusted":true},"execution_count":null,"outputs":[]}]}