{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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":30786,"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":"2024-11-06T07:55:24.023650Z","iopub.execute_input":"2024-11-06T07:55:24.024497Z","iopub.status.idle":"2024-11-06T07:55:24.032968Z","shell.execute_reply.started":"2024-11-06T07:55:24.024450Z","shell.execute_reply":"2024-11-06T07:55:24.031815Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:24.039115Z","iopub.execute_input":"2024-11-06T07:55:24.039960Z","iopub.status.idle":"2024-11-06T07:55:24.045546Z","shell.execute_reply.started":"2024-11-06T07:55:24.039915Z","shell.execute_reply":"2024-11-06T07:55:24.044566Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(r'/kaggle/input/digit-recognizer/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:24.058711Z","iopub.execute_input":"2024-11-06T07:55:24.059135Z","iopub.status.idle":"2024-11-06T07:55:26.416032Z","shell.execute_reply.started":"2024-11-06T07:55:24.059095Z","shell.execute_reply":"2024-11-06T07:55:26.414840Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:26.418401Z","iopub.execute_input":"2024-11-06T07:55:26.419111Z","iopub.status.idle":"2024-11-06T07:55:26.425613Z","shell.execute_reply.started":"2024-11-06T07:55:26.419048Z","shell.execute_reply":"2024-11-06T07:55:26.424559Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:26.427160Z","iopub.execute_input":"2024-11-06T07:55:26.427536Z","iopub.status.idle":"2024-11-06T07:55:26.449976Z","shell.execute_reply.started":"2024-11-06T07:55:26.427495Z","shell.execute_reply":"2024-11-06T07:55:26.448848Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(df.iloc[1,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:26.452641Z","iopub.execute_input":"2024-11-06T07:55:26.453340Z","iopub.status.idle":"2024-11-06T07:55:26.691026Z","shell.execute_reply.started":"2024-11-06T07:55:26.453288Z","shell.execute_reply":"2024-11-06T07:55:26.689954Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = df.iloc[:,1:]\ny = df.iloc[:,0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:26.692470Z","iopub.execute_input":"2024-11-06T07:55:26.692957Z","iopub.status.idle":"2024-11-06T07:55:26.699945Z","shell.execute_reply.started":"2024-11-06T07:55:26.692903Z","shell.execute_reply":"2024-11-06T07:55:26.698512Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:26.701874Z","iopub.execute_input":"2024-11-06T07:55:26.702379Z","iopub.status.idle":"2024-11-06T07:55:26.708466Z","shell.execute_reply.started":"2024-11-06T07:55:26.702294Z","shell.execute_reply":"2024-11-06T07:55:26.707259Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:26.709965Z","iopub.execute_input":"2024-11-06T07:55:26.711161Z","iopub.status.idle":"2024-11-06T07:55:26.881394Z","shell.execute_reply.started":"2024-11-06T07:55:26.711103Z","shell.execute_reply":"2024-11-06T07:55:26.880280Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:26.883386Z","iopub.execute_input":"2024-11-06T07:55:26.883937Z","iopub.status.idle":"2024-11-06T07:55:26.909868Z","shell.execute_reply.started":"2024-11-06T07:55:26.883886Z","shell.execute_reply":"2024-11-06T07:55:26.908617Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:26.911811Z","iopub.execute_input":"2024-11-06T07:55:26.912283Z","iopub.status.idle":"2024-11-06T07:55:26.920686Z","shell.execute_reply.started":"2024-11-06T07:55:26.912231Z","shell.execute_reply":"2024-11-06T07:55:26.919667Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:26.925766Z","iopub.execute_input":"2024-11-06T07:55:26.926601Z","iopub.status.idle":"2024-11-06T07:55:26.932425Z","shell.execute_reply.started":"2024-11-06T07:55:26.926549Z","shell.execute_reply":"2024-11-06T07:55:26.931122Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn =  KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:26.934054Z","iopub.execute_input":"2024-11-06T07:55:26.934490Z","iopub.status.idle":"2024-11-06T07:55:26.941512Z","shell.execute_reply.started":"2024-11-06T07:55:26.934442Z","shell.execute_reply":"2024-11-06T07:55:26.940267Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:26.942915Z","iopub.execute_input":"2024-11-06T07:55:26.943276Z","iopub.status.idle":"2024-11-06T07:55:27.083008Z","shell.execute_reply.started":"2024-11-06T07:55:26.943234Z","shell.execute_reply":"2024-11-06T07:55:27.081771Z"}},"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":"2024-11-06T07:55:27.084651Z","iopub.execute_input":"2024-11-06T07:55:27.085171Z","iopub.status.idle":"2024-11-06T07:55:41.846978Z","shell.execute_reply.started":"2024-11-06T07:55:27.085115Z","shell.execute_reply":"2024-11-06T07:55:41.845702Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nprint(accuracy_score(y_test,y_pred))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:41.848952Z","iopub.execute_input":"2024-11-06T07:55:41.849470Z","iopub.status.idle":"2024-11-06T07:55:41.859579Z","shell.execute_reply.started":"2024-11-06T07:55:41.849417Z","shell.execute_reply":"2024-11-06T07:55:41.858198Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:41.860992Z","iopub.execute_input":"2024-11-06T07:55:41.861453Z","iopub.status.idle":"2024-11-06T07:55:41.869292Z","shell.execute_reply.started":"2024-11-06T07:55:41.861405Z","shell.execute_reply":"2024-11-06T07:55:41.866014Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = scaler.fit_transform(X_train)\nX_test = scaler.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:41.870834Z","iopub.execute_input":"2024-11-06T07:55:41.872011Z","iopub.status.idle":"2024-11-06T07:55:42.361652Z","shell.execute_reply.started":"2024-11-06T07:55:41.871952Z","shell.execute_reply":"2024-11-06T07:55:42.360696Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"## PCA\nfrom sklearn.decomposition import PCA","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:42.363009Z","iopub.execute_input":"2024-11-06T07:55:42.363379Z","iopub.status.idle":"2024-11-06T07:55:42.368752Z","shell.execute_reply.started":"2024-11-06T07:55:42.363342Z","shell.execute_reply":"2024-11-06T07:55:42.367537Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca = PCA(n_components=100)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:42.370080Z","iopub.execute_input":"2024-11-06T07:55:42.370568Z","iopub.status.idle":"2024-11-06T07:55:42.379837Z","shell.execute_reply.started":"2024-11-06T07:55:42.370513Z","shell.execute_reply":"2024-11-06T07:55:42.378852Z"}},"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":"2024-11-06T07:55:42.381350Z","iopub.execute_input":"2024-11-06T07:55:42.381860Z","iopub.status.idle":"2024-11-06T07:55:46.244814Z","shell.execute_reply.started":"2024-11-06T07:55:42.381778Z","shell.execute_reply":"2024-11-06T07:55:46.243807Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:46.246342Z","iopub.execute_input":"2024-11-06T07:55:46.246989Z","iopub.status.idle":"2024-11-06T07:55:46.253391Z","shell.execute_reply.started":"2024-11-06T07:55:46.246943Z","shell.execute_reply":"2024-11-06T07:55:46.252477Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn1 = KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:46.254864Z","iopub.execute_input":"2024-11-06T07:55:46.255470Z","iopub.status.idle":"2024-11-06T07:55:46.263461Z","shell.execute_reply.started":"2024-11-06T07:55:46.255425Z","shell.execute_reply":"2024-11-06T07:55:46.262441Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(X_train_trf,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:46.265146Z","iopub.execute_input":"2024-11-06T07:55:46.266178Z","iopub.status.idle":"2024-11-06T07:55:46.299395Z","shell.execute_reply.started":"2024-11-06T07:55:46.266124Z","shell.execute_reply":"2024-11-06T07:55:46.298461Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred1 = knn.predict(X_test_trf)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:46.300899Z","iopub.execute_input":"2024-11-06T07:55:46.301508Z","iopub.status.idle":"2024-11-06T07:55:48.453155Z","shell.execute_reply.started":"2024-11-06T07:55:46.301461Z","shell.execute_reply":"2024-11-06T07:55:48.452154Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(accuracy_score(y_test,y_pred1))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:48.454559Z","iopub.execute_input":"2024-11-06T07:55:48.455403Z","iopub.status.idle":"2024-11-06T07:55:48.462813Z","shell.execute_reply.started":"2024-11-06T07:55:48.455357Z","shell.execute_reply":"2024-11-06T07:55:48.461586Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# for i in range(1,785):\n#     pca = PCA(n_components=i)\n#     X_train_trf = pca.fit_transform(X_train)\n#     X_test_trf = pca.transform(X_test)\n    \n#     knn1 = KNeighborsClassifier()\n\n#     knn.fit(X_train_trf,y_train)\n#     y_pred1 = knn.predict(X_test_trf)\n#     print(accuracy_score(y_test,y_pred1))\n    \n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:48.464175Z","iopub.execute_input":"2024-11-06T07:55:48.464597Z","iopub.status.idle":"2024-11-06T07:55:48.471071Z","shell.execute_reply.started":"2024-11-06T07:55:48.464549Z","shell.execute_reply":"2024-11-06T07:55:48.469974Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"## Transforming in 2d\npca1 = PCA(n_components=2)\nX_train_trf = pca1.fit_transform(X_train)\nX_test_trf = pca1.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:48.472278Z","iopub.execute_input":"2024-11-06T07:55:48.472625Z","iopub.status.idle":"2024-11-06T07:55:49.492446Z","shell.execute_reply.started":"2024-11-06T07:55:48.472583Z","shell.execute_reply":"2024-11-06T07:55:49.491307Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train_trf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:49.494161Z","iopub.execute_input":"2024-11-06T07:55:49.494941Z","iopub.status.idle":"2024-11-06T07:55:49.503395Z","shell.execute_reply.started":"2024-11-06T07:55:49.494886Z","shell.execute_reply":"2024-11-06T07:55:49.502297Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import plotly.express as px\ny_train_trf = y_train.astype(str)\nfig = px.scatter(df, x= X_train_trf[:,0],y=X_train_trf[:,1],\n                    color=y_train_trf,color_discrete_sequence=px.colors.qualitative.G10)\n# fig.update_layout(\n#     margin=dict(l=20,r=20,t=20,b=20)\n# )\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:55:49.510199Z","iopub.execute_input":"2024-11-06T07:55:49.510913Z","iopub.status.idle":"2024-11-06T07:55:49.672109Z","shell.execute_reply.started":"2024-11-06T07:55:49.510858Z","shell.execute_reply":"2024-11-06T07:55:49.670865Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"## Transforming in 2d\npca1 = PCA(n_components=3)\nX_train_trf = pca1.fit_transform(X_train)\nX_test_trf = pca1.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T07:59:39.985044Z","iopub.execute_input":"2024-11-06T07:59:39.985466Z","iopub.status.idle":"2024-11-06T07:59:40.992764Z","shell.execute_reply.started":"2024-11-06T07:59:39.985426Z","shell.execute_reply":"2024-11-06T07:59:40.991617Z"}},"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, 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)\nfig.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T08:00:08.032936Z","iopub.execute_input":"2024-11-06T08:00:08.033355Z","iopub.status.idle":"2024-11-06T08:00:08.199727Z","shell.execute_reply.started":"2024-11-06T08:00:08.033316Z","shell.execute_reply":"2024-11-06T08:00:08.198554Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca1.explained_variance_   # lambda values explains variance \n## Eigen Values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T08:16:16.716973Z","iopub.execute_input":"2024-11-06T08:16:16.719018Z","iopub.status.idle":"2024-11-06T08:16:16.733437Z","shell.execute_reply.started":"2024-11-06T08:16:16.718934Z","shell.execute_reply":"2024-11-06T08:16:16.732254Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"## Eigen Vecors\npca1.components_.shape   ## 3 is eign vectors from 784 dimenstions-features","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T08:07:24.546508Z","iopub.execute_input":"2024-11-06T08:07:24.546973Z","iopub.status.idle":"2024-11-06T08:07:24.554668Z","shell.execute_reply.started":"2024-11-06T08:07:24.546928Z","shell.execute_reply":"2024-11-06T08:07:24.553299Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Optimum No of PC required","metadata":{}},{"cell_type":"markdown","source":"- Lamda (should explain variance that should be equal to or atleast 90% of total variance )","metadata":{}},{"cell_type":"code","source":"## variance contribution\npca1.explained_variance_ratio_","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T08:19:06.501928Z","iopub.execute_input":"2024-11-06T08:19:06.502385Z","iopub.status.idle":"2024-11-06T08:19:06.511178Z","shell.execute_reply.started":"2024-11-06T08:19:06.502343Z","shell.execute_reply":"2024-11-06T08:19:06.509639Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca2= PCA(n_components=None)\nX_train_trf = pca2.fit_transform(X_train)\nX_test_trf = pca2.transform(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T08:26:07.478028Z","iopub.execute_input":"2024-11-06T08:26:07.479415Z","iopub.status.idle":"2024-11-06T08:26:19.469388Z","shell.execute_reply.started":"2024-11-06T08:26:07.479344Z","shell.execute_reply":"2024-11-06T08:26:19.468274Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca2.explained_variance_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T08:26:32.871935Z","iopub.execute_input":"2024-11-06T08:26:32.872428Z","iopub.status.idle":"2024-11-06T08:26:32.880919Z","shell.execute_reply.started":"2024-11-06T08:26:32.872385Z","shell.execute_reply":"2024-11-06T08:26:32.879562Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca2.components_.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T08:26:56.327567Z","iopub.execute_input":"2024-11-06T08:26:56.328946Z","iopub.status.idle":"2024-11-06T08:26:56.337107Z","shell.execute_reply.started":"2024-11-06T08:26:56.328888Z","shell.execute_reply":"2024-11-06T08:26:56.335618Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.cumsum(pca2.explained_variance_ratio_)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T08:28:17.712570Z","iopub.execute_input":"2024-11-06T08:28:17.713118Z","iopub.status.idle":"2024-11-06T08:28:17.729338Z","shell.execute_reply.started":"2024-11-06T08:28:17.713072Z","shell.execute_reply":"2024-11-06T08:28:17.728076Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(np.cumsum(pca2.explained_variance_ratio_))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-06T08:28:41.673527Z","iopub.execute_input":"2024-11-06T08:28:41.674451Z","iopub.status.idle":"2024-11-06T08:28:41.945417Z","shell.execute_reply.started":"2024-11-06T08:28:41.674396Z","shell.execute_reply":"2024-11-06T08:28:41.944191Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## When PCA doesnt work?\n- when data are equal in each dimension  (circular data)\n- mirror data across dimension","metadata":{"execution":{"iopub.status.busy":"2024-11-06T08:31:38.982996Z","iopub.execute_input":"2024-11-06T08:31:38.984521Z","iopub.status.idle":"2024-11-06T08:31:38.992956Z","shell.execute_reply.started":"2024-11-06T08:31:38.984458Z","shell.execute_reply":"2024-11-06T08:31:38.991408Z"}}},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}