0

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?

6
  • 1
    np.outer(np.array([[1, -2, 1, 0], [1, 0, 0, -1]]), np.random.random(size=3072)) returns a shape of (8, 3072), so can it be that you are looking for this kind of operation? Commented Oct 18, 2017 at 13:00
  • Does this have to be done with numpy? It's much easier with the standard library, actually Commented Oct 18, 2017 at 13:11
  • @sascha, No, I'm looking forward to expending the array dimension. Commented Oct 18, 2017 at 13:16
  • @bendl, actually it has to be done with numpy without any for loop, or it would be meaningless. Commented Oct 18, 2017 at 13:18
  • Extending is easy, especially 8 to 2x4 (reshape). The more relevant question here is: are you trying to implement some common math-op without declaring it as such (and maybe different input to outer would work). Commented Oct 18, 2017 at 13:18

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, that's exactly what I need.
Isn't xi supposed to be 2D, like xi = np.array([[1, 2, 3], [4, 5, 6]])?
Not according to his words (where xi is a 1D numpy array with 3072 size; at least that's my interpretation).
@DavidHx I edited the reshape-operation (more generic now). You will need it with different number of rows!
No, but t is supposed to be 2D: (2, 3072). And the product we are looking for is between a and t, not between a and xi (which is only one row of t).
|
1

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)

Comments

0

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

1 Comment

The result is what I need, but can you do it without any for-loop? Because it may affect performance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.