简体   繁体   English

如何从列表中的每个numpy数组中提取最大值?

[英]How to extract maximum values from each numpy arrays in a list?

Actually, i'm very new to python and working on some image problem statement. 实际上,我是python的新手,正在研究一些图像问题声明。 Stuck in a problem and not able to get out of this. 陷入问题,无法摆脱困境。

I have data frame like: 我有像这样的数据框:

Image                       RGB                 max_1 max_2 max_3
file1   [[224,43,234][22,5,224][234,254,220]]     234   224   254
file2   [[22,143,113][221,124,224][234,254,123]]  143   224   254
file3   [[44,45,2][2,5,4][34,254,220]]             45     5   254
file4   [[224,243,34][22,5,24][24,25,20]]         243    24    25
file5   [[210,13,34][22,5,224][234,254,220]]      210   224   254

I tried np.max() but it gave me unexpected results that means for the first row this gave me output 254 , and so on. 我尝试了np.max()但是它给了我意外的结果,这意味着对于第一行,这给了我输出254 ,依此类推。

I'm expecting the output as column max_1, max_2, and max_3. 我期望输出为列max_1,max_2和max_3。

I'll assume that you want the max values of R, G and B respectively. 我假设您分别需要R,G和B的最大值。 If you want this then, here is one way to do it: 如果您要这样做,这是一种方法:

a = np.array([ [224,43,234], [22,5,224], [234,254,220]])
r_max = a.T[0].max()
g_max = a.T[1].max()
b_max = a.T[2].max()

Using list-comprehension : 使用list-comprehension

a = np.array([[224,43,234], [22,5,224], [234,254,220]])

print([x.max() for x in a])

OUTPUT : 输出

[234, 224, 254]

You can do something like this perhaps: 您可以执行以下操作:

file1 = [[224,43,234],[22,5,224],[234,254,220]]

for idx, inner_list in enumerate(file1):
    print('max_'+str(idx+1)+' : '+str(max(inner_list)))

Another way: 其他方式:

import numpy as np

a=np.array([[1,2,3],[11,12,13],[21,22,23]])
print(a)

maxInRows = np.amax(a, axis=1)
print('Max value of every Row: ', maxInRows)

You said you have a data frame, so I assume it's a pandas DataFrame object. 您说您有一个数据框,所以我认为它是一个pandas DataFrame对象。 In which case, you can use list comprehension to take the max from each sub-list in the list, and assign each element to a new column (this loop isn't elegant but will work): 在这种情况下,您可以使用列表推导从列表中的每个子列表中获取最大值,然后将每个元素分配给新的列(此循环并不完美,但可以使用):

df['max_colors'] = df['RGB'].apply(lambda x: [np.max(color) for color in x])
for i in range(3):
    df['max_'+str(i)] = df['max_colors'].apply(lambda x: x[i])

Solved the problem like this. 解决了这样的问题。 Thanks for having all the answers. 感谢您提供所有答案。

df['max_1'] = 0
df['max_2'] = 0
df['max_3'] = 0
for i in range(5):
    df['max_1'][i] = df['RGB'][i][0].max()
    df['max_2'][i] = df['RGB'][i][1].max()
    df['max_3'][i] = df['RGB'][i][2].max()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从arrays的numpy数组中获取每个数组的N个最大值 - How to get N maximum values of each array from a numpy array of arrays 从 numpy 数组列表中提取具有 0 个元素的行? - Extract rows with 0 element from a list of numpy arrays? 如何从排列的 numpy 数组中提取数组? - How to extract arrays from an arranged numpy array? 如何从Theano张量提取numpy数组? - How to extract numpy arrays from Theano tensor? 如何按元素比较3个numpy数组,并获得具有最大值的数组的结果? - How to compare 3 numpy arrays elementwise and get the results as the array with maximum values? 将 numpy 数组中的单个值提取到列表中 - Extract individual values from numpy array into a list 从 numpy 数组的每个子数组中查找最大值 - Finding maximum values from each subarrays of a numpy array 如何从loadmat函数生成的嵌套numpy数组中有效地提取值? - How to efficiently extract values from nested numpy arrays generated by loadmat function? 使用列表从numpy数组中提取元素 - extract elements from numpy array of arrays using list 如何通过应用 numpy 向量化使用条件检查从 python 列表或 numpy 数组中提取值? - How to extract values from a python list or numpy array using conditional checks by the application of numpy vectorization?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM