<jupyter_start><jupyter_text>Introductory applied machine learning (INFR10069)   Lab 3: Support Vector Machine (SVM) Classification and Evaluation In this lab we initially re-examine the spam filtering problem from Lab 2. This time, we train a Logistic Regression model and a linear Support Vector Machine for the spam or non-spam classification task. In the second part of the lab we examine classification evaluation by using a K-nearest neighbour classifier.All the datasets that you will need for this lab are located at the `./datasets` directory which is adjacent to this file.<jupyter_code># Import packages
from __future__ import division, print_function # Imports from __future__ since we're running Python 2
import os
import numpy as np 
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.linear_model import LogisticRegression
from sklearn.svm import LinearSVC, SVC
from sklearn.neighbors import KNeighborsClassifier
KNeighboursClassifier = KNeighborsClassifier
from sklearn.metrics import confusion_matrix, log_loss
%matplotlib inline<jupyter_output><empty_output><jupyter_text>1. Spam filtering  ========== Question 1.1 ==========Load `spambase_binary.csv` into a pandas DataFrame structure called `spambase`. Display the number of instances and attributes and the first 5 samples. Remember that the attributes have been binarised. The instances have also been shuffled (i.e. their order has been randomised).<jupyter_code># Your code goes here<jupyter_output><empty_output><jupyter_text>========== Question 1.2 ==========We are going to use hold-out validation to evaluate our models below. Split the dataset into training and testing subsets; there is a [built-in sklearn function](http://scikit-learn.org/0.17/modules/generated/sklearn.cross_validation.train_test_split.html) to do this. Call the resulting matrices `X_train`, `X_test`, `y_train`, `y_test`. Use 90% of the data for training and the remaining 10% for testing. Make sure you don't include the target variable `is_spam` in the input features (`X_train` / `X_test`)!If you want to be able to reproduce your results exactly, what argument must you remember to set?<jupyter_code># Your code goes here<jupyter_output><empty_output><jupyter_text>*Your answer goes here*  ========== Question 1.3 ==========Train a [`LogisticRegression`](http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html) classifier by using training data. Use the `lbfgs` solver and default settings for the other parameters. Report the classification accuracy on both the training and test sets. Does your classifier generalise well on unseen data?<jupyter_code># Your code goes here<jupyter_output><empty_output><jupyter_text>*Your answer goes here*  ========== Question 1.4 ==========Print the coefficients for class 1 for the attributes `word_freq_hp_binarized` and `char_freq_$_binarized`. Generally, we would expect the string `$` to appear in spam, and the string `hp` to appear in non-spam e-mails, as the data was collected from HP Labs. Do the regression coefficients make sense given that class 1 is spam? *Hint: Consider the sigmoid function and how it transforms values into a probability between 0 and 1. Since our attributes are boolean, a positive coefficient can only increase the total sum fed through the sigmoid and thus move the output of the sigmoid towards 1. What can happen if we have continuous, real-valued attributes?*<jupyter_code># Your code goes here<jupyter_output><empty_output><jupyter_text>*Your answer goes here*  ========== Question 1.5 ==========Train a [`LinearSVC`](http://scikit-learn.org/stable/modules/generated/sklearn.svm.LinearSVC.htmlsklearn.svm.LinearSVC) (i.e. Linear Support Vector classifier) by using default parameters. Report the classification accuracy on the training and test sets.<jupyter_code># Your code goes here<jupyter_output><empty_output><jupyter_text>========== Question 1.6 ==========What are the coefficients for the attributes `word_freq_hp_binarized` and `char_freq_`$`_binarized`? Compare these to the ones you found with Logistic Regression.<jupyter_code># Your code goes here<jupyter_output><empty_output><jupyter_text>*Your answer goes here*  ========== Question 1.7 ==========How does a linear SVM relate to Logistic Regression? *Hint: Consider the classification boundary learnt in each model.* *Your answer goes here*  ========== Question 1.8 ==========By using the [`SVC`](http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.htmlsklearn.svm.SVC) class train two new support vector classifiers with Gaussian (`rbf`) and polynomial (`poly`) kernels. Again, report classification accuracies on training and test sets and compare with your results from Question 1.5.<jupyter_code># Your code goes here<jupyter_output><empty_output><jupyter_text>*Your answer goes here*  2. Performance assessmentWe will now look at a few ways of assessing the performance of a classifier. To do so we will introduce a new data set, the [Splice](https://archive.ics.uci.edu/ml/datasets/Molecular+Biology+%28Splice-junction+Gene+Sequences%29) data set. The classification task is to identify `intron` and `exon` boundaries on gene sequences. For more information, you can read the dataset description in the link. The class attribute can take on 3 values: `N`, `IE` and `EI`.  ========== Question 2.1 ==========Load the `splice_train.csv` and `splice_test.csv` into two separate dataframes. Display the shape and first 10 instances for each dataframe.<jupyter_code># Your code goes here<jupyter_output><empty_output><jupyter_text>========== Question 2.2 ========== Convert the categorical attributes into numeric ones by using the [`LabelEncoder`](http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html) tool. Make sure to not transform the target variable (`class`).<jupyter_code># Your code goes here<jupyter_output><empty_output><jupyter_text>========== Question 2.3 ==========Store the training and testing data into numpy arrays `X_train`, `y_train`, `X_test` and `y_test`. Display the shapes of the four arrays.<jupyter_code># Your code goes here<jupyter_output><empty_output><jupyter_text>========== Question 2.4 ==========Familiarise yourself with [Nearest Neighbours Classification](http://scikit-learn.org/stable/modules/neighbors.htmlclassification). Use a [`KNeighborsClassifier`](http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.htmlsklearn.neighbors.KNeighborsClassifier)by using a single neighbour. Report the classification accuracy on the training set.<jupyter_code># Your code goes here<jupyter_output><empty_output><jupyter_text>========== Question 2.5 ==========Is the above result meaningful? Why is testing on the training data a particularly bad idea for a 1-nearest neighbour classifier? Do you expect the performance of the classifier on a test set to be as good? *Your answer goes here*  ========== Question 2.6 ==========Now report the classification accuracy on the test set and check your expectations.<jupyter_code># Your code goes here<jupyter_output><empty_output><jupyter_text>========== Question 2.7 ==========Plot a histogram of the target variable (i.e. `class`) in the test set. *Hint: matplotlib won't allow you to plot a histogram for categorical values. Instead, you can use Pandas bulit-in bar plot tool in conjunction with the [`value_counts`](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.value_counts.html).<jupyter_code># Your code goes here<jupyter_output><empty_output><jupyter_text>========== Question 2.8 ==========What would be the accuracy of the classifier, if all points were labelled as `N`? **Pro Tip** - You should always use a ['Dummy Model'](http://scikit-learn.org/stable/modules/model_evaluation.htmldummy-estimators) (a ridiculously simple model) like this to compare with your 'real' models. It's very common for complex models to be outperformed by a simple model, such as predicting the most common class. When complex models are outperformed by 'Dummies', you should investigate why: often there was an issue with the code, the data, or the way the model works was misunderstood.<jupyter_code># Your code goes here<jupyter_output><empty_output><jupyter_text>========== Question 2.9 ==========Now we want to explore the effect of the `k` parameter. To do this, train the classifier multiple times, each time setting the KNN option to a different value. Try `5`, `10`, `50`, `100`, `200`, `500`, `1000`, `1500` and `2000` and test the classifier on the test set. How does the k parameter effect the results? *Hint: Consider how well the classifier is generalising to previously unseen data, and how it compares to the dumb prediction accuracy.*<jupyter_code># Your code goes here<jupyter_output><empty_output><jupyter_text>*Your answer goes here*  ========== Question 2.10 ==========Plot the results (k-value on the x-axis and classification accuracy on the y-axis), making sure to mark the axes. Can you conclude anything from observing the plot?<jupyter_code># Your code goes here<jupyter_output><empty_output><jupyter_text>*Your answer goes here*  ========== Question 2.11 ==========Select best value for `k` from Questions 2.9 and 2.10 and plot the normalised confusion matrix on the test set. Then plot the confusion matrix for a 5-nearest neighbour classifier. What do you observe?<jupyter_code># Your code goes here<jupyter_output><empty_output><jupyter_text>*Your answer goes here*  ========== Question 2.12 ==========Read about the [logarithimic loss](http://scikit-learn.org/stable/modules/generated/sklearn.metrics.log_loss.html) (or cross-entropy loss). It is often the error metric used when we are trying to optimise classification models.This metric takes as input the true labels and the estimated probability distributions (bernouli or multinomial). It makes sense to use this metric when we are interested not only in the predicted labels, but also in the confidence with which these labels are predicted.For instance, think of the situation where you have a single test point and two classifiers. Both classifiers predict the label correctly, however classifier A predicts tha the test point belongs to the class with probability 0.55, whereas classifier B predicts the correct class with probability 0.99. Classification accuracy would be the same for the two classifiers (why?) but the `log_loss` metric would indicate that classifier B should be favoured.Produce a scatter plot similar to the one in Question 2.10 but this time show `log_loss` on your y axis. Which value for `k` would you pick if `log_loss` was the error metric? Comment on whether accuracy or logarithmic loss is better for selecting a model.<jupyter_code># Your code goes here<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10342622201918684, 'fraction_numerical': 0.01068981269986295, 'mean_word_length': 5.945431472081219, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 77, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '589413', 'license': 'MIT', 'n_tokens_mistral': 3132, 'n_tokens_neox': 2905, 'n_words': 1297, 'path': '06_Lab_3_SVM_Classification_Evaluation.ipynb', 'repo_name': 'CaesarZhang070497/Iaml', 'size': '10945'}