Python program to create matrix using numpy

Here, we are going to learn how to create a matrix (two-dimensional array) using numpy in Python programming language?
Submitted by IncludeHelp, on March 26, 2020

By using numpy class we can create a matrix using two ways.

  1. Using numpy.array()
    mat = numpy.array([[10,20,30],[40,50,60],[70,70,90]])
    
  2. Using numpy.matrix()
    mat = numpy.matrix("10 20 30; 40 50 60; 70 80 90")
    

Consider the below program,

# Python matrix creation using numpy

# importing the numpy
import numpy as np

# creating matrix using numpy.array()
mat1 = np.array([[10,20,30],[40,50,60],[70,70,90]])
# printing matrix
print("mat1...")
print(mat1)

# creating matrix using numpy.matrix()
mat2 = np.matrix("10 20 30; 40 50 60; 70 80 90")

# printing matrix
print("mat2...")
print(mat2)

Output

mat1...
[[10 20 30]
 [40 50 60]
 [70 70 90]]
mat2...
[[10 20 30]
 [40 50 60]
 [70 80 90]]

Python Array Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.