user5896534 user5896534 - 1 month ago 8
Python Question

Given a list of arrays or 3 dimensional array, how do you find the maximum value?

Here is my output for the list of arrays/3 dimensional array(don't know which it is), I don't understand why the words array are in there but I'm hoping that's not a reason why I'm not able to calculate the maximum value, I obtained this list by calculating the Pearson r-value between columns of a data set.

[array([[ 1. , 0.31276108],
[ 0.31276108, 1. ]]), array([[ 1. , 0.23618345],
[ 0.23618345, 1. ]]), array([[ 1. , 0.31610011],
[ 0.31610011, 1. ]]), array([[ 1. , 0.3304167],
[ 0.3304167, 1. ]]), array([[ 1. , -0.31138519],
[-0.31138519, 1. ]]), array([[ 1. , 0.49419313],
[ 0.49419313, 1. ]]), array([[ 1. , 0.49811488],
[ 0.49811488, 1. ]]), array([[ 1. , 0.39335085],
[ 0.39335085, 1. ]]), array([[ 1. , -0.44059693],
[-0.44059693, 1. ]]), array([[ 1. , 0.22362626],
[ 0.22362626, 1. ]]), array([[ 1. , -0.19201056],
[-0.19201056, 1. ]]), array([[ 1. , 0.64372004],
[ 0.64372004, 1. ]]), array([[ 1. , -0.63371678],
[-0.63371678, 1. ]]), array([[ 1. , 0.56546829],
[ 0.56546829, 1. ]]), array([[ 1. , -0.42881494],
[-0.42881494, 1. ]]), array([[ 1. , 0.5190671],
[ 0.5190671, 1. ]]), array([[ 1. , -0.5032696],
[-0.5032696, 1. ]]), array([[ 1. , 0.7871939],
[ 0.7871939, 1. ]]), array([[ 1. , 0.69994936],
[ 0.69994936, 1. ]]), array([[ 1. , 0.06600394],
[ 0.06600394, 1. ]]), array([[ 1. , -0.27676855],
[-0.27676855, 1. ]]), array([[ 1. , 0.00391123],
[ 0.00391123, 1. ]]), array([[ 1. , -0.36871043],
[-0.36871043, 1. ]]), array([[ 1. , 0.07234319],
[ 0.07234319, 1. ]]), array([[ 1. , -0.78822959],
[-0.78822959, 1. ]]), array([[ 1. , -0.52181319],
[-0.52181319, 1. ]]), array([[ 1. , 0.29554425],
[ 0.29554425, 1. ]]), array([[ 1. , -0.26263963],
[-0.26263963, 1. ]]), array([[ 1. , 0.54347857],
[ 0.54347857, 1. ]]), array([[ 1. , 0.43368134],
[ 0.43368134, 1. ]]), array([[ 1. , 0.0553982],
[ 0.0553982, 1. ]]), array([[ 1. , -0.27395522],
[-0.27395522, 1. ]]), array([[ 1. , -0.07466689],
[-0.07466689, 1. ]]), array([[ 1. , -0.56129569],
[-0.56129569, 1. ]]), array([[ 1. , -0.0717472],
[-0.0717472, 1. ]]), array([[ 1. , -0.61736921],
[-0.61736921, 1. ]]), array([[ 1. , -0.02524993],
[-0.02524993, 1. ]]), array([[ 1. , 0.13905701],
[ 0.13905701, 1. ]]), array([[ 1. , -0.1723794],
[-0.1723794, 1. ]]), array([[ 1. , -0.05513642],
[-0.05513642, 1. ]]), array([[ 1. , 0.19995001],
[ 0.19995001, 1. ]]), array([[ 1. , 0.01873198],
[ 0.01873198, 1. ]]), array([[ 1. , 0.25888726],
[ 0.25888726, 1. ]]), array([[ 1. , 0.24898534],
[ 0.24898534, 1. ]]), array([[ 1. , 0.5463642],
[ 0.5463642, 1. ]]), array([[ 1. , 0.26566757],
[ 0.26566757, 1. ]]), array([[ 1. , -0.3658451],
[-0.3658451, 1. ]]), array([[ 1. , 0.65269177],
[ 0.65269177, 1. ]]), array([[ 1. , 0.61241308],
[ 0.61241308, 1. ]]), array([[ 1. , 0.23644061],
[ 0.23644061, 1. ]]), array([[ 1. , -0.19732684],
[-0.19732684, 1. ]]), array([[ 1. , 0.00965194],
[ 0.00965194, 1. ]]), array([[ 1. , -0.22074619],
[-0.22074619, 1. ]]), array([[ 1. , 0.13669791],
[ 0.13669791, 1. ]]), array([[ 1. , -0.49912982],
[-0.49912982, 1. ]]), array([[ 1. , -0.53789961],
[-0.53789961, 1. ]]), array([[ 1. , -0.4499353],
[-0.4499353, 1. ]]), array([[ 1. , -0.25629405],
[-0.25629405, 1. ]]), array([[ 1. , 0.36192172],
[ 0.36192172, 1. ]]), array([[ 1. , 0.18623045],
[ 0.18623045, 1. ]]), array([[ 1. , 0.29297713],
[ 0.29297713, 1. ]]), array([[ 1. , -0.15592947],
[-0.15592947, 1. ]]), array([[ 1. , 0.48910916],
[ 0.48910916, 1. ]]), array([[ 1. , 0.8645635],
[ 0.8645635, 1. ]]), array([[ 1. , 0.19578377],
[ 0.19578377, 1. ]]), array([[ 1. , -0.35136986],
[-0.35136986, 1. ]]), array([[ 1. , 0.11507728],
[ 0.11507728, 1. ]]), array([[ 1. , -0.41100659],
[-0.41100659, 1. ]]), array([[ 1. , 0.23681493],
[ 0.23681493, 1. ]]), array([[ 1. , -0.84749754],
[-0.84749754, 1. ]]), array([[ 1. , 0.21440123],
[ 0.21440123, 1. ]]), array([[ 1. , -0.32111332],
[-0.32111332, 1. ]]), array([[ 1. , 0.12897954],
[ 0.12897954, 1. ]]), array([[ 1. , -0.335167],
[-0.335167, 1. ]]), array([[ 1. , 0.28910112],
[ 0.28910112, 1. ]]), array([[ 1. , -0.71916334],
[-0.71916334, 1. ]]), array([[ 1. , -0.08333309],
[-0.08333309, 1. ]]), array([[ 1. , 0.28658669],
[ 0.28658669, 1. ]]), array([[ 1. , -0.0545751],
[-0.0545751, 1. ]]), array([[ 1. , 0.27079823],
[ 0.27079823, 1. ]]), array([[ 1. , -0.20917939],
[-0.20917939, 1. ]]), array([[ 1. , 0.44336719],
[ 0.44336719, 1. ]]), array([[ 1. , 0.2885004],
[ 0.2885004, 1. ]]), array([[ 1. , -0.31023514],
[-0.31023514, 1. ]]), array([[ 1. , 0.51785911],
[ 0.51785911, 1. ]]), array([[ 1. , 0.16404547],
[ 0.16404547, 1. ]]), array([[ 1. , 0.2115446],
[ 0.2115446, 1. ]]), array([[ 1. , -0.04964322],
[-0.04964322, 1. ]]), array([[ 1. , 0.09439694],
[ 0.09439694, 1. ]]), array([[ 1. , 0.4377762],
[ 0.4377762, 1. ]]), array([[ 1. , -0.32822194],
[-0.32822194, 1. ]])]


Here is my code for finding the maximum value,

for i in range(len(r)):
for j in range(len(r)):
if r[i][1][0]==r[j][1][0]:
data=r[i][1][0]
elif r[i][1][0] < r[j][1][0]:
data=r[j][1][0]
else:
data=r[i][1][0]


and this is giving me the output of -0.328, where by inspection it is obvious the maximum is 0.79

Answer

I suspect that whatever led you to this mess isn't the best way of trying to achieve whatever it is you are doing. However, let's say your list of 2x2 arrays is a, and you know that the values of the antidiagonal of a given 2x2 array are the same, then you can simply do:

max(arr[0,1] for arr in a)

If you aren't sure they are always equal, then you could use something like this:

max(np.flipud(arr).diagonal().max() for arr in a)
Comments