Home » Python » Python programs

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]]





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.