{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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":30918,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"**PCA for Feature Reduction**\n\n**Why Use PCA?**\nPrincipal Component Analysis (PCA) is a dimensionality reduction technique used to transform high-dimensional data into a lower-dimensional space while preserving as much variance as possible.\n\n**Reducing the number of features helps:**\nImprove model efficiency.\nReduce computational cost.\nMinimize noise and redundancy in the dataset.\n\n**Steps for Applying PCA**\n\n**Standardization:** Since PCA is sensitive to feature scale, we first normalize the data using StandardScaler to ensure all features have a mean of 0 and a standard deviation of 1.\n\n**Compute Principal Components:** PCA finds new orthogonal axes (principal components) that capture maximum variance in the data.\n\n**Dimensionality Reduction:** We select the top N components that explain the most variance, reducing the feature space while retaining key information.\n\n**Transform the Data:** The dataset is projected onto these components, creating a lower-dimensional representation.","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","trusted":true,"execution":{"iopub.status.busy":"2025-02-26T10:00:45.845438Z","iopub.execute_input":"2025-02-26T10:00:45.845834Z","iopub.status.idle":"2025-02-26T10:00:45.853252Z","shell.execute_reply.started":"2025-02-26T10:00:45.845796Z","shell.execute_reply":"2025-02-26T10:00:45.852163Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T10:00:45.855083Z","iopub.execute_input":"2025-02-26T10:00:45.855456Z","iopub.status.idle":"2025-02-26T10:00:47.553229Z","shell.execute_reply.started":"2025-02-26T10:00:45.855423Z","shell.execute_reply":"2025-02-26T10:00:47.552165Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T10:00:47.554551Z","iopub.execute_input":"2025-02-26T10:00:47.554913Z","iopub.status.idle":"2025-02-26T10:00:47.560763Z","shell.execute_reply.started":"2025-02-26T10:00:47.554886Z","shell.execute_reply":"2025-02-26T10:00:47.559653Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T10:00:47.5627Z","iopub.execute_input":"2025-02-26T10:00:47.563028Z","iopub.status.idle":"2025-02-26T10:00:47.591954Z","shell.execute_reply.started":"2025-02-26T10:00:47.563004Z","shell.execute_reply":"2025-02-26T10:00:47.590967Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt \nplt.imshow(df.iloc[15056,1:].values.reshape(28,28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T10:00:47.593336Z","iopub.execute_input":"2025-02-26T10:00:47.593751Z","iopub.status.idle":"2025-02-26T10:00:47.789557Z","shell.execute_reply.started":"2025-02-26T10:00:47.593692Z","shell.execute_reply":"2025-02-26T10:00:47.788546Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x=df.iloc[:,1:]\ny=df.iloc[:,0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T10:00:47.790626Z","iopub.execute_input":"2025-02-26T10:00:47.790981Z","iopub.status.idle":"2025-02-26T10:00:47.796566Z","shell.execute_reply.started":"2025-02-26T10:00:47.790955Z","shell.execute_reply":"2025-02-26T10:00:47.795508Z"}},"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-02-26T10:00:47.797517Z","iopub.execute_input":"2025-02-26T10:00:47.797836Z","iopub.status.idle":"2025-02-26T10:00:47.976092Z","shell.execute_reply.started":"2025-02-26T10:00:47.797805Z","shell.execute_reply":"2025-02-26T10:00:47.975063Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T10:00:47.977127Z","iopub.execute_input":"2025-02-26T10:00:47.977389Z","iopub.status.idle":"2025-02-26T10:00:47.98381Z","shell.execute_reply.started":"2025-02-26T10:00:47.977367Z","shell.execute_reply":"2025-02-26T10:00:47.982977Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T10:00:47.986553Z","iopub.execute_input":"2025-02-26T10:00:47.986867Z","iopub.status.idle":"2025-02-26T10:00:48.006023Z","shell.execute_reply.started":"2025-02-26T10:00:47.986842Z","shell.execute_reply":"2025-02-26T10:00:48.005122Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T10:00:48.008156Z","iopub.execute_input":"2025-02-26T10:00:48.008441Z","iopub.status.idle":"2025-02-26T10:00:48.022016Z","shell.execute_reply.started":"2025-02-26T10:00:48.008418Z","shell.execute_reply":"2025-02-26T10:00:48.021065Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn=KNeighborsClassifier()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T10:00:48.022982Z","iopub.execute_input":"2025-02-26T10:00:48.023254Z","iopub.status.idle":"2025-02-26T10:00:48.038979Z","shell.execute_reply.started":"2025-02-26T10:00:48.023232Z","shell.execute_reply":"2025-02-26T10:00:48.037979Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(x_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T10:00:48.039998Z","iopub.execute_input":"2025-02-26T10:00:48.040361Z","iopub.status.idle":"2025-02-26T10:00:48.187502Z","shell.execute_reply.started":"2025-02-26T10:00:48.040327Z","shell.execute_reply":"2025-02-26T10:00:48.186553Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn.fit(x_train,y_train)\nimport time\nstart=time.time()\ny_pred=knn.predict(x_test)\nprint(time.time()-start)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T10:00:48.188408Z","iopub.execute_input":"2025-02-26T10:00:48.18875Z","iopub.status.idle":"2025-02-26T10:01:03.319171Z","shell.execute_reply.started":"2025-02-26T10:00:48.188693Z","shell.execute_reply":"2025-02-26T10:01:03.318253Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(y_test,y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T10:01:03.320071Z","iopub.execute_input":"2025-02-26T10:01:03.320319Z","iopub.status.idle":"2025-02-26T10:01:03.327932Z","shell.execute_reply.started":"2025-02-26T10:01:03.320297Z","shell.execute_reply":"2025-02-26T10:01:03.326823Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler=StandardScaler()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T10:01:03.329057Z","iopub.execute_input":"2025-02-26T10:01:03.329333Z","iopub.status.idle":"2025-02-26T10:01:03.34691Z","shell.execute_reply.started":"2025-02-26T10:01:03.32931Z","shell.execute_reply":"2025-02-26T10:01:03.345835Z"}},"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":"2025-02-26T10:01:03.347877Z","iopub.execute_input":"2025-02-26T10:01:03.348239Z","iopub.status.idle":"2025-02-26T10:01:03.877574Z","shell.execute_reply.started":"2025-02-26T10:01:03.348211Z","shell.execute_reply":"2025-02-26T10:01:03.876634Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca = PCA(n_components=100)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T10:01:03.878532Z","iopub.execute_input":"2025-02-26T10:01:03.878951Z","iopub.status.idle":"2025-02-26T10:01:03.883279Z","shell.execute_reply.started":"2025-02-26T10:01:03.878914Z","shell.execute_reply":"2025-02-26T10:01:03.882143Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train_trf=pca.fit_transform(x_train)\nx_test_trf=pca.transform(x_test)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T10:01:03.884394Z","iopub.execute_input":"2025-02-26T10:01:03.884744Z","iopub.status.idle":"2025-02-26T10:01:05.819983Z","shell.execute_reply.started":"2025-02-26T10:01:03.884683Z","shell.execute_reply":"2025-02-26T10:01:05.818925Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train_trf.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T10:01:05.820912Z","iopub.execute_input":"2025-02-26T10:01:05.821196Z","iopub.status.idle":"2025-02-26T10:01:05.827019Z","shell.execute_reply.started":"2025-02-26T10:01:05.821172Z","shell.execute_reply":"2025-02-26T10:01:05.826093Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"knn=KNeighborsClassifier()\nknn.fit(x_train_trf,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T10:01:05.82794Z","iopub.execute_input":"2025-02-26T10:01:05.828242Z","iopub.status.idle":"2025-02-26T10:01:05.86227Z","shell.execute_reply.started":"2025-02-26T10:01:05.828217Z","shell.execute_reply":"2025-02-26T10:01:05.86123Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred=knn.predict(x_test_trf)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T10:01:05.86333Z","iopub.execute_input":"2025-02-26T10:01:05.863728Z","iopub.status.idle":"2025-02-26T10:01:07.880837Z","shell.execute_reply.started":"2025-02-26T10:01:05.863671Z","shell.execute_reply":"2025-02-26T10:01:07.879817Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"accuracy_score(y_test,y_pred)*100","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T10:01:07.881838Z","iopub.execute_input":"2025-02-26T10:01:07.882108Z","iopub.status.idle":"2025-02-26T10:01:07.890057Z","shell.execute_reply.started":"2025-02-26T10:01:07.882085Z","shell.execute_reply":"2025-02-26T10:01:07.889084Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range (1,785):\n    pca = PCA(n_components=i)\n    \n    x_train_trf=pca.fit_transform(x_train)\n    x_test_trf=pca.transform(x_test)\n    \n    knn=KNeighborsClassifier()\n    knn.fit(x_train_trf,y_train)\n    \n    y_pred=knn.predict(x_test_trf)\n    \n    print(accuracy_score(y_test,y_pred)*100)\n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-26T10:02:33.424385Z","iopub.execute_input":"2025-02-26T10:02:33.424743Z","execution_failed":"2025-02-26T10:07:22.794Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}