I am looking for a more elegant/efficient way of finding the min/max value of every column from a numpy array within a dictionary.
For example:
inputs = ['test1','book','panda']
total['test1'] = np.array([[1,1.5,2],[14,20,8],[5,9,2]])
total['book'] = np.array([[4,8,12],[44,2,81],[3,8,3]])
total['panda'] = np.array([[1,3,8],[104,4,51]])
Now i want to find the min/max value of every column; which should result in an array something like this:
[1, 1.5, 2],
[104, 20, 81]
I am using the following code to find the min/max of column 1, and i could use a for-loop of this code to find it for all columns, but i guess there should be a more elegant/effcient way of achieving this:
# Find min and max
min_list = []
max_list = []
for e,nam in enumerate(inputs):
min_list.append((total[nam][:,1].min()))
max_list.append((total[nam][:,1].max()))
min_val = min(min_list)
max_val = max(max_list)
Thanks in advance!
You can concatenate your individual lists into a single Numpy array and then just use min
and max
along the desired axis:
total = {}
total['test1'] = np.array([[1,1.5,2],[14,20,8],[5,9,2]])
total['book'] = np.array([[4,8,12],[44,2,81],[3,8,3]])
total['panda'] = np.array([[1,3,8],[104,4,51]])
stacked = np.concatenate(list(total.values()))
stacked.min(axis=0)
# array([1. , 1.5, 2. ])
stacked.max(axis=0)
# array([104., 20., 81.])