摘要
hello,又见面了,这次写的是New York university homework4 ,题目是SVM Classifier with different kernels
首先,了解一下数据集以及目标
Dataset: using the one of most popular classification dataset which is Iris dataset. Iris dataset is having 4 features of iris flower and one target class. The flower species type is the target class which has 3 types.
The idea of implementing SVM classifier in Python is to use the iris features to train an svm classifier and use the trained svm model to predict the Iris species type.
1, Importing Iris dataset from Scikit-Learn and understand Iris dataset
# Required Packages
from sklearn import datasets
from sklearn import svm
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import pandas as pd
%matplotlib inline
# import iris data to model Svm classifier (3 points)
iris = datasets.load_iris()
# Using the DESCR key over the iris_dataset to describ the dataset (3 points)
iris.DESCR
# To get the iris features and the target classes (3 points)
X = iris.data
y = iris.target
# To check the target data (3 points)
print(y)
2, Visualizing the Iris dataset
2.1 Visualizing the relationship between sepal and target classes
X = iris.data[:,:2]
y = iris.target
plt.scatter(X[y==0,0],X[y==0,1],color = 'r',marker='o')
plt.scatter(X[y==1,0],X[y==1,1],color = 'b',marker='*')
plt.scatter(X[y==2,0],X[y==2,1],color = 'g',marker='+')
plt.title('the relationship between sepal and target classes')
plt.xlabel('sepal length')
plt.ylabel('sepal width')
plt.show()
结果:
2.2 Visualizing the relationship between Petal and target classes
X = iris.data[:,2:]
y = iris.target
plt.scatter(X[y==0,0],X[y==0,1],color = 'r',marker='o')
plt.scatter(X