{"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":"","metadata":{}},{"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-09-07T16:00:16.356453Z","iopub.execute_input":"2022-09-07T16:00:16.357265Z","iopub.status.idle":"2022-09-07T16:00:16.392685Z","shell.execute_reply.started":"2022-09-07T16:00:16.357167Z","shell.execute_reply":"2022-09-07T16:00:16.391068Z"},"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-09-07T16:00:28.271108Z","iopub.execute_input":"2022-09-07T16:00:28.271493Z","iopub.status.idle":"2022-09-07T16:00:31.703170Z","shell.execute_reply.started":"2022-09-07T16:00:28.271460Z","shell.execute_reply":"2022-09-07T16:00:31.701982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:00:35.455992Z","iopub.execute_input":"2022-09-07T16:00:35.456485Z","iopub.status.idle":"2022-09-07T16:00:35.468588Z","shell.execute_reply.started":"2022-09-07T16:00:35.456439Z","shell.execute_reply":"2022-09-07T16:00:35.467546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sample()","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:00:39.135104Z","iopub.execute_input":"2022-09-07T16:00:39.135642Z","iopub.status.idle":"2022-09-07T16:00:39.168463Z","shell.execute_reply.started":"2022-09-07T16:00:39.135593Z","shell.execute_reply":"2022-09-07T16:00:39.167593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:00:50.486767Z","iopub.execute_input":"2022-09-07T16:00:50.487193Z","iopub.status.idle":"2022-09-07T16:00:50.492513Z","shell.execute_reply.started":"2022-09-07T16:00:50.487157Z","shell.execute_reply":"2022-09-07T16:00:50.491318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(df.iloc[13051,1:].values.reshape(28,28))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:01:13.408122Z","iopub.execute_input":"2022-09-07T16:01:13.408825Z","iopub.status.idle":"2022-09-07T16:01:13.613566Z","shell.execute_reply.started":"2022-09-07T16:01:13.408784Z","shell.execute_reply":"2022-09-07T16:01:13.612518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df.iloc[:,1:]\ny = df.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:01:20.673575Z","iopub.execute_input":"2022-09-07T16:01:20.674012Z","iopub.status.idle":"2022-09-07T16:01:20.680605Z","shell.execute_reply.started":"2022-09-07T16:01:20.673971Z","shell.execute_reply":"2022-09-07T16:01:20.679403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:01:29.786237Z","iopub.execute_input":"2022-09-07T16:01:29.786694Z","iopub.status.idle":"2022-09-07T16:01:30.263161Z","shell.execute_reply.started":"2022-09-07T16:01:29.786652Z","shell.execute_reply":"2022-09-07T16:01:30.261698Z"},"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=42)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:01:45.714552Z","iopub.execute_input":"2022-09-07T16:01:45.715166Z","iopub.status.idle":"2022-09-07T16:01:46.028778Z","shell.execute_reply.started":"2022-09-07T16:01:45.715089Z","shell.execute_reply":"2022-09-07T16:01:46.027484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:01:48.349456Z","iopub.execute_input":"2022-09-07T16:01:48.350302Z","iopub.status.idle":"2022-09-07T16:01:48.362209Z","shell.execute_reply.started":"2022-09-07T16:01:48.350248Z","shell.execute_reply":"2022-09-07T16:01:48.361379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:01:52.276031Z","iopub.execute_input":"2022-09-07T16:01:52.276429Z","iopub.status.idle":"2022-09-07T16:01:52.426974Z","shell.execute_reply.started":"2022-09-07T16:01:52.276396Z","shell.execute_reply":"2022-09-07T16:01:52.425978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:01:54.654613Z","iopub.execute_input":"2022-09-07T16:01:54.655390Z","iopub.status.idle":"2022-09-07T16:01:54.661199Z","shell.execute_reply.started":"2022-09-07T16:01:54.655344Z","shell.execute_reply":"2022-09-07T16:01:54.659580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(X=X_train, y=y_train)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:02:08.082239Z","iopub.execute_input":"2022-09-07T16:02:08.082683Z","iopub.status.idle":"2022-09-07T16:02:08.105789Z","shell.execute_reply.started":"2022-09-07T16:02:08.082643Z","shell.execute_reply":"2022-09-07T16:02:08.104565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\n\nstart = time.time()\n\ny_pred = knn.predict(X=X_test)\n\nprint(time.time() - start)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:02:27.760219Z","iopub.execute_input":"2022-09-07T16:02:27.760763Z","iopub.status.idle":"2022-09-07T16:02:46.464697Z","shell.execute_reply.started":"2022-09-07T16:02:27.760726Z","shell.execute_reply":"2022-09-07T16:02:46.463233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\n\n\naccuracy_score(y_true=y_test, y_pred=y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:03:05.578926Z","iopub.execute_input":"2022-09-07T16:03:05.579406Z","iopub.status.idle":"2022-09-07T16:03:05.590082Z","shell.execute_reply.started":"2022-09-07T16:03:05.579363Z","shell.execute_reply":"2022-09-07T16:03:05.588770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\n\n\nscaler = StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:03:13.756626Z","iopub.execute_input":"2022-09-07T16:03:13.757448Z","iopub.status.idle":"2022-09-07T16:03:13.762779Z","shell.execute_reply.started":"2022-09-07T16:03:13.757400Z","shell.execute_reply":"2022-09-07T16:03:13.761433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = scaler.fit_transform(X=X_train)\n\nX_test = scaler.transform(X=X_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:03:39.160761Z","iopub.execute_input":"2022-09-07T16:03:39.161173Z","iopub.status.idle":"2022-09-07T16:03:39.669040Z","shell.execute_reply.started":"2022-09-07T16:03:39.161139Z","shell.execute_reply":"2022-09-07T16:03:39.667731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# PCA \nfrom sklearn.decomposition import PCA","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:03:50.395388Z","iopub.execute_input":"2022-09-07T16:03:50.395847Z","iopub.status.idle":"2022-09-07T16:03:50.401064Z","shell.execute_reply.started":"2022-09-07T16:03:50.395809Z","shell.execute_reply":"2022-09-07T16:03:50.399624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components=200)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:03:55.638130Z","iopub.execute_input":"2022-09-07T16:03:55.638536Z","iopub.status.idle":"2022-09-07T16:03:55.643788Z","shell.execute_reply.started":"2022-09-07T16:03:55.638489Z","shell.execute_reply":"2022-09-07T16:03:55.642553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf = pca.fit_transform(X=X_train)\n\nX_test_trf = pca.transform(X=X_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:04:11.188759Z","iopub.execute_input":"2022-09-07T16:04:11.189166Z","iopub.status.idle":"2022-09-07T16:04:17.403447Z","shell.execute_reply.started":"2022-09-07T16:04:11.189133Z","shell.execute_reply":"2022-09-07T16:04:17.399006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:04:20.275587Z","iopub.execute_input":"2022-09-07T16:04:20.277017Z","iopub.status.idle":"2022-09-07T16:04:20.285305Z","shell.execute_reply.started":"2022-09-07T16:04:20.276873Z","shell.execute_reply":"2022-09-07T16:04:20.284010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn2 = KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:04:41.024377Z","iopub.execute_input":"2022-09-07T16:04:41.024817Z","iopub.status.idle":"2022-09-07T16:04:41.029542Z","shell.execute_reply.started":"2022-09-07T16:04:41.024780Z","shell.execute_reply":"2022-09-07T16:04:41.028569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn2.fit(X=X_train_trf, y=y_train)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:04:45.644185Z","iopub.execute_input":"2022-09-07T16:04:45.644590Z","iopub.status.idle":"2022-09-07T16:04:45.664298Z","shell.execute_reply.started":"2022-09-07T16:04:45.644556Z","shell.execute_reply":"2022-09-07T16:04:45.663023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = knn2.predict(X=X_test_trf)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:05:02.010381Z","iopub.execute_input":"2022-09-07T16:05:02.011885Z","iopub.status.idle":"2022-09-07T16:05:10.218846Z","shell.execute_reply.started":"2022-09-07T16:05:02.011827Z","shell.execute_reply":"2022-09-07T16:05:10.217610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_true=y_test, y_pred=y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:05:39.892195Z","iopub.execute_input":"2022-09-07T16:05:39.892688Z","iopub.status.idle":"2022-09-07T16:05:39.902172Z","shell.execute_reply.started":"2022-09-07T16:05:39.892645Z","shell.execute_reply":"2022-09-07T16:05:39.900981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(1, 20):\n    pca = PCA(n_components=i)\n    X_train_trf = pca.fit_transform(X=X_train)\n    X_test_trf = pca.transform(X=X_test)\n    \n    knn = KNeighborsClassifier()\n    \n    knn.fit(X_train_trf,y_train)\n    \n    y_pred = knn.predict(X=X_test_trf)\n    \n    print(accuracy_score(y_true=y_test, y_pred=y_pred))\n    ","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:13:09.839175Z","iopub.execute_input":"2022-09-07T16:13:09.839620Z","iopub.status.idle":"2022-09-07T16:14:45.967107Z","shell.execute_reply.started":"2022-09-07T16:13:09.839580Z","shell.execute_reply":"2022-09-07T16:14:45.965730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transforming to a 2D coordinate system\n\npca2 = PCA(n_components=2)\n\nX_train_trf2 = pca2.fit_transform(X=X_train)\n\nX_test_trf2 = pca2.transform(X=X_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:07:31.962582Z","iopub.execute_input":"2022-09-07T16:07:31.963076Z","iopub.status.idle":"2022-09-07T16:07:34.718817Z","shell.execute_reply.started":"2022-09-07T16:07:31.963034Z","shell.execute_reply":"2022-09-07T16:07:34.717100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf2","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:07:38.296637Z","iopub.execute_input":"2022-09-07T16:07:38.297039Z","iopub.status.idle":"2022-09-07T16:07:38.306429Z","shell.execute_reply.started":"2022-09-07T16:07:38.297006Z","shell.execute_reply":"2022-09-07T16:07:38.305431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px\n\ny_train_trf2 = y_train.astype(str)\n\nfig = px.scatter(x=X_train_trf2[:,0],\n                 y=X_train_trf2[:,1],\n                 color=y_train_trf2,\n                 color_discrete_sequence=px.colors.qualitative.G10\n                )\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:08:37.132409Z","iopub.execute_input":"2022-09-07T16:08:37.132865Z","iopub.status.idle":"2022-09-07T16:08:40.009875Z","shell.execute_reply.started":"2022-09-07T16:08:37.132828Z","shell.execute_reply":"2022-09-07T16:08:40.008638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transforming in 3D\n\npca3 = PCA(n_components=3)\n\nX_train_trf3 = pca3.fit_transform(X=X_train)\n\nX_test_trf3 = pca3.transform(X=X_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:09:20.938462Z","iopub.execute_input":"2022-09-07T16:09:20.938873Z","iopub.status.idle":"2022-09-07T16:09:23.735142Z","shell.execute_reply.started":"2022-09-07T16:09:20.938839Z","shell.execute_reply":"2022-09-07T16:09:23.733583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf3","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:09:25.335616Z","iopub.execute_input":"2022-09-07T16:09:25.336010Z","iopub.status.idle":"2022-09-07T16:09:25.343991Z","shell.execute_reply.started":"2022-09-07T16:09:25.335978Z","shell.execute_reply":"2022-09-07T16:09:25.342979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px\n\ny_train_trf3 = y_train.astype(str)\n\nfig = px.scatter_3d(df, x=X_train_trf3[:,0], y=X_train_trf3[:,1], z=X_train_trf3[:,2],\n              color=y_train_trf3)\nfig.update_layout(\n    margin = dict(l=20, r=20, t=20, b=20),\n    paper_bgcolor = \"LightSteelBlue\",\n)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:10:32.824859Z","iopub.execute_input":"2022-09-07T16:10:32.825320Z","iopub.status.idle":"2022-09-07T16:10:33.061627Z","shell.execute_reply.started":"2022-09-07T16:10:32.825281Z","shell.execute_reply":"2022-09-07T16:10:33.060041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca3.explained_variance_\n\n# Eigen values","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:10:45.588778Z","iopub.execute_input":"2022-09-07T16:10:45.589195Z","iopub.status.idle":"2022-09-07T16:10:45.597367Z","shell.execute_reply.started":"2022-09-07T16:10:45.589162Z","shell.execute_reply":"2022-09-07T16:10:45.596472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca3.components_.shape\n# Eigen vectors","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:10:50.898290Z","iopub.execute_input":"2022-09-07T16:10:50.898720Z","iopub.status.idle":"2022-09-07T16:10:50.906057Z","shell.execute_reply.started":"2022-09-07T16:10:50.898681Z","shell.execute_reply":"2022-09-07T16:10:50.904788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca3.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:10:56.867455Z","iopub.execute_input":"2022-09-07T16:10:56.867879Z","iopub.status.idle":"2022-09-07T16:10:56.875792Z","shell.execute_reply.started":"2022-09-07T16:10:56.867842Z","shell.execute_reply":"2022-09-07T16:10:56.874668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca4 = PCA(n_components=None)\n\nX_train_trf4 = pca4.fit_transform(X_train)\n\nX_test_trf4 = pca4.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:11:26.749410Z","iopub.execute_input":"2022-09-07T16:11:26.750008Z","iopub.status.idle":"2022-09-07T16:11:32.705689Z","shell.execute_reply.started":"2022-09-07T16:11:26.749956Z","shell.execute_reply":"2022-09-07T16:11:32.703926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca4.explained_variance_.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:11:39.447287Z","iopub.execute_input":"2022-09-07T16:11:39.447703Z","iopub.status.idle":"2022-09-07T16:11:39.455335Z","shell.execute_reply.started":"2022-09-07T16:11:39.447668Z","shell.execute_reply":"2022-09-07T16:11:39.454424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca4.components_.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:11:42.127658Z","iopub.execute_input":"2022-09-07T16:11:42.128085Z","iopub.status.idle":"2022-09-07T16:11:42.136130Z","shell.execute_reply.started":"2022-09-07T16:11:42.128048Z","shell.execute_reply":"2022-09-07T16:11:42.134929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.cumsum(pca4.explained_variance_ratio_)","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:11:45.145148Z","iopub.execute_input":"2022-09-07T16:11:45.145559Z","iopub.status.idle":"2022-09-07T16:11:45.162779Z","shell.execute_reply.started":"2022-09-07T16:11:45.145524Z","shell.execute_reply":"2022-09-07T16:11:45.161572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(np.cumsum(pca4.explained_variance_ratio_))","metadata":{"execution":{"iopub.status.busy":"2022-09-07T16:11:56.714007Z","iopub.execute_input":"2022-09-07T16:11:56.714416Z","iopub.status.idle":"2022-09-07T16:11:56.942888Z","shell.execute_reply.started":"2022-09-07T16:11:56.714383Z","shell.execute_reply":"2022-09-07T16:11:56.941752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}