I have an array in which the first row is a category, the second row is a subcategory, and the third row is a value that I would like to condense.
我有一个数组,其中第一行是一个类别,第二行是一个子类别,第三行是我想要压缩的值。
I'm trying to rearrange array A
我正在尝试重新安排阵列A.
[[ 4 4 19 19 20 21 25 26 26 27 27 29] # category
[ 1 2 1 2 1 2 1 1 2 1 2 2] # subcategory
[ 1 1 3 3 1 2 1 1 1 2 2 2]] # count
into array B
进入阵列B.
[[ 4 19 20 21 25 26 27 29] # category
[ 1 3 1 0 1 1 2 0] # subcategory 1 count
[ 1 3 0 2 0 1 2 2]] # subcategory 2 count
I'm as far as this
我就此而已
categories, subcategories = np.unique(A[0], return_counts=True)
B = np.zeros((np.amax(subcategories) + 1, A[0].size))
B[0] = categories
but not sure how to populate the rest. Any ideas?
但不知道如何填充其余部分。有任何想法吗?
2 个解决方案
#1
1
This should do the trick:
这应该是诀窍:
cat_index = np.searchsorted(categories, A[0])
B[A[1], cat_index] = A[2]
#2
1
This should work even when the input isn't sorted:
即使未对输入进行排序,这也应该有效:
import numpy as np
A = np.array(
[[ 4, 4,19,19,20,21,25,26,26,27,27,29], # category
[ 1, 2, 1, 2, 1, 2, 1, 1, 2, 1, 2, 2], # subcategory
[ 1, 1, 3, 3, 1, 2, 1, 1, 1, 2, 2, 2]]) # count
values, inverse = np.unique(A[0], return_inverse=True)
B = np.zeros((3, len(values)))
B[0] = values
B[1,inverse[A[1] == 1]] = A[2,A[1] == 1]
B[2,inverse[A[1] == 2]] = A[2,A[1] == 2]
Which gives:
这使:
[[ 4 19 20 21 25 26 27 29]
[ 1 3 1 0 1 1 2 0]
[ 1 3 0 2 0 1 2 2]]
#1
1
This should do the trick:
这应该是诀窍:
cat_index = np.searchsorted(categories, A[0])
B[A[1], cat_index] = A[2]
#2
1
This should work even when the input isn't sorted:
即使未对输入进行排序,这也应该有效:
import numpy as np
A = np.array(
[[ 4, 4,19,19,20,21,25,26,26,27,27,29], # category
[ 1, 2, 1, 2, 1, 2, 1, 1, 2, 1, 2, 2], # subcategory
[ 1, 1, 3, 3, 1, 2, 1, 1, 1, 2, 2, 2]]) # count
values, inverse = np.unique(A[0], return_inverse=True)
B = np.zeros((3, len(values)))
B[0] = values
B[1,inverse[A[1] == 1]] = A[2,A[1] == 1]
B[2,inverse[A[1] == 2]] = A[2,A[1] == 2]
Which gives:
这使:
[[ 4 19 20 21 25 26 27 29]
[ 1 3 1 0 1 1 2 0]
[ 1 3 0 2 0 1 2 2]]