This is an article for numpy beginners. Shortly after I started using Python, I misunderstood the output of an identity matrix shape. I think it's relatively easy to get caught, so I will share it based on actual examples.
First, take a look at the shape of the array below.
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.shape)
The output looks like this:
(2, 3)
Matrix rows are displayed on the left and columns are displayed on the right. It is understandable because it is mathematically expressed as 2 rows and 3 columns.
\begin{bmatrix}
1 & 2 & 3 \\
4 & 5 & 6
\end{bmatrix}
Reduce the number of lines by one and output the shape again.
b = np.array([1, 2, 3])
print(b.shape)
The output is below.
(3, )
...? I thought that (1, 3) would be output in 1 row and 3 columns, but it wasn't. ..
Try to calculate the matrix product as follows
a = np.array([2, 2])
b = np.array([[1, 2], [3, 4]])
print(np.dot(a, b))
print(np.dot(b, a))
Then the result is
[ 8 12]
[ 6 14]
When calculating the matrix product, it can be seen that it flexibly calculates as a row vector and a column vector according to the shape of another matrix.
The shape of a one-dimensional array is a tuple with one element and is expressed as (number of elements,). Mathematically, a matrix with only one row is called a row vector, and a matrix with only one column is called a column vector, but there is no distinction between a row vector and a column vector in a one-dimensional array of ndarray.
Recommended Posts