{"cells":[{"metadata":{"id":"6uI3xz_nppcP","colab_type":"text"},"cell_type":"markdown","source":"# MNIST - Digit Recognizer with SVM"},{"metadata":{"trusted":true,"id":"8BsuovEappcQ","colab_type":"code","colab":{}},"cell_type":"code","source":"import pandas as pd, numpy as np\nimport matplotlib.pyplot as plt\nfrom timeit import default_timer as timer\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"su9_Un9SppcT","colab_type":"code","colab":{}},"cell_type":"code","source":"import seaborn as sns\nsns.set(rc={'figure.figsize':(11, 4)})\n\n#pandas option to display all columns\npd.set_option('max_columns',None)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"lHFXfqEeppcV","colab_type":"code","outputId":"c49c0a25-926d-429f-e421-892422d91f20","colab":{"base_uri":"https://localhost:8080/","height":215}},"cell_type":"code","source":"#Read the data files\ntrain_df=pd.read_csv('../input/train.csv')\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"L-A63MHgppcX","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":215},"outputId":"74fe16cf-e4ab-420a-97ef-474a5d0cb11b"},"cell_type":"code","source":"#Read the test dataset\ntest_df=pd.read_csv('../input/test.csv')\ntest_df.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"jn2qvFYkppcZ","colab_type":"text"},"cell_type":"markdown","source":"## Basic Data Quality Check"},{"metadata":{"trusted":true,"_kg_hide-output":true,"id":"3KTNjXtkppca","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":1058},"outputId":"6f5a2a04-6874-4ae8-bdb5-12084bd20607"},"cell_type":"code","source":"#Check if there are any nulls\ntrain_df.isnull().sum(axis=0) >0","execution_count":null,"outputs":[]},{"metadata":{"id":"QD0wbAD5ppcc","colab_type":"text"},"cell_type":"markdown","source":"There doesn't appear to be any rows that have null values or any columns that have null values"},{"metadata":{"trusted":true,"_kg_hide-output":true,"id":"X_0ZQyWWppcd","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":304},"outputId":"462a3c5b-5d5e-46b6-f658-0f3d3a59e4e1"},"cell_type":"code","source":"train_df.describe()","execution_count":null,"outputs":[]},{"metadata":{"id":"xJFcaNzyppcf","colab_type":"text"},"cell_type":"markdown","source":"Since the pixel values can vary bewteen 0 and 255, there is not much we can gather from the above table. But we notice that the pixels number 0-11, 16-31, 52-57 etc are all 0's. This is because most of the digits are centerd and the spaces in the corners are blank. These pixels correspond to such spaces where there is nothing written."},{"metadata":{"trusted":true,"id":"QufGse17ppcf","colab_type":"code","colab":{}},"cell_type":"code","source":"#define a function to draw the image\n#x-> dataframe with data\n#y-> dataframe with labels\ndef draw_digit(x,y, label='Actual'):\n    plt.figure(figsize=(20,5))\n    #check the length of input dataset and divide by 8 to print 10 digits on each line\n    nrows=(len(x)//10)+1\n    #ncols=(len(x)%10)\n    ncols=10\n    print('Lenght of input: {}, nrows: {}, ncols: {}, label: {}'.format(len(x), nrows, ncols, y.shape))\n    #print(x.shape)\n    #iterate over all the digits passed in the array\n   \n    for idx,i in enumerate(x.index):\n        #loop to iterate over blocks of 10 digits            \n            plt.subplot(nrows,10,idx+1)  #subplots start with 1\n            plt.subplots_adjust(top=1.3)\n            plt.imshow(x.loc[i].values.reshape(28,28), cmap=plt.cm.gray, interpolation='nearest',clim=(0, 255))\n            plt.title(label+' %i\\n' % y.iloc[idx,:].values, fontsize = 11)\n            #plt.title('Actual {}\\n'.format(y.iloc[idx,:].values), fontsize = 11)\n           \n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"LV3ZBqw9ppcj","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":416},"outputId":"1260745e-75ad-44ce-c935-91fcf9399677"},"cell_type":"code","source":"#Display image of digits\ndraw_digit(train_df.iloc[:15,1:], train_df.iloc[:15,:1],'Actual')","execution_count":null,"outputs":[]},{"metadata":{"id":"u5qogVS9ppcm","colab_type":"text"},"cell_type":"markdown","source":"### Check the distribution of labels"},{"metadata":{"trusted":true,"id":"s3xFH3YXppcm","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":330},"outputId":"cef66154-f0fc-458d-cf8a-560a8b76116b"},"cell_type":"code","source":"#check the distribution of dataset labels\nsns.countplot(train_df['label'], palette='viridis')\n","execution_count":null,"outputs":[]},{"metadata":{"id":"1dnavqoYOLOX","colab_type":"text"},"cell_type":"markdown","source":"We see that there is a uniform distribution of different classes, and there is no class imbalance or skewness."},{"metadata":{"id":"1ZzaHh6Vppcp","colab_type":"text"},"cell_type":"markdown","source":"## Take a subset of data.\n__Since the data set is quite large, and tuning all the hyperparameters will be expensive, we will run the training on a subset of this data.__"},{"metadata":{"trusted":true,"id":"N2UKZB0Cppcp","colab_type":"code","colab":{}},"cell_type":"code","source":"import random\nsubset_percent=25","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"i4sS9XNippcr","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"aa89cefc-04e7-4068-a57c-0cc33c62004b"},"cell_type":"code","source":"#np.floor(len(digi_df)*21/100), len(digi_df)*21/100\n#list(digi_df.index)\nint(len(train_df)*subset_percent/100)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"nd2Dv1d2ppct","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"b8c7dc2a-990f-46dd-ee15-893089972010"},"cell_type":"code","source":"print ('Sampling {}% of training, Labels and test data'.format(subset_percent))\ntrain_idx=random.sample(list(train_df.index),int(len(train_df)*subset_percent/100))\ntest_idx=random.sample(list(test_df.index),int(len(test_df)*subset_percent/100))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"-9-BSo_jppcv","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"a8960d10-9314-4e2b-8522-403a7f2d6d6c"},"cell_type":"code","source":"print('Training Data size: {}, test data size: {}'.format(len(train_idx), len(test_idx)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"aAIQILsKppcy","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":215},"outputId":"dcd6b987-0b06-460f-b930-a08093eda12a"},"cell_type":"code","source":"train_df.iloc[train_idx].head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"lgY5WSQYppc1","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":118},"outputId":"f799500c-4dc1-40ca-f96b-a188af567a6c"},"cell_type":"code","source":"train_df.loc[train_idx,'label'].head()","execution_count":null,"outputs":[]},{"metadata":{"id":"Q3ZN13bXppc4","colab_type":"text"},"cell_type":"markdown","source":"## Split the training dataset into training and validation\n"},{"metadata":{"trusted":true,"id":"L0krlarjppc5","colab_type":"code","colab":{}},"cell_type":"code","source":"#Seprate the label from the training data \ntrain_label=train_df['label']\ntrain_df.drop('label', axis=1, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"O50DrM5vppc8","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"9d322d06-eed8-427a-d764-8aeb0df14211"},"cell_type":"code","source":"print('Data: {}, Label: {}'.format(train_df.shape, train_label.shape))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"9ptdKIOwppc-","colab_type":"code","colab":{}},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n# from the original data, we will take a subset (25% calculated above) and split this subset into training and validation set\n\ntrain_x, test_x, train_y, test_y = train_test_split(train_df.iloc[train_idx], train_label.iloc[train_idx], \\\n                                                    train_size=0.7, test_size=0.3, random_state=4)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"yUZq7kAnppdA","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":50},"outputId":"71ffe703-5bbb-46cf-e924-3269470877ff"},"cell_type":"code","source":"print('Data - training Set: {}, test set: {}'.format(train_x.shape, test_x.shape))\nprint('Label - training Set: {}, test set: {}'.format(train_y.shape, test_y.shape))","execution_count":null,"outputs":[]},{"metadata":{"id":"1oFswbA_ppdC","colab_type":"text"},"cell_type":"markdown","source":"## Use Cross Validation to train the model\n\n__We already have a test set, but to tune the hyperparameters, we will use K-fold cross validaton on the training set__"},{"metadata":{"trusted":true,"id":"_zcoq6xsppdD","colab_type":"code","colab":{}},"cell_type":"code","source":"from sklearn.model_selection import GridSearchCV","execution_count":null,"outputs":[]},{"metadata":{"id":"oMCDy87eppdG","colab_type":"text"},"cell_type":"markdown","source":"### Standardize the training and validation dataset"},{"metadata":{"trusted":true,"id":"HAZhyDLgppdH","colab_type":"code","colab":{}},"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler=StandardScaler()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"Ctkl17CWppdL","colab_type":"code","colab":{}},"cell_type":"code","source":"#Range of different hyperparameters\n#kernel=['rbf','linear','poly']\n#C=[0.001,0.01,0.1,1.0,10]\n#gamma=[0.001,0.01,0.1,1.0]\n\nkernel=['rbf']\nC=[0.001, 0.01, 1, 10,30]\ngamma=[0.001, .01, 1, 10]\n\nparams={'SVM__kernel':kernel, 'SVM__C':C, 'SVM__gamma':gamma}","execution_count":null,"outputs":[]},{"metadata":{"id":"Mp4bzO7dppdJ","colab_type":"text"},"cell_type":"markdown","source":"### We will use a Pipeline with feature scaling and model training as it's steps"},{"metadata":{"trusted":true,"id":"egVu-uQyppdJ","colab_type":"code","colab":{}},"cell_type":"code","source":"#Prepare a pipeline\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.svm import SVC","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"4Xfn5Cu7ppdN","colab_type":"code","colab":{}},"cell_type":"code","source":"#instantiate a class of SVM\nsvm = SVC(cache_size=7000, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"vCuWiZMmppdP","colab_type":"code","colab":{}},"cell_type":"code","source":"#Prepare a pipeline\nsteps=[('Scaler',StandardScaler()), ('SVM',svm)]\npipeline=Pipeline(steps)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"SLk-bsBvppdR","colab_type":"code","colab":{}},"cell_type":"code","source":"#use GridSearch for hyperparameter tuning\ngrid=GridSearchCV(n_jobs=-1, \n                  estimator=pipeline,\n                  param_grid=params,\n                  return_train_score=True,                   \n                  cv=3, \n                  verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"Cf8-qPM8ppdT","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":101},"outputId":"7ed2ff86-4876-4cd4-9a16-5e36675067f0"},"cell_type":"code","source":"grid.estimator.get_params","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"XxpgPwZsppdW","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":151},"outputId":"22caae9a-d777-4c77-d753-c7c559d12a25"},"cell_type":"code","source":"#\nstart=timer()\n#Train on traning dataset\ngrid.fit(train_x, train_y)\nend=timer()","execution_count":null,"outputs":[]},{"metadata":{"id":"1PnGfFgJCJX7","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"706e6782-dfea-4027-f861-b85f581555fc","trusted":true},"cell_type":"code","source":"print('Time taken to fit on train data (in min): {} '.format((end-start)/60))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"0KXcEWdippdY","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"9f78dc46-f05b-4cb4-885a-081171df3d98"},"cell_type":"code","source":"grid.best_params_","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"jlgAURCdppda","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"40344635-0469-475e-b84c-6a83f54accb7"},"cell_type":"code","source":"grid.best_score_, grid.best_params_['SVM__C'], grid.best_params_['SVM__gamma']","execution_count":null,"outputs":[]},{"metadata":{"id":"Uo8B_LVolnwN","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"id":"AAekl0jbk59E","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":551},"outputId":"2a7db552-8483-4cc3-e8ab-f871c4806f6c","trusted":true},"cell_type":"code","source":"cv_result= pd.DataFrame(grid.cv_results_)\ncv_result.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"pzl4qwKIlqpn","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":134},"outputId":"b9243868-036b-4ad3-d81c-7aba76ca8816","trusted":true},"cell_type":"code","source":"cv_result.columns","execution_count":null,"outputs":[]},{"metadata":{"id":"2iFXHPUnmjAZ","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":639},"outputId":"e6aaa4c4-e506-4d1a-8812-48fa7723d5da","trusted":true},"cell_type":"code","source":"cv_result[['rank_test_score','mean_test_score','mean_train_score','param_SVM__C','param_SVM__gamma']].sort_values(by='rank_test_score')","execution_count":null,"outputs":[]},{"metadata":{"id":"ZZwjQaJqnCov","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"-nLT58IOppdd","colab_type":"code","colab":{}},"cell_type":"code","source":"#Use the above values of C and gamma to train the model\nsvm_1 = SVC(C=grid.best_params_['SVM__C'], gamma=grid.best_params_['SVM__gamma'], kernel='rbf', random_state=21)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"Ezx4kA5Kppdf","colab_type":"code","colab":{}},"cell_type":"code","source":"#Re-create the pipeline model, with the SVM class corresponding to one with best parameters\nsteps=[('Scaler',StandardScaler()), ('SVM',svm_1)]\npipeline=Pipeline(steps)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"lMu5X5yOppdh","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":84},"outputId":"a65bd872-9fa8-4ebc-e4da-615dab384cbc"},"cell_type":"code","source":"start=timer()\npipeline.fit(train_x, train_y)\nend=timer()","execution_count":null,"outputs":[]},{"metadata":{"id":"N8p_b-HICgpg","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"29a04a61-e82b-4e36-debf-e8db0d07ae11","trusted":true},"cell_type":"code","source":"print('Time taken to fit on train data using best hyperparameter (in min): {} '.format((end-start)/60))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"ohQ76VdRppdi","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":50},"outputId":"535052df-cd94-4dfd-8107-22aac061d2f5"},"cell_type":"code","source":"test_pred=pipeline.predict(test_x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"cGw4xx3Xppdk","colab_type":"code","colab":{}},"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, roc_auc_score, classification_report, accuracy_score","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"sdWvKGs6ppdm","colab_type":"code","colab":{}},"cell_type":"code","source":"cmat=confusion_matrix(test_y, test_pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"V0xj6ttIppdo","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":185},"outputId":"353f3a90-c6bd-403b-a564-591ba7649b15"},"cell_type":"code","source":"cmat","execution_count":null,"outputs":[]},{"metadata":{"id":"vh5INGOcDVzC","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"b9cd7f9b-6759-4a69-fde7-2c7de39b9dc1","trusted":true},"cell_type":"code","source":"accuracy_score(test_y, test_pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pd.options.display.float_format= '${:,.4f}'.format","execution_count":null,"outputs":[]},{"metadata":{"id":"bSOTB3rzthGl","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":500},"outputId":"71af3405-6b01-465b-c175-b1c6198966d7","trusted":true},"cell_type":"code","source":"plt.figure(figsize=(15,8))\nsns.heatmap(cmat, annot=True, fmt='g')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"mhL6wJ6eppds","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":302},"outputId":"8e0c0f46-cd78-4626-af1e-a22cabe503d0"},"cell_type":"code","source":"print(classification_report(test_y, test_pred))","execution_count":null,"outputs":[]},{"metadata":{"id":"ZvMIVKMiuH0c","colab_type":"text"},"cell_type":"markdown","source":"'**micro'**: Calculate metrics globally by counting the total true positives, false negatives and false positives.<br>\n**'macro':** Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account."},{"metadata":{"id":"vgXlv4zPAwub","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"Q8fQ6l-gppdu","colab_type":"code","colab":{}},"cell_type":"code","source":"test_result=pd.concat([test_y, pd.Series(test_pred, name='predicted', index=test_y.index)],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"EMkzk-nyppdw","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":195},"outputId":"3a4541c9-a78a-44d8-d9f5-eebd90d3749f"},"cell_type":"code","source":"test_result.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"5MtoazSQppdz","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":215},"outputId":"61ae1fe2-6b4f-4e78-bd5f-fcacbb3422c8"},"cell_type":"code","source":"test_x.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"ZdPqDGkOppd2","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":669},"outputId":"1a815a5c-0989-46a8-f21a-6398a4e16971"},"cell_type":"code","source":"test_result.iloc[:21,1:]","execution_count":null,"outputs":[]},{"metadata":{"id":"ms0cGite3Njh","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"00dff09b-3b9e-4c20-963e-dfc0f71cfbbf","trusted":true},"cell_type":"code","source":"train_df.iloc[:15,:1].shape[1], test_result.iloc[:15,1:].shape[1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-output":true,"id":"g_UVDZZJppeA","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":484},"outputId":"91c4581a-62d7-48d5-fde7-26f76acf0486"},"cell_type":"code","source":"#Let's visualize how the predictions come out to be\ndraw_digit(test_x.iloc[:21,:],test_result.iloc[:21,1:],'Predicted')\n#draw_digit(train_df.iloc[:15,1:], train_df.iloc[:15,:1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"id":"Ok5BS0-YppeB","colab_type":"code","colab":{}},"cell_type":"code","source":"#digits that were incorrectly classified\nincorrect_dig=test_result[test_result.label != test_result['predicted']].index\n#draw_digit(test_x.iloc[:21,:],test_result.iloc[:21][['predicted']])","execution_count":null,"outputs":[]},{"metadata":{"id":"-5shUsmj4amc","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"test_xypred=pd.concat([test_x, test_result], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"id":"S1Es6xml86UH","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":215},"outputId":"fc303f3a-7481-4a92-a2f0-9eeb49f8f6e7","trusted":true},"cell_type":"code","source":"test_xypred.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"vq9zklzu88Z4","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":215},"outputId":"b5515424-b860-4f92-b558-23e5f425c251","trusted":true},"cell_type":"code","source":"test_xypred[test_xypred.label != test_xypred.predicted].head()","execution_count":null,"outputs":[]},{"metadata":{"id":"gE23n8uekmIB","colab_type":"text"},"cell_type":"markdown","source":"**Images of digits that were incorrectly predicted by the algorithm**"},{"metadata":{"id":"-EpHBu5ukRFI","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":484},"outputId":"6178eb3d-7b78-45c5-8d65-bced99dea02a","trusted":true},"cell_type":"code","source":"draw_digit(test_xypred[test_xypred.label != test_xypred.predicted].iloc[:21,:784],\\\n           test_xypred[test_xypred.label != test_xypred.predicted].iloc[:21,785:],'Predicted')","execution_count":null,"outputs":[]},{"metadata":{"id":"i_dIk-q2XKhN","colab_type":"text"},"cell_type":"markdown","source":"~190 (out of 7000) digits were incorrectly predicted"},{"metadata":{"id":"6I4D2fGY-bE2","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":34},"outputId":"c51e422c-92a1-4378-fb73-eb98de7e9ade","trusted":true},"cell_type":"code","source":"#Save the model for further testing\nfrom joblib import dump, load\ndump(pipeline, 'mnist_svm_pipeline.joblib') ","execution_count":null,"outputs":[]},{"metadata":{"id":"9td9DRJD-8rb","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"test_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"start=timer()\ntest_predict=test_pred=pipeline.predict(test_df)\nend=timer()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Time take to predict: {}'.format((end-start)/60))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_predict","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df.index","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submit_df =pd.concat([pd.Series(test_df.index, name='ImageId'), pd.Series(test_predict, name='Label')], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submit_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submit_df.to_csv('../submission.csv')","execution_count":null,"outputs":[]}],"metadata":{"colab":{"name":"# MNIST - Digit Recognizer with SVM.ipynb","version":"0.3.2","provenance":[]},"kernelspec":{"name":"python3","display_name":"Python 3"},"accelerator":"GPU"},"nbformat":4,"nbformat_minor":1}