NumPy 学习笔记 NumPy学习大作业

NumPy学习大作业

本次练习使用 鸢尾属植物数据集 .\iris.data ,在这个数据集中,包括了三类不同的鸢尾属植物:

Iris Setosa,Iris Versicolour,Iris Virginica。每类收集了50个样本,因此这个数据集一共包含了

150个样本。

  • sepallength:萼片长度
  • sepalwidth:萼片宽度
  • petallength:花瓣长度
  • petalwidth:花瓣宽度

以上四个特征的单位都是厘米(cm)。

     sepallength  sepalwidth  petallength  petalwidth         species
0            5.1         3.5          1.4         0.2     Iris-setosa
1            4.9         3.0          1.4         0.2     Iris-setosa
2            4.7         3.2          1.3         0.2     Iris-setosa
3            4.6         3.1          1.5         0.2     Iris-setosa
4            5.0         3.6          1.4         0.2     Iris-setosa
..           ...         ...          ...         ...             ...
145          6.7         3.0          5.2         2.3  Iris-virginica
146          6.3         2.5          5.0         1.9  Iris-virginica
147          6.5         3.0          5.2         2.0  Iris-virginica
148          6.2         3.4          5.4         2.3  Iris-virginica
149          5.9         3.0          5.1         1.8  Iris-virginica

[150 rows x 5 columns]

1. 导入鸢尾属植物数据集,保持文本不变。

【知识点:输入和输出】

  • 如何导入存在数字和文本的数据集?
import numpy as np

outfile = r'.\iris.data'
iris_data = np.loadtxt(outfile, dtype=object, delimiter=',', skiprows=1)
print(iris_data[0:10])
# [['4.9' '3.0' '1.4' '0.2' 'Iris-setosa']
#  ['4.7' '3.2' '1.3' '0.2' 'Iris-setosa']
#  ['4.6' '3.1' '1.5' '0.2' 'Iris-setosa']
#  ['5.0' '3.6' '1.4' '0.2' 'Iris-setosa']
#  ['5.4' '3.9' '1.7' '0.4' 'Iris-setosa']
#  ['4.6' '3.4' '1.4' '0.3' 'Iris-setosa']
#  ['5.0' '3.4' '1.5' '0.2' 'Iris-setosa']
#  ['4.4' '2.9' '1.4' '0.2' 'Iris-setosa']
#  ['4.9' '3.1' '1.5' '0.1' 'Iris-setosa']
#  ['5.4' '3.7' '1.5' '0.2' 'Iris-setosa']]

2. 求出鸢尾属植物萼片长度的平均值、中位数和标准差(第1列,sepallength)

【知识点:统计相关】

  • 如何计算numpy数组的均值,中位数,标准差?
import numpy as np

outfile = r'.\iris.data'
sepalLength = np.loadtxt(outfile, dtype=float, delimiter=',', skiprows=1, usecols=[0])
print(sepalLength[0:10])
# [4.9 4.7 4.6 5.  5.4 4.6 5.  4.4 4.9 5.4]
print(np.mean(sepalLength))
# 5.8483221476510066
print(np.median(sepalLength))
# 5.8
print(np.std(sepalLength))
# 0.8258088592766593

3. 创建一种标准化形式的鸢尾属植物萼片长度,其值正好介于0和1之间,这样最小值为0,最大值为1(第1列,sepallength)。

【知识点:统计相关】

  • 如何标准化数组?
import numpy as np
outfile = r'.\iris.data'
sepalLength = np.loadtxt(outfile, dtype=float, delimiter=',', skiprows=1, usecols=[0])
# 方法1
aMax = np.amax(sepalLength)
aMin = np.amin(sepalLength)
x = (sepalLength - aMin) / (aMax - aMin)
print(x[0:10])
# [0.16666667 0.11111111 0.08333333 0.19444444 0.30555556 0.08333333
#  0.19444444 0.02777778 0.16666667 0.30555556]

# 方法2
x = (sepalLength - aMin) / np.ptp(sepalLength)
print(x[0:10])
# [0.16666667 0.11111111 0.08333333 0.19444444 0.30555556 0.08333333
#  0.19444444 0.02777778 0.16666667 0.30555556]

4. 找到鸢尾属植物萼片长度的第5和第95百分位数(第1列,sepallength)。

【知识点:统计相关】

  • 如何找到numpy数组的百分位数?
import numpy as np

outfile = r'.\iris.data'
sepalLength = np.loadtxt(outfile, dtype=float, delimiter=',', skiprows=1, usecols=[0])
x = np.percentile(sepalLength, [5, 95])
print(x)  # [4.6 7.26]

5. 把iris_data数据集中的20个随机位置修改为np.nan值。

