01 Oct 2017 » python, numpy

2017-10-01-python_1

NumPy에 대해 알아보자. ③


Array Operation

Elementwise operations

In [1]:
import numpy as np
a = np.array([1, 2, 3, 4])
a
Out[1]:
array([1, 2, 3, 4])
In [2]:
a + 1
Out[2]:
array([2, 3, 4, 5])

array를 거듭제곱으로도 넣을 수 있다.

$$ [2^1, 2^2, 2^3, 2^4] $$
In [3]:
2**a
Out[3]:
array([ 2,  4,  8, 16])
In [4]:
b = np.ones(4) + 1
b
Out[4]:
array([ 2.,  2.,  2.,  2.])
In [5]:
a - b
Out[5]:
array([-1.,  0.,  1.,  2.])
In [6]:
a + b
Out[6]:
array([ 3.,  4.,  5.,  6.])
In [7]:
c = np.ones((3, 3))
c
Out[7]:
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])
In [8]:
# element-wise, NOT Matrix product
## 매트릭스의 곱이 아니라. 원소간의 곱이다.
c * c
Out[8]:
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])
In [9]:
# matrix product
c.dot(c)
Out[9]:
array([[ 3.,  3.,  3.],
       [ 3.,  3.,  3.],
       [ 3.,  3.,  3.]])
In [10]:
a = np.array([1, 2, 3, 4])
b = np.array([4, 2, 2, 4])

array끼리의 Boolean은 각 원소끼리 비교한다.

In [11]:
a == b
Out[11]:
array([False,  True, False,  True], dtype=bool)
In [12]:
a > b
Out[12]:
array([False, False,  True, False], dtype=bool)
In [13]:
a = np.array([1, 2, 3, 4])
b = np.array([4, 2, 2, 4])
c = np.array([1, 2, 3, 4])

np.array_equal( a, b) a와 b 어레이가 같은지 확인!

In [14]:
np.array_equal(a, b)
Out[14]:
False
In [15]:
np.array_equal(a, c)
Out[15]:
True

특수 함수에 한 번에 넣을 수 있다.

In [16]:
a = np.arange(5)
a
Out[16]:
array([0, 1, 2, 3, 4])
In [17]:
np.sin(a)
Out[17]:
array([ 0.        ,  0.84147098,  0.90929743,  0.14112001, -0.7568025 ])
In [18]:
np.log(a)
/Users/Leo/.pyenv/versions/anaconda3-4.0.0/envs/code_study/lib/python3.6/site-packages/ipykernel_launcher.py:1: RuntimeWarning: divide by zero encountered in log
  """Entry point for launching an IPython kernel.
Out[18]:
array([       -inf,  0.        ,  0.69314718,  1.09861229,  1.38629436])
In [19]:
np.exp(a)
Out[19]:
array([  1.        ,   2.71828183,   7.3890561 ,  20.08553692,  54.59815003])
In [20]:
np.log10(a)
/Users/Leo/.pyenv/versions/anaconda3-4.0.0/envs/code_study/lib/python3.6/site-packages/ipykernel_launcher.py:1: RuntimeWarning: divide by zero encountered in log10
  """Entry point for launching an IPython kernel.
Out[20]:
array([       -inf,  0.        ,  0.30103   ,  0.47712125,  0.60205999])
In [21]:
a = np.arange(4)
b = np.array([1, 2])
a, b
Out[21]:
(array([0, 1, 2, 3]), array([1, 2]))
In [22]:
a + b
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-22-f96fb8f649b6> in <module>()
----> 1 a + b

ValueError: operands could not be broadcast together with shapes (4,) (2,) 

차원이 다르면 → 브로드캐스팅(Broadcasting)

차원이 같을 때, shape이 다르면 → Error

위처럼 에러가 발생한다.


Dimension Reduction Operation

sum

x.sum( ) → 전체 합

x.sum(axis=0)

columns (first dimension) / 세로

x.sum(axis=1)

rows (second dimension) / 가로

