{"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":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30746,"isInternetEnabled":false,"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-08-04T12:22:13.293441Z","iopub.execute_input":"2024-08-04T12:22:13.293880Z","iopub.status.idle":"2024-08-04T12:22:14.450739Z","shell.execute_reply.started":"2024-08-04T12:22:13.293843Z","shell.execute_reply":"2024-08-04T12:22:14.449517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:14.452614Z","iopub.execute_input":"2024-08-04T12:22:14.453078Z","iopub.status.idle":"2024-08-04T12:22:18.014333Z","shell.execute_reply.started":"2024-08-04T12:22:14.453046Z","shell.execute_reply":"2024-08-04T12:22:18.013127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:18.016055Z","iopub.execute_input":"2024-08-04T12:22:18.016430Z","iopub.status.idle":"2024-08-04T12:22:18.046759Z","shell.execute_reply.started":"2024-08-04T12:22:18.016400Z","shell.execute_reply":"2024-08-04T12:22:18.045514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:18.049823Z","iopub.execute_input":"2024-08-04T12:22:18.050342Z","iopub.status.idle":"2024-08-04T12:22:18.057731Z","shell.execute_reply.started":"2024-08-04T12:22:18.050281Z","shell.execute_reply":"2024-08-04T12:22:18.056664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sample()","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:18.059059Z","iopub.execute_input":"2024-08-04T12:22:18.059534Z","iopub.status.idle":"2024-08-04T12:22:18.085634Z","shell.execute_reply.started":"2024-08-04T12:22:18.059503Z","shell.execute_reply":"2024-08-04T12:22:18.084406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:18.087743Z","iopub.execute_input":"2024-08-04T12:22:18.088106Z","iopub.status.idle":"2024-08-04T12:22:18.096088Z","shell.execute_reply.started":"2024-08-04T12:22:18.088073Z","shell.execute_reply":"2024-08-04T12:22:18.094898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(df.iloc[35338,1:].values.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:18.097805Z","iopub.execute_input":"2024-08-04T12:22:18.098167Z","iopub.status.idle":"2024-08-04T12:22:18.381912Z","shell.execute_reply.started":"2024-08-04T12:22:18.098139Z","shell.execute_reply":"2024-08-04T12:22:18.380767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = df.iloc[:,1:]\ny = df.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:18.383357Z","iopub.execute_input":"2024-08-04T12:22:18.383680Z","iopub.status.idle":"2024-08-04T12:22:18.389502Z","shell.execute_reply.started":"2024-08-04T12:22:18.383652Z","shell.execute_reply":"2024-08-04T12:22:18.388214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:18.391040Z","iopub.execute_input":"2024-08-04T12:22:18.391509Z","iopub.status.idle":"2024-08-04T12:22:19.006218Z","shell.execute_reply.started":"2024-08-04T12:22:18.391449Z","shell.execute_reply":"2024-08-04T12:22:19.005127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_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-08-04T12:22:19.007613Z","iopub.execute_input":"2024-08-04T12:22:19.008435Z","iopub.status.idle":"2024-08-04T12:22:19.250336Z","shell.execute_reply.started":"2024-08-04T12:22:19.008401Z","shell.execute_reply":"2024-08-04T12:22:19.249236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:19.255473Z","iopub.execute_input":"2024-08-04T12:22:19.255828Z","iopub.status.idle":"2024-08-04T12:22:19.262493Z","shell.execute_reply.started":"2024-08-04T12:22:19.255799Z","shell.execute_reply":"2024-08-04T12:22:19.261397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:19.263748Z","iopub.execute_input":"2024-08-04T12:22:19.264107Z","iopub.status.idle":"2024-08-04T12:22:19.522163Z","shell.execute_reply.started":"2024-08-04T12:22:19.264078Z","shell.execute_reply":"2024-08-04T12:22:19.521021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:19.523489Z","iopub.execute_input":"2024-08-04T12:22:19.523816Z","iopub.status.idle":"2024-08-04T12:22:19.528896Z","shell.execute_reply.started":"2024-08-04T12:22:19.523789Z","shell.execute_reply":"2024-08-04T12:22:19.527762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:19.530374Z","iopub.execute_input":"2024-08-04T12:22:19.530737Z","iopub.status.idle":"2024-08-04T12:22:19.694678Z","shell.execute_reply.started":"2024-08-04T12:22:19.530708Z","shell.execute_reply":"2024-08-04T12:22:19.693430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nstart = time.time()\nypred = knn.predict(x_test)\nprint(time.time()-start)","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:19.698828Z","iopub.execute_input":"2024-08-04T12:22:19.699367Z","iopub.status.idle":"2024-08-04T12:22:32.729224Z","shell.execute_reply.started":"2024-08-04T12:22:19.699307Z","shell.execute_reply":"2024-08-04T12:22:32.727787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(y_test,ypred)","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:32.730464Z","iopub.execute_input":"2024-08-04T12:22:32.730807Z","iopub.status.idle":"2024-08-04T12:22:32.740481Z","shell.execute_reply.started":"2024-08-04T12:22:32.730778Z","shell.execute_reply":"2024-08-04T12:22:32.739288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.score(x_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:32.741938Z","iopub.execute_input":"2024-08-04T12:22:32.742403Z","iopub.status.idle":"2024-08-04T12:22:44.638277Z","shell.execute_reply.started":"2024-08-04T12:22:32.742361Z","shell.execute_reply":"2024-08-04T12:22:44.637091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nsc = StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:44.639577Z","iopub.execute_input":"2024-08-04T12:22:44.639910Z","iopub.status.idle":"2024-08-04T12:22:44.645128Z","shell.execute_reply.started":"2024-08-04T12:22:44.639881Z","shell.execute_reply":"2024-08-04T12:22:44.643932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = sc.fit_transform(x_train)\nx_test = sc.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:44.646499Z","iopub.execute_input":"2024-08-04T12:22:44.646912Z","iopub.status.idle":"2024-08-04T12:22:45.141383Z","shell.execute_reply.started":"2024-08-04T12:22:44.646873Z","shell.execute_reply":"2024-08-04T12:22:45.140010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:45.142868Z","iopub.execute_input":"2024-08-04T12:22:45.143347Z","iopub.status.idle":"2024-08-04T12:22:45.148909Z","shell.execute_reply.started":"2024-08-04T12:22:45.143289Z","shell.execute_reply":"2024-08-04T12:22:45.147754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components=100)","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:45.150357Z","iopub.execute_input":"2024-08-04T12:22:45.150962Z","iopub.status.idle":"2024-08-04T12:22:45.162604Z","shell.execute_reply.started":"2024-08-04T12:22:45.150921Z","shell.execute_reply":"2024-08-04T12:22:45.161391Z"},"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-08-04T12:22:45.164225Z","iopub.execute_input":"2024-08-04T12:22:45.165213Z","iopub.status.idle":"2024-08-04T12:22:48.336037Z","shell.execute_reply.started":"2024-08-04T12:22:45.165174Z","shell.execute_reply":"2024-08-04T12:22:48.334530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_trf.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:48.337663Z","iopub.execute_input":"2024-08-04T12:22:48.338487Z","iopub.status.idle":"2024-08-04T12:22:48.346820Z","shell.execute_reply.started":"2024-08-04T12:22:48.338443Z","shell.execute_reply":"2024-08-04T12:22:48.345643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn = KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:48.348343Z","iopub.execute_input":"2024-08-04T12:22:48.349251Z","iopub.status.idle":"2024-08-04T12:22:48.357012Z","shell.execute_reply.started":"2024-08-04T12:22:48.349198Z","shell.execute_reply":"2024-08-04T12:22:48.355660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(x_train_trf,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:48.358597Z","iopub.execute_input":"2024-08-04T12:22:48.364694Z","iopub.status.idle":"2024-08-04T12:22:48.395229Z","shell.execute_reply.started":"2024-08-04T12:22:48.364634Z","shell.execute_reply":"2024-08-04T12:22:48.394046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.score(x_test_trf,y_test)","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:48.400710Z","iopub.execute_input":"2024-08-04T12:22:48.402016Z","iopub.status.idle":"2024-08-04T12:22:50.085355Z","shell.execute_reply.started":"2024-08-04T12:22:48.401967Z","shell.execute_reply":"2024-08-04T12:22:50.084078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = 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-08-04T12:22:50.086889Z","iopub.execute_input":"2024-08-04T12:22:50.087239Z","iopub.status.idle":"2024-08-04T12:22:51.052465Z","shell.execute_reply.started":"2024-08-04T12:22:50.087210Z","shell.execute_reply":"2024-08-04T12:22:51.051218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_trf","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:51.054198Z","iopub.execute_input":"2024-08-04T12:22:51.054955Z","iopub.status.idle":"2024-08-04T12:22:51.063949Z","shell.execute_reply.started":"2024-08-04T12:22:51.054909Z","shell.execute_reply":"2024-08-04T12:22:51.062765Z"},"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(df,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-08-04T12:22:51.065840Z","iopub.execute_input":"2024-08-04T12:22:51.066819Z","iopub.status.idle":"2024-08-04T12:22:53.710368Z","shell.execute_reply.started":"2024-08-04T12:22:51.066768Z","shell.execute_reply":"2024-08-04T12:22:53.709118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = 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-08-04T12:22:53.711974Z","iopub.execute_input":"2024-08-04T12:22:53.712408Z","iopub.status.idle":"2024-08-04T12:22:54.732648Z","shell.execute_reply.started":"2024-08-04T12:22:53.712370Z","shell.execute_reply":"2024-08-04T12:22:54.731381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_trf","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:54.734551Z","iopub.execute_input":"2024-08-04T12:22:54.735246Z","iopub.status.idle":"2024-08-04T12:22:54.746154Z","shell.execute_reply.started":"2024-08-04T12:22:54.735199Z","shell.execute_reply":"2024-08-04T12:22:54.744587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_trf = y_train.astype(str)\nfig1  = px.scatter_3d(df,x=x_train_trf[:,0],\n                   y = x_train_trf[:,1],\n                      z = x_train_trf[:,2],\n                   color= y_train_trf)\nfig1.update_layout(\nmargin = dict(l=20,r=20,t=20,b=20))\nfig1.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:54.754847Z","iopub.execute_input":"2024-08-04T12:22:54.755907Z","iopub.status.idle":"2024-08-04T12:22:54.987190Z","shell.execute_reply.started":"2024-08-04T12:22:54.755856Z","shell.execute_reply":"2024-08-04T12:22:54.985920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:54.988661Z","iopub.execute_input":"2024-08-04T12:22:54.989161Z","iopub.status.idle":"2024-08-04T12:22:54.996738Z","shell.execute_reply.started":"2024-08-04T12:22:54.989120Z","shell.execute_reply":"2024-08-04T12:22:54.995423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:54.998368Z","iopub.execute_input":"2024-08-04T12:22:54.998856Z","iopub.status.idle":"2024-08-04T12:22:55.011111Z","shell.execute_reply.started":"2024-08-04T12:22:54.998824Z","shell.execute_reply":"2024-08-04T12:22:55.010091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08-04T12:22:55.012580Z","iopub.execute_input":"2024-08-04T12:22:55.012922Z","iopub.status.idle":"2024-08-04T12:22:59.228850Z","shell.execute_reply.started":"2024-08-04T12:22:55.012893Z","shell.execute_reply":"2024-08-04T12:22:59.227524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:59.231091Z","iopub.execute_input":"2024-08-04T12:22:59.232782Z","iopub.status.idle":"2024-08-04T12:22:59.266282Z","shell.execute_reply.started":"2024-08-04T12:22:59.232736Z","shell.execute_reply":"2024-08-04T12:22:59.265086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"execution":{"iopub.status.busy":"2024-08-04T12:22:59.268079Z","iopub.execute_input":"2024-08-04T12:22:59.268540Z","iopub.status.idle":"2024-08-04T12:22:59.551054Z","shell.execute_reply.started":"2024-08-04T12:22:59.268502Z","shell.execute_reply":"2024-08-04T12:22:59.549823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}