首页 > 分享 > 第4周

第4周

鸢尾花数据分析

鸢尾花数据集以鸢尾花的特征作为数据来源,数据集包含150个数据集,有4维,分为3类(setosa、versicolour、virginica),每类50个数据,每个数据包含4个属性,花萼长度、宽度和花瓣长度、宽度

实验步骤

在终端安装实验所需要的包

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple 包名
此处安装的包包括numpy、pandas、sklearn、matplotlib
在这里插入图片描述

用python打开
在这里插入图片描述

取萼片的长宽作为特征进行分类
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 1234567891011

2、 获取数据集

# 获取所需数据集 iris=datasets.load_iris() #每行的数据,一共四列,每一列映射为feature_names中对应的值 X=iris.data print(X) #每行数据对应的分类结果值(也就是每行数据的label值),取值为[0,1,2] Y=iris.target print(Y) 123456789

在这里插入图片描述
3、对数据进行处理

#归一化处理 X = StandardScaler().fit_transform(X) print(X) 1234

在这里插入图片描述
4、训练模型

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

5、绘制模型

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(['#77E0A0', '#FF8080', '#A0A0FF']) 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()

123456789101112131415161718192021

在这里插入图片描述
6、预测模型

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

在这里插入图片描述

相关知识

“春暖花开•植此青绿”——开鲁新村第二小学第4周“3.12植树节”主题升旗仪式
植物组织培养第十二章2几种花卉的组织培养
#家植花卉养护# #...
园艺疗法在抑郁症患者康复中的应用研究
宿州市蔬菜价格高位回落
金线莲试管花卉的制作方法与流程
对雏鸡进行光照管理的技巧
亮室经典摄影培训课程 《极致影像之路》第009期(总第19期)南京班
《极致影像之路》006期(总第16期)武汉教学点招生
第427集

网址: 第4周 https://m.huajiangbk.com/newsview545952.html

所属分类:花卉
上一篇: 什么是聚类分析?
下一篇: 数据挖掘浅谈