[英]Python-1D indice link with 2D array location
Sometimes, I want to get the value of an 2-d array at a random location. 有时,我想在随机位置获取二维数组的值。
For example, there is an array data
in the shape of (20,20). 例如,存在一个(20,20)形状的数组data
。 There is a random number-pair (5,5). 有一个随机数对(5,5)。 Then, I get the data[5,5] as my target value. 然后,将数据[5,5]作为目标值。
On the purpose of using genetic algorithm. 目的是采用遗传算法。 I want to get the samples from an 2-d array as several individuals. 我想以几个人的身份从二维数组中获取样本。 So, I want to generate an linked table which connect an 1d value to 2d position. 因此,我想生成一个将1d值连接到2d位置的链接表。
## data was the 2-d array in the shape of 20x20
data = np.random.randint(0,1000,400)
data = data.reshape(20,20)
## direction was my linked table
direction = {"Indice":[],"X":[],"Y":[]}
k = 0
for i in range(0,data.shape[0],1):
for j in range(0,data.shape[1],1):
k+=1
direction["Indice"].append(k)
direction["X"].append(j)
direction["Y"].append(i)
direction = pd.DataFrame(direction)
## generate an random int and connect with the 2-d value.
loc = np.random.randint(0,400)
XX = np.array(direction[direction.Indice == loc ].X)
YY = np.array(direction[direction.Indice == loc ].Y)
target_value = data[YY,XX]
Are there any neat way to achieve my attempt? 有什么整洁的方法可以实现我的尝试吗?
Any advice would be appreciate! 任何建议,将不胜感激!
You could use np.ravel
to make data
1-dimensional, then index it using the flat index loc
: 您可以使用np.ravel
使data
一维化,然后使用平面索引loc
对其进行索引:
target_value = data.ravel()[loc-1]
Or, if you want XX
and YY
, perhaps you are looking for np.unravel_index
. 或者,如果您想使用XX
和YY
,也许您正在寻找np.unravel_index
。 It maps a flat index or an array of flat indices to a tuple of coordinates. 它将平面索引或平面索引数组映射到坐标元组。
For example, instead of building the direction
DataFrame, you could use 例如,您可以使用以下方法代替构建direction
DataFrame
np.unravel_index(loc-1, data.shape)
instead of 代替
XX = np.array(direction[direction.Indice == loc ].X)
YY = np.array(direction[direction.Indice == loc ].Y)
Then you could define target_value
as : 然后,您可以将target_value
定义为:
target_value = data[np.unravel_index(loc-1, data.shape)]
Alternatively, to simply get a random value from the 2D array data
, you could use 另外,要简单地从2D数组data
获取随机值,您可以使用
target_value = np.random.choice(data.flat)
Or to get N
random values, use 或获取N
随机值,请使用
target_values = np.random.choice(data.flat, size=(N,))
Why the minus one in loc-1
: 为什么在loc-1
减一 :
In your original code, the direction['Indice']
column uses k
values which start at 1, not 0. So when loc
equals 1, the 0th-indexed row of direction
is selected. 在您的原始代码中, direction['Indice']
列使用以1开始的k
值,而不是0。因此,当loc
等于1时,将选择第0个索引direction
行。 I used loc-1
to make 我用loc-1
做
target_value = data[np.unravel_index(loc-1, data.shape)]
return the same result that 返回相同的结果
XX = np.array(direction[direction.Indice == loc ].X)
YY = np.array(direction[direction.Indice == loc ].Y)
target_value = data[YY,XX]
returns. 返回。 Note however, that if loc
equals 0, then np.unravel_index(-1, data.shape)
raises a ValueError
, while your original code would return an empty array for target_value
. 但是请注意,如果loc
等于0,则np.unravel_index(-1, data.shape)
会引发ValueError
,而您的原始代码将为target_value
返回一个空数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.