Python NumPy matrix + Examples

In this Python NumPy tutorial, we will discuss the Python numpy matrix and also cover the below examples:

  • Python NumPy matrix multiplication
  • Python NumPy matrix operation
  • Python NumPy matrix inverse
  • Python NumPy matrix transpose
  • Python NumPy matrix addition
  • Python NumPy matrix power
  • Python NumPy matrix size
  • Python NumPy matrix multiplication element-wise
  • Python NumPy matrix shape

If you are new to Python NumPy, check out What is NumPy in Python.

Python NumPy matrix

  • In this section, we will learn about the Python numpy matrix.
  • Matrix is a rectangular arrangement of data, in other words, we can say that it is a rectangular numpy array of data the horizontal values in the matrix are called rows and the vertical entries are called columns.
  • In Python, we can implement this matrix using nested lists or numpy arrays or using a class called a matrix in the numpy library.
  • First, we will discuss arrays and matrix because this comes under the numpy library.
  • Numpy is a library that allows us to create multi-dimensional arrays.
  • Two Dimensional array means the collection of homogenous data in lists of a list. It is also known as a numpy matrix. In a 2Dimension array, we can easily use two square brackets that is why it said lists of lists.

Syntax:

Here is the syntax of the python numpy matrix

numpy.matrix
            (
             data,
             dtype=None
            )

Example:

import numpy as np

a = np.array([2,3])
b = np.array([4,5])
new_matrix = np.matrix([[2,3],[4,5]])
print(new_matrix)

Here is the Screenshot of the following given code

Python numpy matrix
Python numpy matrix

This is how to use the Python NumPy matrix.

Read: Python NumPy append

Python NumPy matrix multiplication

  • In this section, we will learn about Python numpy matrix multiplication.
  • Matrix is a rectangular arrangement of data or number or in other words, we can say that it is a rectangular array of data the horizontal entries in the matrix are called rows and the vertical entries are called columns.
  • Matrix multiplication and array multiplication are different for array multiplication we use this symbol that is the multiplication symbol but to perform the matrix multiplication we need to use a method called dot.
  • To perform the matrix multiplication we need to use the dot method.
  • It is an operation that takes two matrices as input and produces a single matrix.

Syntax:

Here is the syntax of matrix multiplication

np.dot
      (
       a,
       b,
       out=None
      )
  1. a: array_like(It is a first argument)
  2. b: array_like(It is asecond argument)
  3. out: output argument. This must have the exact value that would be returned if it was not used. In particular, it must have the right data type, it must be contiguous, and its dtype must be in a data type that would be returned for dotmatrix(a,b).

Example:

Let’s take an example to check how to perform matrix multiplication.

import numpy as np

a = np.array([[2,3],[6,7]])
b = np.array([[4,5],[9,7]])
new_matrix = a.dot(b)
print(new_matrix)

Here is the Screenshot of following given code

Python numpy matrix multiplication
Python numpy matrix multiplication

Read: Python NumPy arange

Python NumPy matrix operation

  • In this section, we will learn about the Python numpy matrix operation.
  • Matrix is a rectangular arrangement of data or numbers or in other words, we can say that it is a rectangular numpy array of data the horizontal values in the given matrix are called rows, and the vertical values are called columns.
  • We discuss the three operations that are addition, multiplication, and subtraction.
  • For matrix addition, we will use symbol plus so if A is an array and B is another array we can easily add this matrix a+b. it is similar to the other addition it will perform the value by value addition.
  • Matrix multiplication and array multiplication are different for array multiplication we use this symbol that is the multiplication symbol but to obtain the matrix multiplication we need to use a method called dot matrix.
  • For matrix subtraction, we will use numpy.subtract() to subtract values of two matrices. it returns the subtraction of matrix 1 and matrix 2, value-wise.
  • Numpy is a library that allows us to create multi-dimensional arrays.

Syntax:

