Minimum value of each Python array column

Asked

Viewed 546 times

0

import numpy as np

X = np.random.rand(4,3)
print("X: \n", X)

XMin = np.zeros((1,3), dtype=np.float64)

for j in range(3):
    print("X[{0}]: {1}".format(j,X[j]))
    minimo = np.amin(X[j])
    print("minimo: ", minimo)
    np.append(XMin, minimo, axis=1)

print("XMin: \n", XMin)

I tried to find the minimum value of each column and make Xmin get the minimum values, as in the example below, but could not:

X:[[3.83537931e-01 3.14643360e-02 6.56821484e-04]
   [4.99427675e-01 3.68729746e-01 5.12404699e-01]
   [8.79530786e-01 4.91253677e-01 9.46192774e-01]
   [7.94819914e-01 2.00760544e-01 5.53820343e-01]]

XMin:[[3.83537931e-01 2.00760544e-01 5.12404699e-01]]

1 answer

2


Use np.min()

import numpy as np
a = np.array([[1,2,3],[10,20,30],[100,200,300],[1000,2000,3000]])
print(a)
[[   1    2    3]
 [  10   20   30]
 [ 100  200  300]
 [1000 2000 3000]]

min_cols = a.min(axis=1)
print(min_cols)
[   1   10  100 1000]

Edited
As recalled in the committal of Klaus, I switched the balls that I worked out the answer and took the minimum of each line, for the minimum of each colouna should be axis=0 new example below:

a = np.array([[1000,2000,3],[100,2,300],[10,20,30],[1,200,3000]])
a
array([[1000, 2000,    3],
       [ 100,    2,  300],
       [  10,   20,   30],
       [   1,  200, 3000]])

# Valores minimos nas linhas
a.min(axis=1)
array([ 3,  2, 10,  1])


# Valores minimos nas colunas
a.min(axis=0)
array([1, 2, 3])
  • Using a.min(axis=1) you are picking up the minimum of each line. If you use axis=0, will take the minimum of each column, which will be [1 2 3].

  • Really, I messed up, I’ll edit.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.