David Hx
David Hx

Reputation: 135

Convert a 2D array to 3D array by multiplying vector in numpy

Suppose I have an 2D numpy array a=[[1,-2,1,0], [1,0,0,-1]], but I want to convert it to an 3D numpy array by element-wise multiply a vector t=[[x0,x0,x0,x0],[x1,x1,x1,x1]] where xi is a 1D numpy array with 3072 size. So the result would be a*t=[[x0,-2x0,x0,0],[x1,0,0,-x1]] with the size (2,4,3072). So how should I do that in Python numpy?

Upvotes: 0

Views: 1267

Answers (3)

Nils Werner
Nils Werner

Reputation: 36859

You can use numpy broadcasting for this:

a = numpy.array([[1, -2, 1, 0], [1, 0, 0, -1]])
t = numpy.arange(3072 * 2).reshape(2, 3072)
# array([[   0,    1,    2, ..., 3069, 3070, 3071],            # = x0
#        [3072, 3073, 3074, ..., 6141, 6142, 6143]])           # = x1
a.shape
# (2, 4)
t.shape
# (2, 3072)

c = (a.T[None, :, :] * t.T[:, None, :]).T
# array([[[    0,     1,     2, ...,  3069,  3070,  3071],     # =  1 * x0
#         [    0,    -2,    -4, ..., -6138, -6140, -6142],     # = -2 * x0
#         [    0,     1,     2, ...,  3069,  3070,  3071],     # =  1 * x0
#         [    0,     0,     0, ...,     0,     0,     0]],    # =  0 * x0
# 
#        [[ 3072,  3073,  3074, ...,  6141,  6142,  6143],     # =  1 * x1
#         [    0,     0,     0, ...,     0,     0,     0],     # =  0 * x1
#         [    0,     0,     0, ...,     0,     0,     0],     # =  0 * x1
#         [-3072, -3073, -3074, ..., -6141, -6142, -6143]]])   # = -1 * x1

c.shape
# (2, 4, 3072)

Upvotes: 1

sascha
sascha

Reputation: 33552

Code:

import numpy as np

# Example data taken from bendl's answer !!!
a = np.array([[1,-2,1,0], [1,0,0,-1]])
xi = np.array([1, 2, 3])

b = np.outer(a, xi).reshape(a.shape[0], -1, len(xi))

print('a:')
print(a)
print('b:')
print(b)

Output:

a:
[[ 1 -2  1  0]
 [ 1  0  0 -1]]
b:
[[[ 1  2  3]
  [-2 -4 -6]
  [ 1  2  3]
  [ 0  0  0]]

 [[ 1  2  3]
  [ 0  0  0]
  [ 0  0  0]
  [-1 -2 -3]]]

As i said: it looks like an outer-product and splitting/reshaping this one dimension is easy.

Upvotes: 2

bendl
bendl

Reputation: 1630

Does this do what you need?

import numpy as np

a = np.array([[1,-2,1,0], [1,0,0,-1]])
xi = np.array([1, 2, 3])
a = np.dstack([a * i for i in xi])

The docs for this are here: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.dstack.html

Upvotes: 0

Related Questions