官方文档的搬运工
numpy.random.choice生产一系列随机数
numpy.random.choice(a, size=None, replace=True, p=None)
parameter介绍
-
a: 数组或者整数:如果是数组则从其中采样,若是整数则从range(a)中进行采样
-
size:生成随机数的个数
-
replace:是否重复采样
-
p:a中每个元素的权重=被pick的概率,若果不设置,则默认平均采样
examples:
samples = np.random.choice(20, 10, replace=False)
print(samples)
samples = np.random.choice(range(20),10,replace=False) # 同上
print(samples)
samples = np.random.choice(range(20),10,replace=True) # 可重复采样
print(samples)
结果
numpy.concatenate
相对于append,extend方法,concatenate效率更高,适合大规模数据的拼接,使用方法如下
import numpy as np
arr1 = np.array([[1,2,3,4],[4,5,6,7]])
print("shape of arr1:{}".format(arr1.shape))
arr2 = np.array([[1,9,8,6],[5,6,7,8]])
print("shape of arr2:{}".format(arr2.shape))
arr3 = np.concatenate((arr1,arr2),axis=1) #按照维度1拼接,如果不设定axis,默认axis=0
print("shape of arr3:{}".format(arr3.shape))
arr4 = np.concatenate((arr1, arr2), axis=0)#按照维度0拼接
print("shape of arr4:{}".format(arr4.shape))
打印结果
shape of arr1:(2, 4)
shape of arr2:(2, 4)
shape of arr3:(2, 8)
shape of arr4:(4, 4)
可拼接多个数组
arr5 = np.concatenate((arr1,arr1,arr1),axis=1)
print("shape of arr5:{}".format(arr5.shape))
打印结果
shape of arr5:(2, 12)