Numpy 와 Matplotlib

numpy 복습

a = np.array([1, 2, 3, 4])
a
array([1, 2, 3, 4])
a.dtype
dtype('int64')
a.shape
(4,)
b = np.array([[1, 2, 3, 4],[5, 6, 7, 8]])
b
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])
b.shape
(2, 4)
c = np.array([1, 2, 3.14, 4])
c
array([1.  , 2.  , 3.14, 4.  ])
c.dtype
dtype('float64')
d = np.array([1, 2, 3, 4], dtype=np.float32)
d
array([1., 2., 3., 4.], dtype=float32)

66~69 실행





시퀀스와 난수로 생성

a = np.arange(5)
a
array([0, 1, 2, 3, 4])
a.dtype
dtype('int64')
a.shape
(5,)
b = np.arange(5.0)
b
array([0., 1., 2., 3., 4.])
b.dtype
dtype('float64')
c = np.arange(3, 9, 2)
c
array([3, 5, 7])
np.random.rand()
0.02285858057022161
np.random.randn()
0.48078871515121746
a=np.random.rand(2,3)
a
array([[0.38832821, 0.33165737, 0.14226445],
       [0.16790181, 0.16539196, 0.12832403]])
b=np.random.randn(2,3)
b
array([[-0.13559487,  0.39575157,  0.62658166],
       [ 0.14630926, -0.60472903, -1.59230845]])

dtype 변경

a=np.arange(5)
a
array([0, 1, 2, 3, 4])
a.dtype
dtype('int64')
b = a.astype('float32')
b
array([0., 1., 2., 3., 4.], dtype=float32)

차원 변경

a = np.arange(6)
a
array([0, 1, 2, 3, 4, 5])
b = a.reshape(2,3)
b
array([[0, 1, 2],
       [3, 4, 5]])
c = np.reshape(a, (2,3))
c
array([[0, 1, 2],
       [3, 4, 5]])
d = np.arange(100).reshape(2, -1)
d
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
        16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
        32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
        48, 49],
       [50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
        66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
        82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
        98, 99]])
e = np.arange(100).reshape(-1,5)
e
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24],
       [25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34],
       [35, 36, 37, 38, 39],
       [40, 41, 42, 43, 44],
       [45, 46, 47, 48, 49],
       [50, 51, 52, 53, 54],
       [55, 56, 57, 58, 59],
       [60, 61, 62, 63, 64],
       [65, 66, 67, 68, 69],
       [70, 71, 72, 73, 74],
       [75, 76, 77, 78, 79],
       [80, 81, 82, 83, 84],
       [85, 86, 87, 88, 89],
       [90, 91, 92, 93, 94],
       [95, 96, 97, 98, 99]])
e.shape
(20, 5)
f = np.zeros((2,3))
f
array([[0., 0., 0.],
       [0., 0., 0.]])
f.reshape((6,))
array([0., 0., 0., 0., 0., 0.])
f.reshape(-1)
array([0., 0., 0., 0., 0., 0.])
np.ravel(f)
array([0., 0., 0., 0., 0., 0.])
g=np.arange(10).reshape(2,-1)
g
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
g.T
array([[0, 5],
       [1, 6],
       [2, 7],
       [3, 8],
       [4, 9]])

브로드캐스팅 연산

a = np.ones((2,3))
a
array([[1., 1., 1.],
       [1., 1., 1.]])
c = np.arange(3)
c
array([0, 1, 2])
a+c
array([[1., 2., 3.],
       [1., 2., 3.]])

인덱싱과 슬라이싱


팬시 인덱싱

a = np.arange(5)
a
array([0, 1, 2, 3, 4])
a[[1,3]]
array([1, 3])
a[[True, False, True, False, True]]
array([0, 2, 4])
a=np.arange(10)
a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
b = a>5
b
array([False, False, False, False, False, False,  True,  True,  True,
        True])
a[b]
array([6, 7, 8, 9])
a[a>5]
array([6, 7, 8, 9])
a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
b=np.arange(12).reshape(3,4)
b
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
b[[0,2]]
array([[ 0,  1,  2,  3],
       [ 8,  9, 10, 11]])
