{"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":30684,"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-07-29T10:07:09.269329Z","iopub.execute_input":"2024-07-29T10:07:09.269711Z","iopub.status.idle":"2024-07-29T10:07:10.696242Z","shell.execute_reply.started":"2024-07-29T10:07:09.269681Z","shell.execute_reply":"2024-07-29T10:07:10.694751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:10.698480Z","iopub.execute_input":"2024-07-29T10:07:10.699103Z","iopub.status.idle":"2024-07-29T10:07:15.223134Z","shell.execute_reply.started":"2024-07-29T10:07:10.699060Z","shell.execute_reply":"2024-07-29T10:07:15.221761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:15.224559Z","iopub.execute_input":"2024-07-29T10:07:15.224945Z","iopub.status.idle":"2024-07-29T10:07:15.233542Z","shell.execute_reply.started":"2024-07-29T10:07:15.224913Z","shell.execute_reply":"2024-07-29T10:07:15.231940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:15.237341Z","iopub.execute_input":"2024-07-29T10:07:15.237841Z","iopub.status.idle":"2024-07-29T10:07:15.247076Z","shell.execute_reply.started":"2024-07-29T10:07:15.237800Z","shell.execute_reply":"2024-07-29T10:07:15.245301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(df.iloc[35827,1:].values.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:15.248659Z","iopub.execute_input":"2024-07-29T10:07:15.249054Z","iopub.status.idle":"2024-07-29T10:07:15.598361Z","shell.execute_reply.started":"2024-07-29T10:07:15.249022Z","shell.execute_reply":"2024-07-29T10:07:15.597202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=df.iloc[:,1:]\ny=df.iloc[:,0]\n","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:15.599628Z","iopub.execute_input":"2024-07-29T10:07:15.599941Z","iopub.status.idle":"2024-07-29T10:07:15.605571Z","shell.execute_reply.started":"2024-07-29T10:07:15.599914Z","shell.execute_reply":"2024-07-29T10:07:15.604446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:15.607362Z","iopub.execute_input":"2024-07-29T10:07:15.607871Z","iopub.status.idle":"2024-07-29T10:07:17.390655Z","shell.execute_reply.started":"2024-07-29T10:07:15.607832Z","shell.execute_reply":"2024-07-29T10:07:17.388964Z"},"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-07-29T10:07:17.392408Z","iopub.execute_input":"2024-07-29T10:07:17.392806Z","iopub.status.idle":"2024-07-29T10:07:17.666066Z","shell.execute_reply.started":"2024-07-29T10:07:17.392773Z","shell.execute_reply":"2024-07-29T10:07:17.664448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:17.667554Z","iopub.execute_input":"2024-07-29T10:07:17.667959Z","iopub.status.idle":"2024-07-29T10:07:17.689904Z","shell.execute_reply.started":"2024-07-29T10:07:17.667928Z","shell.execute_reply":"2024-07-29T10:07:17.688726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:17.695592Z","iopub.execute_input":"2024-07-29T10:07:17.695951Z","iopub.status.idle":"2024-07-29T10:07:17.705428Z","shell.execute_reply.started":"2024-07-29T10:07:17.695923Z","shell.execute_reply":"2024-07-29T10:07:17.704286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\nknn=KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:17.706720Z","iopub.execute_input":"2024-07-29T10:07:17.707088Z","iopub.status.idle":"2024-07-29T10:07:18.029554Z","shell.execute_reply.started":"2024-07-29T10:07:17.707060Z","shell.execute_reply":"2024-07-29T10:07:18.028392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:18.031240Z","iopub.execute_input":"2024-07-29T10:07:18.031717Z","iopub.status.idle":"2024-07-29T10:07:18.200340Z","shell.execute_reply.started":"2024-07-29T10:07:18.031653Z","shell.execute_reply":"2024-07-29T10:07:18.198772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=knn.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:18.201787Z","iopub.execute_input":"2024-07-29T10:07:18.202157Z","iopub.status.idle":"2024-07-29T10:07:32.157952Z","shell.execute_reply.started":"2024-07-29T10:07:18.202127Z","shell.execute_reply":"2024-07-29T10:07:32.156486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:32.159255Z","iopub.execute_input":"2024-07-29T10:07:32.159751Z","iopub.status.idle":"2024-07-29T10:07:32.167134Z","shell.execute_reply.started":"2024-07-29T10:07:32.159709Z","shell.execute_reply":"2024-07-29T10:07:32.164562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(accuracy_score(y_pred,y_test))","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:32.168993Z","iopub.execute_input":"2024-07-29T10:07:32.169496Z","iopub.status.idle":"2024-07-29T10:07:32.186892Z","shell.execute_reply.started":"2024-07-29T10:07:32.169454Z","shell.execute_reply":"2024-07-29T10:07:32.185330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nss=StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:32.189436Z","iopub.execute_input":"2024-07-29T10:07:32.189922Z","iopub.status.idle":"2024-07-29T10:07:32.200217Z","shell.execute_reply.started":"2024-07-29T10:07:32.189880Z","shell.execute_reply":"2024-07-29T10:07:32.198478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=ss.fit_transform(x_train)\nx_test=ss.fit_transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:32.202387Z","iopub.execute_input":"2024-07-29T10:07:32.202906Z","iopub.status.idle":"2024-07-29T10:07:32.839501Z","shell.execute_reply.started":"2024-07-29T10:07:32.202862Z","shell.execute_reply":"2024-07-29T10:07:32.838213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:32.841336Z","iopub.execute_input":"2024-07-29T10:07:32.841867Z","iopub.status.idle":"2024-07-29T10:07:32.848057Z","shell.execute_reply.started":"2024-07-29T10:07:32.841828Z","shell.execute_reply":"2024-07-29T10:07:32.846533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca=PCA(n_components=2)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:32.849768Z","iopub.execute_input":"2024-07-29T10:07:32.850278Z","iopub.status.idle":"2024-07-29T10:07:32.872905Z","shell.execute_reply.started":"2024-07-29T10:07:32.850237Z","shell.execute_reply":"2024-07-29T10:07:32.871163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_tr=pca.fit_transform(x_train)\nx_test_tr=pca.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:32.874715Z","iopub.execute_input":"2024-07-29T10:07:32.875213Z","iopub.status.idle":"2024-07-29T10:07:34.033541Z","shell.execute_reply.started":"2024-07-29T10:07:32.875170Z","shell.execute_reply":"2024-07-29T10:07:34.031049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_tr.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:34.035954Z","iopub.execute_input":"2024-07-29T10:07:34.037015Z","iopub.status.idle":"2024-07-29T10:07:34.047825Z","shell.execute_reply.started":"2024-07-29T10:07:34.036957Z","shell.execute_reply":"2024-07-29T10:07:34.045808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn=KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:34.050815Z","iopub.execute_input":"2024-07-29T10:07:34.052017Z","iopub.status.idle":"2024-07-29T10:07:34.060970Z","shell.execute_reply.started":"2024-07-29T10:07:34.051959Z","shell.execute_reply":"2024-07-29T10:07:34.059270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.fit(x_train_tr,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:34.063748Z","iopub.execute_input":"2024-07-29T10:07:34.064367Z","iopub.status.idle":"2024-07-29T10:07:34.162445Z","shell.execute_reply.started":"2024-07-29T10:07:34.064308Z","shell.execute_reply":"2024-07-29T10:07:34.159215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_p=knn.predict(x_test_tr)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:34.165292Z","iopub.execute_input":"2024-07-29T10:07:34.165921Z","iopub.status.idle":"2024-07-29T10:07:34.767778Z","shell.execute_reply.started":"2024-07-29T10:07:34.165834Z","shell.execute_reply":"2024-07-29T10:07:34.766291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(accuracy_score(y_p,y_test))","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:34.769980Z","iopub.execute_input":"2024-07-29T10:07:34.770475Z","iopub.status.idle":"2024-07-29T10:07:34.781077Z","shell.execute_reply.started":"2024-07-29T10:07:34.770434Z","shell.execute_reply":"2024-07-29T10:07:34.779336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_tr","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:34.782601Z","iopub.execute_input":"2024-07-29T10:07:34.783011Z","iopub.status.idle":"2024-07-29T10:07:34.804808Z","shell.execute_reply.started":"2024-07-29T10:07:34.782979Z","shell.execute_reply":"2024-07-29T10:07:34.803225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px\ny_train_tr = y_train.astype(str)\nfig = px.scatter(x=x_train_tr[:,0],\n                 y=x_train_tr[:,1],\n                 color=y_train_tr,\n                 color_discrete_sequence=px.colors.qualitative.G10\n                )\nfig.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:34.806804Z","iopub.execute_input":"2024-07-29T10:07:34.807297Z","iopub.status.idle":"2024-07-29T10:07:38.540045Z","shell.execute_reply.started":"2024-07-29T10:07:34.807247Z","shell.execute_reply":"2024-07-29T10:07:38.538569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca=PCA(n_components=3)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:38.547986Z","iopub.execute_input":"2024-07-29T10:07:38.548516Z","iopub.status.idle":"2024-07-29T10:07:38.561526Z","shell.execute_reply.started":"2024-07-29T10:07:38.548473Z","shell.execute_reply":"2024-07-29T10:07:38.559414Z"},"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-07-29T10:07:38.563720Z","iopub.execute_input":"2024-07-29T10:07:38.564515Z","iopub.status.idle":"2024-07-29T10:07:39.753263Z","shell.execute_reply.started":"2024-07-29T10:07:38.564471Z","shell.execute_reply":"2024-07-29T10:07:39.751602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_trf.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:39.755477Z","iopub.execute_input":"2024-07-29T10:07:39.756416Z","iopub.status.idle":"2024-07-29T10:07:39.764337Z","shell.execute_reply.started":"2024-07-29T10:07:39.756370Z","shell.execute_reply":"2024-07-29T10:07:39.763170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn1=KNeighborsClassifier()","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:39.766452Z","iopub.execute_input":"2024-07-29T10:07:39.767337Z","iopub.status.idle":"2024-07-29T10:07:39.777980Z","shell.execute_reply.started":"2024-07-29T10:07:39.767292Z","shell.execute_reply":"2024-07-29T10:07:39.776366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn1.fit(x_train_trf,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:39.782123Z","iopub.execute_input":"2024-07-29T10:07:39.783527Z","iopub.status.idle":"2024-07-29T10:07:39.857518Z","shell.execute_reply.started":"2024-07-29T10:07:39.783460Z","shell.execute_reply":"2024-07-29T10:07:39.856199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_prd=knn1.predict(x_test_trf)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:39.859761Z","iopub.execute_input":"2024-07-29T10:07:39.860659Z","iopub.status.idle":"2024-07-29T10:07:40.455535Z","shell.execute_reply.started":"2024-07-29T10:07:39.860614Z","shell.execute_reply":"2024-07-29T10:07:40.454225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(accuracy_score(y_prd,y_test))","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:40.456993Z","iopub.execute_input":"2024-07-29T10:07:40.457373Z","iopub.status.idle":"2024-07-29T10:07:40.467894Z","shell.execute_reply.started":"2024-07-29T10:07:40.457340Z","shell.execute_reply":"2024-07-29T10:07:40.465878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px\ny_train_tr = 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.astype('str'))\nfig.update_layout(margin=dict(l=10,r=20,t=20,b=20))\n\nfig.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:40.469835Z","iopub.execute_input":"2024-07-29T10:07:40.470368Z","iopub.status.idle":"2024-07-29T10:07:40.751465Z","shell.execute_reply.started":"2024-07-29T10:07:40.470292Z","shell.execute_reply":"2024-07-29T10:07:40.750031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Eigen_values\npca.explained_variance_","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:40.753603Z","iopub.execute_input":"2024-07-29T10:07:40.754042Z","iopub.status.idle":"2024-07-29T10:07:40.763067Z","shell.execute_reply.started":"2024-07-29T10:07:40.754008Z","shell.execute_reply":"2024-07-29T10:07:40.761775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape","metadata":{"execution":{"iopub.status.busy":"2024-07-29T10:07:40.764751Z","iopub.execute_input":"2024-07-29T10:07:40.765362Z","iopub.status.idle":"2024-07-29T10:07:40.782733Z","shell.execute_reply.started":"2024-07-29T10:07:40.765327Z","shell.execute_reply":"2024-07-29T10:07:40.781397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca=PCA(n_components=None)\nx_train_trf1=pca.fit_transform(x_train)\nx_test_trf1=pca.transform(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-04-22T08:35:26.760625Z","iopub.execute_input":"2024-04-22T08:35:26.760909Z","iopub.status.idle":"2024-04-22T08:35:31.057303Z","shell.execute_reply.started":"2024-04-22T08:35:26.760885Z","shell.execute_reply":"2024-04-22T08:35:31.055761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-22T08:35:31.059699Z","iopub.execute_input":"2024-04-22T08:35:31.060829Z","iopub.status.idle":"2024-04-22T08:35:31.071172Z","shell.execute_reply.started":"2024-04-22T08:35:31.060769Z","shell.execute_reply":"2024-04-22T08:35:31.069223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-22T08:35:31.073331Z","iopub.execute_input":"2024-04-22T08:35:31.074691Z","iopub.status.idle":"2024-04-22T08:35:31.084844Z","shell.execute_reply.started":"2024-04-22T08:35:31.074623Z","shell.execute_reply":"2024-04-22T08:35:31.083210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"execution":{"iopub.status.busy":"2024-04-22T08:35:31.102650Z","iopub.execute_input":"2024-04-22T08:35:31.107742Z","iopub.status.idle":"2024-04-22T08:35:31.147571Z","shell.execute_reply.started":"2024-04-22T08:35:31.107665Z","shell.execute_reply":"2024-04-22T08:35:31.145925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"execution":{"iopub.status.busy":"2024-04-22T08:35:31.154597Z","iopub.execute_input":"2024-04-22T08:35:31.159563Z","iopub.status.idle":"2024-04-22T08:35:31.449686Z","shell.execute_reply.started":"2024-04-22T08:35:31.159482Z","shell.execute_reply":"2024-04-22T08:35:31.448483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}