SVM训练莺尾花数据集

代码在莺尾花数据集上训练SVM,数据集由莺尾花的测量值及其相应的物种标签组成。该模型使用70%数据用于训练,然后剩余部分进行测试。其中′fit′'fit'′fit′方法在训练集上训练数据,′score′'score'′score′数据在返回模型的测试数据上的准确性:

注:其实这篇文章完全由openAI的chatGPT完成,包括代码部分,这里我将生成的英文原意和中文翻译都进行展示

代码

代码如下:

import numpy as np
from sklearn import datasets
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt# Load the iris dataset
iris = datasets.load_iris()
X = iris["data"]
y = iris["target"]# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)# Create the SVM model
model = SVC()# Train the model on the training data
model.fit(X_train, y_train)# Test the model on the test data
accuracy = model.score(X_test, y_test)# Print the test accuracy
print("Test accuracy: {:.2f}".format(accuracy))# Plot the data
fig, ax = plt.subplots()
colors = ["r", "g", "b"]
for i in range(3):indices = np.where(y == i)[0]ax.scatter(X[indices, 0], X[indices, 1], c=colors[i], label=iris["target_names"][i])
ax.legend()
plt.show()

代码相关解释


代码解释

import numpy as np
from sklearn import datasets
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

在这些行中,我们导入必要的库。numpy是 Python 中用于科学计算的库,我们将使用它来存储和操作数据。datasets是scikit-learn库中的一个模块,其中包含许多可用于机器学习的数据集,我们将使用它来加载鸢尾花数据集。SVC是scikit-learn库中支持向量机 (SVM) 的类,我们将使用它来创建 SVM 模型。是我们将用于将数据拆分为训练集和测试集的模块中的train_test_split一个函数。最后,是库中的一个模块,我们将使用它来绘制数据。model_selection,scikit-learn,pyplot,matplotlib

# Load the iris dataset
iris = datasets.load_iris()
X = iris["data"]
y = iris["target"]

在这些行中,我们从模块加载 iris 数据集datasets并将其存储在一个名为iris. 鸢尾花数据集是一个类似字典的对象,其中包含数据和数据标签。我们将数据存储在一个名为 的变量X中,将标签存储在一个名为 的变量中y。数据由每个样本的四个特征(萼片长度、萼片宽度、花瓣长度和花瓣宽度)组成,标签是对应的鸢尾属植物属性

# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

在这一行中,我们使用该train_test_split函数将数据和标签拆分为训练集和测试集。该test_size参数指定应该用于测试的数据部分。在这种情况下,我们使用 30% 的数据进行测试,70% 的数据用于训练。该函数返回四个数组:X_trainandy_train包含训练数据和标签,X_test和y_test包含测试数据和标签。

# Create the SVM model
model = SVC()

SVC在这一行中,我们使用该类创建一个 SVM 模型。我们不需要指定任何参数,因此我们可以像调用函数一样简单地创建类的实例

# Train the model on the training data
model.fit(X_train, y_train)

在这一行中,我们使用对象的fit方法model在训练数据上训练模型。该方法有两个参数:数据和标签。

# Test the model on the test data
accuracy = model.score(X_test, y_test)# Print the test accuracy
print("Test accuracy: {:.2f}".format(accuracy))

在这些行中,我们使用对象的score方法model来评估模型在测试数据上的准确性。该方法有两个参数:测试数据和测试标签。它返回一个介于 0 和 1 之间的浮点数,其中 1 表示完美的精度。我们使用字符串格式将准确性打印到控制台。

# Plot the data
fig, ax = plt.subplots()
colors = ["r", "g", "b"]
for i in range(3):indices = np.where(y == i)[0]ax.scatter(X[indices, 0], X[indices, 1], c=colors[i], label=iris["target_names"][i])
ax.legend()
plt.show()

在这些行中,我们使用subplots函数 frommatplotlib创建一个图形和一个轴对象,然后我们使用scatter轴对象的方法绘制数据。我们遍历三种鸢尾,对于每个物种,我们使用where函数 from选择具有相应标签的样本numpy。然后我们使用该方法绘制这些样本scatter,其中萼片长度为 x 轴,萼片宽度为 y 轴。我们为每个物种使用不同的颜色,并使用该legend方法向图中添加图例。最后,我们使用show函数来显示绘图。