numpy.matrix
            (
             data,
             dtype=None
            )

Example:

import numpy as np

a = np.array([[2,3],[6,7]])
b = np.array([[4,5],[9,7]])
add_matrix = (np.add(a,b)) # addition of matrix
print(add_matrix)

sub_matrix = (np.subtract(a,b)) #subtraction of matrix
print(sub_matrix)

mul_matrix = a.dot(b) #multiplication of matrix
print(mul_matrix)

div_matrix = (np.divide(a,b)) #division of matrix
print(div_matrix)
Python numpy matrix operation
Python numpy matrix operation

Read: Python NumPy Sum + Examples

Python NumPy matrix inverse

  • In this section, we will learn about the Python numpy matrix inverse.
  • Matrix is a rectangular arrangement of data or numbers or in other words, we can say that it is a rectangular array of data the horizontal entries in the matrix are called rows and the vertical entries are called columns.
  • For matrix inverse method, we need to use np.linalg.inv() function.
  • This function will inverse the given matrix.
  • It consists of one parameter that is A and A can be a matrix.
  • Python provides an easy method to calculate the inverse of the matrix. The function helps the user to check numpy.linalg.inv() is available in the Python library.

Syntax:

numpy.linalg.inv(a)

Example:

import numpy as np

a = np.array([[2,3],[6,7]])
inverse_matrix = (np.linalg.inv(a))
print(inverse_matrix)

Here is the Screenshot of following given code

Python numpy matrix inverse
Python numpy matrix inverse

Read: Python NumPy zeros

Python NumPy matrix transpose

  • In this section, we will learn about the Python NumPy matrix transpose.
  • Matrix is a rectangular arrangement of elements or number. In other words, we can say that it is a rectangular numpy array of data the horizontal values in the matrix are called rows and the vertical entries are called columns.
  • First, we will learn and discuss numpy arrays and matrices because this comes under the NumPy library.
  • Numpy is a library that always allows us to declare or create multi-dimensional arrays.
  • This function will rearrange the dimensional of the given NumPy array.
  • The transpose of a given matrix is an operator which flips or reverses a matrix over its diagonal elements.
  • Transpose of a[i][j] rows and columns is obtained by exchanging to a[j][i].
  • For matrix transpose method, we need to use np.transpose() function.

Syntax:

numpy.transpose
               (
                arr,
                axis=None
               )

Example:

import numpy as np

a = np.array([[2,3],[6,7]])
trans_matrix = (np.transpose(a))
print(trans_matrix)

Here is the Screenshot of the following given code

Python numpy matrix transpose
Python numpy matrix transpose

Read: Check if NumPy Array is Empty in Python

Python NumPy matrix addition

  • In this section, we will learn about the Python NumPy matrix addition.
  • It is a rectangular arrangement of number. In other words, we can say that it is a rectangular numpy array of data the horizontal entries in the matrix are called rows and the vertical values are called columns.
  • For matrix addition, we will use plus symbol so if A is any matrix and B is another matrix we can add this matrix a+b. it is similar to the other addition it will perform the element by element addition.
  • For matrix addition, we need to use numpy.add() function.
  • To add() function is used when we want to compute the addition of two arrays.
  • Numpy is a library that allows us to create multi-dimensional arrays.

Syntax:

Here is the Syntax of matrix addition

numpy.add
         (
          arr,
          dtype=None
         )

Example:

import numpy as np

a = np.array([[1,4],[6,7]])
b = np.array([[4,6],[8,7]])
add_matrix = (np.add(a,b)) # addition of matrix
print(add_matrix)

Here is the Screenshot of the following given code

Python numpy matrix addition
Python numpy matrix addition

Read: Python NumPy Random + Examples

