逻辑回归分析实战项目详解:鸢尾花数据

版权声明:转载请注明来源及作者,谢谢! https://blog.csdn.net/qq_42442369/article/details/86568441

项目背景:IRIS (IRIS数据集)

Ref:https://www.sohu.com/a/197631437_752099

数据来自大名鼎鼎的Kaggle网站,里面有很多好玩的数据集. 下面就是Iris数据:
在这里插入图片描述
Iris也称鸢尾花卉数据集,是一类多重变量分析的数据集。通过花萼长度,花萼宽度,花瓣长度,花瓣宽度4个属性预测鸢尾花卉属于(Setosa(山鸢尾),Versicolour(杂色鸢尾),Virginica(维吉尼亚鸢尾))三个种类中的哪一类。
在这里插入图片描述
这个数据集,仅有150行,5列。该数据集的四个特征属性的取值都是数值型的,他们具有相同的量纲,不需要你做任何标准化的处理,第五列为通过前面四列所确定的鸢尾花所属的类别名称。

type(iris)
# sklearn.utils.Bunch

数据集长这样:
在这里插入图片描述
在这里插入图片描述
四个特征属性,引自数据集:
Number of Attributes: 4 numeric, predictive attributes and the class\n :Attribute Information:\n - sepal length in cm\n - sepal width in cm\n - petal length in cm\n - petal width in cm\n
花萼长度,宽度;花瓣长度,宽度

花萼是啥????下图:
在这里插入图片描述
其它比较流行的数据集还有Adult,Wine,Car Evaluation等。
中文名 鸢尾花卉数据集 外文名 Iris data set
在这里插入图片描述

思路

  1. 导入模型。调用逻辑回归的LogisticsRegression()函数
  2. fit()训练。调用fit(x, y)的方法来训练模型,其中x为数据的属性,y为所属类型。
  3. predict()预测。利用训练得到的模型对数据集进行预测,返回预测结果。

程序

# 导入模块
import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression

# 读入数据
iris = load_iris()
X = iris.data[:, :2]
Y = iris.target

# 建立模型
lr = LogisticRegression(C=1e5)
lr.fit(X, Y)

# 生成两个网格矩阵
h = .02
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))

# 预测
Z = lr.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

# 绘制
plt.figure(1, figsize=(8, 6))
plt.pcolormesh(xx, yy, Z, cmap=plt.cm.Paired)
plt.scatter(X[:50, 0], X[:50, 1], color = 'red', marker = 'o', label = 'setosa')
plt.scatter(X[50:100, 0], X[50:100, 1], color = 'blue', marker = 'x', label = 'versicolor')
plt.scatter(X[100:, 0], X[100:, 1], color = 'green', marker = 's', label = 'Virginica')

plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.xlim(xx.min(),xx.max())
plt.ylim(yy.min(),yy.max())
plt.xticks(())
plt.yticks(())
plt.legend(loc=2)
plt.show()

在这里插入图片描述

后续参考

https://www.sohu.com/a/197631437_752099

猜你喜欢

转载自blog.csdn.net/qq_42442369/article/details/86568441
今日推荐