Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Difference Between Matrices and Arrays in Python?
The arrays in Python are ndarray objects from NumPy. The matrix objects are strictly 2-dimensional whereas the ndarray objects can be multi-dimensional. To create arrays in Python, use the NumPy library.
Matrices in Python
A matrix is a special case of two-dimensional array where each data element is of strictly the same size. Matrices are a key data structure for many mathematical and scientific calculations. Every matrix is also a two-dimensional array but not vice versa. Matrix objects are a subclass of ndarray, so they inherit all the attributes and methods of ndarrays.
Creating a Matrix with array()
You can create a matrix using NumPy's array() function ?
import numpy as np
# Create a Matrix
mat = np.array([['A', '12', '16'],
['B', '11', '13'],
['C', '20', '19'],
['D', '22', '21'],
['E', '18', '22'],
['F', '12', '18']])
# Display the Matrix
print("Matrix = \n", mat)
Matrix = [['A' '12' '16'] ['B' '11' '13'] ['C' '20' '19'] ['D' '22' '21'] ['E' '18' '22'] ['F' '12' '18']]
Creating a Matrix with mat()
The mat() function interprets an input as matrix ?
import numpy as np
# Create a Matrix using mat()
mat = np.mat([[5, 10], [15, 20]])
# Display the Matrix
print("Matrix = \n", mat)
print("Type:", type(mat))
Matrix = [[ 5 10] [15 20]] Type: <class 'numpy.matrix'>
Arrays in Python
An array is a container which can hold a fixed number of items and these items should be of the same type. NumPy arrays can be 1-dimensional, 2-dimensional, or multi-dimensional. To work with arrays in Python, import the NumPy library.
Example
Creating a 1-dimensional array ?
import numpy as np
# Create a 1D array
arr = np.array([5, 10, 15, 20])
# Display the Array
print("Array =", arr)
print("Shape:", arr.shape)
print("Dimensions:", arr.ndim)
Array = [ 5 10 15 20] Shape: (4,) Dimensions: 1
Key Differences
| Feature | Matrix | Array |
|---|---|---|
| Dimensions | Always 2D | 1D, 2D, or multi-dimensional |
| Multiplication (*) | Matrix multiplication | Element-wise multiplication |
| Future Support | Being deprecated | Recommended approach |
| Use Case | Linear algebra operations | General numerical computing |
Matrix Operations with NumPy Arrays
As per the official documentation, the numpy.matrix class will be removed in the future. Therefore, matrix algebra operations should be done with NumPy Arrays using the @ operator for matrix multiplication.
Matrix Multiplication
Let us create matrices using arrays and perform multiplication ?
import numpy as np
# Create Matrices using arrays
mat1 = np.array([[5, 10], [3, 9]])
mat2 = np.array([[15, 20], [10, 11]])
# Display the Matrices
print("Matrix1 = \n", mat1)
print("Matrix2 = \n", mat2)
# Matrix multiplication using @ operator
result = mat1 @ mat2
print("\nMatrix Multiplication = \n", result)
Matrix1 = [[ 5 10] [ 3 9]] Matrix2 = [[15 20] [10 11]] Matrix Multiplication = [[175 210] [135 159]]
Matrix Transpose
Getting the transpose of a matrix ?
import numpy as np
# Create a matrix
mat = np.array([[5, 10], [3, 9]])
print("Original Matrix = \n", mat)
print("Transpose = \n", mat.T)
Original Matrix = [[ 5 10] [ 3 9]] Transpose = [[ 5 3] [10 9]]
Conclusion
Use NumPy arrays instead of matrix objects for all operations as matrices are being deprecated. Arrays provide more flexibility with multi-dimensional support while matrices are limited to 2D operations.
