探索鸢尾花数据集:使用Seaborn绘制成对特征散点图

鸢尾花数据集是机器学习领域中一个非常常见且易于理解的数据集,它包含了三种不同种类的鸢尾花(Setosa、Versicolour和Virginica)的四个特征(花萼长度、花萼宽度、花瓣长度和花瓣宽度)的测量值。

首先,我们从sklearn.datasets中导入load_iris函数,用于加载鸢尾花数据集

from sklearn.datasets import load_iris

导入matplotlib.pyplot和seaborn库

import matplotlib.pyplot as plt
import seaborn as sns

导入pandas库,以便将数据转换成更易于处理的DataFrame格式。 

import pandas as pd

定义一个函数dm02_irisdata_show(),这个函数将完成对数据集的加载、处理和可视化工作。

def dm02_irisdata_show():
    iris_df=pd.DataFrame(iris_data['data'],columns=iris_data.feature_names)
    print(iris_df)
    iris_df['target']=iris_data.target
    print(iris_df)

    feature_names=list(iris_data.feature_names)
    print(feature_names)
    for i in range(len(feature_names)):
        for j in range(i+1,len(feature_names)):
            col1=feature_names[i]
            col2=feature_names[j]

            sns.lmplot(x=col1,y=col2,hue='target',data=iris_df,fit_reg=False)
            plt.xlabel(col1)
            plt.ylabel(col2)
            plt.title(f'{col1} vs {col2}')
            plt.show()

dm02_irisdata_show()

这段代码展示了如何使用Python的sklearn和seaborn库来加载、处理和可视化鸢尾花数据集。

运行结果为:

C:\Users\wang\AppData\Local\anaconda3\envs\edumlpy3.9\python.exe C:\Users\wang\实习\鸢尾花.py 
     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
..                 ...               ...                ...               ...
145                6.7               3.0                5.2               2.3
146                6.3               2.5                5.0               1.9
147                6.5               3.0                5.2               2.0
148                6.2               3.4                5.4               2.3
149                5.9               3.0                5.1               1.8

[150 rows x 4 columns]
     sepal length (cm)  sepal width (cm)  ...  petal width (cm)  target
0                  5.1               3.5  ...               0.2       0
1                  4.9               3.0  ...               0.2       0
2                  4.7               3.2  ...               0.2       0
3                  4.6               3.1  ...               0.2       0
4                  5.0               3.6  ...               0.2       0
..                 ...               ...  ...               ...     ...
145                6.7               3.0  ...               2.3       2
146                6.3               2.5  ...               1.9       2
147                6.5               3.0  ...               2.0       2
148                6.2               3.4  ...               2.3       2
149                5.9               3.0  ...               1.8       2

[150 rows x 5 columns]
['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']

进程已结束,退出代码0

可视化结果为:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值