{"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":"import numpy as np, pandas as pd,matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.svm import SVC\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.model_selection import KFold\nimport gc\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:55:27.439968Z","iopub.execute_input":"2021-08-07T14:55:27.440357Z","iopub.status.idle":"2021-08-07T14:55:27.446176Z","shell.execute_reply.started":"2021-08-07T14:55:27.440317Z","shell.execute_reply":"2021-08-07T14:55:27.445312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:55:31.451288Z","iopub.execute_input":"2021-08-07T14:55:31.451799Z","iopub.status.idle":"2021-08-07T14:55:31.461379Z","shell.execute_reply.started":"2021-08-07T14:55:31.451762Z","shell.execute_reply":"2021-08-07T14:55:31.460173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the train Data_set\n#../input/digit-recognizer/test.csv\ndigit_train = pd.read_csv('../input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:55:34.231903Z","iopub.execute_input":"2021-08-07T14:55:34.232234Z","iopub.status.idle":"2021-08-07T14:55:37.532329Z","shell.execute_reply.started":"2021-08-07T14:55:34.232204Z","shell.execute_reply":"2021-08-07T14:55:37.531403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Showing the shape of both data Set\ndigit_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:55:38.661023Z","iopub.execute_input":"2021-08-07T14:55:38.66135Z","iopub.status.idle":"2021-08-07T14:55:38.668519Z","shell.execute_reply.started":"2021-08-07T14:55:38.661322Z","shell.execute_reply":"2021-08-07T14:55:38.667547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"digit_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:55:41.863083Z","iopub.execute_input":"2021-08-07T14:55:41.863443Z","iopub.status.idle":"2021-08-07T14:55:41.896677Z","shell.execute_reply.started":"2021-08-07T14:55:41.863414Z","shell.execute_reply":"2021-08-07T14:55:41.895683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Info of the digit_train data Set\ndigit_train.info()","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:55:44.003436Z","iopub.execute_input":"2021-08-07T14:55:44.003821Z","iopub.status.idle":"2021-08-07T14:55:44.049998Z","shell.execute_reply.started":"2021-08-07T14:55:44.003791Z","shell.execute_reply":"2021-08-07T14:55:44.048915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Select a digit to show in gray\nimg = digit_train.iloc[1,1:]\nimg= img.values.reshape(28,28)\nplt.imshow(img,cmap = 'gray')","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:55:46.093579Z","iopub.execute_input":"2021-08-07T14:55:46.093938Z","iopub.status.idle":"2021-08-07T14:55:46.268226Z","shell.execute_reply.started":"2021-08-07T14:55:46.093906Z","shell.execute_reply":"2021-08-07T14:55:46.267295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#show in gray color\nimg1 = digit_train.iloc[2,1:]\nimg1 = img1.values.reshape(28,28)\nplt.imshow(img1)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:55:48.912609Z","iopub.execute_input":"2021-08-07T14:55:48.912986Z","iopub.status.idle":"2021-08-07T14:55:49.161342Z","shell.execute_reply.started":"2021-08-07T14:55:48.912953Z","shell.execute_reply":"2021-08-07T14:55:49.160216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Summarise the counts of 'label' to see how many labels of each digit are present\n#digit_train.label.astype('category').value_counts()\ndigit_train.label.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:55:52.443147Z","iopub.execute_input":"2021-08-07T14:55:52.443489Z","iopub.status.idle":"2021-08-07T14:55:52.451631Z","shell.execute_reply.started":"2021-08-07T14:55:52.443459Z","shell.execute_reply":"2021-08-07T14:55:52.450785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Data Preparation for Model Building","metadata":{}},{"cell_type":"code","source":"# Spliting the data set in to X and y\nX = digit_train.iloc[:,1:]\ny = digit_train.iloc[:,0]","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:55:55.528606Z","iopub.execute_input":"2021-08-07T14:55:55.528987Z","iopub.status.idle":"2021-08-07T14:55:55.5345Z","shell.execute_reply.started":"2021-08-07T14:55:55.528953Z","shell.execute_reply":"2021-08-07T14:55:55.533317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### # Rescaling the features","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import scale\nX = scale(X)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:55:58.305867Z","iopub.execute_input":"2021-08-07T14:55:58.306205Z","iopub.status.idle":"2021-08-07T14:55:59.432727Z","shell.execute_reply.started":"2021-08-07T14:55:58.306174Z","shell.execute_reply":"2021-08-07T14:55:59.43164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Split the Train data set and show the shape of each data_set","metadata":{}},{"cell_type":"code","source":"X_train, X_test, y_train,y_test = train_test_split(X,y, train_size  = 0.10, random_state = 42)\nX_train.shape, X_test.shape, y_train.shape,y_test.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:56:01.916307Z","iopub.execute_input":"2021-08-07T14:56:01.916687Z","iopub.status.idle":"2021-08-07T14:56:02.319511Z","shell.execute_reply.started":"2021-08-07T14:56:01.916651Z","shell.execute_reply":"2021-08-07T14:56:02.318629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Model Building\n#### Linear SVM Model","metadata":{}},{"cell_type":"code","source":"### Linear SVM\nfrom sklearn import metrics\nfrom sklearn import svm \nmodel_linear = svm.SVC(kernel = 'linear')\nmodel_linear.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:56:04.677517Z","iopub.execute_input":"2021-08-07T14:56:04.677904Z","iopub.status.idle":"2021-08-07T14:56:09.57267Z","shell.execute_reply.started":"2021-08-07T14:56:04.677872Z","shell.execute_reply":"2021-08-07T14:56:09.571888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predict the X_test with the Linear Model ans also show the confusion_matrix\ny_linear_pred = model_linear.predict(X_test)\nmetrics.confusion_matrix(y_test, y_linear_pred)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:56:14.471429Z","iopub.execute_input":"2021-08-07T14:56:14.471829Z","iopub.status.idle":"2021-08-07T14:57:29.075991Z","shell.execute_reply.started":"2021-08-07T14:56:14.471782Z","shell.execute_reply":"2021-08-07T14:57:29.074926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Show the predicted image label\ny_linear_pred[:10]","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:57:32.745504Z","iopub.execute_input":"2021-08-07T14:57:32.745833Z","iopub.status.idle":"2021-08-07T14:57:32.751372Z","shell.execute_reply.started":"2021-08-07T14:57:32.745805Z","shell.execute_reply":"2021-08-07T14:57:32.750583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Measure the accuracyS\nmetrics.accuracy_score(y_test, y_linear_pred)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:57:35.246834Z","iopub.execute_input":"2021-08-07T14:57:35.247154Z","iopub.status.idle":"2021-08-07T14:57:35.256533Z","shell.execute_reply.started":"2021-08-07T14:57:35.247125Z","shell.execute_reply":"2021-08-07T14:57:35.255497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class-wise accuracy\nclass_wise = metrics.classification_report(y_test, y_linear_pred)\nprint(class_wise)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:57:38.261391Z","iopub.execute_input":"2021-08-07T14:57:38.261725Z","iopub.status.idle":"2021-08-07T14:57:38.362832Z","shell.execute_reply.started":"2021-08-07T14:57:38.261694Z","shell.execute_reply":"2021-08-07T14:57:38.361806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# run gc.collect() (garbage collect) to free up memory\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:57:41.211579Z","iopub.execute_input":"2021-08-07T14:57:41.21214Z","iopub.status.idle":"2021-08-07T14:57:41.326069Z","shell.execute_reply.started":"2021-08-07T14:57:41.212093Z","shell.execute_reply":"2021-08-07T14:57:41.324923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Non-Linear SVM\n#### Let's now try a non-linear model with the RBF kernel.","metadata":{}},{"cell_type":"code","source":"model_nonlinear = svm.SVC(kernel = 'rbf')\nmodel_nonlinear.fit(X_train,y_train)\ny_nonlinear_pred = model_nonlinear.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:57:44.533091Z","iopub.execute_input":"2021-08-07T14:57:44.533409Z","iopub.status.idle":"2021-08-07T14:59:54.044573Z","shell.execute_reply.started":"2021-08-07T14:57:44.533381Z","shell.execute_reply":"2021-08-07T14:59:54.043533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Show the predicted image label \ny_nonlinear_pred[0:10]","metadata":{"execution":{"iopub.status.busy":"2021-08-07T15:01:26.486108Z","iopub.execute_input":"2021-08-07T15:01:26.48648Z","iopub.status.idle":"2021-08-07T15:01:26.493465Z","shell.execute_reply.started":"2021-08-07T15:01:26.48645Z","shell.execute_reply":"2021-08-07T15:01:26.49218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Measure the Accuracy Matrix\nmetrics.accuracy_score(y_test, y_nonlinear_pred)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T15:01:30.672295Z","iopub.execute_input":"2021-08-07T15:01:30.67266Z","iopub.status.idle":"2021-08-07T15:01:30.683844Z","shell.execute_reply.started":"2021-08-07T15:01:30.672606Z","shell.execute_reply":"2021-08-07T15:01:30.682729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Show the comfusion Matrix \nmetrics.confusion_matrix(y_test, y_nonlinear_pred)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T15:01:41.466493Z","iopub.execute_input":"2021-08-07T15:01:41.466864Z","iopub.status.idle":"2021-08-07T15:01:41.505512Z","shell.execute_reply.started":"2021-08-07T15:01:41.466829Z","shell.execute_reply":"2021-08-07T15:01:41.504558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class-wise accuracy\nclass_wise = metrics.classification_report(y_test, y_nonlinear_pred)\nprint(class_wise)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T15:01:48.22349Z","iopub.execute_input":"2021-08-07T15:01:48.223868Z","iopub.status.idle":"2021-08-07T15:01:48.321611Z","shell.execute_reply.started":"2021-08-07T15:01:48.223831Z","shell.execute_reply":"2021-08-07T15:01:48.320435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Grid Search Cross-Validation","metadata":{}},{"cell_type":"code","source":"# conduct (grid search) cross-validation to find the optimal values \n# of cost C and the choice of kernel\n\nfrom sklearn.model_selection import GridSearchCV\n\nparameters = {'C':[1, 10, 50, 100], \n             'gamma': [1e-1,1e-2,1e-3, 1e-4]}\n\n# instantiate a model \nsvc_grid_search = svm.SVC(kernel=\"rbf\")\n\n# create a classifier to perform grid search\nmodel_cv = GridSearchCV(svc_grid_search, param_grid=parameters, scoring='accuracy',return_train_score=True)\n\n# fit\nmodel_cv.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T15:02:02.484694Z","iopub.execute_input":"2021-08-07T15:02:02.485061Z","iopub.status.idle":"2021-08-07T15:41:19.946453Z","shell.execute_reply.started":"2021-08-07T15:02:02.485029Z","shell.execute_reply":"2021-08-07T15:41:19.945525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# cv results\ncv_results = pd.DataFrame(model_cv.cv_results_)\ncv_results","metadata":{"execution":{"iopub.status.busy":"2021-08-07T15:41:28.766603Z","iopub.execute_input":"2021-08-07T15:41:28.767107Z","iopub.status.idle":"2021-08-07T15:41:28.805863Z","shell.execute_reply.started":"2021-08-07T15:41:28.767073Z","shell.execute_reply":"2021-08-07T15:41:28.8051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# printing the optimal accuracy score and hyperparameters\nbest_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":{"execution":{"iopub.status.busy":"2021-08-07T15:41:49.486536Z","iopub.execute_input":"2021-08-07T15:41:49.487027Z","iopub.status.idle":"2021-08-07T15:41:49.491692Z","shell.execute_reply.started":"2021-08-07T15:41:49.486993Z","shell.execute_reply":"2021-08-07T15:41:49.490938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# converting C to numeric type for plotting on x-axis\ncv_results['param_C'] = cv_results['param_C'].astype('int')\n\n# # plotting\nplt.figure(figsize=(16,6))\n\n# subplot 1/3\nplt.subplot(131)\ngamma_01 = cv_results[cv_results['param_gamma']==0.01]\n\nplt.plot(gamma_01[\"param_C\"], gamma_01[\"mean_test_score\"])\nplt.plot(gamma_01[\"param_C\"], gamma_01[\"mean_train_score\"])\nplt.xlabel('C')\nplt.ylabel('Accuracy')\nplt.title(\"Gamma=0.01\")\nplt.ylim([0.60, 1])\nplt.legend(['test accuracy', 'train accuracy'], loc='lower right')\nplt.xscale('log')\n\n# subplot 2/3\nplt.subplot(132)\ngamma_001 = cv_results[cv_results['param_gamma']==0.001]\n\nplt.plot(gamma_001[\"param_C\"], gamma_001[\"mean_test_score\"])\nplt.plot(gamma_001[\"param_C\"], gamma_001[\"mean_train_score\"])\nplt.xlabel('C')\nplt.ylabel('Accuracy')\nplt.title(\"Gamma=0.001\")\nplt.ylim([0.60, 1])\nplt.legend(['test accuracy', 'train accuracy'], loc='lower right')\nplt.xscale('log')\n\n# subplot 3/3\nplt.subplot(133)\ngamma_0001 = cv_results[cv_results['param_gamma']==0.0001]\n\nplt.plot(gamma_0001[\"param_C\"], gamma_0001[\"mean_test_score\"])\nplt.plot(gamma_0001[\"param_C\"], gamma_0001[\"mean_train_score\"])\nplt.xlabel('C')\nplt.ylabel('Accuracy')\nplt.title(\"Gamma=0.0001\")\nplt.ylim([0.60, 1])\nplt.legend(['test accuracy', 'train accuracy'], loc='lower right')\nplt.xscale('log')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-07T15:41:57.057066Z","iopub.execute_input":"2021-08-07T15:41:57.057593Z","iopub.status.idle":"2021-08-07T15:41:58.034174Z","shell.execute_reply.started":"2021-08-07T15:41:57.057557Z","shell.execute_reply":"2021-08-07T15:41:58.033033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Final Model\nLet's now build the final model with chosen hyperparameters.","metadata":{}},{"cell_type":"code","source":"svm_final = SVC(kernel = 'rbf',C= 10, gamma = 0.001)\n\n# fit the model\nsvm_final.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T15:42:11.609249Z","iopub.execute_input":"2021-08-07T15:42:11.609611Z","iopub.status.idle":"2021-08-07T15:42:20.481648Z","shell.execute_reply.started":"2021-08-07T15:42:11.609574Z","shell.execute_reply":"2021-08-07T15:42:20.480578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict on the Spited data Set of train.csv, that is X_test \ny_final_pred = svm_final.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T15:42:28.023606Z","iopub.execute_input":"2021-08-07T15:42:28.024059Z","iopub.status.idle":"2021-08-07T15:44:10.771837Z","shell.execute_reply.started":"2021-08-07T15:42:28.024021Z","shell.execute_reply":"2021-08-07T15:44:10.770881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Measure the Accuracy of the Model\nmetrics.accuracy_score(y_test, y_final_pred)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T15:44:39.20038Z","iopub.execute_input":"2021-08-07T15:44:39.200877Z","iopub.status.idle":"2021-08-07T15:44:39.211516Z","shell.execute_reply.started":"2021-08-07T15:44:39.200842Z","shell.execute_reply":"2021-08-07T15:44:39.210813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Measure the Confusion Metrix\nmetrics.confusion_matrix(y_test, y_final_pred)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T15:44:44.505916Z","iopub.execute_input":"2021-08-07T15:44:44.506255Z","iopub.status.idle":"2021-08-07T15:44:44.547127Z","shell.execute_reply.started":"2021-08-07T15:44:44.506226Z","shell.execute_reply":"2021-08-07T15:44:44.546367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class-wise accuracy\nclass_wise = metrics.classification_report(y_test, y_final_pred)\nprint(class_wise)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T15:44:48.190375Z","iopub.execute_input":"2021-08-07T15:44:48.190887Z","iopub.status.idle":"2021-08-07T15:44:48.288992Z","shell.execute_reply.started":"2021-08-07T15:44:48.190855Z","shell.execute_reply":"2021-08-07T15:44:48.287796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Prediction on the test.csv","metadata":{}},{"cell_type":"code","source":"# Load the test data_Set\nX1_test = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-08-07T15:44:52.834211Z","iopub.execute_input":"2021-08-07T15:44:52.834632Z","iopub.status.idle":"2021-08-07T15:44:54.526387Z","shell.execute_reply.started":"2021-08-07T15:44:52.834576Z","shell.execute_reply":"2021-08-07T15:44:54.525329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X1_test.columns","metadata":{"execution":{"iopub.status.busy":"2021-08-07T15:45:40.886028Z","iopub.execute_input":"2021-08-07T15:45:40.886394Z","iopub.status.idle":"2021-08-07T15:45:40.893156Z","shell.execute_reply.started":"2021-08-07T15:45:40.886342Z","shell.execute_reply":"2021-08-07T15:45:40.892115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X1_test.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-07T15:45:43.391142Z","iopub.execute_input":"2021-08-07T15:45:43.39148Z","iopub.status.idle":"2021-08-07T15:45:43.39815Z","shell.execute_reply.started":"2021-08-07T15:45:43.391451Z","shell.execute_reply":"2021-08-07T15:45:43.396819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X1_test.info()","metadata":{"execution":{"iopub.status.busy":"2021-08-07T15:45:47.104794Z","iopub.execute_input":"2021-08-07T15:45:47.105164Z","iopub.status.idle":"2021-08-07T15:45:47.147187Z","shell.execute_reply.started":"2021-08-07T15:45:47.105134Z","shell.execute_reply":"2021-08-07T15:45:47.14621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Show the image from test\nimg1 = X1_test.iloc[0,0:]\nimg1 = img1.values.reshape(28,28)\nplt.imshow(img1, cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2021-08-07T15:45:52.477096Z","iopub.execute_input":"2021-08-07T15:45:52.477446Z","iopub.status.idle":"2021-08-07T15:45:52.620584Z","shell.execute_reply.started":"2021-08-07T15:45:52.47741Z","shell.execute_reply":"2021-08-07T15:45:52.618471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img1 = X1_test.iloc[10,0:]\nimg1 = img1.values.reshape(28,28)\nplt.imshow(img1)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T15:45:55.371321Z","iopub.execute_input":"2021-08-07T15:45:55.371657Z","iopub.status.idle":"2021-08-07T15:45:55.510451Z","shell.execute_reply.started":"2021-08-07T15:45:55.371611Z","shell.execute_reply":"2021-08-07T15:45:55.509409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Scale the data_set","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import scale\nX1_test = scale(X1_test)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T15:49:32.992002Z","iopub.execute_input":"2021-08-07T15:49:32.992359Z","iopub.status.idle":"2021-08-07T15:49:33.756218Z","shell.execute_reply.started":"2021-08-07T15:49:32.992327Z","shell.execute_reply":"2021-08-07T15:49:33.755309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#prediction on the data_set (X1_test) \ntest_pred = pd.DataFrame(svm_final.predict(X1_test))\n#test_pred = pd.DataFrame(test_pred.idxmax(axis = 1))\ntest_pred.index.name = 'ImageId'\ntest_pred = test_pred.rename(columns = {0: 'Label'}).reset_index()\ntest_pred['ImageId'] = test_pred['ImageId'] + 1\n\ntest_pred.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-07T15:51:12.763808Z","iopub.execute_input":"2021-08-07T15:51:12.764198Z","iopub.status.idle":"2021-08-07T15:52:30.160706Z","shell.execute_reply.started":"2021-08-07T15:51:12.764166Z","shell.execute_reply":"2021-08-07T15:52:30.159669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Predicted Image label save in sample_submission.csv","metadata":{}},{"cell_type":"code","source":"test_pred.to_csv('./sample_submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T15:52:45.988271Z","iopub.execute_input":"2021-08-07T15:52:45.988643Z","iopub.status.idle":"2021-08-07T15:52:46.02753Z","shell.execute_reply.started":"2021-08-07T15:52:45.98859Z","shell.execute_reply":"2021-08-07T15:52:46.026708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}