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).