Reputation: 11
Starting with a 2D Numpy array I would like to create a 1D array in which each value corresponds to the minimum value of each row in the 2D array.
For example if
dog=[[1,2],[4,3],[6,7]]
then I would like to create an array from
'dog':[1,3,6]
This seems like it should be easy to do, but I'm not getting it so far.
Upvotes: 0
Views: 547
Reputation: 879113
In [54]: dog=[[1,2],[4,3],[6,7]]
In [55]: np.min(dog, axis=1)
Out[55]: array([1, 3, 6])
or, if dog
is a NumPy array, you could call its min
method:
In [57]: dog = np.array([[1,2],[4,3],[6,7]])
In [58]: dog.min(axis=1)
Out[58]: array([1, 3, 6])
Since dog.shape
is (3,2), (for 3 rows, 2 columns), the axis=1
refers to the second dimension in the shape -- the one with 2 elements. Putting axis=1
in the call to dog.min
tells NumPy to take the min over the axis=1
direction, thus eliminating the axis of length 2. The result is thus of shape (3,)
.
Upvotes: 4