【知识点:随机抽样】

  • 如何在数组中的随机位置修改值?
import numpy as np
outfile = r'.\iris.data'

# 方法1
iris_data = np.loadtxt(outfile, dtype=object, delimiter=',', skiprows=1)
i, j = iris_data.shape
np.random.seed(20201128)
iris_data[np.random.randint(i, size=20), np.random.randint(j, size=20)] = np.nan
print(iris_data[0:10])
# [['4.9' '3.0' '1.4' '0.2' 'Iris-setosa']
#  ['4.7' '3.2' '1.3' '0.2' 'Iris-setosa']
#  ['4.6' '3.1' '1.5' '0.2' 'Iris-setosa']
#  ['5.0' '3.6' '1.4' '0.2' 'Iris-setosa']
#  ['5.4' '3.9' '1.7' '0.4' 'Iris-setosa']
#  ['4.6' '3.4' '1.4' '0.3' 'Iris-setosa']
#  ['5.0' '3.4' '1.5' '0.2' 'Iris-setosa']
#  ['4.4' '2.9' '1.4' '0.2' 'Iris-setosa']
#  ['4.9' '3.1' '1.5' '0.1' 'Iris-setosa']
#  ['5.4' '3.7' '1.5' '0.2' 'Iris-setosa']]

# 方法2
iris_data = np.loadtxt(outfile, dtype=object, delimiter=',', skiprows=1)
i, j = iris_data.shape
np.random.seed(20201128)
iris_data[np.random.choice(i, size=20), np.random.choice(j, size=20)] = np.nan
print(iris_data[0:10])
# [['4.9' '3.0' '1.4' '0.2' 'Iris-setosa']
#  ['4.7' '3.2' '1.3' '0.2' 'Iris-setosa']
#  ['4.6' '3.1' '1.5' '0.2' 'Iris-setosa']
#  ['5.0' '3.6' '1.4' '0.2' 'Iris-setosa']
#  ['5.4' '3.9' '1.7' '0.4' 'Iris-setosa']
#  ['4.6' '3.4' '1.4' '0.3' 'Iris-setosa']
#  ['5.0' '3.4' '1.5' '0.2' 'Iris-setosa']
#  ['4.4' '2.9' '1.4' '0.2' 'Iris-setosa']
#  ['4.9' '3.1' '1.5' '0.1' 'Iris-setosa']
#  ['5.4' '3.7' '1.5' '0.2' 'Iris-setosa']]

6. 在iris_data的sepallength中查找缺失值的个数和位置(第1列)。

【知识点:逻辑函数、搜索】

  • 如何在numpy数组中找到缺失值的位置?
import numpy as np
outfile = r'.\iris.data'
iris_data = np.loadtxt(outfile, dtype=float, delimiter=',', skiprows=1, usecols=[0, 1, 2,3])
i, j = iris_data.shape
np.random.seed(20201128)
iris_data[np.random.randint(i, size=20), np.random.randint(j, size=20)] = np.nan
sepallength = iris_data[:, 0]
x = np.isnan(sepallength)
print(sum(x))
# 4
print(np.where(x))
# (array([ 23,  72,  74, 107], dtype=int64),)

7. 筛选具有 sepallength(第1列)< 5.0 并且 petallength(第3列)> 1.5 的 iris_data行。

【知识点:搜索】

  • 如何根据两个或多个条件筛选numpy数组?
import numpy as np

outfile = r'.\iris.data'
iris_data = np.loadtxt(outfile, dtype=float, delimiter=',', skiprows=1, usecols=[0, 1, 2, 3])
sepallength = iris_data[:, 0]
petallength = iris_data[:, 2]
index = np.where(np.logical_and(petallength > 1.5, sepallength < 5.0))
print(iris_data[index])
# [[4.8 3.4 1.6 0.2]
# [4.8 3.4 1.9 0.2]
# [4.7 3.2 1.6 0.2]
# [4.8 3.1 1.6 0.2]
# [4.9 2.4 3.3 1. ]
# [4.9 2.5 4.5 1.7]]

8. 选择没有任何 nan 值的 iris_data行。

【知识点:逻辑函数、搜索】

  • 如何从numpy数组中删除包含缺失值的行?
import numpy as np

outfile = r'.\iris.data'
iris_data = np.loadtxt(outfile, dtype=float, delimiter=',', skiprows=1, usecols=[0, 1, 2, 3])
i, j = iris_data.shape
np.random.seed(20201128)
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[0:10])
# [[4.9 3.  1.4 0.2]
#  [4.7 3.2 1.3 0.2]
#  [4.6 3.1 1.5 0.2]
#  [5.  3.6 1.4 0.2]
#  [5.4 3.9 1.7 0.4]
#  [4.6 3.4 1.4 0.3]
#  [5.  3.4 1.5 0.2]
#  [4.4 2.9 1.4 0.2]
#  [4.9 3.1 1.5 0.1]
#  [5.4 3.7 1.5 0.2]]

