0

In the code below I have to multiply a 5x3 matrix with a vector. To me the vector seems like a 1x3 matrix, however, python sees it as a 3x1 matrix.

Why is w in the code below a 3x1 matrix or why does python make it a 3x1 matrix?

Thank you in advance.

import numpy

X = numpy.array([[4.6, 3.4, 1.4],
    [6.5, 3.2, 5.1],
    [5.7, 2.9, 4.2],
    [6.6, 3.,  4.4],
    [6.,  2.9, 4.5]])

print(X)
print(X.shape)
print("")

wb0 = numpy.array([0.0, 1.0, 1.0, 1.0])
print(wb0)
print("shape wb0",wb0.shape)

print()

w = wb0[1:]
print(w)
print("shape w",w.shape)

print("")
print(numpy.dot(X,w)) 
Code Pope
  • 4,649
  • 6
  • 22
  • 53
  • `w.shape` is `(3,)`, 1d. `dot` has special rules for a 1d array – hpaulj Apr 30 '20 at 10:31
  • It is just for convenience. You can make `wb0` 1x4 explicitly with `wb=0numpy.array([[0.0, 1.0, 1.0, 1.0]])` – BlackBear Apr 30 '20 at 10:32
  • The text book definition for `dot product` talks about 2 sequences of numbers. `np.dot` is more than the linear algebra 'matrix product'. – hpaulj Apr 30 '20 at 16:01

2 Answers2

0

the calculation in numpy.dot() heavily depends on your input variables. The doc-string gives:

dot(a, b, out=None)

[...]

  • If a is an N-D array and b is a 1-D array, it is a sum product over the last axis of a and b.
  • If both a and b are 2-D arrays, it is matrix multiplication, but using :func:matmul or a @ b is preferred.

[...]

If you want to use matrix multiplication you got to define w as a 2-D array:

wb0 = numpy.array([[0.0, 1.0, 1.0, 1.0]])
w = wb0[0,1:]
Marc
  • 495
  • 3
  • 7
0

Don't confuse the meaning of dimension used in math with the dimension in numpy.
Your first matrix has the shape (5,3), this is correct. And it has a dimension of 2.
The second vector is not a 3x1 matrix for numpy. It has a shape of (3,) and a dimension of 1:

wb0 = np.array([ 1.0, 1.0, 1.0])

The equivalent 3x1 matrix for numpy would be the following:

wb1 = numpy.array([[1.0], [1.0], [1.0]])
print(wb1)
print("shape wb1",wb1.shape)

As you can see, here we have two dimension for wb1 compared to wb0. Although wb0 looks to you as 3x1 matrix, for numpy it is a (3,) matrix.
A 1x3 matrix of wb01 would be the following:

wb2 = numpy.array([[1.0, 1.0, 1.0]])
print(wb2)
print("shape wb0",wb2.shape)

Thus, dimension and shapes in numpy are different from dimension used for matrix in general algebra. A very throrough explanation can be found in the following thread: Difference between numpy.array shape (R, 1) and (R,)

In your case the reason, that numpy is able to make a matrix multiplication between a (5,3) matrix and a (3,) matrix is the implementation of the dot function:

If a is an N-D array and b is a 1-D array, it is a sum product over the last axis of a and b.

You would get the same result if you use the dot function for (5,3) matrix and (3,1) matrix:

import numpy

X = numpy.array([[4.6, 3.4, 1.4],
    [6.5, 3.2, 5.1],
    [5.7, 2.9, 4.2],
    [6.6, 3.,  4.4],
    [6.,  2.9, 4.5]])

print(X)
print(X.shape)
print("")

wb0 = numpy.array([0.0, 1.0, 1.0, 1.0])
print(wb0)
print("shape wb0",wb0.shape)

print()

w = numpy.array([[1.0], [1.0], [1.0]])
print(w)
print("shape w",w.shape)

print("")
print(numpy.dot(X,w)) 

The only difference would be that in your case the end result has a shape of (5,) and in the second case the result would have a shape of (5,1).

Code Pope
  • 4,649
  • 6
  • 22
  • 53
  • @ReneeSnelting If this is the answer to your question, I would be glad if you upvote and mark it as the answer. – Code Pope May 01 '20 at 13:21