当前位置:网站首页>5分钟掌握机器学习鸢尾花逻辑回归分类
5分钟掌握机器学习鸢尾花逻辑回归分类
2022-07-06 09:24:00 【ブリンク】
本文将使用5分钟时间帮助大家掌握机器学习中最经典的鸢尾花分类案例。
简述
使用scikit-learn库,配合Numpy、Pandas可以使机器学习变得简单,利用基于Matplotlib的seaborn库可以更简单地实现可视化。
首先导入所要用到的库:
from sklearn import datasets
# 我们从sklearn自带的数据集中获取数据即可
import numpy as np
import pandas as pd
import seaborn as sns
from sklearn.linear_model import LogisticRegression
# 使用逻辑回归进行学习
from sklearn.model_selection import train_test_split
# 使用它分割数据为训练集和测试集
导入数据
sklearn为我们准备好了一些用作练习使用的数据集,其中就包括现在要使用的鸢尾花数据,我们只需要使用datasets的 l o a d i r i s ( ) load_iris() loadiris()方法即可:
iris_data = datasets.load_iris()
得到的iris_data是sklearn中自带的类型,我们可以使用 i r i s . k e y s ( ) iris.keys() iris.keys()方法查看它包含有哪些内容,他会返回一个字典:
>>> iris.keys()
dict_keys(['data', 'target', 'frame',
'target_names', 'DESCR', 'feature_names',
'filename', 'data_module'])
其中包含150组数据,data表示包含的数据,target表示标签,也就是这朵花属于哪一类的鸢尾花,数据中的鸢尾花一共有3种setosa, versicolor和virginica,它们包含在target_names中,表示标签的名称。feature_names表示特征的名称,也就是鸢尾花特点的描述,比如有在数据集中有花瓣长度、宽度和花萼长度、宽度。其余内容在本例中用不到,不做过多介绍。
下面将数据和标签提取出来,并存储在Pandas的DataFrame中,:
>>> data = iris.data
>>> data = data.pd.DataFrame(data,columns = iris.target_names)
# 把列名改为特征的名称
>>> data.head()
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
0 5.1 3.5 1.4 0.2
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2
数据可视化
使用seaborn的 p a i r p l o t ( ) pairplot() pairplot()方法可以快速查看每两个变量之间的关系,包括与它们自己:
sns.pairplot(data)
模型建立
使用sklearn的估计器建立一个逻辑回归的模型:
modle = LogisticRegression()
数据预处理
首先把所有的数据处理为训练集和测试集,以便我们对模型进行测试,使用train_test_split()方法可以很容易做到这一点,它将分别返回x训练集,x测试集,y训练集,y测试集:
x_train,x_test,y_train,y_test = train_test_split(X=data,y=iris.target,train_size=0.8)
# 80%的数据作为训练集 其余作为测试集
训练模型
使用估计器的 f i t ( ) fit() fit()方法可以对模型进行训练:
model.fit(x_train,y_train)
模型评估
可以直接估计器的 s c o r e ( ) score() score()方法计算模型在测试集下的得分或者说准确率:
>>> model.score(x_test,y_test)
0.9333333333333333 # 正确率达到了93.33%,这与训练集和测试机的划分有关
模型预测
可以使用训练好的模型对测试机数据进行预测,也就是说,当你得知了一组关于鸢尾花特征的数据,就可以使用该模型得知它属于哪一种类:
>>> s = model.predict(x_test)
array([1, 2, 1, 2, 1, 0, 2, 1, 0,
0, 0, 2, 1, 0, 2, 0, 1, 2,
1, 1, 2, 2,1, 2, 0, 2, 1, 2, 0, 0])
# 其中0表示setosa,1表示versicolor,2表示virginica
边栏推荐
- Library management system
- Intranet information collection of Intranet penetration (5)
- The United States has repeatedly revealed that the yield of interest rate hiked treasury bonds continued to rise
- “Hello IC World”
- Fire! One day transferred to go engineer, not fire handstand sing Conquest (in serial)
- Statistics 8th Edition Jia Junping Chapter 5 probability and probability distribution
- Statistics, 8th Edition, Jia Junping, Chapter 6 Summary of knowledge points of statistics and sampling distribution and answers to exercises after class
- 《统计学》第八版贾俊平第二章课后习题及答案总结
- JVM memory model concept
- Wei Shen of Peking University revealed the current situation: his class is not very good, and there are only 5 or 6 middle-term students left after leaving class
猜你喜欢
JDBC事务、批处理以及连接池(超详细)
Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class
关于超星脚本出现乱码问题
《統計學》第八版賈俊平第七章知識點總結及課後習題答案
Statistics 8th Edition Jia Junping Chapter 5 probability and probability distribution
Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
Intranet information collection of Intranet penetration (5)
Constants, variables, and operators of SystemVerilog usage
Statistics 8th Edition Jia Junping Chapter 14 summary of index knowledge points and answers to exercises after class
Interpretation of iterator related "itertools" module usage
随机推荐
Internet Management (Information Collection)
《统计学》第八版贾俊平第十三章时间序列分析和预测知识点总结及课后习题答案
Which is more advantageous in short-term or long-term spot gold investment?
Database monitoring SQL execution
Attack and defense world misc practice area (GIF lift table ext3)
关于交换a和b的值的四种方法
Intranet information collection of Intranet penetration (5)
《统计学》第八版贾俊平第三章课后习题及答案总结
xray與burp聯動 挖掘
Always of SystemVerilog usage_ comb 、always_ iff
《统计学》第八版贾俊平第七章知识点总结及课后习题答案
2022华中杯数学建模思路
【指针】统计一字符串在另一个字符串中出现的次数
Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)
Xray and Burp linked Mining
关于超星脚本出现乱码问题
Markdown font color editing teaching
Constants, variables, and operators of SystemVerilog usage
AQS details
The difference between layer 3 switch and router