How to create a matrix in NumPy

You can create a matrix in NumPy with the following code. If you want to learn Python then I will highly recommend you to read This Book.

How to create a matrix in NumPy
How to create a matrix in NumPy

Method 1

import numpy as np
a = np.matrix('1 2 3; 4 5 6; 7 8 9')
print(a)
print(type(a))
print(a.ndim)
Output:
[[1 2 3]
 [4 5 6]
 [7 8 9]]
<class 'numpy.matrix'>
2

Method 2

import numpy as np
a = np.matrix([[1, 2, 3], [4, 5, 6],[7, 8, 9]])
print(a)
print(type(a))
print(a.ndim)
Output:
[[1 2 3]
 [4 5 6]
 [7 8 9]]
<class 'numpy.matrix'>
2

People are also reading:

Best Python Books

What is Computer Vision? Examples, Applications, Techniques

Top 10 Computer Vision Books with Python

Books for Machine Learning (ML)

Free Learning Resources

Leave a Comment

Your email address will not be published.