Home > database >  Why does axis=0 make sort work for column vector, and why does it work for array as well?
Why does axis=0 make sort work for column vector, and why does it work for array as well?

Time:09-24

If I have a column vector and I want to sort its elements

array([[2.96745334],
       [3.29622333],
       [6.1303673 ],
       [6.28396456],
       [6.15874633]])

If I do np.sort(z, axis=1), thus sort by column, the result is the same vector with no sort.

If I do np.sort(z, axis=0), the vector is correctly sorted:

array([[2.96745334],
       [3.29622333],
       [6.1303673 ],
       [6.15874633],
       [6.28396456]])

Why does this happen, since axis=0 should mean "sort by row" while axis=1 means "sort by column"?

Moreover with axis=0 also an array will be sorted

np.array([2, 1, 5, 4, 3])
np.sort(z, axis=0)
array([1, 2, 3, 4, 5])

So it seems very convenient to always use axis=0 if you don't know if a row or column vector is coming. Is this correct?

CodePudding user response:

I think your notion of what the axis argument means is not quite correct. Think of np.sort(v, axis=n) that we will sort along the n-th axis:

If we define a vector v = np.array([[1],[5],[1],[2],[3],[5]]) of shape (6, 1) (what you call a "column vector"), and sort along axis=0, these 6 values will get sorted. If we instead sort it along axis=1, we have bascially sort six different lists of length 1, so the result will be the same.

By default (that is, without defining anything else), np.sort will sort along the last axis (by default axis=-1). So if you have an array of shape (6,3,7,3,8), calling np.sort on it will sort a number of lists of length 8.

I hope this clears up the confusion.

CodePudding user response:

As you can see below, a column array is made within the bracket. If you add a comma, the interpreter puts the rest of your input in the next row.

import numpy as np

arr = np.array([[2.96745334],
       [3.29622333],
       [6.1303673 ],
       [6.28396456],
       [6.15874633]])
print(arr.shape)
#result: (5, 1)    #An array with 5 row and 1 column

arr3 = np.array([[3, 5, 2, 4]])
print(arr3.shape)
#result: (1, 4)    #An array with 1 row and 4 columns

But just to see it as a matrix as well:

arr2 = np.array([[3, 5, 2, 4],
                 [7, 6, 8, 8],
                 [1, 6, 7, 7]])
print(arr2.shape)
#result: (3, 4)    # Three rows are defined by the commas between the brackets.
  • Related