9. 计算 iris_data 中sepalLength(第1列)和petalLength(第3列)之间的相关系数。

【知识点:统计相关】

  • 如何计算numpy数组两列之间的相关系数?
import numpy as np

outfile = r'.\iris.data'
iris_data = np.loadtxt(outfile, dtype=float, delimiter=',', skiprows=1, usecols=[0, 1, 2, 3])
sepalLength = iris_data[:, 0]
petalLength = iris_data[:, 2]

# 方法1
m1 = np.mean(sepalLength)
m2 = np.mean(petalLength)
cov = np.dot(sepalLength - m1, petalLength - m2)
std1 = np.sqrt(np.dot(sepalLength - m1, sepalLength - m1))
std2 = np.sqrt(np.dot(petalLength - m2, petalLength - m2))
print(cov / (std1 * std2))
# 0.8712829406718028

# 方法2
x = np.mean((sepalLength - m1) * (petalLength - m2))
y = np.std(sepalLength) * np.std(petalLength)
print(x / y)
# 0.871282940671803

# 方法3
x = np.cov(sepalLength, petalLength, ddof=False)
y = np.std(sepalLength) * np.std(petalLength)
print(x[0, 1] / y)
# 0.8712829406718028

# 方法4
x = np.corrcoef(sepalLength, petalLength)
print(x)
# [[1.         0.87128294]
#  [0.87128294 1.        ]]

10. 找出iris_data是否有任何缺失值。

【知识点:逻辑函数】

  • 如何查找给定数组是否具有空值?
import numpy as np

outfile = r'.\iris.data'
iris_data = np.loadtxt(outfile, dtype=float, delimiter=',', skiprows=1, usecols=[0, 1, 2, 3])
x = np.isnan(iris_data)
print(np.any(x))
# False

11. 在numpy数组中将所有出现的nan替换为0。

【知识点:逻辑函数】

  • 如何在numpy数组中用0替换所有缺失值?
import numpy as np

outfile = r'.\iris.data'
iris_data = np.loadtxt(outfile, dtype=float, delimiter=',', skiprows=1, usecols=[0, 1, 2, 3])
i, j = iris_data.shape
np.random.seed(20201128)
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[0:10])
# [[4.9 3.  1.4 0.2]
#  [4.7 3.2 1.3 0.2]
#  [4.6 3.1 1.5 0.2]
#  [5.  3.6 1.4 0.2]
#  [5.4 3.9 1.7 0.4]
#  [4.6 3.4 1.4 0.3]
#  [5.  3.4 1.5 0.2]
#  [4.4 2.9 1.4 0.2]
#  [4.9 3.1 1.5 0.1]
#  [5.4 3.7 1.5 0.2]]

12. 找出鸢尾属植物物种中的唯一值和唯一值出现的数量。

【知识点:数组操作】

  • 如何在numpy数组中查找唯一值的计数?
import numpy as np
outfile = r'.\iris.data'
iris_data = np.loadtxt(outfile, dtype=object, delimiter=',', skiprows=1, usecols=[4])
x = np.unique(iris_data, return_counts=True)
print(x)
# (array(['Iris-setosa', 'Iris-versicolor', 'Iris-virginica'], dtype=object), array([49, 50, 50], dtype=int64))

13. 将 iris_data 的花瓣长度(第3列)以形成分类变量的形式显示。定义:Less than 3 --> ‘small’;3-5 --> ‘medium’;’>=5 --> ‘large’。

【知识点:统计相关】

  • 如何将数字转换为分类(文本)数组?
import numpy as np