In [23]:
x = np.array([1, 2, 3, 4])
x
Out[23]:
array([1, 2, 3, 4])
In [24]:
np.sum(x)
Out[24]:
10
In [25]:
x.sum()
Out[25]:
10
In [26]:
x = np.array([[1, 1], [2, 2]])
x
Out[26]:
array([[1, 1],
       [2, 2]])

In [27]:
x.sum()
Out[27]:
6
In [28]:
x.sum(axis=0)   # columns (first dimension)
Out[28]:
array([3, 3])
In [29]:
x.sum(axis=1)   # rows (second dimension)
Out[29]:
array([2, 4])
In [30]:
x = np.array([1, 3, 2])

min, max, argmin, argmax

x.argmin( )

index of minimum

x.argmax( )

index of maximum

In [31]:
x.min()
Out[31]:
1
In [32]:
x.max()
Out[32]:
3
In [33]:
x.argmin()  # index of minimum
Out[33]:
0
In [34]:
x.argmax()  # index of maximum
Out[34]:
1

all, any

all : 모든 것이 True

any : True가 하나라도 있으면 True, 모든 것이 False이면, False이다.

In [35]:
np.all([True, True, False])
Out[35]:
False
In [36]:
np.any([True, True, False])
Out[36]:
True
In [37]:
# 아래 dtype을 int로 지정을 해줘야, 아래서 올바른 Boolean 값을 얻을 수 있다.
a = np.zeros((100, 100), dtype=np.int)
a
Out[37]:
array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ..., 
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]])
In [38]:
np.any(a != 0)
Out[38]:
False
In [39]:
np.all(a == a)
Out[39]:
True
In [40]:
a = np.array([1, 2, 3, 2])
b = np.array([2, 2, 3, 2])
c = np.array([6, 4, 4, 5])
In [41]:
((a <= b) & (b <= c)).all()
Out[41]:
True

mean, median, std, var

In [42]:
x = np.array([1, 2, 3, 1])
y = np.array([[1, 2, 3], [5, 6, 1]])
In [43]:
x.mean()
Out[43]:
1.75
In [44]:
np.median(x)
Out[44]:
1.5
In [45]:
# last axis = 마지막 axis = -1 여기서는 axis = 1 (가로)
np.median(y, axis=-1)
Out[45]:
array([ 2.,  5.])
In [46]:
x.std()          # full population standard dev.
Out[46]:
0.82915619758884995

Broadcasting

In [47]:
a = np.tile(np.arange(0, 40, 10), (3, 1)).T
a
Out[47]:
array([[ 0,  0,  0],
       [10, 10, 10],
       [20, 20, 20],
       [30, 30, 30]])
In [48]:
b = np.array([0, 1, 2])
b
Out[48]:
array([0, 1, 2])
In [49]:
a + b
Out[49]:
array([[ 0,  1,  2],
       [10, 11, 12],
       [20, 21, 22],
       [30, 31, 32]])
In [50]:
a[:,0][:, np.newaxis]
Out[50]:
array([[ 0],
       [10],
       [20],
       [30]])
In [51]:
a[:,0][:, np.newaxis] + b
Out[51]:
array([[ 0,  1,  2],
       [10, 11, 12],
       [20, 21, 22],
       [30, 31, 32]])
In [52]:
a = np.ones((4, 5))
a
Out[52]:
array([[ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.]])
In [53]:
a[0]
Out[53]:
array([ 1.,  1.,  1.,  1.,  1.])
In [54]:
a[0] = 2
a
Out[54]:
array([[ 2.,  2.,  2.,  2.,  2.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.]])
In [55]:
x, y = np.arange(5), np.arange(5)[:, np.newaxis]
In [56]:
x
Out[56]:
array([0, 1, 2, 3, 4])
In [57]:
y
Out[57]:
array([[0],
       [1],
       [2],
       [3],
       [4]])
