{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30733,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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-06-27T08:55:57.526810Z","iopub.execute_input":"2024-06-27T08:55:57.527624Z","iopub.status.idle":"2024-06-27T08:55:57.534216Z","shell.execute_reply.started":"2024-06-27T08:55:57.527593Z","shell.execute_reply":"2024-06-27T08:55:57.533235Z"},"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":"2024-06-27T08:55:57.543845Z","iopub.execute_input":"2024-06-27T08:55:57.544146Z","iopub.status.idle":"2024-06-27T08:56:00.311527Z","shell.execute_reply.started":"2024-06-27T08:55:57.544121Z","shell.execute_reply":"2024-06-27T08:56:00.310738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:00.313184Z","iopub.execute_input":"2024-06-27T08:56:00.313687Z","iopub.status.idle":"2024-06-27T08:56:00.330269Z","shell.execute_reply.started":"2024-06-27T08:56:00.313652Z","shell.execute_reply":"2024-06-27T08:56:00.329402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:00.331553Z","iopub.execute_input":"2024-06-27T08:56:00.331902Z","iopub.status.idle":"2024-06-27T08:56:00.341014Z","shell.execute_reply.started":"2024-06-27T08:56:00.331870Z","shell.execute_reply":"2024-06-27T08:56:00.340144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sample()","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:00.343535Z","iopub.execute_input":"2024-06-27T08:56:00.343985Z","iopub.status.idle":"2024-06-27T08:56:00.362663Z","shell.execute_reply.started":"2024-06-27T08:56:00.343949Z","shell.execute_reply":"2024-06-27T08:56:00.361809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:00.363710Z","iopub.execute_input":"2024-06-27T08:56:00.364001Z","iopub.status.idle":"2024-06-27T08:56:00.372156Z","shell.execute_reply.started":"2024-06-27T08:56:00.363973Z","shell.execute_reply":"2024-06-27T08:56:00.371460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(df.iloc[14649,1:].values.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:00.373226Z","iopub.execute_input":"2024-06-27T08:56:00.373527Z","iopub.status.idle":"2024-06-27T08:56:00.563204Z","shell.execute_reply.started":"2024-06-27T08:56:00.373503Z","shell.execute_reply":"2024-06-27T08:56:00.562353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=df.iloc[:,1:]\ny=df.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:00.564201Z","iopub.execute_input":"2024-06-27T08:56:00.564462Z","iopub.status.idle":"2024-06-27T08:56:00.569301Z","shell.execute_reply.started":"2024-06-27T08:56:00.564439Z","shell.execute_reply":"2024-06-27T08:56:00.568292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:00.570659Z","iopub.execute_input":"2024-06-27T08:56:00.571183Z","iopub.status.idle":"2024-06-27T08:56:00.597115Z","shell.execute_reply.started":"2024-06-27T08:56:00.571151Z","shell.execute_reply":"2024-06-27T08:56:00.596180Z"},"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)\n","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:00.598310Z","iopub.execute_input":"2024-06-27T08:56:00.598674Z","iopub.status.idle":"2024-06-27T08:56:00.811168Z","shell.execute_reply.started":"2024-06-27T08:56:00.598639Z","shell.execute_reply":"2024-06-27T08:56:00.810351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:00.814372Z","iopub.execute_input":"2024-06-27T08:56:00.814646Z","iopub.status.idle":"2024-06-27T08:56:00.835694Z","shell.execute_reply.started":"2024-06-27T08:56:00.814623Z","shell.execute_reply":"2024-06-27T08:56:00.834738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:00.837030Z","iopub.execute_input":"2024-06-27T08:56:00.837455Z","iopub.status.idle":"2024-06-27T08:56:00.843548Z","shell.execute_reply.started":"2024-06-27T08:56:00.837423Z","shell.execute_reply":"2024-06-27T08:56:00.842604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Without PCA**","metadata":{}},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\n","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:00.844668Z","iopub.execute_input":"2024-06-27T08:56:00.844913Z","iopub.status.idle":"2024-06-27T08:56:00.851411Z","shell.execute_reply.started":"2024-06-27T08:56:00.844892Z","shell.execute_reply":"2024-06-27T08:56:00.850667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn=KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:00.852463Z","iopub.execute_input":"2024-06-27T08:56:00.852716Z","iopub.status.idle":"2024-06-27T08:56:00.861603Z","shell.execute_reply.started":"2024-06-27T08:56:00.852694Z","shell.execute_reply":"2024-06-27T08:56:00.860745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:00.862879Z","iopub.execute_input":"2024-06-27T08:56:00.863731Z","iopub.status.idle":"2024-06-27T08:56:00.990117Z","shell.execute_reply.started":"2024-06-27T08:56:00.863706Z","shell.execute_reply":"2024-06-27T08:56:00.989158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Without PCA time taken**","metadata":{}},{"cell_type":"code","source":"import time\nstart=time.time()\ny_pred=knn.predict(X_test)\nprint(time.time()-start)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:00.991277Z","iopub.execute_input":"2024-06-27T08:56:00.991563Z","iopub.status.idle":"2024-06-27T08:56:12.476942Z","shell.execute_reply.started":"2024-06-27T08:56:00.991538Z","shell.execute_reply":"2024-06-27T08:56:12.475953Z"},"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-06-27T08:56:12.478148Z","iopub.execute_input":"2024-06-27T08:56:12.478444Z","iopub.status.idle":"2024-06-27T08:56:12.486778Z","shell.execute_reply.started":"2024-06-27T08:56:12.478416Z","shell.execute_reply":"2024-06-27T08:56:12.485838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**With PCA**","metadata":{}},{"cell_type":"code","source":"X_train","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:12.488068Z","iopub.execute_input":"2024-06-27T08:56:12.488444Z","iopub.status.idle":"2024-06-27T08:56:12.514164Z","shell.execute_reply.started":"2024-06-27T08:56:12.488409Z","shell.execute_reply":"2024-06-27T08:56:12.513125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler=StandardScaler()\n","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:12.515330Z","iopub.execute_input":"2024-06-27T08:56:12.515582Z","iopub.status.idle":"2024-06-27T08:56:12.519996Z","shell.execute_reply.started":"2024-06-27T08:56:12.515560Z","shell.execute_reply":"2024-06-27T08:56:12.519159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=scaler.fit_transform(X_train)\nX_test=scaler.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:12.521179Z","iopub.execute_input":"2024-06-27T08:56:12.521484Z","iopub.status.idle":"2024-06-27T08:56:12.985332Z","shell.execute_reply.started":"2024-06-27T08:56:12.521456Z","shell.execute_reply":"2024-06-27T08:56:12.984251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:12.986609Z","iopub.execute_input":"2024-06-27T08:56:12.986899Z","iopub.status.idle":"2024-06-27T08:56:12.993211Z","shell.execute_reply.started":"2024-06-27T08:56:12.986875Z","shell.execute_reply":"2024-06-27T08:56:12.992356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca=PCA(n_components=300)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:12.994514Z","iopub.execute_input":"2024-06-27T08:56:12.994876Z","iopub.status.idle":"2024-06-27T08:56:13.003707Z","shell.execute_reply.started":"2024-06-27T08:56:12.994849Z","shell.execute_reply":"2024-06-27T08:56:13.002800Z"},"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)\n","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:13.004950Z","iopub.execute_input":"2024-06-27T08:56:13.005258Z","iopub.status.idle":"2024-06-27T08:56:19.063614Z","shell.execute_reply.started":"2024-06-27T08:56:13.005234Z","shell.execute_reply":"2024-06-27T08:56:19.062824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:19.064764Z","iopub.execute_input":"2024-06-27T08:56:19.065114Z","iopub.status.idle":"2024-06-27T08:56:19.071632Z","shell.execute_reply.started":"2024-06-27T08:56:19.065083Z","shell.execute_reply":"2024-06-27T08:56:19.070650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn=KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:19.072675Z","iopub.execute_input":"2024-06-27T08:56:19.073071Z","iopub.status.idle":"2024-06-27T08:56:19.085931Z","shell.execute_reply.started":"2024-06-27T08:56:19.073039Z","shell.execute_reply":"2024-06-27T08:56:19.084994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(X_train_trf,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:19.086983Z","iopub.execute_input":"2024-06-27T08:56:19.087247Z","iopub.status.idle":"2024-06-27T08:56:19.131734Z","shell.execute_reply.started":"2024-06-27T08:56:19.087224Z","shell.execute_reply":"2024-06-27T08:56:19.130846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_trf=knn.predict(X_test_trf)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:19.133112Z","iopub.execute_input":"2024-06-27T08:56:19.133479Z","iopub.status.idle":"2024-06-27T08:56:21.562897Z","shell.execute_reply.started":"2024-06-27T08:56:19.133445Z","shell.execute_reply":"2024-06-27T08:56:21.562046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test,y_pred_trf)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:21.563962Z","iopub.execute_input":"2024-06-27T08:56:21.564233Z","iopub.status.idle":"2024-06-27T08:56:21.571710Z","shell.execute_reply.started":"2024-06-27T08:56:21.564209Z","shell.execute_reply":"2024-06-27T08:56:21.570832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Accuracy with no.of PCA**","metadata":{}},{"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(y_test,y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-06-27T08:56:21.576028Z","iopub.execute_input":"2024-06-27T08:56:21.576593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}