Python 如何保持列表列表中每行的最大值
我有一份清单如下Python 如何保持列表列表中每行的最大值,python,numpy,Python,Numpy,我有一份清单如下 array([[9.88945457e-01, 1.12012004e-11, 1.10545428e-02], [3.19212970e-03, 9.60482604e-01, 3.63252661e-02], [9.97873928e-01, 1.10315885e-12, 2.12607185e-03], [5.13391890e-07, 9.84294588e-01, 1.57048982e-02]]) 我想保留每行中的最大值
array([[9.88945457e-01, 1.12012004e-11, 1.10545428e-02],
[3.19212970e-03, 9.60482604e-01, 3.63252661e-02],
[9.97873928e-01, 1.10315885e-12, 2.12607185e-03],
[5.13391890e-07, 9.84294588e-01, 1.57048982e-02]])
我想保留每行中的最大值,如下所示
array([[9.88945457e-01],
[9.60482604e-01],
[9.97873928e-01],
[9.84294588e-01]])
这是否需要一个循环来遍历每一行?或者这是enumerate或iterrows可以处理的吗?要找出最大的数字,您可以这样做:
def largest(arr,n):
# Initialize maximum element
max = arr[0]
# Traverse array elements from second
# and compare every element with
# current max
for i in range(1, n):
if arr[i] > max:
max = arr[i]
return max
# Driver Code
arr = [10, 324, 45, 90, 9808]
n = len(arr)
Ans = largest(arr,n)
print ("Largest in given array is",Ans)
array = np.array([[9.88945457e-01, 1.12012004e-11, 1.10545428e-02],
... [3.19212970e-03, 9.60482604e-01, 3.63252661e-02],
... [9.97873928e-01, 1.10315885e-12, 2.12607185e-03],
... [5.13391890e-07, 9.84294588e-01, 1.57048982e-02]])
res = []
for sub_list in array:
max_value = max(sub_list)
res.append([max_value])
具有此阵列:
array = np.array([[9.88945457e-01, 1.12012004e-11, 1.10545428e-02],
... [3.19212970e-03, 9.60482604e-01, 3.63252661e-02],
... [9.97873928e-01, 1.10315885e-12, 2.12607185e-03],
... [5.13391890e-07, 9.84294588e-01, 1.57048982e-02]])
如果你的数组很大,最好按照@Guillem或@Muhammad的建议使用max numpy函数
请注意,max numpy函数将返回一个numpy数组
np.max(a, axis=1)
>>> array([0.98894546, 0.9604826 , 0.99787393, 0.98429459])
myarray.max(axis=1)
但是,我认为在python中,使用数组上的循环,然后使用max函数,可以达到相同的目标
像这样的事情:
def largest(arr,n):
# Initialize maximum element
max = arr[0]
# Traverse array elements from second
# and compare every element with
# current max
for i in range(1, n):
if arr[i] > max:
max = arr[i]
return max
# Driver Code
arr = [10, 324, 45, 90, 9808]
n = len(arr)
Ans = largest(arr,n)
print ("Largest in given array is",Ans)
array = np.array([[9.88945457e-01, 1.12012004e-11, 1.10545428e-02],
... [3.19212970e-03, 9.60482604e-01, 3.63252661e-02],
... [9.97873928e-01, 1.10315885e-12, 2.12607185e-03],
... [5.13391890e-07, 9.84294588e-01, 1.57048982e-02]])
res = []
for sub_list in array:
max_value = max(sub_list)
res.append([max_value])
如果是numpy数组,此方法将返回标准python列表
np.max(a, axis=1)
>>> array([0.98894546, 0.9604826 , 0.99787393, 0.98429459])
myarray.max(axis=1)
除了在阵列上循环外,向量化操作的效率要高得多 为此,必须使用max函数的axis参数。在这种情况下,应使用轴=1 将numpy作为np导入 a=np.数组[[9.88945457e-01、1.12012004e-11、1.10545428e-02], [3.19212970e-03、9.60482604e-01、3.63252661e-02], [9.97873928e-01、1.10315885e-12、2.12607185e-03], [5.13391890e-07、9.84294588e-01、1.57048982e-02]] printnp.maxa,轴=1
您正在使用numpy,但在纯python中,它只是:
maxes = [max(x) for x in numlists]
或
argmax获取每行最大值的索引 “重塑”将一维数据提升为二维数据
getArrayMax = lambda array: array[range(array.shape[0]), numpy.argmax(array, axis=1)].reshape(array.shape[0], 1)
>>> getArrayMax(f)
array([[0.98894546],
[0.9604826 ],
[0.99787393],
[0.98429459]])
这看起来像是用Python编写的C答案。加上标签缺失。您是新手,但您应该查看输入答案的帮助,它将解释如何更好地格式化代码。缩进在Pythonyes中是至关重要的,你是对的,这是python中的一个c答案。。。但它是有效的,所以我想他只是想要结果。当然,代码中的缩进并不完美。函数的代码还需要一个选项卡