numpy基础用法-学习笔记-task10

大作业

本次练习使用 鸢尾属植物数据集 .\iris.data ,在这个数据集中,包括了三类不同的鸢尾属植物:Iris Setosa,Iris Versicolour,Iris Virginica。每类收集了50个样本,因此这个数据集一共包含了150个样本。
sepallength:萼片长度
sepalwidth:萼片宽度
petallength:花瓣长度
petalwidth:花瓣宽度
以上四个特征的单位都是厘米(cm)
  1. 导入鸢尾属植物数据集,保持文本不变。
    【知识点:输入和输出】
import numpy as np
outfile="E:/document/python学习笔记/numpy/Iris数据集/iris.txt"
#skiprows跳过第一行表头
#delimiter:分割字符串,以逗号隔。
iris_data  = np.loadtxt(outfile, dtype=object, delimiter=' ', skiprows=1)
iris_data[0:10]
  1. 求出鸢尾属植物萼片长度的平均值、中位数和标准差(第1列,sepallength)
    【知识点:统计相关】
import numpy as np
outfile="E:/document/python学习笔记/numpy/Iris数据集/iris.txt"
#skiprows跳过第一行表头
#delimiter:分割字符串,以逗号隔。
#usecols=1:只读第一列
iris_data  = np.loadtxt(outfile, dtype=float, delimiter=' ', skiprows=1,usecols=1)
print("均值:",np.mean(iris_data))
print("中位数:",np.median(iris_data))
print("方差:",np.std(iris_data))

  1. 创建一种标准化形式的鸢尾属植物萼片长度,其值正好介于0和1之间,这样最小值为0,最大值为1(第1列,sepallength)。
    【知识点:统计相关】
import numpy as np
outfile="E:/document/python学习笔记/numpy/Iris数据集/iris.txt"
iris_data_sepallength  = np.loadtxt(outfile, dtype=float, 
                                    delimiter=' ', skiprows=1,usecols=1)
aMax = np.amax(iris_data_sepallength)
aMin = np.amin(iris_data_sepallength)
x = (iris_data_sepallength-aMin) / (aMax-aMin)
#x = (iris_data_sepallength ‐ aMin) / np.ptp(sepalLength)
print(x)
  1. 找到鸢尾属植物萼片长度的第5和第95百分位数(第1列,sepallength)。
    【知识点:统计相关】
import numpy as np
outfile="E:/document/python学习笔记/numpy/Iris数据集/iris.txt"
iris_data_sepallength  = np.loadtxt(outfile, dtype=float, 
                                    delimiter=' ', skiprows=1,usecols=1)
x=np.percentile(iris_data_sepallength,[5,95])
print(x)#[4.6   7.255]
  1. 把iris_data数据集中的20个随机位置修改为np.nan值。
    【知识点:随机抽样】
import numpy as np
outfile="E:/document/python学习笔记/numpy/Iris数据集/iris.txt"
np.random.seed(20201130)
iris_data  = np.loadtxt(outfile, dtype=object, delimiter=' ', skiprows=1)
x,y=iris_data.shape
iris_data[np.random.randint(x,size=20),np.random.randint(1,y,size=20)]=np.nan
print(iris_data)
  1. 在iris_data的sepallength中查找缺失值的个数和位置(第1列)。
    【知识点:逻辑函数、搜索】
import numpy as np
np.random.seed(20201130)
outfile="E:/document/python学习笔记/numpy/Iris数据集/iris.txt"
iris_data_sepallength  = np.loadtxt(outfile, dtype=float, 
                                    delimiter=' ', skiprows=1,usecols=1)
iris_data_sepallength[np.random.randint(x,size=20)]=np.nan
print(iris_data_sepallength)
x=np.isnan(iris_data_sepallength)
print(sum(x))
print(np.where(x))
  1. 筛选具有 sepallength(第1列)< 5.0 并且 petallength(第3列)> 1.5 的 iris_data行。
    【知识点:搜索】
import numpy as np
outfile="E:/document/python学习笔记/numpy/Iris数据集/iris.txt"
iris_data = np.loadtxt(outfile, dtype=float, delimiter=' ', skiprows=1,usecols=[1,3])
sepallength = iris_data[:,0]
petallength = iris_data[:,1]
index = np.where(np.logical_and(petallength > 1.5, sepallength < 5.0))
print(iris_data[index])
  1. 选择没有任何 nan 值的 iris_data行。
    【知识点:逻辑函数、搜索】
import numpy as np
outfile = "E:/document/python学习笔记/numpy/Iris数据集/iris.txt"
np.random.seed(20201130)
iris_data = np.loadtxt(outfile, dtype=float, delimiter=' ', skiprows=1, usecols=[1,2,3])
i, j = iris_data.shape
iris_data[np.random.randint(i, size=20), np.random.randint(j, size=20)] = np.nan
x = iris_data[np.sum(np.isnan(iris_data), axis=1) == 0]
print(x)
  1. 计算 iris_data 中sepalLength(第1列)和petalLength(第3列)之间的相关系数。
    【知识点:统计相关】
import numpy as np
outfile = "E:/document/python学习笔记/numpy/Iris数据集/iris.txt"
np.random.seed(20201130)
iris_data = np.loadtxt(outfile, dtype=float, delimiter=' ', 
                       skiprows=1, usecols=[1,3])
sepallength = iris_data[:,0]
petallength = iris_data[:,1]
z = np.corrcoef(sepallength, petallength)
print(z)
# [[1.         0.87175378]
#  [0.87175378 1.        ]]
  1. 找出iris_data是否有任何缺失值。
    【知识点:逻辑函数】
import numpy as np
outfile = "E:/document/python学习笔记/numpy/Iris数据集/iris.txt"
iris_data = np.loadtxt(outfile, dtype=float, delimiter=' ',skiprows=1, usecols=[1,2,3])
x = np.isnan(iris_data)
print(np.any(x)) # False
  1. 在numpy数组中将所有出现的nan替换为0。
    【知识点:逻辑函数】
import numpy as np
outfile = "E:/document/python学习笔记/numpy/Iris数据集/iris.txt"
np.random.seed(20201130)
iris_data = np.loadtxt(outfile, dtype=float, delimiter=' ', 
                       skiprows=1, usecols=[1,2,3])
i, j = iris_data.shape
iris_data[np.random.randint(i, size=20), np.random.randint(j, size=20)] = np.nan
iris_data[np.isnan(iris_data)]=0
print(iris_data)
  1. 找出鸢尾属植物物种中的唯一值和唯一值出现的数量。
    【知识点:数组操作】
import numpy as np
outfile = "E:/document/python学习笔记/numpy/Iris数据集/iris.txt"
np.random.seed(20201130)
iris_data = np.loadtxt(outfile, dtype=object, delimiter=' ',skiprows=1, usecols=[5])
#该函数是去除数组中的重复数字,并进行排序之后输出。
x = np.unique(iris_data, return_counts=True)
print(x)
  1. 将 iris_data 的花瓣长度(第3列)以形成分类变量的形式显示。定义:Less than 3 -->‘small’;3-5 --> ‘medium’;’>=5 --> ‘large’。
    【知识点:统计相关】
import numpy as np
outfile = "E:/document/python学习笔记/numpy/Iris数据集/iris.txt"
np.random.seed(20201130)
iris_data = np.loadtxt(outfile, dtype=float, delimiter=' ',skiprows=1, usecols=[3])
petal_length_bin = np.digitize(iris_data, [0, 3, 5, 10])#直方图
label_map = {1: 'small', 2: 'medium', 3: 'large', 4: np.nan}
petal_length_cat = [label_map[x] for x in petal_length_bin]
print(petal_length_cat[0:10])
# ['small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small','small']
  1. 在 iris_data 中创建一个新列,其中 volume 是 (pi x petallength x sepallength ^ 2)/ 3 。
    【知识点:数组操作】
import numpy as np
outfile = "E:/document/python学习笔记/numpy/Iris数据集/iris.txt"
iris_data = np.loadtxt(outfile, dtype=object, delimiter=' ', skiprows=1)
sepalLength = iris_data[:, 1].astype(float)
petalLength = iris_data[:, 3].astype(float)
volume = (np.pi * petalLength * sepalLength ** 2) / 3
print(volume.shape)#(150,)
volume = volume[:, np.newaxis]#np.newaxis的作用是增加一个维度
print(volume.shape)#(150, 1)
iris_data = np.concatenate([iris_data, volume], axis=1)
print(iris_data[0:10])

  1. 随机抽鸢尾属植物的种类,使得Iris-setosa的数量是Iris-versicolor和Iris-virginica数量的两倍。
    【知识点:随机抽样】
import numpy as np
species = np.array(['setosa', 'versicolor', 'virginica'])
species_out = np.random.choice(species, 10000, p=[0.5, 0.25, 0.25])
print(np.unique(species_out, return_counts=True))
#(array(['setosa', 'versicolor', 'virginica'], dtype='<U10'), 
# array([4977, 2505, 2518], dtype=int64))

  1. 根据 sepallength 列对数据集进行排序。
    【知识点:排序】
import numpy as np
outfile = "E:/document/python学习笔记/numpy/Iris数据集/iris.txt"
iris_data = np.loadtxt(outfile, dtype=object, delimiter=' ', skiprows=1)
sepalLength = iris_data[:, 1].astype(float)
# print(np.sort(sepalLength))
index = np.argsort(sepalLength)
print(iris_data[index])

  1. 在鸢尾属植物数据集中找到最常见的花瓣长度值(第3列)。
    【知识点:数组操作】
import numpy as np
outfile = "E:/document/python学习笔记/numpy/Iris数据集/iris.txt"
iris_data = np.loadtxt(outfile, dtype=object, delimiter=' ', skiprows=1)
petalLength = iris_data[:, 3].astype(float)
vals, counts = np.unique(petalLength, return_counts=True)
print(vals[np.argmax(counts)]) # 1.5
print(np.amax(counts)) # 14

  1. 在鸢尾花数据集的 petalwidth(第4列)中查找第一次出现的值大于1.0的位置。
    【知识点:搜索】
import numpy as np
outfile = "E:/document/python学习笔记/numpy/Iris数据集/iris.txt"
iris_data = np.loadtxt(outfile, dtype=object, delimiter=' ', skiprows=1)
petalWidth = iris_data[:, 4].astype(float)
index = np.where(petalWidth > 1.0)
print(index)
print(index[0][0]) # 50
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值