본문 바로가기

STUDY/Python

[Pythonic Code] Assignment-Basic Linear Algeba(4)

728x90

 

 


* 부스트코스 "머신러닝을 위한 파이썬"를 듣고 작성한 포스트입니다.

https://github.com/TEAMLAB-Lecture/AI-python-connect/tree/master/lab_assignments/lab_1

 

GitHub - TEAMLAB-Lecture/AI-python-connect

Contribute to TEAMLAB-Lecture/AI-python-connect development by creating an account on GitHub.

github.com


7. matrix_addition

marix간 덧셈

위 코드에서 for matrix in zip(*matrix_variables)을 먼저 분석해보자.

 

- zip(*matrix_variables)

matrix_x와 matrix_y 행렬에서 같은 위치에 있는 것끼리 묶는다. matrix_x의 [2, 2], matrix_y의 [2, 5]가 ([2, 2], [2,5])로 묶인다. 

matrix_x의 [2, 2]와 matrix_y의 [2, 1] 또한 ([2, 2], [2, 1])로 묶인다.

 

- for matrix in zip(*matrix_variables)

zip(*matrix_variables)에서 나온 값 ([2, 2], [2, 2]), ([2, 5], [2, 1])을 matrix로 출력해주면 [그림 1]과 같은 결과를 띤다.

[그림 1] 중간 과정 확인해보기

for matrix in zip(*matrix_variables)에서 구한 matrix 값으로 sum(row) for row in zip(*matrix)를 구한다.

 

- for row in zip(*matrix)

matrix값으로 나온 ([2, 2], [2, 5]), ([2, 2], [2, 1])을 zip함수로 같은 위치 값을 묶어준다.

 

먼저, ([2, 2], [2, 5])을 zip으로 묶어주면 (2, 2)와 (2, 5)로 나온다.

두 번째로 ([2, 2], [2, 1])을 zip으로 묶어주면 (2, 2)와 (2, 1)로 나온다.

[그림 2]를 통해 결과를 확인해볼 수 있다.

 

[그림 2] 중간 과정 확인해보기(2)

 

- sum(row) for row in zip(*matrix)

위에서 살펴본 row값을 sum() 함수를 사용하여 matrix_addition을 해준다. 

 

row 값은 (2, 2), (2, 5), (2, 2), (2, 1)이다. 각 각 더하면 [(2+2), (2+5)] , [(2+2), (2+1)] 이므로 [[4, 7], [4, 3]]의 결과가 나온다. [그림 3]을 통해 결과를 확인해볼 수 있다.

[그림 3] matrix_addtion 실행해보기


✔ 이전 포스팅 보러 가기

https://chaeso-coding.tistory.com/34

 

[Pythonic Code] Assignment-Basic Linear Algebra(3)

* 부스트코스 "머신러닝을 위한 파이썬"를 듣고 작성한 포스트입니다. https://github.com/TEAMLAB-Lecture/AI-python-connect/tree/master/lab_assignments/lab_1 GitHub - TEAMLAB-Lecture/AI-python-connect Co..

chaeso-coding.tistory.com

 

728x90

'STUDY > Python' 카테고리의 다른 글

[백준] 14501번  (0) 2022.01.25
[백준] 1260번  (0) 2022.01.24
[Pythonic Code] Assignment-Basic Linear Algebra(3)  (0) 2022.01.18
[백준] 14487번  (0) 2022.01.17
[백준] 20044번  (0) 2022.01.17