[Python] Matrix multiplication processing time using NumPy

What is NumPy

As you all know, Python is a programming language that is strong in the fields of machine learning and AI. The reason is the abundance of mathematics academic libraries. NumPy is a representative Python library.

Matrix calculation (multiplication) using NumPy

I'm going to use NumPy to handle matrix multiplication. I think that even a square matrix 3 * 3 will be difficult for human manual calculation. Here, I would like to measure the processing time of 100 * 100.

#Import NumPy
import numpy as np
import time
from numpy.random import rand

#Matrix 100*Specify 100
N = 100

#Initialize the matrix and generate random numbers
matA = np.array(rand(N, N))
matB = np.array(rand(N, N)) 
matC = np.array([[0] * N for _ in range(N)]) 

#Get start time
start = time.time()

#Perform matrix multiplication
matC = np.dot(matA, matB)

#Output at the second decimal place
print("Calculation result using NumPy:%.2f[sec]" % float(time.time() - start))

Processing result

Calculation result using NumPy: 0.03 [sec]

Nested and calculated Python for statements without using NumPy

#Import NumPy
import numpy as np
import time
from numpy.random import rand

#Matrix 100*Specify 100
N = 100

#Initialize the matrix and generate random numbers
matA = np.array(rand(N, N))
matB = np.array(rand(N, N)) 
matC = np.array([[0] * N for _ in range(N)]) 

#Get start time
start = time.time()

#Nest for statement
for i in range(N):
    for j in range(N):
        for k in range(N):
            matC[i][j] = matA[i][k] * matB[k][j]

print("Calculation result in Python for statement:%.2f[sec]" % float(time.time() - start))

Processing result

Calculation result in Python for statement: 0.92 [sec]

In 100 * 100 matrix calculation, the processing time difference between NumPy and for statement nesting is about 9 times.

Not only does the library make your code easier and easier to work with, It can be seen that the processing time can be significantly reduced and the load on the system can be significantly reduced.

I'm just studying.

■ References

Learn by moving with Python! New deep learning textbook From basic machine learning to deep learning (AI & TECHNOLOGY) by Aidemy Inc. Toshihiko Ishikawa https://www.shoeisha.co.jp/book/detail/9784798158570

Recommended Posts

[Python] Matrix multiplication processing time using NumPy
Matrix multiplication in python numpy
[Python] Various data processing using Numpy arrays
Python Math Series ② Matrix Multiplication
Using Python mode in Processing
[Python] Multiplication table using for statement
python> Processing time measurement> time.time () --start_time
Matrix multiplication
Periodic execution processing when using tkinter [Python3]
[Python] Speeding up processing using cache tools
VBA user tried using Python / R: Matrix
Video processing using Python + OpenCV on Mac
Matrix Convolution Filtering-Reinventor of Python Image Processing-
[Scientific / technical calculation by Python] Inverse matrix calculation, numpy
My Numpy (Python)
VBA user tried using Python / R: Iterative processing
Time variation analysis of black holes using python
First time python
python image processing
Debug with VS Code using boost python numpy
Start using Python
python time measurement
Python file processing
A clever way to time processing in Python
First time python
#Python basics (#Numpy 1/2)
#Python basics (#Numpy 2/2)
[Python] Matrix operation
Python #Numpy basics
Scraping using Python
[Python] Numpy memo
Image processing by matrix Basics & Table of Contents-Reinventor of Python image processing-
How to measure processing time in Python or Java
[Python] Measures and displays the time required for processing
Process csv data with python (count processing using pandas)
Multi-digit multiplication time up to 300 million digits in python
Python distributed processing Spartan
Python: Time Series Analysis
Python and numpy tips
Operate Redmine using Python Redmine
File processing in Python
Function execution time (Python)
Python: Natural language processing
Communication processing by Python
Fibonacci sequence using Python
Multithreaded processing in python
Dictionary-type processing using items ()
Data analysis using Python 0
Python basics 8 numpy test
First Python image processing
Data cleaning using Python
Python time series question
[Python] Search (NumPy) ABC165C
Text processing in Python
python numpy array calculation
Implemented Matrix Factorization (python)
Queue processing in Python
Using Python #external packages
Output python execution time
WiringPi-SPI communication using Python
Time floor function (Python)