如何在numpy的二维矩阵中随机采样
Posted
技术标签:
【中文标题】如何在numpy的二维矩阵中随机采样【英文标题】:how to randomly sample in 2D matrix in numpy 【发布时间】:2017-11-10 20:17:01 【问题描述】:我有一个像这样的二维数组/矩阵,我将如何从这个二维矩阵中随机选择值,例如获取像 [-62, 29.23]
这样的值。我查看了numpy.choice
,但它是为一维数组构建的。
以下是我的 4 行 8 列示例
Space_Position=[
[[-62,29.23],[-49.73,29.23],[-31.82,29.23],[-14.2,29.23],[3.51,29.23],[21.21,29.23],[39.04,29.23],[57.1,29.23]],
[[-62,11.28],[-49.73,11.28],[-31.82,11.28],[-14.2,11.28],[3.51,11.28],[21.21,11.28] ,[39.04,11.28],[57.1,11.8]],
[[-62,-5.54],[-49.73,-5.54],[-31.82,-5.54] ,[-14.2,-5.54],[3.51,-5.54],[21.21,-5.54],[39.04,-5.54],[57.1,-5.54]],
[[-62,-23.1],[-49.73,-23.1],[-31.82,-23.1],[-14.2,-23.1],[3.51,-23.1],[21.21,-23.1],[39.04,-23.1] ,[57.1,-23.1]]
]
在答案中给出了以下解决方案:
random_index1 = np.random.randint(0, Space_Position.shape[0])
random_index2 = np.random.randint(0, Space_Position.shape[1])
Space_Position[random_index1][random_index2]
这确实可以给我一个样本,像np.choice()
那样提供多个样本怎么样?
我正在考虑的另一种方法是将矩阵转换为数组而不是矩阵,
Space_Position=[
[-62,29.23],[-49.73,29.23],[-31.82,29.23],[-14.2,29.23],[3.51,29.23],[21.21,29.23],[39.04,29.23],[57.1,29.23], ..... ]
最后使用np.choice()
,但是我找不到进行转换的方法,np.flatten()
使数组像
Space_Position=[-62,29.23,-49.73,29.2, ....]
【问题讨论】:
【参考方案1】:只需使用随机索引(在您的情况下为 2,因为您有 3 个维度):
import numpy as np
Space_Position = np.array(Space_Position)
random_index1 = np.random.randint(0, Space_Position.shape[0])
random_index2 = np.random.randint(0, Space_Position.shape[1])
Space_Position[random_index1, random_index2] # get the random element.
另一种方法是实际将其设为 2D:
Space_Position = np.array(Space_Position).reshape(-1, 2)
然后使用一个随机索引:
Space_Position = np.array(Space_Position).reshape(-1, 2) # make it 2D
random_index = np.random.randint(0, Space_Position.shape[0]) # generate a random index
Space_Position[random_index] # get the random element.
如果您想要 N
替换样本:
N = 5
Space_Position = np.array(Space_Position).reshape(-1, 2) # make it 2D
random_indices = np.random.randint(0, Space_Position.shape[0], size=N) # generate N random indices
Space_Position[random_indices] # get N samples with replacement
或不替换:
Space_Position = np.array(Space_Position).reshape(-1, 2) # make it 2D
random_indices = np.arange(0, Space_Position.shape[0]) # array of all indices
np.random.shuffle(random_indices) # shuffle the array
Space_Position[random_indices[:N]] # get N samples without replacement
【讨论】:
谢谢,如果不使用for循环,有什么方法可以采样N(N>1) @user824624 是否有替换样本? 无替换 @user824624 我更新了答案,最后一种方法应该是你需要的:) ,最后一种方法还是给我一个3维数组,我希望得到一个随机的2维数组样本,如[ [-62,29.23],[-49.73,29.23],[-31.82, 29.23],[-14.2,29.23],[3.51,29.23],[21.21,29.23],[39.04,29.23],[57.1,29.23],......]【参考方案2】:Space_Position[np.random.randint(0, len(Space_Position))]
[np.random.randint(0, len(Space_Position))]
给你你想要的
【讨论】:
【参考方案3】:参考numpy.random.choice:
使用此函数无法从二维数组中采样随机行,但使用 Generator.choice 通过其轴关键字可以实现。
生成器文档链接在这里numpy.random.Generator.choice。
利用这些知识。您可以创建一个生成器,然后从您的数组中“选择”:
rng = np.random.default_rng() #creates the generator ==> Generator(PCG64) at 0x2AA703BCE50
N = 3 #Number of Choices
a = np.array(Space_Position) #makes sure, a is an ndarray and numpy-supported
s = a.shape #(4,8,2)
a = a.reshape((s[0] * s[1], s[2])) #makes your array 2 dimensional keeping the last dimension seperated
a.shape #(32, 2)
b = rng.choice(a, N, axis=0, replace=False) #returns N choices of a in array b, e.g. narray([[ 57.1 , 11.8 ], [ 21.21, -5.54], [ 39.04, 11.28]])
#Note: replace=False prevents having the same entry several times in the result
【讨论】:
以上是关于如何在numpy的二维矩阵中随机采样的主要内容,如果未能解决你的问题,请参考以下文章