1
a = np.array([0,1,2])
b = np.array([3,4,5,6,7])

...

c = np.dot(a,b)

I want to transpose b so I can calculate the dot product of a and b.

9
  • Dot product for 1d arrays of different size is not defined. If the 2 arrays have same size, dot produces the inner product. What c do you want? I suspect you want the np.outer product, not inner or matrix product. Commented Feb 9, 2019 at 19:56
  • You can dot the both 1d arrays if you can transpose b. a-colums = b-rows = 1. Or am I wrong? Commented Feb 9, 2019 at 20:03
  • You can't transpose a 1d array. You can, of course, add dimension, making a (1,n) or (n,1). But I still don't know what c is supposed to be. Commented Feb 9, 2019 at 20:17
  • 1
    What c are you expecting? Commented Feb 9, 2019 at 20:39
  • 1
    Reshaping the arrays to those dimensions is a basic numpy operation. No need to think in terms of transposition. Commented Feb 9, 2019 at 21:44

4 Answers 4

5

You can use numpy's broadcasting for this:

import numpy as np

a = np.array([0,1,2])
b = np.array([3,4,5,6,7])


In [3]: a[:,None]*b
Out[3]: 
array([[ 0,  0,  0,  0,  0],
       [ 3,  4,  5,  6,  7],
       [ 6,  8, 10, 12, 14]])

This has nothing to do with a dot product, though. But in the comments you said, that you want this result.

You could also use the numpy function outer:

In [4]: np.outer(a, b)
Out[4]: 
array([[ 0,  0,  0,  0,  0],
       [ 3,  4,  5,  6,  7],
       [ 6,  8, 10, 12, 14]])
Sign up to request clarification or add additional context in comments.

Comments

1

Well for this what you want is the outer product of the two arrays. The function you want to use for this is np.outer, :

a = np.array([0,1,2])
b = np.array([3,4,5,6,7])

np.outer(a,b)

array([[ 0,  0,  0,  0,  0],
       [ 3,  4,  5,  6,  7],
       [ 6,  8, 10, 12, 14]])

3 Comments

What is the difference between np.outer and np.multiply.outer?
Was just wondering, because I just put the function in my answer like two minutes before you posted your answer and I thought, yours did something different :D But now I learned about np.add.outer which is nice :D
Ahh no the same, just that np.ufunc.outer can be used for multiple operations. Good answer btw +1
0

So with NumPy you could reshape swapping axes:

a = np.swapaxes([a], 1, 0)
# [[0]
#  [1]
#  [2]]

Then

print(a * b)

# [[ 0  0  0  0  0]
#  [ 3  4  5  6  7]
#  [ 6  8 10 12 14]]

Swapping b require to transpose the product, se here below.


Or usual NumPy reshape:

a = np.array([0,1,2])
b = np.array([3,4,5,6,7]).reshape(5,1)

print((a * b).T)

# [[ 0  0  0  0  0]
#  [ 3  4  5  6  7]
#  [ 6  8 10 12 14]]

Reshape is like b = np.array([ [bb] for bb in [3,4,5,6,7] ]) then b becomes:

# [[3]
#  [4]
#  [5]
#  [6]
#  [7]]


While reshaping a no need to transpose:

a = np.array([0,1,2]).reshape(3,1)
b = np.array([3,4,5,6,7])

print(a * b)

# [[ 0  0  0  0  0]
#  [ 3  4  5  6  7]
#  [ 6  8 10 12 14]]


Just out of curiosity, good old list comprehension:

a = [0,1,2]
b = [3,4,5,6,7]
print( [ [aa * bb for bb in b] for aa in a ] )

#=> [[0, 0, 0, 0, 0], [3, 4, 5, 6, 7], [6, 8, 10, 12, 14]]

1 Comment

OPs expected output is yours, but transposed, so you might want to add a .T there
0

Others have provided the outer and broadcasted solutions. Here's the dot one(s):

np.dot(a.reshape(3,1), b.reshape(1,5))    
a[:,None].dot(b[None,:])
a[None].T.dot( b[None])

Conceptually I think it's a bit of an overkill, but due to implementation details, it actually is fastest .

1 Comment

This is actually faster than my answer? Do you have some times? Can you explain? I'm really interested tbh

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.