I was trying to multiply and divide a numpy array's each sub numpy array with two numpy arrays.
我试图将numpy数组的每个子numpy数组与两个numpy数组相乘。
I have a numpy array x
with shape [100, 5]
, two numpy arrays y
and y
both with shape (5,)
.
我有一个numpy数组x,形状[100,5],两个numpy数组y和y都有形状(5,)。
I am trying to change the value of the tensor: For each sub numpy array w
along with axis=0 in x
, it should have shape [1, 5]
, I want to do (w - y)*z
.
我试图改变张量的值:对于每个子numpy数组w以及x中的轴= 0,它应该具有形状[1,5],我想做(w-y)* z。
My thought was to for-loop over x
and pick each sub array inside it to do this and then reconstruct the original array. However, this may be not efficient.
我的想法是在x上进行for循环并选择其中的每个子数组来执行此操作,然后重新构建原始数组。但是,这可能效率不高。
1 个解决方案
#1
1
This should work.
这应该工作。
(x - y) * z
sample:
>>> x.shape,y.shape, z.shape
((10L, 5L), (5L,), (5L,))
>>> x
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34],
[35, 36, 37, 38, 39],
[40, 41, 42, 43, 44],
[45, 46, 47, 48, 49]])
>>> y
array([0, 1, 2, 3, 4])
>>> z
array([1, 2, 3, 4, 5])
>>> (x-y)*z
array([[ 0, 0, 0, 0, 0],
[ 5, 10, 15, 20, 25],
[ 10, 20, 30, 40, 50],
[ 15, 30, 45, 60, 75],
[ 20, 40, 60, 80, 100],
[ 25, 50, 75, 100, 125],
[ 30, 60, 90, 120, 150],
[ 35, 70, 105, 140, 175],
[ 40, 80, 120, 160, 200],
[ 45, 90, 135, 180, 225]])
#1
1
This should work.
这应该工作。
(x - y) * z
sample:
>>> x.shape,y.shape, z.shape
((10L, 5L), (5L,), (5L,))
>>> x
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34],
[35, 36, 37, 38, 39],
[40, 41, 42, 43, 44],
[45, 46, 47, 48, 49]])
>>> y
array([0, 1, 2, 3, 4])
>>> z
array([1, 2, 3, 4, 5])
>>> (x-y)*z
array([[ 0, 0, 0, 0, 0],
[ 5, 10, 15, 20, 25],
[ 10, 20, 30, 40, 50],
[ 15, 30, 45, 60, 75],
[ 20, 40, 60, 80, 100],
[ 25, 50, 75, 100, 125],
[ 30, 60, 90, 120, 150],
[ 35, 70, 105, 140, 175],
[ 40, 80, 120, 160, 200],
[ 45, 90, 135, 180, 225]])