IKH

Chepter-17

Importance of matrix class in numpy library

  • D array is called => Vector
  • 2-D array is called => Matrix
  • matrix class is specially designed class to create 2-D arrays

Example

Python
In [417]: 
import numpy as np 
help(np.matrix) 

Output

PowerShell
Help on class matrix in module numpy: 
 
class matrix(ndarray) 
 |  matrix(data, dtype=None, copy=True) 
 |   
 |  matrix(data, dtype=None, copy=True) 
 |   
 |  .. note:: It is no longer recommended to use this class, even for linear 
 |            algebra. Instead use regular arrays. The class may be removed 
 |            in the future. 

creating 2-D arrays

  • By using matrix class
  • By using ndarray class

class matrix(ndarray)

matrix(data, dtype=None, copy=True)

  • data : array_like or string
  • If data is a string, it is interpreted as a matrix with commas or spaces separating
    columns, and semicolons separating rows.

Parameters

  • . data : array_like or string
  • If data is a string, it is interpreted as a matrix with commas
  • or spaces separating columns, and semicolons separating rows.
  • dtype : data-type
  • Data-type of the output matrix.
  • copy : bool
  • If data is already an ndarray, then this flag determines
  • whether the data is copied (the default), or whether a view is constructed.

Example

Python
In [418]: 
# Creating matrix object from string 
# a = np.matrix('col1 col2 col3;col1 col2 col3') 
# a = np.matrix('col1,col2,col3;col1,col2,col3') 
a = np.matrix('10,20;30,40') 
b = np.matrix('10 20;30 40') 
print(f"type of a : type(a)") 
print(f"type of b : type(b)") 
print(f"Matrix object creation from string with comma : \n{a}") 
print(f"Matrix object creation from string with space : \n{b}") 

Output

PowerShell
type of a : type(a) 
type of b : type(b) 
Matrix object creation from string with comma :  
[[10 20] 
 [30 40]] 
Matrix object creation from string with space :  
[[10 20] 
 [30 40]]

Example

Python
In [419]: 
#  Creating matrix object from nested list 
a = np.matrix([[10,20],[30,40]]) 
a 

Output

PowerShell
Out[419]: 
matrix([[10, 20], 
        [30, 40]])

Example

Python
In [420]: 
# create a matrix from ndarray 
a = np.arange(6).reshape(3,2) 
b = np.matrix(a) 
print(f"type of a : type(a)") 
print(f"type of b : type(b)") 
print(f'ndarray :\n {a}') 
print(f'matrix :\n {b}') 

Output

PowerShell
type of a : type(a) 
type of b : type(b) 
ndarray : 
 [[0 1] 
 [2 3] 
 [4 5]] 
matrix : 
 [[0 1] 
 [2 3] 
 [4 5]] 

operator in ndarray and matrix

  • In case of both ndarray and matrix + operator behaves in the same way

Figer bnanan

Example

Python
In [421]: 
# + operator in ndarray and matrix 
a = np.array([[1,2],[3,4]]) 
m = np.matrix([[1,2],[3,4]]) 
 
addition_a = a+a 
addition_m = m+m 
 
print(f'ndarray addition :\n {addition_a}') 
print(f'matrix addition :\n {addition_m}') 

Output

PowerShell
ndarray addition : 
 [[2 4] 
 [6 8]] 
matrix addition : 
 [[2 4] 
 [6 8]]

operator in ndarray and matrix

  • In case of ndarray * operator performs element level multiplication
  • In case of matrix * operator performs matrix multiplication

Example

Python
In [422]: 
# * operator in ndarray and matrix 
a = np.array([[1,2],[3,4]]) 
m = np.matrix([[1,2],[3,4]]) 
 
element_mul = a*a 
matrix_mul = m*m 
 
print(f'ndarray multiplication :\n {element_mul}') 
print(f'matrix multiplication :\n {matrix_mul}') 

Output

PowerShell
ndarray multiplication : 
 [[ 1  4] 
 [ 9 16]] 
matrix multiplication : 
 [[ 7 10] 
 [15 22]] 

** operator in ndarray and

  • In case of ndarray ** operator performs power operation at element level
  • In case of matrix ** operator performs power operation at matrix level
    m ** 2 ==> m *m

Figer banana

Example

Python
In [423]: 
# ** operator in ndarray and matrix 
a = np.array([[1,2],[3,4]]) 
m = np.matrix([[1,2],[3,4]]) 
 
element_power = a**2 
matrix_power = m**2 
 
print(f'ndarray power :\n {element_power}') 
print(f'matrix power :\n {matrix_power}') 

Output

PowerShell
ndarray power : 
 [[ 1  4] 
 [ 9 16]] 
matrix power : 
 [[ 7 10] 
 [15 22]]

T in ndarray and matrix

  • In case of both ndarray and matrix T behaves in the same way

Example

Python
In [424]: 
# ** operator in ndarray and matrix 
a = np.array([[1,2],[3,4]]) 
m = np.matrix([[1,2],[3,4]]) 
 
ndarray_T = a.T 
matrix_T = m.T 
 
print(f'ndarray transpose :\n {ndarray_T}') 
print(f'matrix transpose :\n {matrix_T}') 

Output

PowerShell
ndarray transpose : 
 [[1 3] 
 [2 4]] 
matrix transpose : 
 [[1 3] 
 [2 4]]

Conclusions

  • matrix class is the child class of ndarray class. Hence all methods and properties of
  • ndarray class are bydefault available to the matrix class.
  • We can use +, *, T, ** for matrix objects also.
  • In the case of ndarray, operator performs element level multiplication. But in case of
    matrix, operator preforms matrix multiplication.
  • In the case of ndarray, operator performs power operation at element level. But
    in the case of matrix, operator performs ‘matrix’ power.
  • matrix class always meant for 2-D array only.
  • It is no longer recommended to use.

Differences between ndarray and matrix

ndarray

  • It can represent any n-dimension array.
  • We can create from any array_like object but not from string.
  • * operator meant for element mulitplication but not for dot product.
  • ** operator meant for element level power operation
  • It is the parent class
  • It is the recommended to use

matrix

  • It can represent only 2-dimension array.
  • We can create from either array_like object or from string
  • * operator meant for for dot product but not for element mulitplication.
  • ** operator meant for for matrix power operation
  • It is the child class
  • It is not recommended to use and it is deprecated.

Report an error