{"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","jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2024-01-05T05:17:27.440049Z","iopub.execute_input":"2024-01-05T05:17:27.440436Z","iopub.status.idle":"2024-01-05T05:17:27.878657Z","shell.execute_reply.started":"2024-01-05T05:17:27.440404Z","shell.execute_reply":"2024-01-05T05:17:27.877460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_df = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2024-01-05T05:17:27.880906Z","iopub.execute_input":"2024-01-05T05:17:27.881525Z","iopub.status.idle":"2024-01-05T05:17:33.616635Z","shell.execute_reply.started":"2024-01-05T05:17:27.881483Z","shell.execute_reply":"2024-01-05T05:17:33.615380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X,train_y = train_df.iloc[:,1:].to_numpy(), train_df.iloc[:,[0]].to_numpy()\ntest_X,test_y = test_df.iloc[:,1:].to_numpy(), test_df.iloc[:,[0]].to_numpy()","metadata":{"execution":{"iopub.status.busy":"2024-01-05T05:17:33.618081Z","iopub.execute_input":"2024-01-05T05:17:33.618433Z","iopub.status.idle":"2024-01-05T05:17:33.633394Z","shell.execute_reply.started":"2024-01-05T05:17:33.618403Z","shell.execute_reply":"2024-01-05T05:17:33.631931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nss = StandardScaler()\ntrain_X = ss.fit_transform(train_X)\ntest_X = ss.fit_transform(test_X)","metadata":{"execution":{"iopub.status.busy":"2024-01-05T05:17:33.636048Z","iopub.execute_input":"2024-01-05T05:17:33.636460Z","iopub.status.idle":"2024-01-05T05:17:35.116147Z","shell.execute_reply.started":"2024-01-05T05:17:33.636427Z","shell.execute_reply":"2024-01-05T05:17:35.115173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Before PCA","metadata":{}},{"cell_type":"code","source":"print(f\"Columns before PCA: {train_X.shape[1]}\")","metadata":{"execution":{"iopub.status.busy":"2024-01-05T05:17:35.118072Z","iopub.execute_input":"2024-01-05T05:17:35.118978Z","iopub.status.idle":"2024-01-05T05:17:35.125495Z","shell.execute_reply.started":"2024-01-05T05:17:35.118932Z","shell.execute_reply":"2024-01-05T05:17:35.124195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.decomposition import PCA\n# variance = []\n# for i in range(1,780,100):\n#     find_pca = PCA(n_components=i)\n#     find_pca.fit(train_X)\n#     print(f\"Fitting to {find_pca.n_components}, {find_pca.explained_variance_ratio_[0]}\")\n#     variance.append(find_pca.explained_variance_ratio_[0])","metadata":{"execution":{"iopub.status.busy":"2024-01-05T05:17:35.127693Z","iopub.execute_input":"2024-01-05T05:17:35.129239Z","iopub.status.idle":"2024-01-05T05:17:35.151665Z","shell.execute_reply.started":"2024-01-05T05:17:35.129165Z","shell.execute_reply":"2024-01-05T05:17:35.150489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.decomposition import PCA\nfind_pca = PCA()\nfind_pca.fit(train_X)\nexp_var_PCA = find_pca.explained_variance_ratio_\ncumsum_exp_var = np.cumsum(exp_var_PCA)\nplt.step(\n    range(0, len(cumsum_exp_var)), \n    cumsum_exp_var, \n    label='Cumulative explained variance', \n    color='C1'\n)\nplt.plot()","metadata":{"execution":{"iopub.status.busy":"2024-01-05T05:17:35.153082Z","iopub.execute_input":"2024-01-05T05:17:35.153573Z","iopub.status.idle":"2024-01-05T05:17:40.945906Z","shell.execute_reply.started":"2024-01-05T05:17:35.153540Z","shell.execute_reply":"2024-01-05T05:17:40.944407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_components = np.argmax(cumsum_exp_var > 0.90)\nbest_components","metadata":{"execution":{"iopub.status.busy":"2024-01-05T05:17:40.947486Z","iopub.execute_input":"2024-01-05T05:17:40.947853Z","iopub.status.idle":"2024-01-05T05:17:40.957166Z","shell.execute_reply.started":"2024-01-05T05:17:40.947822Z","shell.execute_reply":"2024-01-05T05:17:40.955613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_pca = PCA(n_components=best_components)\ntrain_X_pca = best_pca.fit_transform(train_X)\ntest_X_pca = best_pca.fit_transform(test_X)","metadata":{"execution":{"iopub.status.busy":"2024-01-05T05:17:40.958894Z","iopub.execute_input":"2024-01-05T05:17:40.959631Z","iopub.status.idle":"2024-01-05T05:17:54.821451Z","shell.execute_reply.started":"2024-01-05T05:17:40.959593Z","shell.execute_reply":"2024-01-05T05:17:54.820327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Columns before PCA: {train_X.shape[1]}\")\nprint(f\"Columns after PCA: {train_X_pca.shape[1]}\")","metadata":{"execution":{"iopub.status.busy":"2024-01-05T05:17:54.824955Z","iopub.execute_input":"2024-01-05T05:17:54.825468Z","iopub.status.idle":"2024-01-05T05:17:54.832946Z","shell.execute_reply.started":"2024-01-05T05:17:54.825426Z","shell.execute_reply":"2024-01-05T05:17:54.831458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating model","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import GridSearchCV\nfrom sklearn.svm import SVC\nparam_grid = {'kernel':['linear', 'poly'],\n              'degree':[1, 2,],\n              'gamma': [0.01, 0.1],\n              'coef0': [0.5, 1]}\ngrid_svm = GridSearchCV(estimator=SVC(probability = True),param_grid=param_grid,scoring='accuracy',verbose=3,return_train_score=True,cv=3,refit=True)\ngrid_svm.fit(train_X_pca,train_y)","metadata":{"execution":{"iopub.status.busy":"2024-01-05T05:23:38.062170Z","iopub.execute_input":"2024-01-05T05:23:38.062628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_svm.best_params_,grid_svm.best_estimator_","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_test = grid_svm.predict(test_X_pca)\npredictions_train = grid_svm.predict(train_X_pca)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report,accuracy_score\nprint(classification_report(test_y,predictions_test))\naccuracy_score(test_y,predictions_test)","metadata":{"execution":{"iopub.status.busy":"2024-01-05T05:21:37.725324Z","iopub.execute_input":"2024-01-05T05:21:37.725827Z","iopub.status.idle":"2024-01-05T05:21:37.788536Z","shell.execute_reply.started":"2024-01-05T05:21:37.725796Z","shell.execute_reply":"2024-01-05T05:21:37.787339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}