机器学习:numpy练习题
1. 从数组 a=np.arange(15)提取 5 到 10 之间的所有元素
import numpy as np
a = np.arange(15)
print(a[(a<=10) & (a>=5)])
[ 5 6 7 8 9 10]
2. 将数组 np.arange(20)转变为 4 行 5 列的二维数组,并执行交换第 1 行和第 2 行,交换第 1 列和第 2 列。
b = np.arange(20).reshape(4,5)
print(b)
b = b[[1,0,2,3],:]
print(b)
print(b[:,[1,0,2,3]])
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
[[ 6 5 7 8]
[ 1 0 2 3]
[11 10 12 13]
[16 15 17 18]]
3. 寻找数组 np.random.randint(1,10,size=(5,5))中所有的奇数,并将所有奇数替换为 0.
c = np.random.randint(1,10,size=(5,5))
print(c)
c[c%2!=0]=0
print(c)
[[8 3 4 3 5]
[4 7 1 7 9]
[6 5 2 1 2]
[3 7 8 4 2]
[8 4 3 6 4]]
[[8 0 4 0 0]
[4 0 0 0 0]
[6 0 2 0 2]
[0 0 8 4 2]
[8 4 0 6 4]]
4. 从1—50之间的均匀地产生随机数字20个,存储数组 a 中,替换大于等于 30 数为 0,并获取给定数组 a 中前 5 个最大值的位置。
d = np.random.randint(1,50,size=(20))
print(d)
d[d>=30]=0
print(d)
d=d.argsort()[::-1][0:5]
print(d)
[29 40 4 43 27 23 1 44 15 28 22 14 38 17 34 16 35 11 1 32]
[29 0 4 0 27 23 1 0 15 28 22 14 0 17 0 16 0 11 1 0]
[ 0 9 4 5 10]
5. 利用下列方式获取数组 iris_2d
url="https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
iris_2d=np.genfromtxt(url, delimiter=',', dtype= 'float')
url="https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
iris_2d=np.genfromtxt(url, delimiter=',', dtype= 'float')
print(iris_2d)
[[5.1 3.5 1.4 0.2 nan]
[4.9 3. 1.4 0.2 nan]
[4.7 3.2 1.3 0.2 nan]
[4.6 3.1 1.5 0.2 nan]
[5. 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. 5.8 2.2 nan]
[7.6 3. 6.6 2.1 nan]
[4.9 2.5 4.5 1.7 nan]
[7.3 2.9 6.3 1.8 nan]
[6.7 2.5 5.8 1.8 nan]
[7.2 3.6 6.1 2.5 nan]
[6.5 3.2 5.1 2. nan]
[6.4 2.7 5.3 1.9 nan]
[6.8 3. 5.5 2.1 nan]
[5.7 2.5 5. 2. nan]
[5.8 2.8 5.1 2.4 nan]
[6.4 3.2 5.3 2.3 nan]
[6.5 3. 5.5 1.8 nan]
[7.7 3.8 6.7 2.2 nan]
[7.7 2.6 6.9 2.3 nan]
[6. 2.2 5. 1.5 nan]
[6.9 3.2 5.7 2.3 nan]
[5.6 2.8 4.9 2. nan]
[7.7 2.8 6.7 2. nan]
[6.3 2.7 4.9 1.8 nan]
[6.7 3.3 5.7 2.1 nan]
[7.2 3.2 6. 1.8 nan]
[6.2 2.8 4.8 1.8 nan]
[6.1 3. 4.9 1.8 nan]
[6.4 2.8 5.6 2.1 nan]
[7.2 3. 5.8 1.6 nan]
[7.4 2.8 6.1 1.9 nan]
[7.9 3.8 6.4 2. nan]
[6.4 2.8 5.6 2.2 nan]
[6.3 2.8 5.1 1.5 nan]
[6.1 2.6 5.6 1.4 nan]
[7.7 3. 6.1 2.3 nan]
[6.3 3.4 5.6 2.4 nan]
[6.4 3.1 5.5 1.8 nan]
[6. 3. 4.8 1.8 nan]
[6.9 3.1 5.4 2.1 nan]
[6.7 3.1 5.6 2.4 nan]
[6.9 3.1 5.1 2.3 nan]
[5.8 2.7 5.1 1.9 nan]
[6.8 3.2 5.9 2.3 nan]
[6.7 3.3 5.7 2.5 nan]
[6.7 3. 5.2 2.3 nan]
[6.3 2.5 5. 1.9 nan]
[6.5 3. 5.2 2. nan]
[6.2 3.4 5.4 2.3 nan]
[5.9 3. 5.1 1.8 nan]]
(1)在 iris_2d 数据集的 20 个随机位插入 np.nan 值
iris_2d[np.random.randint(0,150,size=20),np.random.randint(0,4,size=20)]=np.nan
iris_2d
array([[5.1, 3.5, 1.4, 0.2, nan],
[nan, 3. , 1.4, 0.2, nan],
[4.7, 3.2, 1.3, 0.2, nan],
[4.6, 3.1, 1.5, 0.2, nan],
[nan, 3.6, 1.4, 0.2, nan],
[5.4, nan, 1.7, 0.4, nan],
[4.6, 3.4, nan, nan, nan],
[5. , 3.4, 1.5, 0.2, nan],
[4.4, 2.9, 1.4, 0.2, nan],
[4.9, 3.1, nan, 0.1, nan],
[5.4, 3.7, 1.5, 0.2, nan],
[4.8, nan, 1.6, 0.2, nan],
[4.8, 3. , 1.4, 0.1, nan],
[nan, 3. , nan, 0.1, nan],
[5.8, 4. , 1.2, 0.2, nan],
[5.7, 4.4, 1.5, nan, nan],
[nan, 3.9, 1.3, 0.4, nan],
[5.1, 3.5, 1.4, 0.3, nan],
[5.7, 3.8, 1.7, 0.3, nan],
[nan, 3.8, 1.5, 0.3, nan],
[5.4, 3.4, 1.7, 0.2, nan],
[5.1, 3.7, 1.5, 0.4, nan],
[4.6, 3.6, nan, 0.2, nan],
[5.1, 3.3, 1.7, 0.5, nan],
[4.8, 3.4, 1.9, 0.2, nan],
[5. , 3. , 1.6, 0.2, nan],
[5. , 3.4, 1.6, 0.4, nan],
[5.2, 3.5, 1.5, 0.2, nan],
[5.2, 3.4, 1.4, 0.2, nan],
[4.7, 3.2, 1.6, 0.2, nan],
[4.8, nan, 1.6, 0.2, nan],
[5.4, 3.4, 1.5, 0.4, nan],
[5.2, 4.1, 1.5, 0.1, nan],
[5.5, 4.2, 1.4, nan, nan],
[nan, 3.1, 1.5, 0.1, nan],
[5. , 3.2, 1.2, nan, nan],
[5.5, 3.5, 1.3, 0.2, nan],
[4.9, 3.1, 1.5, 0.1, nan],
[4.4, nan, 1.3, 0.2, nan],
[5.1, 3.4, 1.5, 0.2, nan],
[5. , 3.5, 1.3, 0.3, nan],
[4.5, nan, 1.3, nan, nan],
[4.4, nan, 1.3, nan, nan],
[nan, 3.5, 1.6, 0.6, nan],
[5.1, 3.8, 1.9, 0.4, nan],
[4.8, 3. , 1.4, 0.3, nan],
[5.1, 3.8, nan, 0.2, nan],
[4.6, 3.2, 1.4, 0.2, nan],
[5.3, 3.7, nan, 0.2, nan],
[5. , 3.3, nan, nan, nan],
[7. , 3.2, 4.7, nan, nan],
[6.4, 3.2, 4.5, 1.5, nan],
[6.9, 3.1, 4.9, 1.5, nan],
[nan, 2.3, 4. , 1.3, nan],
[6.5, 2.8, 4.6, nan, nan],
[5.7, 2.8, 4.5, 1.3, nan],
[6.3, 3.3, 4.7, 1.6, nan],
[4.9, 2.4, nan, 1. , nan],
[6.6, 2.9, 4.6, 1.3, nan],
[5.2, 2.7, 3.9, nan, nan],
[5. , 2. , 3.5, 1. , nan],
[5.9, 3. , 4.2, 1.5, nan],
[6. , 2.2, 4. , 1. , nan],
[6.1, 2.9, 4.7, 1.4, nan],
[5.6, 2.9, 3.6, 1.3, nan],
[6.7, 3.1, 4.4, 1.4, nan],
[5.6, 3. , 4.5, 1.5, nan],
[5.8, 2.7, nan, 1. , nan],
[6.2, 2.2, 4.5, 1.5, nan],
[5.6, 2.5, 3.9, 1.1, nan],
[5.9, 3.2, 4.8, 1.8, nan],
[6.1, 2.8, 4. , 1.3, nan],
[6.3, nan, 4.9, 1.5, nan],
[6.1, 2.8, 4.7, 1.2, nan],
[6.4, 2.9, 4.3, nan, nan],
[6.6, 3. , 4.4, 1.4, nan],
[6.8, 2.8, 4.8, 1.4, nan],
[6.7, 3. , 5. , 1.7, nan],
[6. , 2.9, 4.5, nan, nan],
[nan, 2.6, 3.5, 1. , nan],
[5.5, 2.4, 3.8, 1.1, nan],
[5.5, nan, nan, 1. , nan],
[5.8, 2.7, 3.9, 1.2, nan],
[6. , 2.7, nan, nan, nan],
[5.4, 3. , 4.5, 1.5, nan],
[6. , 3.4, 4.5, 1.6, nan],
[6.7, 3.1, 4.7, nan, nan],
[6.3, 2.3, 4.4, 1.3, nan],
[5.6, 3. , 4.1, 1.3, nan],
[5.5, 2.5, 4. , 1.3, nan],
[5.5, nan, 4.4, 1.2, nan],
[6.1, 3. , 4.6, 1.4, nan],
[5.8, 2.6, 4. , 1.2, nan],
[5. , 2.3, 3.3, nan, nan],
[nan, 2.7, nan, 1.3, nan],
[5.7, 3. , nan, 1.2, nan],
[5.7, 2.9, nan, 1.3, nan],
[6.2, 2.9, 4.3, 1.3, nan],
[5.1, 2.5, 3. , 1.1, nan],
[nan, 2.8, 4.1, 1.3, nan],
[6.3, 3.3, nan, 2.5, nan],
[5.8, 2.7, 5.1, 1.9, nan],
[7.1, 3. , nan, 2.1, nan],
[6.3, 2.9, 5.6, 1.8, nan],
[6.5, 3. , 5.8, 2.2, nan],
[7.6, 3. , 6.6, 2.1, nan],
[4.9, 2.5, 4.5, 1.7, nan],
[7.3, 2.9, 6.3, nan, nan],
[6.7, 2.5, 5.8, 1.8, nan],
[7.2, 3.6, 6.1, 2.5, nan],
[6.5, 3.2, 5.1, 2. , nan],
[6.4, 2.7, 5.3, nan, nan],
[6.8, nan, 5.5, 2.1, nan],
[5.7, 2.5, 5. , 2. , nan],
[5.8, 2.8, 5.1, 2.4, nan],
[6.4, 3.2, 5.3, nan, nan],
[6.5, 3. , 5.5, 1.8, nan],
[7.7, 3.8, 6.7, 2.2, nan],
[7.7, 2.6, 6.9, 2.3, nan],
[6. , 2.2, 5. , 1.5, nan],
[6.9, 3.2, 5.7, 2.3, nan],
[5.6, nan, 4.9, 2. , nan],
[nan, 2.8, 6.7, 2. , nan],
[6.3, 2.7, 4.9, nan, nan],
[6.7, 3.3, 5.7, 2.1, nan],
[7.2, 3.2, 6. , 1.8, nan],
[6.2, nan, 4.8, 1.8, nan],
[6.1, 3. , 4.9, 1.8, nan],
[6.4, 2.8, 5.6, nan, nan],
[nan, 3. , 5.8, 1.6, nan],
[7.4, 2.8, 6.1, 1.9, nan],
[7.9, nan, nan, 2. , nan],
[6.4, 2.8, 5.6, 2.2, nan],
[nan, 2.8, 5.1, 1.5, nan],
[6.1, 2.6, 5.6, 1.4, nan],
[nan, 3. , 6.1, 2.3, nan],
[6.3, nan, nan, 2.4, nan],
[6.4, 3.1, 5.5, 1.8, nan],
[6. , 3. , 4.8, nan, nan],
[6.9, 3.1, 5.4, 2.1, nan],
[nan, 3.1, 5.6, 2.4, nan],
[6.9, 3.1, 5.1, 2.3, nan],
[5.8, nan, 5.1, nan, nan],
[6.8, 3.2, nan, 2.3, nan],
[6.7, 3.3, 5.7, 2.5, nan],
[nan, 3. , 5.2, 2.3, nan],
[6.3, nan, 5. , 1.9, nan],
[6.5, 3. , 5.2, 2. , nan],
[6.2, 3.4, 5.4, 2.3, nan],
[nan, 3. , 5.1, 1.8, nan]])
(2) 在 iris_2d 的 sepallength(第 1 列)中查找缺失值的数量和位置
print("Number of missing values: \n", np.isnan(iris_2d[:, 0]).sum())
print("Position of missing values: \n", np.where(np.isnan(iris_2d[:, 0])))
Number of missing values:
18
Position of missing values:
(array([ 1, 4, 13, 16, 19, 34, 43, 53, 79, 94, 99, 122, 129,
133, 135, 140, 145, 149]),)
(3) 过滤具有 petallength(第 3 列)> 1.5 和 sepallength(第 1 列)<5.0 的 iris_2d 的行;
condition = (iris_2d[:, 2] > 1.5) & (iris_2d[:, 0] < 5.0)
r1 = iris_2d[condition]
print(r1)
[[4.8 nan 1.6 0.2 nan]
[4.8 3.4 1.9 0.2 nan]
[4.7 3.2 1.6 0.2 nan]
[4.8 nan 1.6 0.2 nan]
[4.9 2.5 4.5 1.7 nan]]
(4) 选择没有 nan 值的 iris_2d 数组的行
r = iris_2d[np.sum(np.isnan(iris_2d), axis = 1) == 0][:5]
print(r)
[]
(6) 在 numpy 数组中用 0 替换 nan
iris_2d[np.random.randint(150, size=20), np.random.randint(4, size=20)] = np.nan
iris_2d[np.isnan(iris_2d)] = 0
r1 = iris_2d[::]
print(r1)
[[5.1 3.5 1.4 0.2 0. ]
[0. 3. 1.4 0.2 0. ]
[4.7 3.2 1.3 0.2 0. ]
[4.6 3.1 1.5 0.2 0. ]
[0. 3.6 1.4 0.2 0. ]
[5.4 0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3-5 则为'中','> = 5 则为'大';
names = ('sepallength', 'sepalwidth', 'petallength', 'petalwidth', 'species')
petal_length_bin = np.digitize(iris_2d[:, 2].astype('float'), [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]
r = petal_length_cat[::]
print(r)
['small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'small', 'medium', 'medium', 'medium', 'medium', 'small', 'medium', 'medium', 'small', 'medium', 'medium', 'medium', 'small', 'medium', 'medium', 'medium', 'small', 'medium', 'small', 'medium', 'small', 'medium', 'medium', 'medium', 'medium', 'medium', 'medium', 'medium', 'small', 'medium', 'medium', 'medium', 'small', 'medium', 'small', 'medium', 'medium', 'medium', 'medium', 'medium', 'medium', 'medium', 'medium', 'medium', 'medium', 'small', 'small', 'small', 'medium', 'medium', 'medium', 'small', 'large', 'small', 'large', 'large', 'large', 'medium', 'large', 'large', 'large', 'large', 'large', 'large', 'large', 'large', 'large', 'large', 'large', 'large', 'large', 'large', 'medium', 'large', 'medium', 'large', 'large', 'medium', 'medium', 'large', 'large', 'large', 'small', 'large', 'large', 'large', 'large', 'small', 'large', 'medium', 'large', 'large', 'large', 'large', 'small', 'large', 'large', 'large', 'large', 'large', 'large']
(8) 在 iris_2d 中为 volume 创建一个新列,其中 volume 是(pi petallength
sepal_length ^2)/ 3;
sepallength = iris_2d[:, 0].astype('float')
petallength = iris_2d[:, 2].astype('float')
volume = (np.pi * petallength * (sepallength**2))/3
volume = volume[:, np.newaxis]
out = np.hstack([iris_2d, volume])
r1 = out[:10]
print(r1)
[[ 5.1 3.5 1.4 0.2 0. 38.13265163]
[ 0. 3. 1.4 0.2 0. 0. ]
[ 4.7 3.2 1.3 0.2 0. 30.07237208]
[ 4.6 3.1 1.5 0.2 0. 33.23805027]
[ 0. 3.6 1.4 0.2 0. 0. ]
[ 5.4 0. 1.7 0.4 0. 51.91167701]
[ 4.6 3.4 0. 0. 0. 0. ]
[ 5. 3.4 1.5 0. 0. 39.26990817]
[ 4.4 2.9 1.4 0.2 0. 28.38324243]
[ 4.9 3.1 0. 0.1 0. 0. ]]
(9) 查找在 iris 数据集的第 4 列花瓣宽度中第一次出现值大于 1.0 的位置
r1 = np.argwhere(iris_2d[:, 3].astype(float) > 1.0)[0]
print(r1)
[51]
6. 使用 numpy 数组计算由 5 个坐标:(1,9)、(5,12)、(8,20)、(4,10)、(2,8) 构成的图形的周长。
import numpy as np
v1=np.array([1,9,5,12,8,20,4,10,2,8])
v2=np.array([5,12,8,20,4,10,2,8,1,9])
dis1=np.sqrt(np.sum(np.square(v1-v2)))
#求范数
dis2=np.linalg.norm(v1-v2)
print(dis1)
14.966629547095765