{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.10","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":30096,"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-10-07T17:10:42.207324Z","iopub.execute_input":"2024-10-07T17:10:42.207789Z","iopub.status.idle":"2024-10-07T17:10:42.22563Z","shell.execute_reply.started":"2024-10-07T17:10:42.207741Z","shell.execute_reply":"2024-10-07T17:10:42.2244Z"},"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-10-07T17:10:43.968988Z","iopub.execute_input":"2024-10-07T17:10:43.969426Z","iopub.status.idle":"2024-10-07T17:10:47.263498Z","shell.execute_reply.started":"2024-10-07T17:10:43.96939Z","shell.execute_reply":"2024-10-07T17:10:47.262375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:10:47.265458Z","iopub.execute_input":"2024-10-07T17:10:47.265841Z","iopub.status.idle":"2024-10-07T17:10:47.272408Z","shell.execute_reply.started":"2024-10-07T17:10:47.265794Z","shell.execute_reply":"2024-10-07T17:10:47.271219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sample()","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:10:48.649887Z","iopub.execute_input":"2024-10-07T17:10:48.650321Z","iopub.status.idle":"2024-10-07T17:10:48.672404Z","shell.execute_reply.started":"2024-10-07T17:10:48.650283Z","shell.execute_reply":"2024-10-07T17:10:48.671218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:10:55.909648Z","iopub.execute_input":"2024-10-07T17:10:55.910077Z","iopub.status.idle":"2024-10-07T17:10:55.914674Z","shell.execute_reply.started":"2024-10-07T17:10:55.910031Z","shell.execute_reply":"2024-10-07T17:10:55.913584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(df.iloc[13051,1:].values.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:10:58.240275Z","iopub.execute_input":"2024-10-07T17:10:58.24075Z","iopub.status.idle":"2024-10-07T17:10:58.405839Z","shell.execute_reply.started":"2024-10-07T17:10:58.240713Z","shell.execute_reply":"2024-10-07T17:10:58.404575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df.iloc[:,1:]\ny = df.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:11:01.227668Z","iopub.execute_input":"2024-10-07T17:11:01.228274Z","iopub.status.idle":"2024-10-07T17:11:01.234907Z","shell.execute_reply.started":"2024-10-07T17:11:01.22822Z","shell.execute_reply":"2024-10-07T17:11:01.233698Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:11:10.442514Z","iopub.execute_input":"2024-10-07T17:11:10.442903Z","iopub.status.idle":"2024-10-07T17:11:10.752015Z","shell.execute_reply.started":"2024-10-07T17:11:10.442869Z","shell.execute_reply":"2024-10-07T17:11:10.750757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:11:12.031856Z","iopub.execute_input":"2024-10-07T17:11:12.032292Z","iopub.status.idle":"2024-10-07T17:11:12.037136Z","shell.execute_reply.started":"2024-10-07T17:11:12.032254Z","shell.execute_reply":"2024-10-07T17:11:12.035824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:11:13.828386Z","iopub.execute_input":"2024-10-07T17:11:13.828785Z","iopub.status.idle":"2024-10-07T17:11:13.835143Z","shell.execute_reply.started":"2024-10-07T17:11:13.828749Z","shell.execute_reply":"2024-10-07T17:11:13.833914Z"},"trusted":true},"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-10-07T17:11:18.454574Z","iopub.execute_input":"2024-10-07T17:11:18.45515Z","iopub.status.idle":"2024-10-07T17:11:19.414145Z","shell.execute_reply.started":"2024-10-07T17:11:18.455104Z","shell.execute_reply":"2024-10-07T17:11:19.412959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import accuracy_score,confusion_matrix","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:11:23.582484Z","iopub.execute_input":"2024-10-07T17:11:23.582959Z","iopub.status.idle":"2024-10-07T17:11:23.588797Z","shell.execute_reply.started":"2024-10-07T17:11:23.582914Z","shell.execute_reply":"2024-10-07T17:11:23.587153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nfrom sklearn.preprocessing import LabelEncoder","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:11:26.761003Z","iopub.execute_input":"2024-10-07T17:11:26.761458Z","iopub.status.idle":"2024-10-07T17:11:26.767618Z","shell.execute_reply.started":"2024-10-07T17:11:26.761419Z","shell.execute_reply":"2024-10-07T17:11:26.765916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:11:27.598429Z","iopub.execute_input":"2024-10-07T17:11:27.598854Z","iopub.status.idle":"2024-10-07T17:11:27.623018Z","shell.execute_reply.started":"2024-10-07T17:11:27.598815Z","shell.execute_reply":"2024-10-07T17:11:27.621685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoder = LabelEncoder()\ndf['label'] = encoder.fit_transform(df['label'])","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:11:29.160111Z","iopub.execute_input":"2024-10-07T17:11:29.160594Z","iopub.status.idle":"2024-10-07T17:11:29.170228Z","shell.execute_reply.started":"2024-10-07T17:11:29.160548Z","shell.execute_reply":"2024-10-07T17:11:29.168769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# PCA \nfrom sklearn.decomposition import PCA\npca = PCA(n_components=2)","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:11:40.844725Z","iopub.execute_input":"2024-10-07T17:11:40.845157Z","iopub.status.idle":"2024-10-07T17:11:40.850399Z","shell.execute_reply.started":"2024-10-07T17:11:40.845114Z","shell.execute_reply":"2024-10-07T17:11:40.849199Z"},"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-10-07T17:11:46.926661Z","iopub.execute_input":"2024-10-07T17:11:46.927064Z","iopub.status.idle":"2024-10-07T17:11:49.612684Z","shell.execute_reply.started":"2024-10-07T17:11:46.927004Z","shell.execute_reply":"2024-10-07T17:11:49.61084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:11:52.943046Z","iopub.execute_input":"2024-10-07T17:11:52.943458Z","iopub.status.idle":"2024-10-07T17:11:52.950905Z","shell.execute_reply.started":"2024-10-07T17:11:52.943424Z","shell.execute_reply":"2024-10-07T17:11:52.949587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:13:19.421518Z","iopub.execute_input":"2024-10-07T17:13:19.421928Z","iopub.status.idle":"2024-10-07T17:13:19.429507Z","shell.execute_reply.started":"2024-10-07T17:13:19.421892Z","shell.execute_reply":"2024-10-07T17:13:19.428301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:11:58.645753Z","iopub.execute_input":"2024-10-07T17:11:58.646204Z","iopub.status.idle":"2024-10-07T17:11:58.654815Z","shell.execute_reply.started":"2024-10-07T17:11:58.646165Z","shell.execute_reply":"2024-10-07T17:11:58.653455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sample()","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:12:05.343867Z","iopub.execute_input":"2024-10-07T17:12:05.344359Z","iopub.status.idle":"2024-10-07T17:12:05.369286Z","shell.execute_reply.started":"2024-10-07T17:12:05.344319Z","shell.execute_reply":"2024-10-07T17:12:05.367786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = LogisticRegression(multi_class='multinomial',solver='saga')","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:15:42.65524Z","iopub.execute_input":"2024-10-07T17:15:42.655658Z","iopub.status.idle":"2024-10-07T17:15:42.66058Z","shell.execute_reply.started":"2024-10-07T17:15:42.655612Z","shell.execute_reply":"2024-10-07T17:15:42.659502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf.fit(X_train_trf,y_train)","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:15:45.413625Z","iopub.execute_input":"2024-10-07T17:15:45.414042Z","iopub.status.idle":"2024-10-07T17:15:48.100459Z","shell.execute_reply.started":"2024-10-07T17:15:45.414002Z","shell.execute_reply":"2024-10-07T17:15:48.099395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = clf.predict(X_test_trf)","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:17:36.49779Z","iopub.execute_input":"2024-10-07T17:17:36.498288Z","iopub.status.idle":"2024-10-07T17:17:36.504312Z","shell.execute_reply.started":"2024-10-07T17:17:36.498246Z","shell.execute_reply":"2024-10-07T17:17:36.503193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(accuracy_score(y_test,y_pred))","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:18:42.040346Z","iopub.execute_input":"2024-10-07T17:18:42.040795Z","iopub.status.idle":"2024-10-07T17:18:42.048407Z","shell.execute_reply.started":"2024-10-07T17:18:42.040755Z","shell.execute_reply":"2024-10-07T17:18:42.047114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(1,785):\n    pca = PCA(n_components=i)\n    X_train_trf = pca.fit_transform(X_train)\n    X_test_trf = pca.transform(X_test)\n    \n    clf = LogisticRegression(multi_class='multinomial',solver='saga',max_iter=2000)\n    \n    clf.fit(X_train_trf,y_train)\n    \n    y_pred = clf.predict(X_test_trf)\n    \n    print(accuracy_score(y_test,y_pred))\n    ","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:22:39.473691Z","iopub.execute_input":"2024-10-07T17:22:39.474174Z","iopub.status.idle":"2024-10-07T17:23:34.463429Z","shell.execute_reply.started":"2024-10-07T17:22:39.474127Z","shell.execute_reply":"2024-10-07T17:23:34.459024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:23:40.653655Z","iopub.execute_input":"2024-10-07T17:23:40.65411Z","iopub.status.idle":"2024-10-07T17:23:40.66182Z","shell.execute_reply.started":"2024-10-07T17:23:40.654048Z","shell.execute_reply":"2024-10-07T17:23:40.660519Z"},"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(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-10-07T17:23:45.359386Z","iopub.execute_input":"2024-10-07T17:23:45.359804Z","iopub.status.idle":"2024-10-07T17:23:45.824754Z","shell.execute_reply.started":"2024-10-07T17:23:45.359767Z","shell.execute_reply":"2024-10-07T17:23:45.823456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transforming in 3D\npca = 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-10-07T17:23:49.332568Z","iopub.execute_input":"2024-10-07T17:23:49.332977Z","iopub.status.idle":"2024-10-07T17:23:52.217867Z","shell.execute_reply.started":"2024-10-07T17:23:49.33294Z","shell.execute_reply":"2024-10-07T17:23:52.216036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_trf","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:23:52.220836Z","iopub.execute_input":"2024-10-07T17:23:52.221499Z","iopub.status.idle":"2024-10-07T17:23:52.23178Z","shell.execute_reply.started":"2024-10-07T17:23:52.221439Z","shell.execute_reply":"2024-10-07T17:23:52.23025Z"},"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_3d(df, x=X_train_trf[:,0], y=X_train_trf[:,1], z=X_train_trf[:,2],\n              color=y_train_trf)\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-10-07T17:23:55.102305Z","iopub.execute_input":"2024-10-07T17:23:55.102718Z","iopub.status.idle":"2024-10-07T17:23:55.799138Z","shell.execute_reply.started":"2024-10-07T17:23:55.102684Z","shell.execute_reply":"2024-10-07T17:23:55.797651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_\n# Eigen values","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:23:59.516456Z","iopub.execute_input":"2024-10-07T17:23:59.516852Z","iopub.status.idle":"2024-10-07T17:23:59.523923Z","shell.execute_reply.started":"2024-10-07T17:23:59.516809Z","shell.execute_reply":"2024-10-07T17:23:59.522652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape\n# Eigen vectors","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:24:01.740519Z","iopub.execute_input":"2024-10-07T17:24:01.740944Z","iopub.status.idle":"2024-10-07T17:24:01.747738Z","shell.execute_reply.started":"2024-10-07T17:24:01.740892Z","shell.execute_reply":"2024-10-07T17:24:01.746508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:24:03.050871Z","iopub.execute_input":"2024-10-07T17:24:03.051366Z","iopub.status.idle":"2024-10-07T17:24:03.060827Z","shell.execute_reply.started":"2024-10-07T17:24:03.051325Z","shell.execute_reply":"2024-10-07T17:24:03.059263Z"},"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-10-07T17:24:10.120344Z","iopub.execute_input":"2024-10-07T17:24:10.12099Z","iopub.status.idle":"2024-10-07T17:24:14.699754Z","shell.execute_reply.started":"2024-10-07T17:24:10.120929Z","shell.execute_reply":"2024-10-07T17:24:14.69824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:24:14.702323Z","iopub.execute_input":"2024-10-07T17:24:14.703301Z","iopub.status.idle":"2024-10-07T17:24:14.71423Z","shell.execute_reply.started":"2024-10-07T17:24:14.703224Z","shell.execute_reply":"2024-10-07T17:24:14.712167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:24:18.436301Z","iopub.execute_input":"2024-10-07T17:24:18.436779Z","iopub.status.idle":"2024-10-07T17:24:18.444956Z","shell.execute_reply.started":"2024-10-07T17:24:18.436741Z","shell.execute_reply":"2024-10-07T17:24:18.443459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:24:24.205328Z","iopub.execute_input":"2024-10-07T17:24:24.205845Z","iopub.status.idle":"2024-10-07T17:24:24.227168Z","shell.execute_reply.started":"2024-10-07T17:24:24.205806Z","shell.execute_reply":"2024-10-07T17:24:24.225488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"execution":{"iopub.status.busy":"2024-10-07T17:24:26.66884Z","iopub.execute_input":"2024-10-07T17:24:26.66935Z","iopub.status.idle":"2024-10-07T17:24:26.857897Z","shell.execute_reply.started":"2024-10-07T17:24:26.669306Z","shell.execute_reply":"2024-10-07T17:24:26.856466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}