Using the iris dataset from sklearn provide code to train andtest a support vector classification (SVC) model.
After you load the data and labels, split the data and labelsinto training and testing sets using the test_train_split functionfrom the sklearn.model_selection package. You can look at itsdocumentation here. This function works similarly to the functionyou wrote for last week’s Lecture Activity, except that it worksout the number of categories itself and it returns the split arraysin a different order.
To make sure that the split preserves the proportion (as you didlast week), pass the option stratify=labels to the test_train_splitfunction.
Next, train the SVC model by first creating a classifier objectby calling:
from sklearn import svm # import support vector machine packagesvc = svm.SVC(kernel=’linear’) # create an SVC object with a linear kernel (separate data with hyperplanes)
and then calling its fit function on the training data youobtained from the split.
Now test your SVC model by calling its predict function on thetesting data you obtained from the split and then computing itsaccuracy (proportion of the predicted labels [iris types] that itgets correct).
Finally, repeat the process with a new SVCobject (call it svc2) that uses a polynomial kernel (using theoption kernel=’poly’ instead of kernel=’linear’).
Expert Answer
Answer to Using the iris dataset from sklearn provide code to train and test a support vector classification (SVC) model. After yo…