b[[0,2],[2,3]]
array([ 2, 11])

병합과 분리

a = np.arange(4).reshape(2,2)
a
array([[0, 1],
       [2, 3]])
b=np.arange(10,14).reshape(2,2)
b
array([[10, 11],
       [12, 13]])
np.vstack((a,b))
array([[ 0,  1],
       [ 2,  3],
       [10, 11],
       [12, 13]])
np.hstack((a,b))
array([[ 0,  1, 10, 11],
       [ 2,  3, 12, 13]])
np.concatenate((a,b),0)
array([[ 0,  1],
       [ 2,  3],
       [10, 11],
       [12, 13]])
np.concatenate((a,b),1)
array([[ 0,  1, 10, 11],
       [ 2,  3, 12, 13]])
a=np.arange(12).reshape(4,3)
a
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
b=np.arange(10,130,10).reshape(4,3)
b
array([[ 10,  20,  30],
       [ 40,  50,  60],
       [ 70,  80,  90],
       [100, 110, 120]])
c=np.stack((a,b),0)
c
array([[[  0,   1,   2],
        [  3,   4,   5],
        [  6,   7,   8],
        [  9,  10,  11]],

       [[ 10,  20,  30],
        [ 40,  50,  60],
        [ 70,  80,  90],
        [100, 110, 120]]])
d=np.stack((a,b),1)
d
array([[[  0,   1,   2],
        [ 10,  20,  30]],

       [[  3,   4,   5],
        [ 40,  50,  60]],

       [[  6,   7,   8],
        [ 70,  80,  90]],

       [[  9,  10,  11],
        [100, 110, 120]]])
e=np.stack((a,b),2)
e
array([[[  0,  10],
        [  1,  20],
        [  2,  30]],

       [[  3,  40],
        [  4,  50],
        [  5,  60]],

       [[  6,  70],
        [  7,  80],
        [  8,  90]],

       [[  9, 100],
        [ 10, 110],
        [ 11, 120]]])
ee=np.stack((a,b),-1)
ee
array([[[  0,  10],
        [  1,  20],
        [  2,  30]],

       [[  3,  40],
        [  4,  50],
        [  5,  60]],

       [[  6,  70],
        [  7,  80],
        [  8,  90]],

       [[  9, 100],
        [ 10, 110],
        [ 11, 120]]])
a=np.arange(12)
a
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
np.hsplit(a,3)
[array([0, 1, 2, 3]), array([4, 5, 6, 7]), array([ 8,  9, 10, 11])]
np.hsplit(a,[3,6])
[array([0, 1, 2]), array([3, 4, 5]), array([ 6,  7,  8,  9, 10, 11])]
np.hsplit(a,[3,6,9])
[array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8]), array([ 9, 10, 11])]
np.split(a,3,0)
[array([0, 1, 2, 3]), array([4, 5, 6, 7]), array([ 8,  9, 10, 11])]
np.split(a,[3,6,9],0)
[array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8]), array([ 9, 10, 11])]
b=np.arange(12).reshape(4,3)
b
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
np.vsplit(b,2)
[array([[0, 1, 2],
        [3, 4, 5]]),
 array([[ 6,  7,  8],
        [ 9, 10, 11]])]
np.split(b,2,0)
[array([[0, 1, 2],
        [3, 4, 5]]),
 array([[ 6,  7,  8],
        [ 9, 10, 11]])]
np.hsplit(b,[1])
[array([[0],
        [3],
        [6],
        [9]]),
 array([[ 1,  2],
        [ 4,  5],
        [ 7,  8],
        [10, 11]])]
np.split(b,[1],1)
[array([[0],
        [3],
        [6],
        [9]]),
 array([[ 1,  2],
        [ 4,  5],
        [ 7,  8],
        [10, 11]])]

검색

