numpy.loadtxt('iris.data.txt', delimiter = ',', converters ={4:read_label2})

本文展示了如何使用Python的NumPy和Matplotlib库读取并可视化Iris数据集。通过将数据集的最后一列标签转换为数值,并使用不同的颜色来区分三种不同类型的鸢尾花,实现了数据的直观展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

data in iris.data.txt file

such as:

4.6,3.2,1.4,0.2,Iris-setosa
5.3,3.7,1.5,0.2,Iris-setosa
7.0,3.2,4.7,1.4,Iris-versicolor
6.4,3.2,4.5,1.5,Iris-versicolor

######################################################################

import numpy as np
import matplotlib.pyplot as plt

label_set = (
    b'Iris-setosa',
    b'Iris-versicolor',
    b'Iris-virginica',
)  #tuple

def read_label2(label):
    return label_set.index(label)

#The last column that gives the label of each point is a string that can take three possible values—Iris-virginica, Iris-versicolor, and Iris-Vertosa.
data = np.loadtxt('iris.data.txt', delimiter = ',', converters ={4:read_label2})

color_set = ('.00', '.50', '.75')
color_list = [color_set[int(label)] for label in data[:,4]
             ]
plt.scatter(data[:,0], data[:,1], color=color_list)
plt.show()

data[:4] 

output:

array([[5.1, 3.5, 1.4, 0.2, 0. ],
       [4.9, 3. , 1.4, 0.2, 0. ],
       [4.7, 3.2, 1.3, 0.2, 1. ],
       [4.6, 3.1, 1.5, 0.2, 1. ]])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LIQING LIN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值