This particular post explores Support Vector Machines, or SVM. Please see part 1 of this series to get an explanation of the problem and data preparation steps.

SVM is a supervised learning technique used for both regression or classification. It works by reprojecting the coordinates of each data row in n-dimensional space. The algorithm seeks to find the hyperplane in n-dimensional space that maximizes the distance between the classes as defined by the label. The strength of the algorithm is dependent on the data and how well the classes can be differentiated by the hyperplane. For this post we are performing a multi-class classification with three classes.

I did all of the analysis in Python using Jupyter notebooks, Pandas and Scikit-Learn.

First we randomize the order of the dataframe and split into training and test sets, using an 80%/20% split. Some data science references recoemmend a 70%/20%/10% split between train/test/validation, where 10% of the dataset is held completely separate and used for cross-validation purposes. If you are doing enough folds for the cross-validation (I did 10 fold) I think it is safe to have an 80%/20% split as it is highly unlikely that a systemically bad split would influence all of the validation attempts.

The code below is the guts of the analysis. I create a Pipeline object in Scikit-Learn. The pipeline has three major components: the vectorizer, the transformer and the classifier. Once you specify the steps of the pipeline, the next section specified the parameters for each component of the pipeline. The Support Vector Classifier can take many parameters, only some of which are listed below. Changing some of these parameters will have a small impact on the model results and others can have a huge impact; especially the kernel function. In the example below it is set to linear, so the solution will use a linear function to separate the classes. There are other options like polynomial, rbf (radial basis function), etc.

The final section is where you outline the cross-validation steps. By doing a grid search you can also identify the optimal hyperparameter values employing a grid search technique for each run through the pipeline.

Below is the code to run the pipeline and print out the optimal hyperparameter values from the grid search. Please note that for each unique combination of parameters, it will do a 10 fold cross validation so for the model as it is constructed right now, will iterate through many dozens of models (and thus take a long time). But it's a great technique to run through many iterations overnight and see what the best hyperparameter results are at the end.

So, the results are a bit of a mixed bag. The model did a great job of identifying legit webform messages (97.7% True Positive), and a reasonably good job of identifying trash messages. Misfire message identification was not good, with more misfire messages classified as legit rather than misfire (67.1% vs 23.4%).

Potential Next Steps

The magnitude of the disparity in the misclassifications implies that tweaking hyperparameters will not suffice to improve the model enough. So trying a completely different classification approach could be warranted. The results for the legit class are very encouraging so my inclination for a next step is to do some feature engineering and add variable(s) to the model. Specifically doing POS (Part Of Speech) tagging on the total_text field or even adding the respective lengths of the strings that make up the subject and description field could be warranted for inclusion into the model to improve performance.