{"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":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.preprocessing import scale\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.svm import SVC\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.model_selection import validation_curve\nfrom sklearn.model_selection import KFold\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.model_selection import GridSearchCV\n\ndf = pd.read_csv(\"../input/digit-recognizer/train.csv\")\n\nprint(df.shape)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-17T06:55:27.750993Z","iopub.execute_input":"2023-11-17T06:55:27.752165Z","iopub.status.idle":"2023-11-17T06:55:30.985297Z","shell.execute_reply.started":"2023-11-17T06:55:27.752115Z","shell.execute_reply":"2023-11-17T06:55:30.984122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-11-17T06:51:12.79051Z","iopub.execute_input":"2023-11-17T06:51:12.791257Z","iopub.status.idle":"2023-11-17T06:51:12.837304Z","shell.execute_reply.started":"2023-11-17T06:51:12.791214Z","shell.execute_reply":"2023-11-17T06:51:12.836113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df.drop('label', axis=1)  # Features\nX = X/255.0\nX = scale(X)\ny = df['label']  # Labels","metadata":{"execution":{"iopub.status.busy":"2023-11-17T06:52:31.449762Z","iopub.execute_input":"2023-11-17T06:52:31.45028Z","iopub.status.idle":"2023-11-17T06:52:32.690993Z","shell.execute_reply.started":"2023-11-17T06:52:31.450237Z","shell.execute_reply":"2023-11-17T06:52:32.689775Z"},"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)\nprint(\"X-Train Shape:\", X_train.shape)\nprint(\"X-Test Shape:\", X_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T06:52:36.634505Z","iopub.execute_input":"2023-11-17T06:52:36.634889Z","iopub.status.idle":"2023-11-17T06:52:37.056575Z","shell.execute_reply.started":"2023-11-17T06:52:36.63486Z","shell.execute_reply":"2023-11-17T06:52:37.055372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folds = KFold(n_splits = 5, shuffle = True, random_state = 10)\n\nhyper_params = [{'kernel': ['linear','poly','rbf'],'gamma': [1e-2, 1e-3, 1e-4],'C': [0.1,1,5,10], 'degree':[2,5,10]}]\n\nmodel_gaussian = SVC()\n\nmodel_cv = GridSearchCV(estimator = model_gaussian, \n                        param_grid = hyper_params, \n                        scoring= 'accuracy', \n                        cv = folds, \n                        verbose = 1,\n                        return_train_score=True)      \n\nmodel_cv.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T06:56:26.850043Z","iopub.execute_input":"2023-11-17T06:56:26.850475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv_results = pd.DataFrame(model_cv.cv_results_)\ncv_results","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_score = model_cv.best_score_\nbest_hyperparams = model_cv.best_params_\n\nprint(\"The best test score is {0} corresponding to hyperparameters {1}\".format(best_score, best_hyperparams))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = SVC(C=best_hyperparams['C'], gamma=best_hyperparams['gamma'], kernel=\"rbf\")\n\nmodel.fit(X_train, y_train)\ny_pred = model.predict(X_test)\n\n# metrics\nprint(\"accuracy\", metrics.accuracy_score(y_test, y_pred), \"\\n\")\nprint(metrics.confusion_matrix(y_test, y_pred), \"\\n\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}