Python Program on Matrix Concepts
Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
In this article, we will explore a Python program that focuses on matrix operations using the NumPy library. NumPy provides extensive support for matrix-related operations, making it a powerful tool for linear algebra and scientific computing.
The program showcases the creation of a NumPy array, converting it into a matrix, and performing various matrix-related functions such as finding the maximum and minimum values, calculating the sum and product, and sorting elements.
Prerequisites
- Fundamental Python Knowledge (Variables, Data Types, Syntax)
- Basic Familiarity with the NumPy Library (Numerical Computing, Basic Array Operations
Topic Explanation
This program kicks off by importing the NumPy library as ‘np’ and initializing a NumPy array. Subsequently, it delves into the transformation of the array into a NumPy matrix using the ‘np.matrix()’ function.
Within the program, readers will explore a range of matrix-related functions, including those for discovering maximum and minimum values, performing summation and multiplication operations, and facilitating the sorting of elements along rows. This comprehensive exploration equips readers with practical insights into matrix operations, enhancing their proficiency in numerical computing and data manipulation with NumPy in Python.
Code:
import numpy as np
# Create a numpy array
myar=np.array([[10,3,4],[7,6,9],[12,88,9]])
# Convert array to matrix
mt1=np.matrix(myar)
# Print matrix
print(mt1)
# Print maximum value in matrix
print(mt1.max())
# Print minimum value in matrix
print(mt1.min())
# Print sum of all values in matrix
print(mt1.sum())
# Print product of all values in matrix
print(mt1.prod())
# Print matrix with each row sorted
print("Sorted elements of Matrix Row :")
print(np.sort(mt1))Output:
[[10 3 4]
[ 7 6 9]
[12 88 9]]
88
3
148
431101440
Sorted elements of Matrix Row :
[[ 3 4 10]
[ 6 7 9]
[ 9 12 88]]
Code Explanation:
- Import the numpy package with alias np
- Create a 3×3 numpy array called myar with the given values
- Convert myar to a matrix called mt1 using np.matrix()
- Print out mt1 to see the matrix
- Print the maximum value in mt1 using the .max() method
- Print the minimum value in mt1 using the .min() method
- Print the sum of all values in mt1 using the .sum() method
- Print the product of all values in mt1 using the .prod() method
- Print a message saying we will print the sorted elements
- Use np.sort() to sort each row of mt1 and print the sorted matrix
Summary
In summary, this Python program serves as a practical guide to matrix operations using the NumPy library. Understanding these functions is crucial for tasks involving linear algebra, scientific computing, and data analysis in Python. Proficiency in these functions not only paves the way for efficient and precise mathematical computations but also empowers individuals to solve real-world problems across diverse domains, from machine learning to engineering, with confidence and precision.
We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

