1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 利用SVM sklearn对iris数据集进行分类

利用SVM sklearn对iris数据集进行分类

时间:2019-10-22 12:11:35

相关推荐

利用SVM sklearn对iris数据集进行分类

摘要

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 datasetsfrom sklearn import svmimport numpy as npimport matplotlib.pyplot as pltfrom sklearn.model_selection import train_test_splitimport 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.datay = 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.targetplt.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.targetplt.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 Petal and target classes')plt.xlabel('Petal length')plt.ylabel('Petal width')plt.show()

结果:

3. Modeling Different Kernel SVM classifier using Iris Sepal features: with kernels of linear, RBF, and polynomial with degree=3

X_train, X_test, y_train, y_test = train_test_split(iris.data[:,:2], iris.target, test_size=0.3, random_state=0)lin_svc = svm.SVC(kernel='linear').fit(X_train, y_train)rbf_svc = svm.SVC(kernel='rbf').fit(X_train, y_train)poly_svc = svm.SVC(kernel='poly', degree=3).fit(X_train, y_train)

4. Visualizing the modeled svm classifiers with Iris Sepal features

Notice: there are 3 plots to describ Iris Sepal features with svm classifiers about SVC with linear kernel, SVC with RBF kernel, SVC with polynomial (degree=3) kernel

# the step of the gridh = .02 # to create the grid , so that we can plot the images on itx_min, x_max = X_train[:, 0].min() - 1, X_train[:, 0].max() + 1y_min, y_max = X_train[:, 1].min() - 1, X_train[:, 1].max() + 1xx, yy = np.meshgrid(np.arange(x_min, x_max, h),np.arange(y_min, y_max, h))# the title of the graphtitles = ['LinearSVC (linear kernel)','SVC with RBF kernel','SVC with polynomial (degree 3) kernel']for i, clf in enumerate((lin_svc, rbf_svc, poly_svc)):# to plot the edge of different classes# to create a 2*2 grid , and set the i image as current imageplt.subplot(2, 2, i + 1) # set the margin between different sub-plotplt.subplots_adjust(wspace=0.4, hspace=0.4)# SVM input :xx and yy output: an arrayZ = clf.predict(np.c_[xx.ravel(), yy.ravel()]) # to plot the resultZ = Z.reshape(xx.shape) #(220, 280)plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.8) plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired)plt.xlabel('Sepal length')plt.ylabel('Sepal width')plt.xlim(xx.min(), xx.max())plt.ylim(yy.min(), yy.max())plt.xticks(())plt.yticks(())plt.title(titles[i])plt.show()

结果:

5. Accuracy of predicting Iris Sepal features by the 3 kernels of linear, RBF and polynomial with degree=3

lin_svc_pre = lin_svc.predict(X_test)acc_lin_svc = sum(lin_svc_pre==y_test)/len(y_test)rbf_svc_pre = rbf_svc.predict(X_test)acc_rbf_svc = sum(rbf_svc_pre==y_test)/len(y_test)poly_svc_pre = poly_svc.predict(X_test)acc_poly_svc = sum(poly_svc_pre==y_test)/len(y_test)print(acc_lin_svc)print(acc_rbf_svc)print(acc_poly_svc)

6. Modeling Different Kernel SVM classifier using Iris Petal features: with kernels of linear, RBF and polynomial with degree=3

X_train, X_test, y_train, y_test = train_test_split(iris.data[:,2:], iris.target, test_size=0.3, random_state=0)svc = svm.SVC(kernel='linear').fit(X_train, y_train)lin_svc = svm.SVC(kernel='linear').fit(X_train, y_train)rbf_svc = svm.SVC(kernel='rbf').fit(X_train, y_train)poly_svc = svm.SVC(kernel='poly', degree=3).fit(X_train, y_train)print(X_train)

7.Visualizing the modeled svm classifiers with Iris Petal features

Notice: there are 3 plots to describ Iris Pepal features with svm classifiers about SVC with linear kernel, SVC with RBF kernel, SVC with polynomial (degree=3) kernel

# the step of the gridh = .02 # to create the grid , so that we can plot the images on itx_min, x_max = X_train[:, 0].min() - 1, X_train[:, 0].max() + 1y_min, y_max = X_train[:, 1].min() - 1, X_train[:, 1].max() + 1xx, yy = np.meshgrid(np.arange(x_min, x_max, h),np.arange(y_min, y_max, h))# the title of the graphtitles = ['LinearSVC (linear kernel)','SVC with RBF kernel','SVC with polynomial (degree 3) kernel']for i, clf in enumerate(( lin_svc, rbf_svc, poly_svc)):# to plot the edge of different classes# to create a 2*2 grid , and set the i image as current imageplt.subplot(2, 2, i + 1) # set the margin between different sub-plotplt.subplots_adjust(wspace=0.4, hspace=0.4)# SVM input :xx and yy output: an arrayZ = clf.predict(np.c_[xx.ravel(), yy.ravel()]) # to plot the resultZ = Z.reshape(xx.shape) #(220, 280)plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.8) plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired)plt.xlabel('Petal length')plt.ylabel('Petal width')plt.xlim(xx.min(), xx.max())plt.ylim(yy.min(), yy.max())plt.xticks(())plt.yticks(())plt.title(titles[i])plt.show()

结果:

8.Accuracy of predicting Iris Petal features by the 3 kernels of linear, RBF and polynomial with degree=3

lin_svc_pre = lin_svc.predict(X_test)acc_lin_svc = sum(lin_svc_pre==y_test)/len(y_test)rbf_svc_pre = rbf_svc.predict(X_test)acc_rbf_svc = sum(rbf_svc_pre==y_test)/len(y_test)poly_svc_pre = poly_svc.predict(X_test)acc_poly_svc = sum(poly_svc_pre==y_test)/len(y_test)print(acc_lin_svc)print(acc_rbf_svc)print(acc_poly_svc)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。