triphook
triphook

Reputation: 3097

Convert value to row index in NumPy array

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

[[ 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

[[ 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?

Upvotes: 4

Views: 496

Answers (2)

jme
jme

Reputation: 20765

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]]

Upvotes: 1

user2379410
user2379410

Reputation:

This should do the trick:

cat_index = np.searchsorted(categories, A[0])
B[A[1], cat_index] = A[2]

Upvotes: 1

Related Questions