Python Math Series ② Matrix Multiplication

Background

I'm reading Deep Learning Book, but I get sleepy when I just read the characters, so I'll try to deepen my understanding by implementing the items that appear in python. I don't know if there is demand, but it's in a series (laughs). The table of contents is here. This time I'm going to do matrix multiplication. Since python has abundant libraries, you can calculate without thinking if you rely on it, but I dare to make it myself. *** This is my last article on the ad event calendar. @ Bigface00 will close the end of the calendar. *** ***

Matrix multiplication

Matrix multiplication can be calculated with the image below. Reference source Screenshot from 2020-12-22 20-40-38.png

Easy implementation

If you refer to the article here, it seems that you can easily implement it by using numpy.dot or numpy.matmal.

arr1 = np.arange(4).reshape((2, 2))
print(arr1)
# [[0 1]
#  [2 3]]

arr2 = np.arange(6).reshape((2, 3))
print(arr2)
# [[0 1 2]
#  [3 4 5]]

print(np.dot(arr1, arr2))
# [[ 3  4  5]
#  [ 9 14 19]]

print(arr1.dot(arr2))
# [[ 3  4  5]
#  [ 9 14 19]]

print(np.matmul(arr1, arr2))
# [[ 3  4  5]
#  [ 9 14 19]]

I made it myself

First, count the number of rows and columns of the final answer. Since the number of rows of the answer is the number of rows of the previous matrix and the number of columns of the answer is the number of columns of the latter matrix, the for statement is rotated based on that. In the third for statement, we are calculating the rows of the previous matrix and the columns of the subsequent matrix. I put the calculation result for each column of the previous matrix in tmp_list, and finally put all the calculation results in ans_list.

def matrix_mlp(list1, list2):
	ans_row = len(list1)
	ans_col = len(list2[0])
	ans_list = []
	for i in range(ans_row):
		tmp_list = []
		for j in range(ans_col):
			tmp = 0
			for k in range(ans_row):
				tmp += list1[i][k] * list2[k][j]
			tmp_list.append(tmp)
		ans_list.append(tmp_list)
	return ans_list

def main():
	print("~~matrix_multiple_test~~")
	arr1 = np.arange(4).reshape((2, 2))
	arr2 = np.arange(6).reshape((2, 3))
	print("Before1  :", arr1.tolist())
	print("Before2  :", arr2.tolist())
	print("correct  :", np.dot(arr1, arr2).tolist())
	print("my_answer:", matrix_mlp(arr1.tolist(), arr2.tolist()))

It's an annual event in this series, but I don't guarantee that it will match because the implementation is the one I came up with earlier (laughs). Please let me know if there is any difference here or if there is such a method. I'm reading it loosely like this.

Recommended Posts

Python Math Series ② Matrix Multiplication
Matrix multiplication in python numpy
Matrix multiplication
Python Math Series ⓪ Table of Contents
[Python] Matrix multiplication processing time using NumPy
[Python] Matrix operation
Python: Time Series Analysis
Python time series question
Implemented Matrix Factorization (python)
Python Mathematics Series ① Transpose
Python application: Pandas Part 2: Series
Python 2 series and 3 series (Anaconda edition)
Python Mathematics Series ③ Determinant (replacement)
Transposed matrix in Python standard
Python 3 series installation for mac
[Python] Plot time series data
About installing Pwntools and Python2 series
Matrix representation with Python standard input
Python: Time Series Analysis: Preprocessing Time Series Data
[Python] How to use Pandas Series
Draw a scatterplot matrix in python
Division of timedelta in Python 2.7 series
Difference between python2 series and python3 series dict.keys ()
Time series plot started ~ python edition ~
[Python] Multiplication table using for statement