patrick patrick - 2 years ago 107
Python Question

Why does the numpy function `take` change the shape of my array?

I am trying to calculate which points in my data set (in the shape of a numpy array called "matrix") are closest to a vector (array called "vector") in ndimensional space. Then, I want to extract these same vectors from a data set which is identical to "matrix" but includes additional labels (="matrix_with_labels").

matrix=[[1,2,3,...], [2,4,6,...], ...]]
matrix_with_labels=[[a,1,2,3,...], [b,2,4,6,...], ...]]

Thus, I compute the distances between the vector and each item in the matrix:


Then I sort these distances to identify the closest neighbors:

sorted_index=np.argsort(dist, axis=0)

Then I try to sort the "matrix_with_labels" by "sorted_index", using
as explained in this post on SO.

result= matrix_with_labels.take(sorted_index, 0)

The outcome looks just fine until I try to process it further - it seems to have changed shape:

print result.shape
(20, 1, 11)

When I look at the shape of the initial "matrix_with_labels", however:

(20, 11)

The documentation on take says:

subarray : ndarray
The returned array has the same type as a.

What am I doing wrong? Any help is appreciated!

Answer Source

If you're starting with a (20, 11) shape, I think the only way to get a (20, 1, 11) shape is if x has shape (1, 11).

Try result = matrix_with_labels.take(x.reshape(-1), 0).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download