Python 从numpy数组中获取元素并创建新numpy数组的最快方法
我有Python 从numpy数组中获取元素并创建新numpy数组的最快方法,python,python-2.7,python-3.x,numpy,scipy,Python,Python 2.7,Python 3.x,Numpy,Scipy,我有numpy数组,称为data的维度150x4 我想通过从数据中选择随机元素,创建一个新的numpy数组,名为mean,维度为3x4 我目前的做法是: cols = (data.shape[1]) K=3 mean = np.zeros((K,cols)) for row in range(K): index = np.random.randint(data.shape[0]) for col in range(cols):
numpy
数组,称为data
的维度150x4
我想通过从数据
中选择随机元素,创建一个新的numpy
数组,名为mean
,维度为3x4
我目前的做法是:
cols = (data.shape[1])
K=3
mean = np.zeros((K,cols))
for row in range(K):
index = np.random.randint(data.shape[0])
for col in range(cols):
mean[row][col] = data[index][col]
有没有更快的方法来实现这一点?您可以在
numpy.randint
中指定随机整数的数量(第三个参数)。另外,您应该熟悉numpy.array
的索引符号。在这里,您可以通过:
说明符访问一行中的所有元素
mean = data[np.random.randint(0,len(data),3),:]