给定一个形状为(1000000,6)
的矩阵a,我已经找到了如何获得每行的最小最右边值,并在函数中实现了它:
def calculate_row_minima_indices(h): # h is the given matrix.
"""Returns the indices of the rightmost minimum per row for matrix h."""
flipped = numpy.fliplr(h) # flip the matrix to get the rightmost minimum.
flipped_indices = numpy.argmin(flipped, axis=1)
indices = numpy.array([2]*dim) - flipped_indices
return indices
indices = calculate_row_minima_indices(h)
for col, row in enumerate(indices):
print col, row, h[col][row] # col_index, row_index and value of minimum which should be removed.
每行都有一个最小值。因此,我需要知道的是移除具有最小值的条目并将具有形状(1000000,6)
的矩阵收缩成具有形状(1000000,5)
的矩阵。在
我将生成一个低维的新矩阵,并使用for循环填充我希望它携带的值,但我害怕运行时。那么有没有一些内置的方法或技巧可以通过每行的最小值来缩小矩阵?
0.0可能等于所有信息的值。在
您可以使用boolmask数组进行选择,但是内存使用量有点大。在
假设您有足够的内存来保存布尔掩码(原始数组和新数组的形状),下面是一种方法:
这就产生了:
^{pr2}$我在这里使用的一个不太可读的技巧是在数组比较期间利用numpy的广播。作为一个简单的例子,请考虑以下内容:
这就产生了:
因此,如果我们知道要删除的项的列索引,就可以对列索引数组的操作进行向量化,这正是
pop_col
所做的。在相关问题 更多 >
编程相关推荐