{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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":"2023-05-18T19:27:24.764864Z","iopub.execute_input":"2023-05-18T19:27:24.765320Z","iopub.status.idle":"2023-05-18T19:27:24.774749Z","shell.execute_reply.started":"2023-05-18T19:27:24.765288Z","shell.execute_reply":"2023-05-18T19:27:24.773654Z"},"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":"2023-05-18T19:27:24.777031Z","iopub.execute_input":"2023-05-18T19:27:24.777710Z","iopub.status.idle":"2023-05-18T19:27:29.850001Z","shell.execute_reply.started":"2023-05-18T19:27:24.777671Z","shell.execute_reply":"2023-05-18T19:27:29.848597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-18T19:27:29.851670Z","iopub.execute_input":"2023-05-18T19:27:29.852069Z","iopub.status.idle":"2023-05-18T19:27:29.861611Z","shell.execute_reply.started":"2023-05-18T19:27:29.852029Z","shell.execute_reply":"2023-05-18T19:27:29.860204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sample()","metadata":{"execution":{"iopub.status.busy":"2023-05-18T19:27:29.864709Z","iopub.execute_input":"2023-05-18T19:27:29.865196Z","iopub.status.idle":"2023-05-18T19:27:29.918905Z","shell.execute_reply.started":"2023-05-18T19:27:29.865158Z","shell.execute_reply":"2023-05-18T19:27:29.917649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df.iloc[:,1:]\ny = df.iloc[:,]","metadata":{"execution":{"iopub.status.busy":"2023-05-18T19:27:29.920359Z","iopub.execute_input":"2023-05-18T19:27:29.920739Z","iopub.status.idle":"2023-05-18T19:27:29.926131Z","shell.execute_reply.started":"2023-05-18T19:27:29.920705Z","shell.execute_reply":"2023-05-18T19:27:29.924813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.imshow(df.iloc[1302,1:].values.reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2023-05-18T19:27:29.928100Z","iopub.execute_input":"2023-05-18T19:27:29.928759Z","iopub.status.idle":"2023-05-18T19:27:30.215303Z","shell.execute_reply.started":"2023-05-18T19:27:29.928715Z","shell.execute_reply":"2023-05-18T19:27:30.214257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"****Splitting Data into Training and Testing Sets:****","metadata":{}},{"cell_type":"code","source":"X = df.iloc[:,1:]\ny = df.iloc[:,0]\nprint(X)\nprint(y)","metadata":{"execution":{"iopub.status.busy":"2023-05-18T19:27:30.216745Z","iopub.execute_input":"2023-05-18T19:27:30.217541Z","iopub.status.idle":"2023-05-18T19:27:30.236450Z","shell.execute_reply.started":"2023-05-18T19:27:30.217500Z","shell.execute_reply":"2023-05-18T19:27:30.235339Z"},"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.25,random_state = 12 )\nX_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-18T19:27:30.238027Z","iopub.execute_input":"2023-05-18T19:27:30.238416Z","iopub.status.idle":"2023-05-18T19:27:30.667932Z","shell.execute_reply.started":"2023-05-18T19:27:30.238383Z","shell.execute_reply":"2023-05-18T19:27:30.666779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sklearn.linear_model\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import accuracy_score\n\n# Create a Logistic Regression classifier\nlogistic_reg = LogisticRegression()\n\n# Train the model\nlogistic_reg.fit(X_train, y_train)\n\n# Make predictions on the test set\ny_pred_logistic = logistic_reg.predict(X_test)\n\n# Evaluate accuracy\naccuracy_logistic = accuracy_score(y_test, y_pred_logistic)\nprint(\"Accuracy (Logistic Regression):\", accuracy_logistic)","metadata":{"execution":{"iopub.status.busy":"2023-05-18T19:27:30.671127Z","iopub.execute_input":"2023-05-18T19:27:30.671545Z","iopub.status.idle":"2023-05-18T19:27:47.144641Z","shell.execute_reply.started":"2023-05-18T19:27:30.671508Z","shell.execute_reply":"2023-05-18T19:27:47.143023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Standardizing the Data using StandardScaler:****","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()\nX_train = scaler.fit_transform(X_train)\nX_test = scaler.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-05-18T19:27:47.146820Z","iopub.execute_input":"2023-05-18T19:27:47.147774Z","iopub.status.idle":"2023-05-18T19:27:47.923021Z","shell.execute_reply.started":"2023-05-18T19:27:47.147715Z","shell.execute_reply":"2023-05-18T19:27:47.921893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"PCA****","metadata":{}},{"cell_type":"code","source":"from sklearn.decomposition import PCA\npca = PCA(n_components=200)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)\nX_train_trf.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-18T19:27:47.924667Z","iopub.execute_input":"2023-05-18T19:27:47.925311Z","iopub.status.idle":"2023-05-18T19:27:52.871011Z","shell.execute_reply.started":"2023-05-18T19:27:47.925248Z","shell.execute_reply":"2023-05-18T19:27:52.869796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\n# Create a Logistic Regression classifier\nlogistic_reg = LogisticRegression()\n# Train the model\nlogistic_reg.fit(X_train_trf, y_train)\n\n# Make predictions on the test set\ny_pred_logistic = logistic_reg.predict(X_test_trf)\n\n# Evaluate accuracy\naccuracy_logistic = accuracy_score(y_test, y_pred_logistic)\nprint(\"Accuracy (Logistic Regression with PCA):\", accuracy_logistic)","metadata":{"execution":{"iopub.status.busy":"2023-05-18T19:58:49.484644Z","iopub.execute_input":"2023-05-18T19:58:49.486159Z","iopub.status.idle":"2023-05-18T19:59:00.754167Z","shell.execute_reply.started":"2023-05-18T19:58:49.486098Z","shell.execute_reply":"2023-05-18T19:59:00.750587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transforming to a 2D coordinate system\npca = PCA(n_components=2)\nX_train_trf = pca.fit_transform(X_train)\nX_test_trf = pca.transform(X_test)\nX_train_trf","metadata":{"execution":{"iopub.status.busy":"2023-05-18T19:33:12.167084Z","iopub.execute_input":"2023-05-18T19:33:12.167554Z","iopub.status.idle":"2023-05-18T19:33:15.085890Z","shell.execute_reply.started":"2023-05-18T19:33:12.167522Z","shell.execute_reply":"2023-05-18T19:33:15.084347Z"},"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":"2023-05-18T19:33:31.734624Z","iopub.execute_input":"2023-05-18T19:33:31.735650Z","iopub.status.idle":"2023-05-18T19:33:34.436379Z","shell.execute_reply.started":"2023-05-18T19:33:31.735606Z","shell.execute_reply":"2023-05-18T19:33:34.434894Z"},"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)\nX_train_trf","metadata":{"execution":{"iopub.status.busy":"2023-05-18T19:33:54.006853Z","iopub.execute_input":"2023-05-18T19:33:54.007884Z","iopub.status.idle":"2023-05-18T19:33:56.757662Z","shell.execute_reply.started":"2023-05-18T19:33:54.007845Z","shell.execute_reply":"2023-05-18T19:33:56.756020Z"},"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()\n","metadata":{"execution":{"iopub.status.busy":"2023-05-18T19:34:14.973598Z","iopub.execute_input":"2023-05-18T19:34:14.974056Z","iopub.status.idle":"2023-05-18T19:34:15.228524Z","shell.execute_reply.started":"2023-05-18T19:34:14.974023Z","shell.execute_reply":"2023-05-18T19:34:15.226347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_\n# Eigen values","metadata":{"execution":{"iopub.status.busy":"2023-05-18T19:35:06.688694Z","iopub.execute_input":"2023-05-18T19:35:06.690002Z","iopub.status.idle":"2023-05-18T19:35:06.698097Z","shell.execute_reply.started":"2023-05-18T19:35:06.689954Z","shell.execute_reply":"2023-05-18T19:35:06.696710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape\n# Eigen vectors","metadata":{"execution":{"iopub.status.busy":"2023-05-18T19:35:16.159903Z","iopub.execute_input":"2023-05-18T19:35:16.160419Z","iopub.status.idle":"2023-05-18T19:35:16.168794Z","shell.execute_reply.started":"2023-05-18T19:35:16.160376Z","shell.execute_reply":"2023-05-18T19:35:16.167324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2023-05-18T19:35:29.155482Z","iopub.execute_input":"2023-05-18T19:35:29.156332Z","iopub.status.idle":"2023-05-18T19:35:29.165697Z","shell.execute_reply.started":"2023-05-18T19:35:29.156279Z","shell.execute_reply":"2023-05-18T19:35:29.164083Z"},"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":"2023-05-18T19:36:53.624395Z","iopub.execute_input":"2023-05-18T19:36:53.624850Z","iopub.status.idle":"2023-05-18T19:36:57.991178Z","shell.execute_reply.started":"2023-05-18T19:36:53.624816Z","shell.execute_reply":"2023-05-18T19:36:57.989576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.explained_variance_.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-18T19:37:07.887865Z","iopub.execute_input":"2023-05-18T19:37:07.888341Z","iopub.status.idle":"2023-05-18T19:37:07.897488Z","shell.execute_reply.started":"2023-05-18T19:37:07.888300Z","shell.execute_reply":"2023-05-18T19:37:07.895820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.components_.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-18T19:37:17.914418Z","iopub.execute_input":"2023-05-18T19:37:17.914865Z","iopub.status.idle":"2023-05-18T19:37:17.922610Z","shell.execute_reply.started":"2023-05-18T19:37:17.914830Z","shell.execute_reply":"2023-05-18T19:37:17.921234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.cumsum(pca.explained_variance_ratio_)","metadata":{"execution":{"iopub.status.busy":"2023-05-18T19:37:29.954498Z","iopub.execute_input":"2023-05-18T19:37:29.956039Z","iopub.status.idle":"2023-05-18T19:37:29.975152Z","shell.execute_reply.started":"2023-05-18T19:37:29.955985Z","shell.execute_reply":"2023-05-18T19:37:29.973619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(np.cumsum(pca.explained_variance_ratio_))","metadata":{"execution":{"iopub.status.busy":"2023-05-18T19:37:42.655428Z","iopub.execute_input":"2023-05-18T19:37:42.655854Z","iopub.status.idle":"2023-05-18T19:37:42.853382Z","shell.execute_reply.started":"2023-05-18T19:37:42.655824Z","shell.execute_reply":"2023-05-18T19:37:42.852193Z"},"trusted":true},"execution_count":null,"outputs":[]}]}