Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

When I multiply two numpy arrays of sizes (n x n)*(n x 1), I get a matrix of size (n x n). Following normal matrix multiplication rules, a (n x 1) vector is expected, but I simply cannot find any information about how this is done in Python's Numpy module.

The thing is that I don't want to implement it manually to preserve the speed of the program.

Example code is shown below:

a = np.array([[ 5, 1 ,3], [ 1, 1 ,1], [ 1, 2 ,1]]) 

b = np.array([1, 2, 3]) 

print a*b 

Output:-

[[5 2 9] [1 2 3] [1 4 3]]

What I want is:

print a*b 

>> 

[16 6 8]

1 Answer

0 votes
by (106k points)

For numpy matrix vector multiplication you can use numpy.dot or a.dot(b)

>>> a = np.array([[ 5, 1 ,3], [ 1, 1 ,1], [ 1, 2 ,1]]) 

>>> b = np.array([1, 2, 3]) 

>>> print a.dot(b) 

array([16, 6, 8])

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
asked Oct 15, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Mar 21, 2021 in Python by laddulakshana (16.4k points)
+1 vote
1 answer
asked Jul 31, 2019 in Python by Eresh Kumar (45.3k points)

Browse Categories

...