Numpy: Create a matrix

31 July 2020

Code

It is recommended to use np.array() instead of np.matrix() as suggested in docs.

# Import library
import numpy as np

# 1 - Create matrix or array
x = np.array([[1,2,3],
              [4,5,6]])

# 2 - Create matrix
# Note: This is not recommended
y = np.matrix([[1,2,3],
              [4,5,6]])

# Output
print("Using np.array():\n", x, '\n Type: ', type(x), '\n')
print("Using np.array():\n", y, '\n Type: ', type(y))

Output

Using np.eye(): 
 [[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]

 Using np.identity(): 
 [[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]






Any errors in code above?
Please send a message.