{"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":30626,"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-01-02T16:11:51.941509Z","iopub.execute_input":"2024-01-02T16:11:51.942136Z","iopub.status.idle":"2024-01-02T16:11:51.962772Z","shell.execute_reply.started":"2024-01-02T16:11:51.942096Z","shell.execute_reply":"2024-01-02T16:11:51.961341Z"},"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-01-02T16:11:51.964995Z","iopub.execute_input":"2024-01-02T16:11:51.965411Z","iopub.status.idle":"2024-01-02T16:11:55.380779Z","shell.execute_reply.started":"2024-01-02T16:11:51.965380Z","shell.execute_reply":"2024-01-02T16:11:55.379766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sample()","metadata":{"execution":{"iopub.status.busy":"2024-01-02T16:11:55.382520Z","iopub.execute_input":"2024-01-02T16:11:55.382903Z","iopub.status.idle":"2024-01-02T16:11:55.408500Z","shell.execute_reply.started":"2024-01-02T16:11:55.382872Z","shell.execute_reply":"2024-01-02T16:11:55.407034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-01-02T16:11:55.411242Z","iopub.execute_input":"2024-01-02T16:11:55.411710Z","iopub.status.idle":"2024-01-02T16:11:55.418920Z","shell.execute_reply.started":"2024-01-02T16:11:55.411676Z","shell.execute_reply":"2024-01-02T16:11:55.417406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(df.iloc[32733,1:].values.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2024-01-02T16:11:55.421195Z","iopub.execute_input":"2024-01-02T16:11:55.421707Z","iopub.status.idle":"2024-01-02T16:11:55.665133Z","shell.execute_reply.started":"2024-01-02T16:11:55.421657Z","shell.execute_reply":"2024-01-02T16:11:55.663911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2024-01-02T16:11:55.666816Z","iopub.execute_input":"2024-01-02T16:11:55.667236Z","iopub.status.idle":"2024-01-02T16:11:55.678454Z","shell.execute_reply.started":"2024-01-02T16:11:55.667195Z","shell.execute_reply":"2024-01-02T16:11:55.676860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX = df.iloc[:,1:]\ny = df.iloc[:,0]\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-01-02T16:11:55.680260Z","iopub.execute_input":"2024-01-02T16:11:55.680799Z","iopub.status.idle":"2024-01-02T16:11:55.947480Z","shell.execute_reply.started":"2024-01-02T16:11:55.680752Z","shell.execute_reply":"2024-01-02T16:11:55.946147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()\n\nX_train = scaler.fit_transform(X_train)\nX_test = scaler.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-01-02T16:11:55.949082Z","iopub.execute_input":"2024-01-02T16:11:55.949471Z","iopub.status.idle":"2024-01-02T16:11:56.467295Z","shell.execute_reply.started":"2024-01-02T16:11:55.949440Z","shell.execute_reply":"2024-01-02T16:11:56.466251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca = PCA(n_components=200)","metadata":{"execution":{"iopub.status.busy":"2024-01-02T16:11:56.470887Z","iopub.execute_input":"2024-01-02T16:11:56.471257Z","iopub.status.idle":"2024-01-02T16:11:56.476200Z","shell.execute_reply.started":"2024-01-02T16:11:56.471227Z","shell.execute_reply":"2024-01-02T16:11:56.475107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train1 = pca.fit_transform(X_train)\nX_test1 = pca.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-01-02T16:11:56.478042Z","iopub.execute_input":"2024-01-02T16:11:56.478447Z","iopub.status.idle":"2024-01-02T16:12:02.933534Z","shell.execute_reply.started":"2024-01-02T16:11:56.478416Z","shell.execute_reply":"2024-01-02T16:12:02.932273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train1.shape,X_train.shape)","metadata":{"execution":{"iopub.status.busy":"2024-01-02T16:12:02.934860Z","iopub.execute_input":"2024-01-02T16:12:02.935613Z","iopub.status.idle":"2024-01-02T16:12:02.941474Z","shell.execute_reply.started":"2024-01-02T16:12:02.935577Z","shell.execute_reply":"2024-01-02T16:12:02.940256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nlg = LogisticRegression(max_iter = 400) ","metadata":{"execution":{"iopub.status.busy":"2024-01-02T16:12:02.943434Z","iopub.execute_input":"2024-01-02T16:12:02.943798Z","iopub.status.idle":"2024-01-02T16:12:02.958151Z","shell.execute_reply.started":"2024-01-02T16:12:02.943761Z","shell.execute_reply":"2024-01-02T16:12:02.956673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lg.fit(X_train1,y_train)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-02T16:12:02.960243Z","iopub.execute_input":"2024-01-02T16:12:02.960823Z","iopub.status.idle":"2024-01-02T16:12:19.776980Z","shell.execute_reply.started":"2024-01-02T16:12:02.960783Z","shell.execute_reply":"2024-01-02T16:12:19.775646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = lg.predict(X_test1)","metadata":{"execution":{"iopub.status.busy":"2024-01-02T16:12:19.778957Z","iopub.execute_input":"2024-01-02T16:12:19.779349Z","iopub.status.idle":"2024-01-02T16:12:19.791657Z","shell.execute_reply.started":"2024-01-02T16:12:19.779317Z","shell.execute_reply":"2024-01-02T16:12:19.790285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\n\nprint(accuracy_score(y_test,y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-01-02T16:12:19.793612Z","iopub.execute_input":"2024-01-02T16:12:19.793966Z","iopub.status.idle":"2024-01-02T16:12:19.802728Z","shell.execute_reply.started":"2024-01-02T16:12:19.793936Z","shell.execute_reply":"2024-01-02T16:12:19.801130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\n\nknn = KNeighborsClassifier()\nknn.fit(X_train1,y_train)\ny_pred2 = knn.predict(X_test1)","metadata":{"execution":{"iopub.status.busy":"2024-01-02T16:12:19.804290Z","iopub.execute_input":"2024-01-02T16:12:19.804650Z","iopub.status.idle":"2024-01-02T16:12:22.731850Z","shell.execute_reply.started":"2024-01-02T16:12:19.804620Z","shell.execute_reply":"2024-01-02T16:12:22.730611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(accuracy_score(y_test,y_pred2))","metadata":{"execution":{"iopub.status.busy":"2024-01-02T16:12:22.733369Z","iopub.execute_input":"2024-01-02T16:12:22.733754Z","iopub.status.idle":"2024-01-02T16:12:22.741948Z","shell.execute_reply.started":"2024-01-02T16:12:22.733722Z","shell.execute_reply":"2024-01-02T16:12:22.740775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# accuracy =[]\n# for i in range(1,784):\n    \n#     pca = PCA(n_components = i)\n#     X_train1 = pca.fit_transform(X_train)\n#     X_test1 = pca.transform(X_test)\n#     knn = KNeighborsClassifier()\n#     knn.fit(X_train1,y_train)\n#     y_pred2 = knn.predict(X_test1)\n    \n#     print(accuracy_score(y_test,y_pred2))\n","metadata":{"execution":{"iopub.status.busy":"2024-01-02T16:12:22.743604Z","iopub.execute_input":"2024-01-02T16:12:22.743982Z","iopub.status.idle":"2024-01-02T16:12:22.752561Z","shell.execute_reply.started":"2024-01-02T16:12:22.743950Z","shell.execute_reply":"2024-01-02T16:12:22.751284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transforming in 3D\npca = PCA(n_components=3)\nX_train1 = pca.fit_transform(X_train)\nX_test1 = pca.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-01-02T16:12:22.754092Z","iopub.execute_input":"2024-01-02T16:12:22.754503Z","iopub.status.idle":"2024-01-02T16:12:24.501270Z","shell.execute_reply.started":"2024-01-02T16:12:22.754470Z","shell.execute_reply":"2024-01-02T16:12:24.499789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px\ny_train1 = y_train.astype(str)\nfig = px.scatter_3d(df, x=X_train1[:,0], y=X_train1[:,1], z=X_train1[:,2],\n              color=y_train1)\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-01-02T16:12:24.502731Z","iopub.execute_input":"2024-01-02T16:12:24.503080Z","iopub.status.idle":"2024-01-02T16:12:24.698790Z","shell.execute_reply.started":"2024-01-02T16:12:24.503051Z","shell.execute_reply":"2024-01-02T16:12:24.696971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}