Minimum value of each Python array column

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 receive the minimum values, as in the example below, but I 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]]
    
asked by anonymous 26.11.2018 / 02:31

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 I remembered in Klaus comments, I changed the balls when I wrote 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])
    
26.11.2018 / 16:23