大作业回顾
1. 导入鸢尾属植物数据集,保持文本不变。
【知识点:输入和输出】
如何导入存在数字和文本的数据集?
- numpy.loadtxt()
2. 求出鸢尾属植物萼片长度的平均值、中位数和标准差(第1列,sepallength)
【知识点:统计相关】
如何计算numpy数组的均值,中位数,标准差?
- numpy.mean()
- numpy.medium()
- numpy.std()
3. 创建一种标准化形式的鸢尾属植物萼片长度,其值正好介于0和1之间,这样最小值为0,最大值为1(第1列,sepallength)。
【知识点:统计相关】如何标准化数组?
# 方法一
aMax = np.amax(sepalLength)
aMin = np.amin(sepalLength)
x = (sepalLength ‐ aMin) / (aMax ‐ aMin) # 方法2
x = (sepalLength ‐ aMin) /np.ptp(sepalLength)
4. 找到鸢尾属植物萼片长度的第5和第95百分位数(第1列,sepallength)。
【知识点:统计相关】
如何找到numpy数组的百分位数?
x = np.percentile(sepalLength, [5, 95])
5. 把iris_data数据集中的20个随机位置修改为np.nan值。
【知识点:随机抽样】
如何在数组中的随机位置修改值?
# 方法1
iris_data = np.loadtxt(outfile, dtype=object, delimiter=',', skiprows=1)
i, j = iris_data.shape np.random.seed(20200621) iris_data[np.random.randint(i, size=20), np.random.randint(j, size=20)] = np.nan print(iris_data[0:10])
# 方法2
iris_data = np.loadtxt(outfile, dtype=object, delimiter=',', skiprows=1)
i, j = iris_data.shape np.random.seed(20200620) iris_data[np.random.choice(i, size=20), np.random.choice(j, size=20)] = np.nan print(iris_data[0:10])
6. 在iris_data的sepallength中查找缺失值的个数和位置(第1列)。
【知识点:逻辑函数、搜索】
如何在numpy数组中找到缺失值的位置?
i, j = iris_data.shape np.random.seed(20200621) 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))
print(np.where(x))
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])
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(20200621) 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])
9. 计算 iris_data 中sepalLength(第1列)和petalLength(第3列)之间的相关系数。
【知识点:统计相关】
如何计算numpy数组两列之间的相关系数?
# 方法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.8717541573048712
# 方法2
x = np.mean((sepalLength ‐ m1) * (petalLength ‐ m2))
y = np.std(sepalLength) * np.std(petalLength)
print(x / y)
# 0.8717541573048712
# 方法3
x = np.cov(sepalLength, petalLength, ddof=False)
y = np.std(sepalLength) * np.std(petalLength)
print(x[0, 1] / y)
# 0.8717541573048716
# 方法4
x = np.corrcoef(sepalLength, petalLength)
print(x)
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