[机器学习]08-基于逻辑回归模型的鸢尾花数据集分类

使用sklearnLogisticRegression多分类模型

程序代码:

import numpy as np
from sklearn.linear_model import LogisticRegression
import matplotlib.pyplot as plt
import matplotlib as mpl
from sklearn import datasets
from sklearn import preprocessing
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipelinedf = pd.read_csv('http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data', header=0)
x = df.values[:, :-1]
y = df.values[:, -1]
print('x = \n', x)
print('y = \n', y)
le = preprocessing.LabelEncoder()
le.fit(['Iris-setosa', 'Iris-versicolor', 'Iris-virginica'])
print(le.classes_)
y = le.transform(y)
print('Last Version, y = \n', y)x = x[:, 0:2]
print(x)
print(y)
#x = StandardScaler().fit_transform(x)
lr = LogisticRegression()   # Logistic回归模型
lr.fit(x, y.ravel())        # 根据数据[x,y],计算回归参数X = x
Y = y
N, M = 500, 500     # 横纵各采样多少个值
x1_min, x1_max = X[:, 0].min(), X[:, 0].max()   # 第0列的范围
x2_min, x2_max = X[:, 1].min(), X[:, 1].max()   # 第1列的范围
t1 = np.linspace(x1_min, x1_max, N)
t2 = np.linspace(x2_min, x2_max, M)
x1, x2 = np.meshgrid(t1, t2)                    # 生成网格采样点
x_test = np.stack((x1.flat, x2.flat), axis=1)   # 测试点
print(x_test)cm_light = mpl.colors.ListedColormap(['#009933', '#ff6666', '#33ccff'])
cm_dark = mpl.colors.ListedColormap(['g', 'r', 'b'])
y_hat = lr.predict(x_test)       # 预测值
y_hat = y_hat.reshape(x1.shape)                 # 使之与输入的形状相同
plt.pcolormesh(x1, x2, y_hat)     # 预测值的显示
plt.scatter(X[:, 0], X[:, 1], c=Y.ravel(), edgecolors='k', s=50, cmap=cm_dark)
plt.xlabel('petal length')
plt.ylabel('petal width')
plt.xlim(x1_min, x1_max)
plt.ylim(x2_min, x2_max)
plt.grid()
plt.show()

运行结果:

x = 
[[4.9 3.0 1.4 0.2]
[4.7 3.2 1.3 0.2]
[4.6 3.1 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3.8 6.7 2.2]
[7.7 2.6 6.9 2.3]
[6.0 2.2 5.0 1.5]
[6.9 3.2 5.7 2.3]
[5.6 2.8 4.9 2.0]
[7.7 2.8 6.7 2.0]
[6.3 2.7 4.9 1.8]
[6.7 3.3 5.7 2.1]
[7.2 3.2 6.0 1.8]
[6.2 2.8 4.8 1.8]
[6.1 3.0 4.9 1.8]
[6.4 2.8 5.6 2.1]
[7.2 3.0 5.8 1.6]
[7.4 2.8 6.1 1.9]
[7.9 3.8 6.4 2.0]
[6.4 2.8 5.6 2.2]
[6.3 2.8 5.1 1.5]
[6.1 2.6 5.6 1.4]
[7.7 3.0 6.1 2.3]
[6.3 3.4 5.6 2.4]
[6.4 3.1 5.5 1.8]
[6.0 3.0 4.8 1.8]
[6.9 3.1 5.4 2.1]
[6.7 3.1 5.6 2.4]
[6.9 3.1 5.1 2.3]
[5.8 2.7 5.1 1.9]
[6.8 3.2 5.9 2.3]
[6.7 3.3 5.7 2.5]
[6.7 3.0 5.2 2.3]
[6.3 2.5 5.0 1.9]
[6.5 3.0 5.2 2.0]
[6.2 3.4 5.4 2.3]
[5.9 3.0 5.1 1.8]]
y = 
['Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'
'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'
'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'
'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'
'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'
'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'
'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'
'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'
'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa'
'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-setosa' 'Iris-versicolor'
'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'
'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'
'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'
'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'
'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'
'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'
'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'
'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'
'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'
'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'
'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'
'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor' 'Iris-versicolor'
'Iris-versicolor' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'
'Iris-virginica' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'
'Iris-virginica' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'
'Iris-virginica' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'
'Iris-virginica' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'
'Iris-virginica' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'
'Iris-virginica' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'
'Iris-virginica' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'
'Iris-virginica' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'
'Iris-virginica' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'
'Iris-virginica' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'
'Iris-virginica' 'Iris-virginica' 'Iris-virginica' 'Iris-virginica'
'Iris-virginica' 'Iris-virginica' 'Iris-virginica']
['Iris-setosa' 'Iris-versicolor' 'Iris-virginica']
Last Version, y = 
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2]
[[4.3        2.        ]
[4.30721443 2.        ]
[4.31442886 2.        ]
...
[7.88557114 4.4       ]
[7.89278557 4.4       ]
[7.9        4.4       ]]

