摘要

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[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 grid
h = .02
# to create the grid , so that we can plot the images on it
x_min, x_max = X_train[:, 0].min() - 1, X_train[:, 0].max() + 1
y_min, y_max = X_train[:, 1].min() - 1, X_train[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),np.arange(y_min, y_max, h))# the title of the graph
titles = ['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 grid
h = .02
# to create the grid , so that we can plot the images on it
x_min, x_max = X_train[:, 0].min() - 1, X_train[:, 0].max() + 1
y_min, y_max = X_train[:, 1].min() - 1, X_train[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),np.arange(y_min, y_max, h))# the title of the graph
titles = ['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)

利用SVM,sklearn对iris数据集进行分类相关推荐

  1. 机器学习笔记2 – sklearn之iris数据集

    前言 本篇我会使用scikit-learn这个开源机器学习库来对iris数据集进行分类练习. 我将分别使用两种不同的scikit-learn内置算法--Decision Tree(决策树)和kNN(邻 ...

  2. 用matlab实现用Bp神经网络对iris数据集进行分类(以及影响分类性能的参数条件)

    数据集已上传,结尾链接下载即可!!! 一.实验内容 Iris鸢尾花卉数据集,是一类多重变量分析的数据集.数据集包含150个数据样本,分为3类,每类50个数据,每个数据包含4个属性,分别对应花萼长度,花 ...

  3. [机器学习-sklearn]鸢尾花Iris数据集

    鸢尾花数据集 1. 鸢尾花Iris数据集介绍 2. Sklearn代码获取Iris 2. 描述性统计 3. 箱线图 4. 数据分布情况 1. 鸢尾花Iris数据集介绍 Iris flower数据集是1 ...

  4. python聚类分析散点图_使用sklearn对iris数据集进行聚类分析

    导入库 import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns fro ...

  5. (转载)基于sklearn的iris数据集及简介

    (一)iris数据集简介 Iris数据集是机器学习任务中常用的分类实验数据集,由Fisher在1936收集整理.Iris中文名是安德森鸢尾花卉数据集,英文全称是Anderson's Iris data ...

  6. Sklearn学习-iris数据集学习

    Sklearn学习-逻辑回归(iris数据集) 使用load_iris加载数据集,查看包含的keys 查看数据的列名,分类目标的名称 获取data和target,并打印各自的shape 拆分训练集和测 ...

  7. 支持向量机SVM Iris数据集 分类预测

    目录 支持向量机对iris数据集进行分类预测 1. 基础概念 2. 实验步骤与分析 2.1  数据理解 2.2  数据读入 2.3  训练集和测试集划分 2.4  支持向量机 2.5  预测 2.6  ...

  8. Python实现knn分类算法(Iris 数据集)

    1.KNN分类算法 KNN分类算法(K-Nearest-Neighbors Classification),又叫K近邻算法,是一个概念极其简单,而分类效果又很优秀的分类算法. 他的核心思想就是,要确定 ...

  9. ML之SVM:利用SVM算法(超参数组合进行多线程网格搜索+3fCrVa)对20类新闻文本数据集进行分类预测、评估

    ML之SVM:利用SVM算法(超参数组合进行多线程网格搜索+3fCrVa)对20类新闻文本数据集进行分类预测.评估 目录 输出结果 设计思路 核心代码 输出结果 Fitting 3 folds for ...

最新文章

  1. linux下kerberos教程
  2. python培训班排行榜-深圳python培训机构排行榜
  3. 20181204-1 每周例行报告
  4. Case Study. Technical and Commercial understating. Internal use only.
  5. java 只显示文本文件_Java设计并实现一个应用程序,能够读取一个文本文件中的内容并显示,同时能够计算出文本中的行数。...
  6. JVM系列之:从汇编角度分析Volatile
  7. 浅谈 UNIX、Linux、ios、android 他们之间的关系
  8. 线谱法 时钟分量的提取 matlab,LMD局域均值分解的matlab程序及示例
  9. word vba 打开弹出msgbox,每隔10秒刷新一次域值。
  10. 内网信息安全厂商对客户的误导
  11. C# ActiveX开发部署更新
  12. WebApi实现验证授权Token,WebApi生成文档等
  13. 小学生都能听懂的傅里叶变换讲解
  14. 教你怎么短期内备考并通过PMP考试!
  15. 固高板卡mct2008调试轴回零_固高运动控制的Home回零过程
  16. 谷粒商城开发代码笔记记录
  17. mysql中exec语句,sqlserver 中EXEC和sp
  18. 信息安全之加密域可逆信息隐藏
  19. openpbs环境下GPU版NAMD的作业提交问题
  20. maven安装测试报JAVA_HOME路径安装错误

热门文章

  1. 别人问我:为什么程序员都不善言辞?惭愧啊!
  2. 美国SIG声学相机G100主要功能
  3. 详解java集合框架
  4. 两个不同的自然数A和B,如果整数A的全部因子(包括1,不包括A本身)之和等于B;且整数B的全部因子(包括1,不包括B本身)之和等于A,则将整数A和B称为亲密数。求3000以内的全部亲密数。
  5. AVplayer断网播放出错时player的duration、playableDuration、totalTime
  6. freeBSD的VNET_DEFINE跟SYSCTL_VNET_INT
  7. System源码浅析- initializeSystemClass(initProperties)
  8. Fireworks 8 编辑图片使用技巧1
  9. OATS-正交表测试策略
  10. 如何“谨慎”使用“数据驱动”的风控模型(二)——决策篇