outfile = r'.\iris.data'
iris_data = np.loadtxt(outfile, dtype=float, delimiter=',', skiprows=1, usecols=[0, 1, 2, 3])
petal_length_bin = np.digitize(iris_data[:, 2], [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']

14. 在 iris_data 中创建一个新列,其中 volume 是 (pi x petallength x sepallength ^ 2)/ 3。

【知识点:数组操作】

  • 如何从numpy数组的现有列创建新列?
import numpy as np

outfile = r'.\iris.data'
iris_data = np.loadtxt(outfile, dtype=object, delimiter=',', skiprows=1)
sepalLength = iris_data[:, 0].astype(float)
petalLength = iris_data[:, 2].astype(float)
volume = (np.pi * petalLength * sepalLength ** 2) / 3
volume = volume[:, np.newaxis]
iris_data = np.concatenate([iris_data, volume], axis=1)
print(iris_data[0:10])
# [['4.9' '3.0' '1.4' '0.2' 'Iris-setosa' 35.200498485922445]
#  ['4.7' '3.2' '1.3' '0.2' 'Iris-setosa' 30.0723720777127]
#  ['4.6' '3.1' '1.5' '0.2' 'Iris-setosa' 33.238050274980004]
#  ['5.0' '3.6' '1.4' '0.2' 'Iris-setosa' 36.65191429188092]
#  ['5.4' '3.9' '1.7' '0.4' 'Iris-setosa' 51.911677007917746]
#  ['4.6' '3.4' '1.4' '0.3' 'Iris-setosa' 31.022180256648003]
#  ['5.0' '3.4' '1.5' '0.2' 'Iris-setosa' 39.269908169872416]
#  ['4.4' '2.9' '1.4' '0.2' 'Iris-setosa' 28.38324242763259]
#  ['4.9' '3.1' '1.5' '0.1' 'Iris-setosa' 37.714819806345474]
#  ['5.4' '3.7' '1.5' '0.2' 'Iris-setosa' 45.80442088933919]]

15. 随机抽鸢尾属植物的种类,使得Iris-setosa的数量是Iris-versicolor和Iris-virginica数量的两倍。

【知识点:随机抽样】

  • 如何在numpy中进行概率抽样?
import numpy as np

species = np.array(['Iris‐setosa', 'Iris‐versicolor', 'Iris‐virginica'])
species_out = np.random.choice(species, 10000, p=[0.5, 0.25, 0.25])
print(np.unique(species_out, return_counts=True))
# (array(['Iris‐setosa', 'Iris‐versicolor', 'Iris‐virginica'], dtype='<U15'), array([4952, 2566, 2482], dtype=int64))

16. 根据 sepallength 列对数据集进行排序。

【知识点:排序】

  • 如何按列对2D数组进行排序?
import numpy as np

outfile = r'.\iris.data'
iris_data = np.loadtxt(outfile, dtype=object, delimiter=',', skiprows=1)
sepalLength = iris_data[:, 0]
index = np.argsort(sepalLength)
print(iris_data[index][0:10])
# [['4.3' '3.0' '1.1' '0.1' 'Iris-setosa']
#  ['4.4' '3.2' '1.3' '0.2' 'Iris-setosa']
#  ['4.4' '3.0' '1.3' '0.2' 'Iris-setosa']
#  ['4.4' '2.9' '1.4' '0.2' 'Iris-setosa']
#  ['4.5' '2.3' '1.3' '0.3' 'Iris-setosa']
#  ['4.6' '3.2' '1.4' '0.2' 'Iris-setosa']
#  ['4.6' '3.1' '1.5' '0.2' 'Iris-setosa']
#  ['4.6' '3.4' '1.4' '0.3' 'Iris-setosa']
#  ['4.6' '3.6' '1.0' '0.2' 'Iris-setosa']
#  ['4.7' '3.2' '1.6' '0.2' 'Iris-setosa']]

17. 在鸢尾属植物数据集中找到最常见的花瓣长度值(第3列)。

【知识点:数组操作】

  • 如何在numpy数组中找出出现次数最多的值?
import numpy as np

outfile = r'.\iris.data'
iris_data = np.loadtxt(outfile, dtype=object, delimiter=',', skiprows=1)
petalLength = iris_data[:, 2]
vals, counts = np.unique(petalLength, return_counts=True)
print(vals[np.argmax(counts)])
# 1.5
print(np.amax(counts))
# 14

18. 在鸢尾花数据集的 petalwidth(第4列)中查找第一次出现的值大于1.0的位置。

【知识点:搜索】

  • 如何找到第一次出现大于给定值的位置?
import numpy as np

outfile = r'.\iris.data'
iris_data = np.loadtxt(outfile, dtype=float, delimiter=',', skiprows=1, usecols=[0, 1, 2, 3])
petalWidth = iris_data[:, 3]
index = np.where(petalWidth > 1.0)
print(index)
# (array([ 49,  50,  51,  52,  53,  54,  55,  57,  58,  60,  62,  63,  64,
#         65,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,  79,
#         81,  82,  83,  84,  85,  86,  87,  88,  89,  90,  91,  93,  94,
#         95,  96,  97,  98,  99, 100, 101, 102, 103, 104, 105, 106, 107,
#        108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120,
#        121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133,
#        134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146,
#        147, 148], dtype=int64),)
print(index[0][0])
# 49
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

沧浪之水、

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

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

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

打赏作者

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

抵扣说明:

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

余额充值