鸢尾花数据集的线性多分类【Logistic回归模型】

本文介绍了使用LogisticRegression模型对鸢尾花数据集进行线性多分类的实验。首先,阐述了鸢尾花数据集的基本信息和实验环境。接着,详细展示了模型的导入、训练、预测及参数设置。通过可视化手段,描绘了分类边界并评估了模型的准确性。最后,实验结果显示模型具有较高的分类精度,总结了逻辑回归在鸢尾花数据集上的应用效果。
摘要由CSDN通过智能技术生成

‘鸢尾花数据集’

Iris flower数据集是1936年由Sir Ronald Fisher引入的经典多维数据集,可以作为判别分析(discriminant analysis)的样本,该数据集包含Iris花的三个品种(Iris setosa, Iris virginica and Iris versicolor)各50个样本,每个样本还有4个特征参数(分别是萼片的长宽和花瓣的长宽,以厘米为单位)

一、实验说明

  • 实验环境:Anaconda + python3 + jupyter
  • 实验内容:使用Logistic回归模型对鸢尾花数据集进行线性多分类、可视化显示和测试精度。
  • LogisticRegression:逻辑回归(logistic regression)是统计学习中的经典分类方法,属于对数线性模型,所以也被称为对数几率回归。这里要注意,虽然带有回归的字眼,但是该模型是一种分类算法,逻辑斯谛回归是一种线性分类器,针对的是线性可分问题。利用logistic回归进行分类的主要思想是:根据现有的数据对分类边界线建立回归公式,以此进行分类。

二、LogisticRegression回归模型在Sklearn中的使用

1、 导入模型

from sklearn.linear_model import LogisticRegression  #导入逻辑回归模型 

2、fit()训练

clf = LogisticRegression()
print(clf)
clf.fit(train_feature,label)

3、predict()预测

predict['label'] = clf.predict(predict_feature)

4、LogisticRegression回归模型参数说明

LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
          intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,
          penalty='l2', random_state=None, solver='liblinear', tol=0.0001,
          verbose=0, warm_start=False)
  • 正则化选择参数(penalty)

  • 优化算法选择参数(solver)

  • 分类方式选择参数(multi_class)

  • 类型权重参数(class_weight)

  • 样本权重参数(sample_weight)
    详细内容:逻辑回归(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

2、获取数据集

df = 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)

在这里插入图片描述

3、对数据进行处理

x = x[:, :2] 
print(x)
print(y)
x = StandardScaler().fit_transform(x)
lr = LogisticRegression()   # Logistic回归模型
lr.fit(x, y.ravel())        # 根据数据[x,y],计算回归参数

在这里插入图片描述
4、绘制分类图像

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()

在这里插入图片描述

5、预测结果查看

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))

在这里插入图片描述

四、总结

逻辑回归的使用过程主要包括导入整个模型包,训练模型,预测结果。训练的结果。模型的准确度较好。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值