In [58]:
# 원소 각각 제곱한후, Broadcasting
distance = np.sqrt(x ** 2 + y ** 2)
distance
Out[58]:
array([[ 0.        ,  1.        ,  2.        ,  3.        ,  4.        ],
       [ 1.        ,  1.41421356,  2.23606798,  3.16227766,  4.12310563],
       [ 2.        ,  2.23606798,  2.82842712,  3.60555128,  4.47213595],
       [ 3.        ,  3.16227766,  3.60555128,  4.24264069,  5.        ],
       [ 4.        ,  4.12310563,  4.47213595,  5.        ,  5.65685425]])

ogrid, mgrid, meshgrid

실제로는 mgrid, meshgrid를 많이 쓴다.

shape을 서로 맞춰준다.

(가로, 세로)

3차원 그림을 그릴 때, 꼭 알아야 한다.

np.ogrid

In [59]:
x, y = np.ogrid[0:3, 0:5]
In [60]:
x
Out[60]:
array([[0],
       [1],
       [2]])
In [61]:
y
Out[61]:
array([[0, 1, 2, 3, 4]])
In [62]:
# -1 ~ 1까지 3조각으로 나눠라, 5조각으로 나눠라. j가 없으면 stack으로 인식한다.
np.ogrid[-1:1:3j, -1:1:5j]
Out[62]:
[array([[-1.],
        [ 0.],
        [ 1.]]), array([[-1. , -0.5,  0. ,  0.5,  1. ]])]
In [63]:
# j가 없으면 stack으로 인식한다.
np.ogrid[-1:1:3, -1:1:5]
Out[63]:
[array([[-1]]), array([[-1]])]

np.mgrid

In [64]:
# Broadcasting을 위한 차원 뻥튀기를 해준다.
x, y = np.mgrid[0:3, 0:5]
In [65]:
x
Out[65]:
array([[0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2]])
In [66]:
y
Out[66]:
array([[0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4]])
In [67]:
np.mgrid[-1:1:3j, -1:1:5j]
Out[67]:
array([[[-1. , -1. , -1. , -1. , -1. ],
        [ 0. ,  0. ,  0. ,  0. ,  0. ],
        [ 1. ,  1. ,  1. ,  1. ,  1. ]],

       [[-1. , -0.5,  0. ,  0.5,  1. ],
        [-1. , -0.5,  0. ,  0.5,  1. ],
        [-1. , -0.5,  0. ,  0.5,  1. ]]])
In [68]:
X, Y = np.meshgrid(np.arange(3), np.arange(5))
In [69]:
X
Out[69]:
array([[0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2]])
In [70]:
Y
Out[70]:
array([[0, 0, 0],
       [1, 1, 1],
       [2, 2, 2],
       [3, 3, 3],
       [4, 4, 4]])
In [71]:
# ravel()는 짝 펴주는 메소드, view
## (x,y)로 점을 찍고 싶을 때 사용
a = zip(X.ravel(), Y.ravel())
list(a)
Out[71]:
[(0, 0),
 (1, 0),
 (2, 0),
 (0, 1),
 (1, 1),
 (2, 1),
 (0, 2),
 (1, 2),
 (2, 2),
 (0, 3),
 (1, 3),
 (2, 3),
 (0, 4),
 (1, 4),
 (2, 4)]

중요!!

In [72]:
import matplotlib.pylab as plt
%matplotlib inline

# -1 ~ 1 까지 10개로, -2 ~ 2까지 10개로 → meshgrid로 인해 Broadcasting된 후 각각 10 by 10 array
## (가로, 세로)
a = np.meshgrid(np.linspace(-1,1,10), np.linspace(-2,2,10))

# meshgrid 결과 나오는 두 array를 아래로 쌓음. 
b = np.vstack(a)

# reshape으로 처음 만들었던 array 두 개가 각각 원소가 되는 어레이로 변경함.
c = b.reshape(2,-1)

# array를 list로 변경
d = c.tolist()

# *를 붙여서 d의 list를 x, y로 인식하게 하여 산점도를 그림
plt.scatter(*d)
Out[72]:
<matplotlib.collections.PathCollection at 0x10eadcf28>


Related Posts