原文

Certainly! Here is a more detailed explanation of the code:

import numpy as np
from sklearn import datasets
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

In these lines, we import the necessary libraries. numpy is a library for scientific computing in Python, and we will use it to store and manipulate the data. datasets is a module from the scikit-learn library that contains a number of datasets that can be used for machine learning, and we will use it to load the iris dataset. SVC is a class for support vector machines (SVMs) in the scikit-learn library, and we will use it to create an SVM model. train_test_split is a function from the model_selection module of scikit-learn that we will use to split the data into training and test sets. Finally, pyplot is a module from the matplotlib library that we will use to plot the data.

# Load the iris dataset
iris = datasets.load_iris()
X = iris["data"]
y = iris["target"]

In these lines, we load the iris dataset from the datasets module and store it in a variable called iris. The iris dataset is a dictionary-like object that contains the data and the labels for the data. We store the data in a variable called X, and the labels in a variable called y. The data consists of four features for each sample (sepal length, sepal width, petal length, and petal width), and the labels are the corresponding species of iris

# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

In this line, we use the train_test_split function to split the data and labels into training and test sets. The test_size argument specifies the fraction of the data that should be used for testing. In this case, we are using 30% of the data for testing and 70% for training. The function returns four arrays: X_train and y_train contain the training data and labels, and X_test and y_test contain the test data and labels.

# Create the SVM model
model = SVC()

In this line, we create an SVM model using the SVC class. We don’t need to specify any arguments, so we can simply create an instance of the class by calling it like a function.

# Train the model on the training data
model.fit(X_train, y_train)

In this line, we use the fit method of the model object to train the model on the training data. The method takes two arguments: the data and the labels.

# Test the model on the test data
accuracy = model.score(X_test, y_test)# Print the test accuracy
print("Test accuracy: {:.2f}".format(accuracy))

In these lines, we use the score method of the model object to evaluate the accuracy of the model on the test data. The method takes two arguments: the test data and the test labels. It returns a floating point number between 0 and 1, where 1 represents perfect accuracy. We print the accuracy to the console using string formatting.

# Plot the data
fig, ax = plt.subplots()
colors = ["r", "g", "b"]
for i in range(3):indices = np.where(y == i)[0]ax.scatter(X[indices, 0], X[indices, 1], c=colors[i], label=iris["target_names"][i])
ax.legend()
plt.show()

In these lines, we use the subplots function from matplotlib to create a figure and an axis object, and then we use the scatter method of the axis object to plot the data. We loop over the three species of iris, and for each species, we select the samples that have the corresponding label using the where function from numpy. We then plot these samples using the scatter method, with the sepal length as the x-axis and the sepal width as the y-axis. We use different colors for each species, and we add a legend to the plot using the legend method. Finally, we use the show function to display the plot.

