numpy堆栈到2d数组,从另一个堆栈按索引选择
我有两组分类( Lc1
和 Lc2
)和两组概率( Lp1
和 Lp2
)。 Lp1
是描述 Lc1
中分类可能性的概率集。我想使用 Lc1
和 Lc2
中的信息> class_result 。
I have two sets of classifications (Lc1
and Lc2
) and two sets of probabilities (Lp1
and Lp2
). Lp1
is the set of probabilities that describes the classification likelihood in Lc1
. I want to combine the information in Lc1
and Lc2
using the classifications that are the most probable into class_result
.
import numpy as np
#example data
Lp1 = np.ones((2,2))*0.5
Lc2 = np.ones((2,2))
Lc1 = np.ones((2,2))
Lp2 = np.ones((2,2))*0.5
#Change some values for the example
Lp1[1,1] =0.95
Lc1[1,1] = 0
Lc2[0,1]=3
Lp2[0,1]=.95
p_stack = np.stack((Lp1,Lp2))
c_stack = np.stack((Lc1,Lc2))
index = np.argmax(p_stack, axis=2)
class_result = np.take(c_stack, index)
我最初的方法是创建 np.stack
用于分类和概率集,并使用 np.argmax
来查找最大值出现在 p_stack
。 np.take 的文档似乎描述了我需要做的操作,但是我不明白为什么它返回一个带1的数组。是否可以通过指定我要选择的值的轴来减少 np.stack
的维数?
My initial approach is to create a np.stack
for the sets of classifications and probabilities and use np.argmax
to find the axis index where the maximum value occurs in p_stack
. The docs for np.take seem to describe the operation I need to do, but I don't understand why it returns an array with ones. Is there a way to reduce the dimensionality of a np.stack
by specifying the axis of the value I want to select?
我想要的结果是:
class_result = np.array([[1,3],[1,0]])
在您的情况下 ìndex
指的是第一个维度,您需要为其他维度创建升序索引。
In your case ìndex
refers to the first dimension, and you need to create ascending indices for the other dimensions.
如果您手动编写它们,则看起来像
If you write them manually it looks like
dim_1 = np.array([[0, 0],
[1, 1]])
dim_2 = np.array([[0, 1],
[0, 1]])
print(c_stack[index, dim_1, dim_2])
您可以使用 np.arange $ c自动创建它们$ c>,
np.vstack
, np.hstack
和 np.tile
, np.column_stack
。有几种方法可以做到这一点。
You can create them automatically using np.arange
, np.vstack
, np.hstack
and np.tile
, np.column_stack
. There are several ways to do this.
例如
x = np.arange(5)
a = np.tile(x, (5, 1))
b = np.column_stack(tuple(a))
print(a)
print(b)
在Numpy中这种技术称为整数数组索引 。
This technique in Numpy is called "Integer array indexing".