amansour
amansour

Reputation: 57

Matrix size in Python

a is a 2x2 matrix

b is a 2x1 matrix

c is a 1x2 matrix

But ... what kind of matrices d is?

import numpy as np
a= np.array([[1,2],[3,4]])
b= np.array([[1],[2]])
c= np.array([[1,2]])
d= np.array([1,2])

Variable explorer

Upvotes: 2

Views: 2824

Answers (2)

Jurgen Strydom
Jurgen Strydom

Reputation: 3930

The variable d is not a matrix but a row vector.

import numpy as np
a= np.array([[1,2],[3,4]])
b= np.array([[1],[2]])
c= np.array([[1,2]])
d= np.array([1,2])

print(a.shape, b.shape, c.shape, d.shape)
print(a.ndim, b.ndim, c.ndim, d.ndim)

outputs shapes:

(2, 2) (2, 1) (1, 2) (2,)

and dimensions:

2 2 2 1

The number of brackets indicate the number of dimensions, for example:

e = np.array([[[1,2]]])

outputs shape (1, 1, 2) and ndim 3 (so 3 dimensional).

Upvotes: 2

lgc_ustc
lgc_ustc

Reputation: 1664

It's a 1-dimensional array with 2 elements in it.

Check the output in the sandbox.

Upvotes: 0

Related Questions