SVM训练莺尾花数据集相关推荐

  1. 莺尾花数据集–贝叶斯分类(day5)

    莺尾花数据集–贝叶斯分类 Step1: 库函数导入 import warnings warnings.filterwarnings('ignore') #忽略了警告错误的输出 import numpy ...

  2. 莺尾花数据集--kNN分类

    Step1: 库函数导入 import numpy as np # 加载莺尾花数据集 from sklearn import datasets # 导入KNN分类器 from sklearn.neig ...

  3. k-近邻(应用sklearn的莺尾花数据集)

    kNN 的作用机制为 在目标周围选取最近k个点,这k个点哪种占比最大,就可以把这个目标分类到那个分类,即有分到相似属性多的类别.  该算法和回归,决策树不同之处是,回归和决策树是通过训练集确定参数,参 ...

  4. KNN实战莺尾花数据集

    0 前言 俗话说,实践才能出真理,能动手就不要逼逼,下面将利用莺尾花数据集实战KNN算法. 1 实战概述 首先,要介绍一下这个实战的整体思路:要做什么.怎么做!第一步,我们应该拿到数据集,了解数据集信 ...

  5. ML之kNNC:基于iris莺尾花数据集(PCA处理+三维散点图可视化)利用kNN算法实现分类预测daiding

    ML之kNNC:基于iris莺尾花数据集(PCA处理+三维散点图可视化)利用kNN算法实现分类预测 目录 基于iris莺尾花数据集(PCA处理+三维散点图可视化)利用kNN算法实现分类预测 设计思路 ...

  6. DL之PerceptronAdalineGD:基于iris莺尾花数据集利用Perceptron感知机和AdalineGD算法实现二分类

    DL之Perceptron&AdalineGD:基于iris莺尾花数据集利用Perceptron感知机和AdalineGD算法实现二分类 目录 基于iris莺尾花数据集利用Perceptron ...

  7. ML之kNNC:基于iris莺尾花数据集(PCA处理+三维散点图可视化)利用kNN算法实现分类预测

    ML之kNNC:基于iris莺尾花数据集(PCA处理+三维散点图可视化)利用kNN算法实现分类预测 目录 基于iris莺尾花数据集(PCA处理+三维散点图可视化)利用kNN算法实现分类预测 设计思路 ...

  8. 机器学习系列(五) -- 逻辑回归(莺尾花数据集)

    加载数据 import numpy as np import pandas as pddata = pd.read_csv('iris.csv') # 去掉不需要的ID列 data.drop('ID' ...

  9. 以莺尾花数据集为例,探讨R模型部署之道

    感谢关注天善智能,走好数据之路↑↑↑ 欢迎关注天善智能,我们是专注于商业智能BI,大数据,数据分析领域的垂直社区,学习,问答.求职一站式搞定! 内容概要: 1.iris数据集简介 2.R模型部署的可能 ...

最新文章

  1. pix2pix tensorflow试验(GAN之图像转图像的操作)
  2. 接班 RT 系统?Windows 10 云服务版首次亮相
  3. Java技术在多数据库系统中的应用研究
  4. 解决eclipse闪退的办法
  5. java鼠标左键按下后拖动实现多选_鼠标拖拽多选功能
  6. 对计算机财务管理的理解,计算机财务管理
  7. Linux IPC实践(3) --具名FIFO
  8. 中国军事可穿戴传感器行业市场供需与战略研究报告
  9. camera主观测试_镜头测试:日本富士EBC 135/2.5+索尼A7微单实拍北京北海公园
  10. 关于JavaScript中的事件代理(例子:ul中无数的li上添加点击事件)
  11. iproxy工具的作用
  12. Database—DML
  13. 服务器CPU和普通CPU的区别
  14. HTML怎样转换繁体字,excel怎么转繁体字 Excel里怎样繁体字转换成简体字
  15. 微信小程序:未找到 app.json 中的定义的 pages “pages/index/index“ 对应的 WXML 文件
  16. ios 换电脑继续使用csr 证书等。
  17. 软件项目管理经验总结
  18. WIN7下WIFI共享上网教程
  19. PythonGUI 使用Tkinter写一个简单时间间隔计算器
  20. http://edelivery.oracle.com/?ARU_LANG=ZHS

热门文章

  1. iptables日志记录访问记录
  2. JVM和操作系统的关系是什么?
  3. vivo是安卓手机吗_安卓手机系统升级必要吗?看看这个就知道
  4. error: failed to push some refs to ‘xx.git‘ hint: Updates were rejected because the tip of your curr
  5. 图像对齐讲座—旷世成都研究院 数据策略产品经理——阿里讲座
  6. import express from 'express'; ^^^^^^^ SyntaxError: Unexpected identifier at Module._com
  7. win 访问linux加密硬盘分区,手把手教你使用BitLocker给win10硬盘分区加密的方法
  8. 基于JAVA三坑购物平台演示录像2020计算机毕业设计源码+数据库+lw文档+系统+部署
  9. 裸金属服务器是什么?关于裸金属服务器架构原理详解
  10. QStackWidget使用 踩坑