首页 > 分享 > 基于Logistic回归模型对鸢尾花数据集的线性多分类

基于Logistic回归模型对鸢尾花数据集的线性多分类

文章目录 一、实验说明二、鸢尾花数据集线性多分类三、参考 一、实验说明

实验环境

Anaconda + python3.6 + jupyter

实验内容

使用Logistic回归模型对鸢尾花数据集进行线性多分类、可视化显示和测试精度。

LogisticRegression

逻辑回归(logistic regression)是统计学习中的经典分类方法,属于对数线性模型,所以也被称为对数几率回归。这里要注意,虽然带有回归的字眼,但是该模型是一种分类算法,逻辑斯谛回归是一种线性分类器,针对的是线性可分问题。利用logistic回归进行分类的主要思想是:根据现有的数据对分类边界线建立回归公式,以此进行分类。

更多详细说明请参考:逻辑回归(logistics regression)

二、鸢尾花数据集线性多分类

1. 导入相关库

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 Pipeline 123456789

2. 加载数据

iris=datasets.load_iris() 1

鸢尾花数据说明

鸢尾花品种包括:Setosa,Versicolor,Virginica,每个类型的花数据有50个鸢尾花的特征数据集(4个花的尺寸信息):花萼长和宽尺寸和花瓣的长和宽尺寸

3. 取花瓣的长宽作为特征进行分类

① 提取花瓣长和宽特征,并作归一化

#每行的数据,一共四列,每一列映射为feature_names中对应的值 X=iris.data #每行数据对应的分类结果值(也就是每行数据的label值),取值为[0,1,2] Y=iris.target #归一化处理 X = X[:, -2:] X = StandardScaler().fit_transform(X) 1234567

② 训练模型

lr = LogisticRegression() # Logistic回归模型 lr.fit(X, Y) # 根据数据[x,y],计算回归参数 12

③ 绘制分类图像

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) # 测试点 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, cmap=cm_light) # 预测值的显示 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()

1234567891011121314151617181920

在这里插入图片描述

④ 预测结果查看

y_hat = lr.predict(X) Y = Y.reshape(-1) result = y_hat == Y print(y_hat) print(result) acc = np.mean(result) print('准确度: %.2f%%' % (100 * acc)) 1234567

在这里插入图片描述

三、参考 基于sklearn的LogisticRegression鸢尾花多类分类实践使用sklearn进行鸢尾花分类预测 模型:LogisticRegression

相关知识

python鸢尾花数据集的分类问题 -- 逻辑回归问题研究
分析鸢尾花数据集
鸢尾花数据集下载
python实践gcForest模型对鸢尾花数据集iris进行分类
基于BP神经网络对鸢尾花的分类的研究
Cotton pest monitoring based on Logistic algorithm and remote sensing image
【人工智能】基于分类算法的学生学业预警系统应用
实验一:鸢尾花数据集分类
鸢尾花分类——神经网络详解
基于多尺度数据集的虫害检测模型

网址: 基于Logistic回归模型对鸢尾花数据集的线性多分类 https://m.huajiangbk.com/newsview387296.html

所属分类:花卉
上一篇: 基于TensorFlow实现LS
下一篇: ==鸢尾花 ==