a=np.arange(10,20)
a
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
np.where(a>15)
(array([6, 7, 8, 9]),)
np.where(a>15, 1,0)
array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1])
a
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
np.where(a>15, 99, a)
array([10, 11, 12, 13, 14, 15, 99, 99, 99, 99])
np.where(a>15,a,0)
array([ 0,  0,  0,  0,  0,  0, 16, 17, 18, 19])
a
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
b=np.arange(12).reshape(3,4)
b
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
coords = np.where(b>6)
coords
(array([1, 2, 2, 2, 2]), array([3, 0, 1, 2, 3]))
np.stack((coords[0],coords[1]),-1)
array([[1, 3],
       [2, 0],
       [2, 1],
       [2, 2],
       [2, 3]])
z = np.array([0,1,2,0,1,2])
np.nonzero(z)
(array([1, 2, 4, 5]),)
zz = np.array([[0,1,2], [1,2,0], [2,0,1]])
zz
array([[0, 1, 2],
       [1, 2, 0],
       [2, 0, 1]])
coords = np.nonzero(zz)
coords
(array([0, 0, 1, 1, 2, 2]), array([1, 2, 0, 1, 0, 2]))
np.stack((coords[0], coords[1]),-1)
array([[0, 1],
       [0, 2],
       [1, 0],
       [1, 1],
       [2, 0],
       [2, 2]])
a
array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
np.nonzero(a>15)
(array([6, 7, 8, 9]),)
np.where(a>15)
(array([6, 7, 8, 9]),)
b
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
np.nonzero(b>6)
(array([1, 2, 2, 2, 2]), array([3, 0, 1, 2, 3]))
np.where(b>6)
(array([1, 2, 2, 2, 2]), array([3, 0, 1, 2, 3]))
t=np.array([True, True, True])
np.all(t)
True
t[1]=False
t
array([ True, False,  True])
np.all(t)
False
tt=np.array([[True, True], [False, True], [True, True]])
tt
array([[ True,  True],
       [False,  True],
       [ True,  True]])
np.all(tt,0)
array([False,  True])
np.all(tt,1)
array([ True, False,  True])
a=np.arange(10)
a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
b=np.arange(10)
b
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
a==b
array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
        True])
np.all(a==b)
True
b[5] = -1
np.all(a==b)
False
np.where(a==b)
(array([0, 1, 2, 3, 4, 6, 7, 8, 9]),)
np.where(a!=b)
(array([5]),)

기초 통계 함수




이미지 생성

import cv2
import numpy as np

img = np.zeros((120,120), dtype=np.uint8)
img[25:35, :] = 45
img[55:65, :] = 115
img[85:95, :] = 160
img[:, 34:45] = 205
img[:, 75:85] = 255
cv2.imshow('Gray', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

이미지읽기

import cv2
import numpy as np

img = np.zeros((120,120,3), dtype=np.uint8)
img[25:35, :] = [255,0,0]
img[55:65, :] = [0,255,0]
img[85:95, :] = [0,0,255]
img[:, 34:45] = [255,255,0]
img[:, 75:85] = [255,0,255]
cv2.imshow('BGR', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

이미지읽기

subplot

import matplotlib.pyplot as plt
import numpy as np

x=np.arange(10)

plt.subplot(2,2,1)
plt.plot(x, x**2)

plt.subplot(2,2,2)
plt.plot(x,x*5)

plt.subplot(223)
plt.plot(x, np.sin(x))

plt.subplot(224)
plt.plot(x,np.cos(x))

plt.show()

이미지읽기

이미지 표시

import cv2
from matplotlib import pyplot as plt

img = cv2.imread('./img/girl.jpg')

plt.imshow(img)
plt.show()

이미지읽기

img1 = cv2.imread('./img/model.jpg')
img2 = cv2.imread('./img/model2.jpg')
img3 = cv2.imread('./img/model3.jpg')

plt.subplot(1, 3, 1)
plt.imshow(img1[:,:,(2,1,0)])   # 색 순서 변환(matplot에서만)
plt.xticks([])    # x축 y축 제거
plt.yticks([])

plt.subplot(1, 3, 2)
plt.imshow(img2[:,:,::-1])
plt.xticks([])
plt.yticks([])

plt.subplot(1, 3, 3)
plt.imshow(img3[:,:,::-1])
plt.xticks([])
plt.yticks([])

plt.show()

이미지읽기