numpy的广播阵 [英] Numpy broadcast array
问题描述
我在numpy的以下数组:
I have the following array in NumPy:
A = array([1, 2, 3])
我怎样才能得到如下矩阵(没有明确的循环)?
How can I obtain the following matrices (without an explicit loop)?
B = [ 1 1 1
2 2 2
3 3 3 ]
C = [ 1 2 3
1 2 3
1 2 3 ]
谢谢!
推荐答案
EDIT2:该任择议定书要求的意见如何计算
The OP asks in the comments how to compute
n(i, j) = l(i, i) + l(j, j) - 2 * l(i, j)
我能想到的两种方式。我喜欢这种方式,因为它很容易概括:
I can think of two ways. I like this way because it generalizes easily:
import numpy as np
l=np.arange(9).reshape(3,3)
print(l)
# [[0 1 2]
# [3 4 5]
# [6 7 8]]
我们的想法是使用 np.ogrid
。这定义了两个numpy的阵列,形状(3,1)中的一个和形状中的一个(1,3)的列表:
The idea is to use np.ogrid
. This defines a list of two numpy arrays, one of shape (3,1) and one of shape (1,3):
grid=np.ogrid[0:3,0:3]
print(grid)
# [array([[0],
# [1],
# [2]]), array([[0, 1, 2]])]
电网[0]
可作为该指数代理我
,和电网[1]
可作为该指数的代理Ĵ
。
grid[0]
can be used as a proxy for the index i
, and
grid[1]
can be used as a proxy for the index j
.
所以在EX pression L(I,I)+ 1(J,J)无处不在 - 2 * L(I,J)
,您只需替换 I
- > 电网[0]
和Ĵ
- > 电网[1]
和numpy的广播照顾其余的:
So everywhere in the expression l(i, i) + l(j, j) - 2 * l(i, j)
, you simply replace i
-->grid[0]
, and j
-->grid[1]
, and numpy broadcasting takes care of the rest:
n=l[grid[0],grid[0]] + l[grid[1],grid[1]] + 2*l
print(n)
# [[ 0 6 12]
# [10 16 22]
# [20 26 32]]
然而,在这种特殊的情况下,由于 L(I,I)
和→(J,J)
只是→
的对角线元素,你可以这样做,而不是:
However, in this particular case, since l(i,i)
and l(j,j)
are just the diagonal elements of l
, you could do this instead:
d=np.diag(l)
print(d)
# [0 4 8]
D [np.newaxis,:]
泵高达 D
的形状,(1,3),和 D [:,np.newaxis]
D
的形状水泵达(3,1)。
d[np.newaxis,:]
pumps up the shape of d
to (1,3), and
d[:,np.newaxis]
pumps up the shape of d
to (3,1).
numpy的广播泵高达 D [np.newaxis,:]
和 D [:,np.newaxis]
塑造(3,3),复制适当的值。
Numpy broadcasting pumps up d[np.newaxis,:]
and d[:,np.newaxis]
to shape (3,3), copying values as appropriate.
n=d[np.newaxis,:] + d[:,np.newaxis] + 2*l
print(n)
# [[ 0 6 12]
# [10 16 22]
# [20 26 32]]
EDIT1:通常你不需要形成 B
或 C
。 numpy的广播的目的是让你在 B的
或 c。使用
。如果你告诉我们,你打算如何使用 A
B
或 C
,我们也许能够告诉你如何做与 A
相同,numpy的广播。
Usually you do not need to form B
or C
. The purpose of Numpy broadcasting is to allow you to use A
in place of B
or C
. If you show us how you plan to use B
or C
, we might be able to show you how to do the same with A
and numpy broadcasting.
(原来的答复):
In [11]: B=A.repeat(3).reshape(3,3)
In [12]: B
Out[12]:
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])
In [13]: C=B.T
In [14]: C
Out[14]:
array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
或
In [25]: C=np.tile(A,(3,1))
In [26]: C
Out[26]:
array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
In [27]: B=C.T
In [28]: B
Out[28]:
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])
从搞鬼部门:
In [57]: np.lib.stride_tricks.as_strided(A,shape=(3,3),strides=(4,0))
Out[57]:
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])
In [58]: np.lib.stride_tricks.as_strided(A,shape=(3,3),strides=(0,4))
Out[58]:
array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
但需要注意的是,这些都是的意见的 A
,而不是副本(因为是上面的解决方案)。更改 B
,变造 A
:
But note that these are views of A
, not copies (as were the solutions above). Changing B
, alters A
:
In [59]: B=np.lib.stride_tricks.as_strided(A,shape=(3,3),strides=(4,0))
In [60]: B[0,0]=100
In [61]: A
Out[61]: array([100, 2, 3])
这篇关于numpy的广播阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!