NumPy Master Class

Chapter2 Arithmetic Operations: Notebook2 ndarray Multiplication, Division and Modulo

Shin's Lab 2020. 1. 8. 18:36

 

 

 

NumPy Master Class

Chapter2 Arithmetic Operations

Notebook2 ndarray Multiplication, Division and Modulo

 

우리가 Python에서 사용하는 Multiplication과 Division,Modulo는 각각 다음과 같다.

In [18]:
import numpy as np
 

Python List

In [9]:
operand1 = 10
operand2 = 3

print(operand1 * operand2)
print(operand1 / operand2)
print(operand1 % operand2)
 
30
3.3333333333333335
1
 

그러면 list를 이용하여 이 연산이 가능할지 알아보자.

In [14]:
operand1 = [10, 20, 30, 40, 50]
operand2 = [3, 3, 3, 3, 3]

print(operand1 * operand2)
print(operand1 / operand2)
print(operand1 % operand2)
 
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-31c311805f5e> in <module>
      2 operand2 = [3, 3, 3, 3, 3]
      3 
----> 4 print(operand1 * operand2)
      5 print(operand1 / operand2)
      6 print(operand1 % operand2)

TypeError: can't multiply sequence by non-int of type 'list'
 

위에서 볼 수 있듯이, python list는 말그대로 list이고 vector가 아니기 때문에 우리가 원하는 결과를 얻기 힘들다.

만약 우리가 broadcating을 생각해서 다음과 같이 연산하려고 한다고 해보자.

In [16]:
operand1 = [10, 20, 30, 40, 50]
operand2 = 3

print(operand1 * operand2)
print(operand1 / operand2)
print(operand1 % operand2)
 
[10, 20, 30, 40, 50, 10, 20, 30, 40, 50, 10, 20, 30, 40, 50]
 
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-60bf9750c1f7> in <module>
      3 
      4 print(operand1 * operand2)
----> 5 print(operand1 / operand2)
      6 print(operand1 % operand2)

TypeError: unsupported operand type(s) for /: 'list' and 'int'
 

Python list의 *연산은 list를 반복하는 연산이므로 원하는 결과가 아니고, division, modulo 연산은 아예 사용할 수 없다. 즉, NumPy를 이용하지 않고 이런 vector operation을 하려고 한다면 우리는 Python에게 너무 많은 것을 바라고 있는 것이다.

 

ndarray Multiplication, division and Modulo

 

ndarray를 사용해보자. 이때 broadcating과 조금 더 친해지기 위해 앞으로 broadcating을 사용해보도록 한다.

In [17]:
operand1 = np.array([10, 20, 30, 40, 50])
operand2 = 3

print(operand1 * operand2)
print(operand1 / operand2)
print(operand1 % operand2)
 
[ 30  60  90 120 150]
[ 3.33333333  6.66666667 10.         13.33333333 16.66666667]
[1 2 0 1 2]
 

위와 같이 우리가 원하는 vector operation을 할 수 있다.

이때 정말 중요한 점은 element-wise operation인 점이다. 즉, ndarray의 *연산은 우리가 아는 veoctor의 dot product가 아니다. 각각 element마다의 연산을 해주는 element-wise operation이라는 것을 꼭 기억하자.

In [40]:
operand1 = np.random.randint(low = 0, high = 10, size = (3,4))
operand2 = np.array([3, 4, 5]).reshape(3,1)

print("====== Original Operands =======")
print("operand1:\n", operand1)
print("operand2:\n", operand2)

print("operand1.shape:", operand1.shape, "operand2.shape:", operand2.shape,  '\n\n')

print("operand1 * operand2:\n", operand1 * operand2)
print("operand1 / operand2:\n", operand1 / operand2)
print("operand1 % operand2:\n", operand1 % operand2)
 
====== Original Operands =======
operand1:
 [[5 6 0 5]
 [1 0 3 6]
 [2 6 8 4]]
operand2:
 [[3]
 [4]
 [5]]
operand1.shape: (3, 4) operand2.shape: (3, 1) 


operand1 * operand2:
 [[15 18  0 15]
 [ 4  0 12 24]
 [10 30 40 20]]
operand1 / operand2:
 [[1.66666667 2.         0.         1.66666667]
 [0.25       0.         0.75       1.5       ]
 [0.4        1.2        1.6        0.8       ]]
operand1 % operand2:
 [[2 0 0 2]
 [1 0 3 2]
 [2 1 3 4]]
 

위의 결과로 알 수 있듯이, addition, subtraction 외에도 multiplication, division, modulo operation들도 모두 동일하게 작동하는 것을 확인할 수 있다.