进程已结束,退出代码0

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

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

相关文章

【STM32入门教程】stm32简介

一、STM32简介二、ARM三、stm32f103c8t6四、命名规则五、系统结构六、引脚定义七、启动配置一般情况下,都是在flash开始程序,而启动程序也可以进行配置在其他地方启动程序,通过配置boot0和boot1来进行配置八、最小系统电路

SAE J2716多协议网关的硬件架构与实时协议转换机制解析

本文解析符合SAE J2716标准的工业级协议转换设备技术架构,通过拆解其四路双向SENT通道与多总线(CANFD/Ethernet/USB)的实时交互机制、MicroSD独立日志系统设计及模拟量动态映射方案,为汽车电子与工业通信开发者提供可复用的技术参…

VS2022+QT5.15.2+OCCT7.9.1的开发环境搭建流程

以下是VS2022 QT5.15.2 OCCT7.9.1开发环境搭建的完整流程: 一、安装Visual Studio 2022 下载安装程序 访问VS官网下载Community版安装组件 选择"使用C的桌面开发"工作负载勾选: MSVC v143 - VS 2022 C x64/x86生成工具Windows 10 SDK (建议…

数据库访问模式详解

数据库访问模式详解数据库访问模式是软件架构中数据访问层(Data Access Layer)设计的核心,它定义了应用程序如何与数据库进行交互的策略和方法。选择合适的访问模式对于系统的性能、可维护性、可扩展性、事务一致性和开发效率至关重要。不同的…

BGE向量算法

一、是什么 什么是BGE向量算法?先说说网上的概念吧。本文不讲解太深的算法知识,主要讲解如何用! BGE(BAAI General Embedding)是北京智源研究院开源的“通用语义向量模型”。一句话:把中文或英文句子变成…

AI数据仓库的核心优势解析

内容概要本文旨在全面解析AI数据仓库的核心优势,为读者提供清晰的框架。文章首先从基础定义出发,探讨其如何高效整合多源数据,并支持人工智能与机器学习应用。随后,将详细阐述处理TB级数据的能力,包括兼容结构化和非结…

具身智能Scaling Law缺失:机器人界的“摩尔定律“何时诞生?

8月9日,在世界机器人大会的演讲台上,宇树科技创始人王兴兴谈论到目前机器人运动控制领域存在的RL Scaling Law问题,他认为现在的机器人在学习一项新的技能时,往往都是需要从头开始研究以及教学。而在未来更加希望的是能够在原有的…

【跨越 6G 安全、防御与智能协作:从APT检测到多模态通信再到AI代理语言革命】

跨越 6G 安全、防御与智能协作:从APT检测到多模态通信再到AI代理语言革命引言单篇总结**2. Integrated Multimodal Sensing and Communication: Challenges, Technologies, and Architectures****3. Why do AI agents communicate in human language?**引言 在迈向…

微前端-解决MicroApp微前端内存泄露问题

前言 之前使用京东微前端框架MicroApp集成10个微前端的页面到AngularJs的后台管理系统中,每个微前端做成一个菜单,一共10个,每次打开都是一个新的微前端,但是发现打开的微前端越多,容易造成内存泄露,下面讲…

线性代数 · 向量运算 | 叉乘 / 几何意义 / 推导

注:本文为 “线性代数 向量运算” 相关合辑。 图片清晰度受引文原图所限。 略作重排,未整理去重。 如有内容异常,请看原文。 数学基础 —— 向量运算(叉乘) keng_s 于 2016-08-05 17:17:57 发布 1_ 向量的叉乘 向量…

方法中只包含查询操作需要添加事务吗?

方法中只包含查询操作需要添加事务吗?绝大部分情况都不需要 是否需要为包含数据库查询操作的方法添加 @Transactional 注解,取决于业务需求和查询操作的特性,不能一概而论。以下是具体分析: 一、不需要添加 @Transactional 的常见场景 如果查询操作满足以下条件,通常不需…

MTK平台Wi-Fi学习--wifi channel 通过国家码进行功率限制和wifi eFEM 基本配置和wifi Tx SEM问题

一. 国家码可以用来限制功率上限,可以针对各国家实现By channel降功率的能力 可以通过country code来设置不同channel的power limit,操作方法如下: 在rlm_txpwr_init.h文件中g_rRlmPowerLimitConfiguration[]下添加需要限制功率的channel, 例如:国家码CN,信道:CH1,po…

MedGemma: 多模态医学文本与图像处理的创新模型

MedGemma: 多模态医学文本与图像处理的创新模型 今天,我有幸参加了在上海举行的Google 2025 I/O大会,这是一场充满创新与突破的技术盛宴。作为全球最具影响力的科技大会之一,Google I/O每年都会吸引来自世界各地的开发者、企业领袖以及科技爱…

深入剖析 C++ STL 中的 std::list 容器

基本介绍在 C 标准库(STL)中,std::list 是一个基于双向链表实现的序列容器。它与 std::vector、std::deque 等连续存储容器不同,提供了在序列中高效插入和删除元素的能力,尤其是在序列中间位置操作时优势明显。1. std:…

大规模调用淘宝商品详情 API 的分布式请求调度实践

在电商数据分析、比价系统、选品工具等业务场景中,往往需要大规模调用淘宝商品详情 API 以获取商品标题、价格、销量、评价等核心数据。然而,面对淘宝开放平台的严格限流策略、海量商品 ID 的处理需求以及系统高可用要求,传统的单节点调用方式…

在 Windows 系统中解决 Git 推送时出现的 Permission denied (publickey) 错误,请按照以下详细步骤操作:

完整解决方案步骤&#xff1a; 1. 检查并生成 SSH 密钥 # 打开 Git Bash ssh-keygen -t rsa -b 4096 -C "your_emailexample.com" 全程按回车&#xff08;使用默认路径&#xff0c;不设密码&#xff09; 密钥将生成在&#xff1a;C:\Users\<用户名>\.ssh\ 目…

【入门级-算法-2、入门算法:枚举法】

枚举法&#xff08;Brute Force&#xff09;&#xff1a;是一种直接遍历所有可能情况的算法思想&#xff0c;适合解决数据范围较小的问题。它的核心是穷举所有可能性&#xff0c;并检查哪些情况符合要求。 枚举法的基本思想&#xff1a;计算机主要功能&#xff0c;或者说它的优…

Python/Node.js 调用taobao API:构建实时商品详情数据采集服务

在电商数据分析、价格监控、竞品分析等场景中&#xff0c;实时获取商品详情数据至关重要。淘宝提供了丰富的 API 接口&#xff0c;允许开发者合法合规地获取商品信息。本文将介绍如何使用 Python 和 Node.js 两种主流语言调用淘宝 API&#xff0c;构建一个实时商品详情数据采集…

【OpenCV】Mat详解

在OpenCV中&#xff0c;cv::Mat是用于存储图像、矩阵等多维数据的核心数据结构&#xff0c;替代了早期的IplImage&#xff08;需手动管理内存&#xff09;&#xff0c;其设计的核心目标是自动内存管理和高效数据操作。下面详细介绍其组成原理及使用方法。 一、cv::Mat的组成原理…

疏老师-python训练营-Day45Tensorboard使用介绍

浙大疏锦行知识点回顾&#xff1a; tensorboard的发展历史和原理tensorboard的常见操作tensorboard在cifar上的实战&#xff1a;MLP和CNN模型 效果展示如下&#xff0c;很适合拿去组会汇报撑页数&#xff1a; 作业&#xff1a;对resnet18在cifar10上采用微调策略下&#xff0c;…