{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.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":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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","trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:27:11.085064Z","iopub.execute_input":"2025-07-24T06:27:11.085357Z","iopub.status.idle":"2025-07-24T06:27:11.524930Z","shell.execute_reply.started":"2025-07-24T06:27:11.085326Z","shell.execute_reply":"2025-07-24T06:27:11.524001Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf_train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:27:11.525933Z","iopub.execute_input":"2025-07-24T06:27:11.526423Z","iopub.status.idle":"2025-07-24T06:27:14.783744Z","shell.execute_reply.started":"2025-07-24T06:27:11.526398Z","shell.execute_reply":"2025-07-24T06:27:14.782737Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"- In MNIST data one image consists of (28X28) pixel.\n- In total there are 784 pixel.\n- Also , each pixel is a column here.","metadata":{}},{"cell_type":"code","source":"#one of the column is for label.\ndf_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:27:14.784660Z","iopub.execute_input":"2025-07-24T06:27:14.784900Z","iopub.status.idle":"2025-07-24T06:27:14.791235Z","shell.execute_reply.started":"2025-07-24T06:27:14.784880Z","shell.execute_reply":"2025-07-24T06:27:14.790455Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train.sample()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:27:14.792355Z","iopub.execute_input":"2025-07-24T06:27:14.792769Z","iopub.status.idle":"2025-07-24T06:27:14.820479Z","shell.execute_reply.started":"2025-07-24T06:27:14.792742Z","shell.execute_reply":"2025-07-24T06:27:14.819515Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.imshow(df_train.iloc[37332,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:27:14.823352Z","iopub.execute_input":"2025-07-24T06:27:14.823638Z","iopub.status.idle":"2025-07-24T06:27:15.010960Z","shell.execute_reply.started":"2025-07-24T06:27:14.823613Z","shell.execute_reply":"2025-07-24T06:27:15.009861Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = df_train.iloc[:,1:]\ny = df_train.iloc[:,0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:27:15.011849Z","iopub.execute_input":"2025-07-24T06:27:15.012138Z","iopub.status.idle":"2025-07-24T06:27:15.017642Z","shell.execute_reply.started":"2025-07-24T06:27:15.012117Z","shell.execute_reply":"2025-07-24T06:27:15.016263Z"}},"outputs":[],"execution_count":null},{"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":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:27:15.018902Z","iopub.execute_input":"2025-07-24T06:27:15.019273Z","iopub.status.idle":"2025-07-24T06:27:15.918056Z","shell.execute_reply.started":"2025-07-24T06:27:15.019235Z","shell.execute_reply":"2025-07-24T06:27:15.917024Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:27:15.919274Z","iopub.execute_input":"2025-07-24T06:27:15.919836Z","iopub.status.idle":"2025-07-24T06:27:15.942401Z","shell.execute_reply.started":"2025-07-24T06:27:15.919793Z","shell.execute_reply":"2025-07-24T06:27:15.941182Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:27:15.943541Z","iopub.execute_input":"2025-07-24T06:27:15.943891Z","iopub.status.idle":"2025-07-24T06:27:15.950564Z","shell.execute_reply.started":"2025-07-24T06:27:15.943848Z","shell.execute_reply":"2025-07-24T06:27:15.949246Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"- Now , It's a perfect data(no missing etc..)","metadata":{}},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nknn = KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:27:15.951773Z","iopub.execute_input":"2025-07-24T06:27:15.952183Z","iopub.status.idle":"2025-07-24T06:27:16.015171Z","shell.execute_reply.started":"2025-07-24T06:27:15.952148Z","shell.execute_reply":"2025-07-24T06:27:16.014076Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:27:16.016230Z","iopub.execute_input":"2025-07-24T06:27:16.016611Z","iopub.status.idle":"2025-07-24T06:27:16.146620Z","shell.execute_reply.started":"2025-07-24T06:27:16.016583Z","shell.execute_reply":"2025-07-24T06:27:16.145698Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import time\nstart = time.time()\ny_pred = knn.predict(X_test)\nprint(time.time() - start)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:27:16.147686Z","iopub.execute_input":"2025-07-24T06:27:16.148358Z","iopub.status.idle":"2025-07-24T06:27:30.351962Z","shell.execute_reply.started":"2025-07-24T06:27:16.148324Z","shell.execute_reply":"2025-07-24T06:27:30.350861Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nprint('Accuracy score: ' , accuracy_score(y_test , y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:27:30.353039Z","iopub.execute_input":"2025-07-24T06:27:30.353349Z","iopub.status.idle":"2025-07-24T06:27:30.361441Z","shell.execute_reply.started":"2025-07-24T06:27:30.353321Z","shell.execute_reply":"2025-07-24T06:27:30.360403Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"- Now , will use PCA.","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()\nX_train = scaler.fit_transform(X_train)\nX_test = scaler.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:27:30.362550Z","iopub.execute_input":"2025-07-24T06:27:30.363095Z","iopub.status.idle":"2025-07-24T06:27:30.969396Z","shell.execute_reply.started":"2025-07-24T06:27:30.363062Z","shell.execute_reply":"2025-07-24T06:27:30.968258Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca = PCA(n_components=200)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:27:30.970837Z","iopub.execute_input":"2025-07-24T06:27:30.971198Z","iopub.status.idle":"2025-07-24T06:27:30.976364Z","shell.execute_reply.started":"2025-07-24T06:27:30.971159Z","shell.execute_reply":"2025-07-24T06:27:30.975369Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:27:30.977320Z","iopub.execute_input":"2025-07-24T06:27:30.977603Z","iopub.status.idle":"2025-07-24T06:27:36.510400Z","shell.execute_reply.started":"2025-07-24T06:27:30.977572Z","shell.execute_reply":"2025-07-24T06:27:36.509377Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:27:36.514951Z","iopub.execute_input":"2025-07-24T06:27:36.515321Z","iopub.status.idle":"2025-07-24T06:27:36.521399Z","shell.execute_reply.started":"2025-07-24T06:27:36.515264Z","shell.execute_reply":"2025-07-24T06:27:36.520466Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:27:36.522461Z","iopub.execute_input":"2025-07-24T06:27:36.522821Z","iopub.status.idle":"2025-07-24T06:27:36.547246Z","shell.execute_reply.started":"2025-07-24T06:27:36.522786Z","shell.execute_reply":"2025-07-24T06:27:36.546345Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train_trf , y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:27:36.548391Z","iopub.execute_input":"2025-07-24T06:27:36.548727Z","iopub.status.idle":"2025-07-24T06:27:36.600391Z","shell.execute_reply.started":"2025-07-24T06:27:36.548702Z","shell.execute_reply":"2025-07-24T06:27:36.599436Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = knn.predict(X_test_trf)\nprint('Accuracy score ' , accuracy_score(y_test,y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:27:36.601308Z","iopub.execute_input":"2025-07-24T06:27:36.601613Z","iopub.status.idle":"2025-07-24T06:27:39.284260Z","shell.execute_reply.started":"2025-07-24T06:27:36.601590Z","shell.execute_reply":"2025-07-24T06:27:39.283262Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range(1,784):\n    pca = PCA(n_components=i)\n    X_train_trf = pca.fit_transform(X_train)\n    X_test_trf = pca.transform(X_test)\n    knn = KNeighborsClassifier()\n    knn.fit(X_train_trf , y_train)\n    y_pred = knn.predict(X_test_trf)\n    print('Accuracy score ' , accuracy_score(y_test,y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:27:39.285130Z","iopub.execute_input":"2025-07-24T06:27:39.285422Z","iopub.status.idle":"2025-07-24T06:28:19.823233Z","shell.execute_reply.started":"2025-07-24T06:27:39.285400Z","shell.execute_reply":"2025-07-24T06:28:19.821789Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"- Now,let's do visualization via PCA.","metadata":{}},{"cell_type":"code","source":"#visualizing the data in 2d\npca = PCA(n_components=2)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:28:26.999085Z","iopub.execute_input":"2025-07-24T06:28:26.999457Z","iopub.status.idle":"2025-07-24T06:28:27.784141Z","shell.execute_reply.started":"2025-07-24T06:28:26.999431Z","shell.execute_reply":"2025-07-24T06:28:27.781101Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#now each image is of 2D\nX_train_trf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:28:31.588082Z","iopub.execute_input":"2025-07-24T06:28:31.588473Z","iopub.status.idle":"2025-07-24T06:28:31.595733Z","shell.execute_reply.started":"2025-07-24T06:28:31.588445Z","shell.execute_reply":"2025-07-24T06:28:31.594460Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import plotly.express as px\ny_train_trf = y_train.astype(str)\nfig = px.scatter(x=X_train_trf[:,0],\n                 y=X_train_trf[:,1],\n                 color=y_train_trf,\n                 color_discrete_sequence=px.colors.qualitative.G10)\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:28:56.250391Z","iopub.execute_input":"2025-07-24T06:28:56.250757Z","iopub.status.idle":"2025-07-24T06:28:56.356901Z","shell.execute_reply.started":"2025-07-24T06:28:56.250734Z","shell.execute_reply":"2025-07-24T06:28:56.354895Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#visualizing it in 3d\npca = PCA(n_components=3)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:30:26.493114Z","iopub.execute_input":"2025-07-24T06:30:26.493480Z","iopub.status.idle":"2025-07-24T06:30:27.513169Z","shell.execute_reply.started":"2025-07-24T06:30:26.493455Z","shell.execute_reply":"2025-07-24T06:30:27.512362Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:30:28.987376Z","iopub.execute_input":"2025-07-24T06:30:28.988523Z","iopub.status.idle":"2025-07-24T06:30:28.996652Z","shell.execute_reply.started":"2025-07-24T06:30:28.988477Z","shell.execute_reply":"2025-07-24T06:30:28.995297Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import plotly.express as px\ny_train_trf = y_train.astype(str)\nfig = px.scatter_3d(df_train, x=X_train_trf[:,0], y=X_train_trf[:,1], z=X_train_trf[:,2],\n              color=y_train_trf)\nfig.update_layout(\n    margin=dict(l=20, r=20, t=20, b=20),\n    paper_bgcolor=\"LightSteelBlue\",\n)\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:30:45.694183Z","iopub.execute_input":"2025-07-24T06:30:45.695135Z","iopub.status.idle":"2025-07-24T06:30:45.875423Z","shell.execute_reply.started":"2025-07-24T06:30:45.695099Z","shell.execute_reply":"2025-07-24T06:30:45.874401Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#explained_variance ->eigen values\npca.explained_variance_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:49:07.016433Z","iopub.execute_input":"2025-07-24T06:49:07.016933Z","iopub.status.idle":"2025-07-24T06:49:07.025104Z","shell.execute_reply.started":"2025-07-24T06:49:07.016899Z","shell.execute_reply":"2025-07-24T06:49:07.023800Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#eigen vector\npca.components_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:55:13.629523Z","iopub.execute_input":"2025-07-24T06:55:13.629979Z","iopub.status.idle":"2025-07-24T06:55:13.637984Z","shell.execute_reply.started":"2025-07-24T06:55:13.629951Z","shell.execute_reply":"2025-07-24T06:55:13.637049Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T06:55:16.959124Z","iopub.execute_input":"2025-07-24T06:55:16.959496Z","iopub.status.idle":"2025-07-24T06:55:16.967395Z","shell.execute_reply.started":"2025-07-24T06:55:16.959469Z","shell.execute_reply":"2025-07-24T06:55:16.966132Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T07:12:34.768443Z","iopub.execute_input":"2025-07-24T07:12:34.768906Z","iopub.status.idle":"2025-07-24T07:12:34.776156Z","shell.execute_reply.started":"2025-07-24T07:12:34.768875Z","shell.execute_reply":"2025-07-24T07:12:34.774869Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Let's find out the optimum number of Principal Components to be taken**","metadata":{}},{"cell_type":"code","source":"pca = PCA(n_components=None)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T07:17:30.976853Z","iopub.execute_input":"2025-07-24T07:17:30.977275Z","iopub.status.idle":"2025-07-24T07:17:36.342249Z","shell.execute_reply.started":"2025-07-24T07:17:30.977246Z","shell.execute_reply":"2025-07-24T07:17:36.340889Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T07:18:03.963320Z","iopub.execute_input":"2025-07-24T07:18:03.963807Z","iopub.status.idle":"2025-07-24T07:18:03.970787Z","shell.execute_reply.started":"2025-07-24T07:18:03.963775Z","shell.execute_reply":"2025-07-24T07:18:03.969621Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.components_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T07:18:18.591104Z","iopub.execute_input":"2025-07-24T07:18:18.591464Z","iopub.status.idle":"2025-07-24T07:18:18.599497Z","shell.execute_reply.started":"2025-07-24T07:18:18.591436Z","shell.execute_reply":"2025-07-24T07:18:18.598055Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#now there are (784 vectors in 784d space)\npca.components_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T07:21:53.320587Z","iopub.execute_input":"2025-07-24T07:21:53.321032Z","iopub.status.idle":"2025-07-24T07:21:53.328403Z","shell.execute_reply.started":"2025-07-24T07:21:53.321001Z","shell.execute_reply":"2025-07-24T07:21:53.327379Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T07:22:16.807350Z","iopub.execute_input":"2025-07-24T07:22:16.808453Z","iopub.status.idle":"2025-07-24T07:22:16.820642Z","shell.execute_reply.started":"2025-07-24T07:22:16.808403Z","shell.execute_reply":"2025-07-24T07:22:16.819631Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T07:23:58.564983Z","iopub.execute_input":"2025-07-24T07:23:58.565736Z","iopub.status.idle":"2025-07-24T07:23:58.577505Z","shell.execute_reply.started":"2025-07-24T07:23:58.565700Z","shell.execute_reply":"2025-07-24T07:23:58.576215Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-24T07:26:06.647409Z","iopub.execute_input":"2025-07-24T07:26:06.647954Z","iopub.status.idle":"2025-07-24T07:26:06.834742Z","shell.execute_reply.started":"2025-07-24T07:26:06.647913Z","shell.execute_reply":"2025-07-24T07:26:06.833464Z"}},"outputs":[],"execution_count":null}]}