Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 查找numpy矩阵中最大列值的行索引_Python_Arrays_Performance_Numpy_Python Imaging Library - Fatal编程技术网

Python 查找numpy矩阵中最大列值的行索引

Python 查找numpy矩阵中最大列值的行索引,python,arrays,performance,numpy,python-imaging-library,Python,Arrays,Performance,Numpy,Python Imaging Library,是否有一种快速有效的方法来查找NxM Numpy数组中每列中具有最高值的行 我目前正在通过Python中的嵌套循环执行此操作,该循环相对较慢: from PIL import Image import numpy as np img = Image.open('sample.jpg').convert('L') width, height = size = img.size y = np.asarray(img.getdata(), dtype=np.float64).reshape((heig

是否有一种快速有效的方法来查找NxM Numpy数组中每列中具有最高值的行

我目前正在通过Python中的嵌套循环执行此操作,该循环相对较慢:

from PIL import Image
import numpy as np
img = Image.open('sample.jpg').convert('L')
width, height = size = img.size
y = np.asarray(img.getdata(), dtype=np.float64).reshape((height, width))
max_rows = [0]*width
for col_i in xrange(y.shape[1]):
    max_vaue, max_row = max([(y[row_i][col_i], row_i) for row_i in xrange(y.shape[0])])
    max_rows[col_i] = max_row

对于640x480图像,这大约需要5秒钟。不太大,但更复杂的图像操作,如模糊,完全在Numpy/PIL/C中实现,需要0.01秒或更短时间。这是我试图在视频流上执行的操作,所以这是一个巨大的瓶颈。除了编写自己的C扩展之外,我如何加快速度呢?

您将要使用它。这将返回与给定轴上的最大值对应的元素索引

row_index = np.argmax(y, axis=0)

# Alternately
row_index = y.argmax(axis=0)
为了举个例子

data = np.random.rand(4,2)
# array([[ 0.09695379,  0.44602826],
#        [ 0.73614533,  0.19700072],
#        [ 0.87843682,  0.21188487],
#        [ 0.11389634,  0.51628872]])

row_index = data.argmax(axis=0)
# array([2, 3])

你会想用这个。这将返回与给定轴上的最大值对应的元素索引

row_index = np.argmax(y, axis=0)

# Alternately
row_index = y.argmax(axis=0)
为了举个例子

data = np.random.rand(4,2)
# array([[ 0.09695379,  0.44602826],
#        [ 0.73614533,  0.19700072],
#        [ 0.87843682,  0.21188487],
#        [ 0.11389634,  0.51628872]])

row_index = data.argmax(axis=0)
# array([2, 3])