SVM训练莺尾花数据集

news2024/10/8 8:50:49

SVM训练莺尾花数据集

代码在莺尾花数据集上训练SVM,数据集由莺尾花的测量值及其相应的物种标签组成。该模型使用70%数据用于训练,然后剩余部分进行测试。其中 ′ f i t ′ 'fit' fit方法在训练集上训练数据, ′ s c o r e ′ '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.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/135934.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

HTC FOCUS3在PC端串流FOHEART H1数据手套(腕带)

本教程介绍使用FOHEART H1数据手套与HTC腕带式追踪器驱动VR中的虚拟手运动,实现手部的追踪及定位。与之前教程(HTC FOCUS 3连接FOHEART H1数据手套)不同,这次我们的场景内容运行在PC端,而不是头显端,使用VI…

基于Vue和SpringBoot的电商管理系统的设计与实现

作者主页:Designer 小郑 作者简介:Java全栈软件工程师一枚,来自浙江宁波,负责开发管理公司OA项目,专注软件前后端开发(Vue、SpringBoot和微信小程序)、系统定制、远程技术指导。CSDN学院、蓝桥云…

8.mysql模块

目录 1 安装mysql模块 2 建立与mysql的连接 3 执行SQL语句 3.1 查询数据 3.2 插入数据 3.2.1 直接写入SQL语句 3.2.2 使用问号进行占位 3.2.3 使用对象传入 3.3 更新数据 3.3.1 使用问号进行占位 3.3.2 使用对象传入 3.4 删除数据 常见的数据库有下面几…

光驱重装系统教程

光驱重装系统是使用最长的系统安装方法,最早时候电脑都有光驱的,很多用户重装电脑系统的时候都会使用光驱重装系统,现在小编来为大家详细的介绍一下光驱重装系统的教程。 工具/原料: 系统版本:win7系统 品牌型号&#…

仪表板工具Stimulsoft Dashboards中的面板组件介绍

Stimulsoft Dashboards.JS是一个功能齐全的仪表盘工具,用于为JavaScript平台创建仪表板。 Stimulsoft Dashboards.JS官方正版下载(qun:740060302)https://www.evget.com/product/4101/download在上一篇文章中,主要介绍…

剑指 Offer 24. 反转链表

一、题目 定义一个函数&#xff0c;输入一个链表的头节点&#xff0c;反转该链表并输出反转后链表的头节点。 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 限制&#xff1a; 0 < 节点个数 < 5000 二、题目解析&…

AI算法工程师 | 09机器学习-概率图模型(六)命名实体识别与CRF

文章目录机器学习 - 概率图模型 之 命名实体识别与CRF一、命名实体识别 NER 的基本介绍1、相关概念2、主要方法&#xff08;导图&#xff09;3、标注策略二、CRF 层之 BiLSTM&#xff08;BiLSTM-CRF&#xff09;1、介绍1.1 BiLSTM-CRF 模型1.2 添加 CRF 层的好处2、CRF 层2.1 E…

华为云工程师HCIA——华为虚拟化平台使用与管理

一、FusionCompute计算虚拟化介绍 1、计算虚拟化相关概念 1.1、虚拟化介绍虚拟化介绍 1.2、虚拟化的特点&#xff08;反过来考定义也要会&#xff09; •分区&#xff1a;分区意味着虚拟化层为多个虚拟机划分服务器资源的能力&#xff1b;每个虚拟机可以同时运行一个单独的操…

xxx.Caffeine进程缓存

Caffeine 是基于Java 8的高性能&#xff0c;接近最佳的缓存库。看上图 赋代码&#xff1a; <dependency><groupId>com.github.ben-manes.caffeine</groupId><artifactId>caffeine</artifactId> </dependency> package com.heima.item.con…

网络编程 - Linux socket编程

前言 socket(套接字)是网络编程编程的一种技巧。通过socket不仅可以实现跨进程通信&#xff0c;还可以实现跨主机的网络通信。使用这种技术&#xff0c;就可以实现全国各地的通讯。例如&#xff1a;深圳的一台电脑接收来自北京一台电脑发来的信息。   本篇不涉及太底层的网络原…

hudi系列-索引机制

1. 索引机制 hudi的索引机制是为了加速upsert/delete操作&#xff0c;它维护着&#xff08;分区 key&#xff09;-> fileID之间的映射关系&#xff0c;所以可以减少对非必要base文件的合并 key是指索引key&#xff0c;可以是表的任意字段&#xff0c;在全局索引中常用主键…

Linux chmod命令详解,Linux修改文件权限

「作者主页」&#xff1a;士别三日wyx 「作者简介」&#xff1a;CSDN top100、阿里云博客专家、华为云享专家、网络安全领域优质创作者 chmod 命令一、常用操作1. 字母形式2. 数字形式3. 递归设置二、文件权限解读三、数字权限四、特殊的root权限五、SUID详解1. 设置SUID2. 取消…

chatgpt小程序版本来了,解决你们手机上想用用不了的问题,chatgpt接口用到小程序里面,调用openai接口,提供前后端源码,可以私有部署使用

现在的chatgpt被玩的都开始加广告&#xff0c;又办会员什么的的&#xff0c;今天就把小程序的前后端无广告版本源码和部署方式说一下。 先看效果&#xff1a; 部署环境 前端用的uniapp&#xff0c;基础模版&#xff0c;单页面没有太多引用 后端使用的python的falsk框架&#…

javassist学习

Java 字节码以二进制的形式存储在 .class 文件中&#xff0c;每一个 .class 文件包含一个 Java 类或接口。Javaassist 就是一个用来 处理 Java 字节码的类库。它可以在一个已经编译好的类中添加新的方法&#xff0c;或者是修改已有的方法&#xff0c;并且不需要对字节码方面有深…

第29届中国国际广告节,酷开科技喜提三奖

第29届中国国际广告节&#xff0c;酷开科技喜提三奖 12月21日至12月23日&#xff0c;在风景如画的鹭岛厦门&#xff0c;第29届中国国际广告节于厦门国际会展中心隆重举行。经过几十年的高歌前行&#xff0c;中国国际广告节业已成为业界的盛会&#xff0c;更是中国广告业发展的…

(十)VUEElement

一、 VUE 1. 概述 Vue 是一套前端框架&#xff0c;免除原生JavaScript中的DOM操作&#xff0c;简化书写。基于MVVM(Model-View-ViewModel)思想&#xff0c;实现数据的双向绑定&#xff0c;将编程的关注点放在数据上。官网&#xff1a;https://cn.vuejs.org 2. 快速入门 &#…

CRM管理系统哪个好用?这五款CRM系统值得推荐

选择一款合适的CRM系统&#xff0c;可以有效解决企业生产经营中分散的数据、不规则的工作流程、缺乏客户服务、销售预测不准确、远程工作效率低、错误的客户沟通、几乎没有销售和市场合作、报告不足等方面的业务难题。 CRM系统的选型可以从以下方面考虑&#xff1a; 下面分享几…

Faster RCNN网络源码解读(Ⅺ) --- 预测结果后处理及预测过程(完结撒花)

目录 一、回顾以及本篇博客内容概述 二、代码解析 2.1 ROIHead类&#xff08;承接上篇博客的2.1节&#xff09; 2.1.1 初始化函数 __init__回顾 2.1.2 正向传播forward回顾及预测结果后处理 2.1.3 postprocess_detections 2.2 FasterRCNNBase类前向传播过程 2.3 Genera…

MySQL Workbench基本用法

MySQL Workbench相当于SQL语言的解释器 目录 1 打开 2 连接数据库 3 创建数据库 4 创建数据表 4.1 字段类型 4.2 字段选项 4.3 其他 5 操作表中的数据 5.1 添加 5.2 更改 5.3 删除 6 代码编辑器 7 保存sql代码 8 加载sql代码 1 打开 搜索MySQL …

滑动窗口题型

先看一个题目&#xff1a;题目描述 题目描述&#xff1a;给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s 中不存在涵盖 t 所有字符的子串&#xff0c;则返回空字符串 "" 。 例如&#xff1a; 输入&#xff1a;s "ADOBECODE…