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.
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))
Calculation result using NumPy: 0.03 [sec]
#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))
Calculation result in Python for statement: 0.92 [sec]
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.
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