Python NumPy matrix power

  • In this section, we will learn about the Python NumPy matrix power.
  • It is a rectangular arrangement of data. In other words, we can say that it is a numpy array of data the horizontal values in the matrix are called rows and the vertical entries are called columns.
  • The function which is used to find out the matrix power is called matrix power.
  • For matrix power, we need to use numpy.linalg.matrix_power().
  • It consists of two parameters array and power(n).
  • If you take n=0 then we will easily get Identify matrix.
  • If you take n>0 then it’s for positive values.
  • If n is negative then the inverse is generated and then raise to the absolute value n.
  • Numpy is a library that always allows us to create multi-dimensional numpy arrays.

Syntax:

Here is the Syntax of matrix power

numpy.linalg.matrix_power
                        (
                         arr,
                         power(n)
                        )

Example:

import numpy as np

a = np.array([[1,4],[6,7]])
pow_matrix = (np.linalg.matrix_power(a,2))
print(pow_matrix)

Here is the Screenshot of the following given code

Python numpy matrix power
Python numpy matrix power

Read: Python NumPy Array

Python NumPy matrix size

  • In this section, we will learn about the Python NumPy matrix size.
  • Matrix is a rectangular arrangement of elements or in other words, we will say that it is a rectangular array of data the horizontal entries in the matrix are called rows and the vertical entries are called columns.
  • It represents the number of elements in the array.
  • For matrix size, we need to use numpy.size() function.
  • First, we will learn and discuss numpy arrays and matrix because this comes under the NumPy library package.
  • Numpy is a library that always allows us to create multi-dimensional numpy arrays.

Example:

import numpy as np

a = np.array([[1,4],[6,7]])
new_matrix = (np.size(a)) #size of matrix
print(new_matrix)

Here is the Screenshot of the following given code.

Python numpy matrix size
Python numpy matrix size

Read: Python NumPy to list

Python NumPy matrix multiplication element-wise

  • In this section, we will learn about Python NumPy matrix multiplication element-wise.
  • Matrix multiplication and array multiplication are different for array multiplication we use this symbol that is the multiplication symbol but to perform the matrix multiplication we need to use a method called dot.
  • To perform the matrix multiplication we need to use the dot method.
  • It is an operation that takes two matrices as input and produces a single matrix.

Syntax:

Here is the syntax of matrix multiplication

np.dot
      (
       a,
       b,
       out=None
      )
  1. a: array_like(first argument)
  2. b: array_like(second argument)
  3. out: output argument. This must have the exact value that would be return if it was not used. In some particular, it should have the right datatype, must be contiguous, and its dtype must be in a dtype that would be returned for matrix dot(a,b).

Example:

Let’s take an example to check how to perform matrix multiplication.

import numpy as np

a = np.array([[4,6],[5,7]])
b = np.array([[9,8],[8,7]])
mul_matrix = a.dot(b)
print(mul_matrix)

Here is the Screenshot of the following given code

PYthon numpy matrix multiplication element wise
Python numpy matrix multiplication element wise

Read: Python sort NumPy array

Python NumPy matrix shape

  • In this section, we will learn about the Python NumPy matrix shape.
  • Matrix is a rectangular arrangement of data or numbers or in other words, we can say that it is a rectangular array of data the horizontal entries in the matrix are called rows and the vertical entries are called columns.
  • The shape of a numpy array is the number of elements in each dimension.
  • For matrix shape, we need to use numpy.shape() method.

Example:

import numpy as np

a = np.array([[4,6],[5,7]])

c = (np.shape(a))
print(c)

Here is the Screenshot of the following given code.

Python numpy matrix shape
Python numpy matrix shape

You may like the following Python tutorials:

In this Python tutorial, we will discuss Python NumPy matrix and also cover the below examples:

  • Python numpy matrix multiplication
  • Python numpy matrix operation
  • Python numpy matrix inverse
  • Python numpy matrix transpose
  • Python numpy matrix addition
  • Python numpy matrix power
  • Python numpy matrix size
  • Python numpy matrix multiplication element-wise
  • Python numpy matrix shape