<jupyter_start><jupyter_text>Exercise 6 - Support Vector Machines=====Support vector machines (SVMs) let us predict categories. This exercise will demonstrate a simple support vector machine that can predict a category from a small number of features. Our problem is that we want to be able to categorise which type of tree an new specimen belongs to. To do this, we will use features of three different types of trees to train an SVM. __Run the code__ in the cell below.<jupyter_code># Run this code!
# It sets up the graphing configuration.
import warnings
warnings.filterwarnings("ignore")
import matplotlib.pyplot as graph
%matplotlib inline
graph.rcParams['figure.figsize'] = (15,5)
graph.rcParams["font.family"] = 'DejaVu Sans'
graph.rcParams["font.size"] = '12'
graph.rcParams['image.cmap'] = 'rainbow'<jupyter_output><empty_output><jupyter_text>Step 1-----First, we will take a look at the raw data first to see what features we have. Replace `` with `print(dataset.head())` and then __run the code__.<jupyter_code>import pandas as pd
import numpy as np

# Loads the SVM library
from sklearn import svm

# Loads the dataset
dataset = pd.read_csv('trees.csv')

###
# REPLACE <printDataHere> with print(dataset.head()) TO PREVIEW THE DATASET
###
print(dataset.head())
###<jupyter_output>leaf_width  leaf_length  trunk_girth  trunk_height  tree_type
0        5.13         6.18         8.26          8.74          0
1        7.49         4.02         8.07          6.78          0
2        9.22         4.16         5.46          8.45          1
3        6.98        11.10         6.96          4.06          2
4        3.46         5.19         8.72         10.40          0<jupyter_text>It looks like we have _four features_ (leaf_width, leaf_length, trunk_girth, trunk_height) and _one label_ (tree_type).Let's plot it.__Run the code__ in the cell below.<jupyter_code># Run this code to plot the leaf features

# This extracts the features. drop() deletes the column we state (tree_type), leaving on the features
allFeatures = dataset.drop(['tree_type'], axis = 1)

# This keeps only the column we state (tree_type), leaving only our label
labels = np.array(dataset['tree_type'])

#Plots the graph
X = allFeatures['leaf_width']
Y = allFeatures['leaf_length']
color=labels
graph.scatter(X, Y, c = color)
graph.title('classification plot for leaf features')
graph.xlabel('leaf width')
graph.ylabel('leaf length')
graph.legend()
graph.show()<jupyter_output>No handles with labels found to put in legend.<jupyter_text>__Run the code__ in the cell below to plot the trunk features<jupyter_code># Run this code to plot the trunk features
graph.scatter(allFeatures['trunk_girth'], allFeatures['trunk_height'], c = labels)
graph.title('Classification plot for trunk features')
graph.xlabel('trunk girth')
graph.ylabel('trunk height')
graph.show()<jupyter_output><empty_output><jupyter_text>Step 2-----Lets make a support vector machine.The syntax for a support vector machine is as follows:__`model = svm.SVC().fit(features, labels)`__Your features set will be called __`train_X`__ and your labels set will be called __`train_Y`__ Let's first run the SVM in the cell below using the first two features, the leaf features.<jupyter_code># Sets up the feature and target sets for leaf features

# Feature 1
feature_one = allFeatures['leaf_width'].values

# Feature 2
feature_two = allFeatures['leaf_length'].values

# Features
train_X = np.asarray([feature_one, feature_two]).transpose()

# Labels
train_Y = labels 

# Fits the SVM model
###
# REPLACE THE <makeSVM> WITH THE CODE TO MAKE A SVM MODEL AS ABOVE
###
model = svm.SVC().fit(train_X, train_Y)
###
print("Model ready. Now plot it to see the result.")<jupyter_output>Model ready. Now plot it to see the result.<jupyter_text>Let's plot it! Run the cell below to visualise the SVM with our dataset.<jupyter_code># Run this to plots the SVM model
X_min, X_max = train_X[:, 0].min() - 1, train_X[:, 0].max() + 1
Y_min, Y_max = train_X[:, 1].min() - 1, train_X[:, 1].max() + 1

XX, YY = np.meshgrid(np.arange(X_min, X_max, .02), np.arange(Y_min, Y_max, .02))
Z = model.predict(np.c_[XX.ravel(), YY.ravel()]).reshape(XX.shape)

graph.scatter(feature_one, feature_two, c = train_Y, cmap = graph.cm.rainbow, zorder = 10, edgecolor = 'k', s = 40)
graph.contourf(XX, YY, Z, cmap = graph.cm.rainbow, alpha = 1.0)
graph.contour(XX, YY, Z, colors = 'k', linestyles = '--', alpha=0.5)

graph.title('SVM plot for leaf features')
graph.xlabel('leaf width')
graph.ylabel('leaf length')

graph.show()<jupyter_output><empty_output><jupyter_text>The graph shows three colored zones that the SVM has chosen to group the datapoints in. Color, here, means type of tree. As we can see, the zones correspond reasonably well with the actual tree types of our training data. This means that the SVM can group, for its training data, quite well calculate tree type based on leaf features.Now let's do the same using trunk features. In the cell below replace: 1. `` with `'trunk_girth'` 2. `` with `'trunk_height'` Then __run the code__.<jupyter_code># Feature 1
###--- REPLACE THE <addTrunkGirth> BELOW WITH 'trunk_girth' (INCLUDING THE QUOTES) ---###
###
trunk_girth = allFeatures['trunk_girth'].values
###

# Feature 2
###--- REPLACE THE <addTrunkHeight> BELOW WITH 'trunk_height' (INCLUDING THE QUOTES) ---###
trunk_height = allFeatures['trunk_height'].values
###

# Features
trunk_features = np.asarray([trunk_girth, trunk_height]).transpose()

# Fits the SVM model
model = svm.SVC().fit(trunk_features, train_Y)

# Plots the SVM model
X_min, X_max = trunk_features[:, 0].min() - 1, trunk_features[:, 0].max() + 1
Y_min, Y_max = trunk_features[:, 1].min() - 1, trunk_features[:, 1].max() + 1

XX, YY = np.meshgrid(np.arange(X_min, X_max, .02), np.arange(Y_min, Y_max, .02))
Z = model.predict(np.c_[XX.ravel(), YY.ravel()]).reshape(XX.shape)

graph.scatter(trunk_girth, trunk_height, c = train_Y, cmap = graph.cm.rainbow, zorder = 10, edgecolor = 'k', s = 40)
graph.contourf(XX, YY, Z, cmap = graph.cm.rainbow, alpha = 1.0)
graph.contour(XX, YY, Z, colors = 'k', linestyles = '--', alpha = 0.5)

graph.title('SVM plot for leaf features')
graph.xlabel('trunk girth')
graph.ylabel('trunk height')

graph.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12254667304930589, 'fraction_numerical': 0.020264879527684698, 'mean_word_length': 4.2364243943191315, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 0, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '130178', 'license': 'MIT', 'n_tokens_mistral': 2176, 'n_tokens_neox': 2079, 'n_words': 698, 'path': '06. Support Vector Machines - Python.ipynb', 'repo_name': 'MontserratGonzalezM/ms-learn-ml-crash-course-python', 'size': '6267'}