{"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","execution":{"iopub.status.busy":"2024-10-08T05:16:44.718014Z","iopub.execute_input":"2024-10-08T05:16:44.718547Z","iopub.status.idle":"2024-10-08T05:16:45.175014Z","shell.execute_reply.started":"2024-10-08T05:16:44.718490Z","shell.execute_reply":"2024-10-08T05:16:45.173714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\nprint(df)","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:16:47.200810Z","iopub.execute_input":"2024-10-08T05:16:47.201724Z","iopub.status.idle":"2024-10-08T05:16:51.345362Z","shell.execute_reply.started":"2024-10-08T05:16:47.201678Z","shell.execute_reply":"2024-10-08T05:16:51.344233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:16:52.736616Z","iopub.execute_input":"2024-10-08T05:16:52.737076Z","iopub.status.idle":"2024-10-08T05:16:52.742738Z","shell.execute_reply.started":"2024-10-08T05:16:52.737032Z","shell.execute_reply":"2024-10-08T05:16:52.741386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#example of random image\nplt.imshow(df.iloc[13108,1:].values.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:16:54.062495Z","iopub.execute_input":"2024-10-08T05:16:54.062953Z","iopub.status.idle":"2024-10-08T05:16:54.371821Z","shell.execute_reply.started":"2024-10-08T05:16:54.062909Z","shell.execute_reply":"2024-10-08T05:16:54.370662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sample()","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:16:55.978898Z","iopub.execute_input":"2024-10-08T05:16:55.979328Z","iopub.status.idle":"2024-10-08T05:16:56.003187Z","shell.execute_reply.started":"2024-10-08T05:16:55.979289Z","shell.execute_reply":"2024-10-08T05:16:56.001731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df.iloc[:,1:]\ny = df.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:18:28.424770Z","iopub.execute_input":"2024-10-08T05:18:28.425245Z","iopub.status.idle":"2024-10-08T05:18:28.431298Z","shell.execute_reply.started":"2024-10-08T05:18:28.425202Z","shell.execute_reply":"2024-10-08T05:18:28.430093Z"},"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":"2024-10-08T05:18:29.408229Z","iopub.execute_input":"2024-10-08T05:18:29.408662Z","iopub.status.idle":"2024-10-08T05:18:29.666753Z","shell.execute_reply.started":"2024-10-08T05:18:29.408620Z","shell.execute_reply":"2024-10-08T05:18:29.665657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape # we will train this dataset","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:18:30.553221Z","iopub.execute_input":"2024-10-08T05:18:30.553706Z","iopub.status.idle":"2024-10-08T05:18:30.561909Z","shell.execute_reply.started":"2024-10-08T05:18:30.553661Z","shell.execute_reply":"2024-10-08T05:18:30.560689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:18:31.190910Z","iopub.execute_input":"2024-10-08T05:18:31.191392Z","iopub.status.idle":"2024-10-08T05:18:31.197050Z","shell.execute_reply.started":"2024-10-08T05:18:31.191346Z","shell.execute_reply":"2024-10-08T05:18:31.195671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:18:31.853253Z","iopub.execute_input":"2024-10-08T05:18:31.853707Z","iopub.status.idle":"2024-10-08T05:18:31.859385Z","shell.execute_reply.started":"2024-10-08T05:18:31.853667Z","shell.execute_reply":"2024-10-08T05:18:31.858148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#form the similar distance\nknn.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:18:32.627058Z","iopub.execute_input":"2024-10-08T05:18:32.627538Z","iopub.status.idle":"2024-10-08T05:18:32.787800Z","shell.execute_reply.started":"2024-10-08T05:18:32.627495Z","shell.execute_reply":"2024-10-08T05:18:32.786578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time #just for the checking time\nstart = time.time()\ny_pred = knn.predict(X_test)\nprint(time.time() - start)","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:18:33.752836Z","iopub.execute_input":"2024-10-08T05:18:33.753337Z","iopub.status.idle":"2024-10-08T05:18:46.750456Z","shell.execute_reply.started":"2024-10-08T05:18:33.753290Z","shell.execute_reply":"2024-10-08T05:18:46.749335Z"},"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":"2024-10-07T11:06:51.769096Z","iopub.execute_input":"2024-10-07T11:06:51.769530Z","iopub.status.idle":"2024-10-07T11:06:51.779450Z","shell.execute_reply.started":"2024-10-07T11:06:51.769490Z","shell.execute_reply":"2024-10-07T11:06:51.777788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:19:00.898346Z","iopub.execute_input":"2024-10-08T05:19:00.898792Z","iopub.status.idle":"2024-10-08T05:19:00.904231Z","shell.execute_reply.started":"2024-10-08T05:19:00.898749Z","shell.execute_reply":"2024-10-08T05:19:00.902892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = scaler.fit_transform(X_train) # this data is mean centered\nX_test = scaler.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:19:04.183003Z","iopub.execute_input":"2024-10-08T05:19:04.184097Z","iopub.status.idle":"2024-10-08T05:19:04.686891Z","shell.execute_reply.started":"2024-10-08T05:19:04.184043Z","shell.execute_reply":"2024-10-08T05:19:04.685539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PCA\n#from sklearn.decomposition import PCA\n#pca = PCA(n_components = None) #just for example","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:19:09.554100Z","iopub.execute_input":"2024-10-08T05:19:09.554565Z","iopub.status.idle":"2024-10-08T05:19:09.559758Z","shell.execute_reply.started":"2024-10-08T05:19:09.554523Z","shell.execute_reply":"2024-10-08T05:19:09.558625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca = PCA(n_components=300) #now taking 200 components and will work on it.","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:19:10.559916Z","iopub.execute_input":"2024-10-08T05:19:10.560438Z","iopub.status.idle":"2024-10-08T05:19:10.565531Z","shell.execute_reply.started":"2024-10-08T05:19:10.560380Z","shell.execute_reply":"2024-10-08T05:19:10.564385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:19:11.838115Z","iopub.execute_input":"2024-10-08T05:19:11.839130Z","iopub.status.idle":"2024-10-08T05:19:18.719979Z","shell.execute_reply.started":"2024-10-08T05:19:11.839083Z","shell.execute_reply":"2024-10-08T05:19:18.718020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf.shape #tranformed data only showing PCA =100","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:19:18.723291Z","iopub.execute_input":"2024-10-08T05:19:18.723856Z","iopub.status.idle":"2024-10-08T05:19:18.734134Z","shell.execute_reply.started":"2024-10-08T05:19:18.723776Z","shell.execute_reply":"2024-10-08T05:19:18.732939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#now we need to train data for\nKNN= KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:19:18.737069Z","iopub.execute_input":"2024-10-08T05:19:18.737679Z","iopub.status.idle":"2024-10-08T05:19:18.745647Z","shell.execute_reply.started":"2024-10-08T05:19:18.737625Z","shell.execute_reply":"2024-10-08T05:19:18.743660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(X_train_trf, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:19:38.419300Z","iopub.execute_input":"2024-10-08T05:19:38.420127Z","iopub.status.idle":"2024-10-08T05:19:38.467766Z","shell.execute_reply.started":"2024-10-08T05:19:38.420079Z","shell.execute_reply":"2024-10-08T05:19:38.466582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = knn.predict(X_test_trf)","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:22:36.217013Z","iopub.execute_input":"2024-10-08T05:22:36.217505Z","iopub.status.idle":"2024-10-08T05:22:39.929186Z","shell.execute_reply.started":"2024-10-08T05:22:36.217460Z","shell.execute_reply":"2024-10-08T05:22:39.928143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test,y_pred)\n\n## if you increase PCA number then you will get best accuracy score.","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:22:41.184557Z","iopub.execute_input":"2024-10-08T05:22:41.185058Z","iopub.status.idle":"2024-10-08T05:22:41.218297Z","shell.execute_reply.started":"2024-10-08T05:22:41.185012Z","shell.execute_reply":"2024-10-08T05:22:41.216670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfor 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.fit_transform(X_test)\n\n    knn = KNeighborsClassifier()\n    knn.fit(X_train_trf, y_train)\n    y_pred = knn.predict(X_test_trf)\n\n    print(accuracy_score(y_test, y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-10-07T11:08:03.076460Z","iopub.execute_input":"2024-10-07T11:08:03.076947Z","iopub.status.idle":"2024-10-07T14:02:43.793861Z","shell.execute_reply.started":"2024-10-07T11:08:03.076900Z","shell.execute_reply":"2024-10-07T14:02:43.792697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#transformaing 2D data a coordinate systum\npca = PCA(n_components =2)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:23:15.226667Z","iopub.execute_input":"2024-10-08T05:23:15.227136Z","iopub.status.idle":"2024-10-08T05:23:16.985467Z","shell.execute_reply.started":"2024-10-08T05:23:15.227092Z","shell.execute_reply":"2024-10-08T05:23:16.983909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:23:23.082841Z","iopub.execute_input":"2024-10-08T05:23:23.083726Z","iopub.status.idle":"2024-10-08T05:23:23.091496Z","shell.execute_reply.started":"2024-10-08T05:23:23.083677Z","shell.execute_reply":"2024-10-08T05:23:23.090253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#we can use matplotlib here also but in this case we case we are using plotly\nimport 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\n                )\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:23:54.813884Z","iopub.execute_input":"2024-10-08T05:23:54.815006Z","iopub.status.idle":"2024-10-08T05:23:58.014545Z","shell.execute_reply.started":"2024-10-08T05:23:54.814953Z","shell.execute_reply":"2024-10-08T05:23:58.013091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transforming in 3D\npca = PCA(n_components=3)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:25:42.330702Z","iopub.execute_input":"2024-10-08T05:25:42.331201Z","iopub.status.idle":"2024-10-08T05:25:44.150430Z","shell.execute_reply.started":"2024-10-08T05:25:42.331143Z","shell.execute_reply":"2024-10-08T05:25:44.149261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:25:54.378616Z","iopub.execute_input":"2024-10-08T05:25:54.379073Z","iopub.status.idle":"2024-10-08T05:25:54.387361Z","shell.execute_reply.started":"2024-10-08T05:25:54.379026Z","shell.execute_reply":"2024-10-08T05:25:54.386235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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    paper_bgcolor=\"LightSteelBlue\",\n)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:26:06.142582Z","iopub.execute_input":"2024-10-08T05:26:06.143056Z","iopub.status.idle":"2024-10-08T05:26:06.383255Z","shell.execute_reply.started":"2024-10-08T05:26:06.143013Z","shell.execute_reply":"2024-10-08T05:26:06.381887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#explained varaince_ # egien value\npca.explained_variance_","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:26:55.479962Z","iopub.execute_input":"2024-10-08T05:26:55.480436Z","iopub.status.idle":"2024-10-08T05:26:55.487997Z","shell.execute_reply.started":"2024-10-08T05:26:55.480391Z","shell.execute_reply":"2024-10-08T05:26:55.486808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape #eignevector #whenever you called fit.transform then is automatacally happed(transfered value)","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:27:03.586328Z","iopub.execute_input":"2024-10-08T05:27:03.586797Z","iopub.status.idle":"2024-10-08T05:27:03.594809Z","shell.execute_reply.started":"2024-10-08T05:27:03.586754Z","shell.execute_reply":"2024-10-08T05:27:03.593590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_ratio_ ","metadata":{"execution":{"iopub.status.busy":"2024-10-07T14:02:54.844388Z","iopub.status.idle":"2024-10-07T14:02:54.845391Z","shell.execute_reply.started":"2024-10-07T14:02:54.845035Z","shell.execute_reply":"2024-10-07T14:02:54.845069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","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":{"execution":{"iopub.status.busy":"2024-10-08T05:27:08.894886Z","iopub.execute_input":"2024-10-08T05:27:08.895343Z","iopub.status.idle":"2024-10-08T05:27:14.384185Z","shell.execute_reply.started":"2024-10-08T05:27:08.895298Z","shell.execute_reply":"2024-10-08T05:27:14.382561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:27:14.387359Z","iopub.execute_input":"2024-10-08T05:27:14.388627Z","iopub.status.idle":"2024-10-08T05:27:14.399998Z","shell.execute_reply.started":"2024-10-08T05:27:14.388553Z","shell.execute_reply":"2024-10-08T05:27:14.398487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:33:18.886763Z","iopub.execute_input":"2024-10-08T05:33:18.887671Z","iopub.status.idle":"2024-10-08T05:33:18.895125Z","shell.execute_reply.started":"2024-10-08T05:33:18.887618Z","shell.execute_reply":"2024-10-08T05:33:18.893968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:33:25.037477Z","iopub.execute_input":"2024-10-08T05:33:25.037941Z","iopub.status.idle":"2024-10-08T05:33:25.056370Z","shell.execute_reply.started":"2024-10-08T05:33:25.037897Z","shell.execute_reply":"2024-10-08T05:33:25.055255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))\n#output explained : # if vectors explained 90% variance then it's relevent here is at 200 points variable are explained. ","metadata":{"execution":{"iopub.status.busy":"2024-10-08T05:33:33.348773Z","iopub.execute_input":"2024-10-08T05:33:33.349246Z","iopub.status.idle":"2024-10-08T05:33:33.571537Z","shell.execute_reply.started":"2024-10-08T05:33:33.349197Z","shell.execute_reply":"2024-10-08T05:33:33.570258Z"},"trusted":true},"execution_count":null,"outputs":[]}]}