利用Numpy读取iris数据集并进行排序、去重、求统计量等操作实战(附源码)

需要源码和数据集请点赞关注收藏后评论区留言私信~~~

下面读取iris数据中鸢尾花的萼片、花瓣长度数据,并对其进行排序、去重、并求出和、累计和、均值、标准差、方差、最小值、最大值等操作

1:导入模块

2:获取数据并打印如下

iris_data = []
with open("data//iris.csv") as csvfile:
    # 使用csv.reader读取csvfile中的文件
    csv_reader = csv.reader(csvfile)  
    # 读取第一行每一列的标题
    birth_header = next(csv_reader)  
    # 将csv 文件中的数据保存到birth_data中
    for row in csv_reader:  
        iris_data.append(row)
iris_data

3:数据清理 去掉索引号

 

iris_list = []
for row in iris_data:
    iris_list.append(tuple(row[1:]))
iris_list

 4:数据统计 

首先创建数据类型

datatype = np.dtype([("Sepal.Length", np.str_, 40), ("Sepal.Width", np.str_, 40), 
                     ("Petal.Length",np.str_, 40), ("Petal.Width", np.str_, 40),("Species",np.str_, 40)])
print(datatype)

 然后创建二维数组

iris_data = np.array(iris_list,dtype = datatype)
iris_data

 然后将待处理的数据类型转化为float类型

PetalLength =iris_data["Petal.Length"].astype(float)
PetalLength

 接着将数据排序

np.sort(PetalLength)

 接着进行数据去重

np.unique(PetalLength)

 最后对指定列求和 均值 标准差 方差 最小值以及最大值

sum: 计算数组的和

mean 计算数组均值

std 计算数组标准差

var 计算数组方差

min 计算数组最小值

max 计算数组最大值

argmin 返回数组最小元素的索引

argmax 返回数组最小元素的索引

cumsum 计算所有元素的累计和

cumprod 计算所有元素的累计积

创作不易 觉得有帮助请点赞关注收藏~~~ 

  • 11
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
### 回答1: 可以使用以下代码来读取iris数据集中鸢尾花的萼片和花瓣长度: ```python import numpy as np # 读取iris数据集 iris = np.genfromtxt('iris.csv', delimiter=',', skip_header=1, usecols=[0, 2]) # 取出鸢尾花的萼片长度和花瓣长度 sepal_length = iris[:, 0] petal_length = iris[:, 1] ``` 接下来可以对鸢尾花的萼片长度进行排序去重、并出和、累计和、均值、标准差、方差、最大值和最小值: ```python # 对鸢尾花的萼片长度进行排序 sepal_length_sorted = np.sort(sepal_length) # 对鸢尾花的萼片长度进行去重 sepal_length_unique = np.unique(sepal_length) # 对鸢尾花的萼片长度进行和 sepal_length_sum = np.sum(sepal_length) # 对鸢尾花的萼片长度进行累计和 sepal_length_cumsum = np.cumsum(sepal_length) # 对鸢尾花的萼片长度进行均值计算 sepal_length_mean = np.mean(sepal_length) # 对鸢尾花的萼片长度进行标准差计算 sepal_length_std = np.std(sepal_length) # 对鸢尾花的萼片长度进行方差计算 sepal_length_var = np.var(sepal_length) # 对鸢尾花的萼片长度进行最大值和最小值计算 sepal_length_max = np.max(sepal_length) sepal_length_min = np.min(sepal_length) ``` 同样的方法也可以用来处理鸢尾花的花瓣长度。 ### 回答2: import numpy as np # 读取iris数据集 data = np.genfromtxt('iris.csv', delimiter=',', usecols=(0, 2)) # 提取鸢尾花的萼片长度 sepal_length = data[:, 0] # 提取鸢尾花的花瓣长度 petal_length = data[:, 1] # 对萼片长度排序 sepal_length_sorted = np.sort(sepal_length) # 对花瓣长度排序 petal_length_sorted = np.sort(petal_length) # 去重 sepal_length_unique = np.unique(sepal_length) petal_length_unique = np.unique(petal_length) # 和 sepal_length_sum = np.sum(sepal_length) petal_length_sum = np.sum(petal_length) # 累计和 sepal_length_cumsum = np.cumsum(sepal_length) petal_length_cumsum = np.cumsum(petal_length) # 均值 sepal_length_mean = np.mean(sepal_length) petal_length_mean = np.mean(petal_length) # 标准差 sepal_length_std = np.std(sepal_length) petal_length_std = np.std(petal_length) # 方差 sepal_length_var = np.var(sepal_length) petal_length_var = np.var(petal_length) # 最大值 sepal_length_max = np.max(sepal_length) petal_length_max = np.max(petal_length) # 最小值 sepal_length_min = np.min(sepal_length) petal_length_min = np.min(petal_length) ### 回答3: 首先,我们可以使用numpy库中的loadtxt函数来读取iris数据集。然后,我们可以使用numpy的索引功能来获取鸢尾花的萼片和花瓣长度。 代码如下所示: ```python import numpy as np # 使用loadtxt函数读取iris数据集 data = np.loadtxt('iris.txt', delimiter=',', usecols=(0, 2)) # 获取鸢尾花的萼片和花瓣长度 sepal_length = data[:, 0] petal_length = data[:, 1] # 对萼片长度进行排序 sorted_sepal_length = np.sort(sepal_length) # 对花瓣长度进行排序 sorted_petal_length = np.sort(petal_length) # 对萼片长度进行去重和 unique_sepal_length = np.unique(sepal_length) sum_sepal_length = np.sum(sepal_length) # 对萼片长度进行累计和 cumulative_sum_sepal_length = np.cumsum(sepal_length) # 对花瓣长度进行累计和 cumulative_sum_petal_length = np.cumsum(petal_length) # 计算萼片长度的均值 mean_sepal_length = np.mean(sepal_length) # 计算花瓣长度的均值 mean_petal_length = np.mean(petal_length) # 计算萼片长度的标准差 std_sepal_length = np.std(sepal_length) # 计算花瓣长度的标准差 std_petal_length = np.std(petal_length) # 计算萼片长度的方差 var_sepal_length = np.var(sepal_length) # 计算花瓣长度的方差 var_petal_length = np.var(petal_length) # 获取萼片长度的最大值和最小值 max_sepal_length = np.max(sepal_length) min_sepal_length = np.min(sepal_length) # 获取花瓣长度的最大值和最小值 max_petal_length = np.max(petal_length) min_petal_length = np.min(petal_length) ``` 以上代码实现了对iris数据集中鸢尾花的萼片和花瓣长度的排序、去重和、累计和、均值、标准差、方差、最大值和最小值的计算。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

showswoller

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

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

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

打赏作者

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

抵扣说明:

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

余额充值