numpy矩阵的综合指数更新 [英] Composite Index updates for Numpy Matrices

查看:97
本文介绍了numpy矩阵的综合指数更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新numpy矩阵的一组特定行和列.这是一个示例:

I am trying to update a set of particular rows and columns of a numpy matrix . Here is an example:

import numpy as np
A=np.zeros((8,8))
rows=[0, 1, 5]
columns=[2, 3]
#(What I am trying to achieve) The following does not update A
A[rows][:,columns]+=1
#while this just does
for i in rows:
    A[i][columns]+=1

我期望的输出是:

In [1]:print(A)
Out[1]: 
    array([[ 0.,  0.,  1.,  1.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  1.,  1.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  1.,  1.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])

有没有一种方法可以同时执行多列和逐行更新而不会循环?

Is there a way to perform a multiple-columnwise and -rowwise updates at the same time without looping?

推荐答案

使用数组或索引列表建立索引属于

Indexing with arrays or lists of indices falls under the category of fancy indexing, and always generates a copy of the relevant portion of the array rather than giving you a view onto the original data.

赋值或就地修改可以很好地工作(例如A[rows] += 1).但是,修改 chained 索引表达式的结果,例如A[rows][:, columns],如果第一个表达式使用花式索引,将不起作用.原因是A[rows]会生成一个副本,因此将A[rows][:, columns]加1不会影响A.

Assignment or in-place modification of the result of a single fancy indexing operation works fine, (e.g. A[rows] += 1). However, modifying the result of chained indexing expressions, e.g. A[rows][:, columns], will not work if the first expression uses fancy indexing. The reason is that A[rows] generates a copy, so adding 1 to A[rows][:, columns] will not affect A.

如果一次性完成所有索引编制,则可以避免此问题.在某些时候,您可能尝试过这样的事情:

You can avoid this problem if you do all of your indexing in one go. At some point you probably tried something like this:

In [38]: A[rows, columns] += 1
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-38-7d2300f59d51> in <module>()
----> 1 A[rows, columns] += 1

ValueError: shape mismatch: objects cannot be broadcast to a single shape

此处numpy将您的索引列表转换为数组,然后尝试广播以相同的形状出现,这是失败的,因为它们的尺寸不兼容.要使其成功,可以将rows设置为(n, 1)数组,将columns设置为(1, m)数组,其中mn是单个行/列索引的数量.这样,它们可以沿大小1的维度扩展为大小(n, m),并用于索引到A:

Here numpy casts your lists of indices to arrays, then tries to broadcast them out to the same shape, which fails because they have incompatible dimensions. To make it succeed, you can make rows into an (n, 1) array and columns into a (1, m) array, where m and n are the number of individual row/column indices. That way they can be expanded along the size 1 dimensions to size (n, m), and used to index into A:

r = np.array(rows)
c = np.array(columns)
A[r[:, None], c[None, :]] += 1
print(A)
# [[ 0.  0.  1.  1.  0.  0.  0.  0.]
#  [ 0.  0.  1.  1.  0.  0.  0.  0.]
#  [ 0.  0.  0.  0.  0.  0.  0.  0.]
#  [ 0.  0.  0.  0.  0.  0.  0.  0.]
#  [ 0.  0.  0.  0.  0.  0.  0.  0.]
#  [ 0.  0.  1.  1.  0.  0.  0.  0.]
#  [ 0.  0.  0.  0.  0.  0.  0.  0.]
#  [ 0.  0.  0.  0.  0.  0.  0.  0.]]

使用None编制索引与使用np.newaxis进行索引编制相同-它具有插入大小为1的新尺寸的作用.

Indexing with None is the same thing as using np.newaxis - it has the effect of inserting a new dimension of size 1.

实际上有一个便捷功能, np.ix_ ,用于根据一维索引序列生成多维索引:

There is actually a convenience function, np.ix_, for generating multidimensional indices from a sequence of 1D indices:

A = np.zeros((8, 8))
A[np.ix_(rows, columns)] += 1

这篇关于numpy矩阵的综合指数更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