[機器學習]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.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5 1.8]
[7.7 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
英文地址,請注明出處:http://en.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;…