(Python 6๊ฐ•) numpy

210804

Numpy

  • Numerical Python

  • ํŒŒ์ด์ฌ์˜ ๊ณ ์„ฑ๋Šฅ ๊ณผํ•™ ๊ณ„์‚ฐ์šฉ ํŒจํ‚ค์ง€

  • Matrix์™€ Vector์™€ ๊ฐ™์€ Array์—ฐ์‚ฐ์˜ ํ‘œ์ค€

  • ํ•œ๊ธ€๋กœ ๋„˜ํŒŒ์ด๋กœ ์ฃผ๋กœ ํ†ต์นญ

ํŠน์ง•

  • ์ผ๋ฐ˜ List์— ๋น„ํ•ด ๋น ๋ฅด๊ณ  ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์ ์ด๋‹ค

  • ๋ฐ˜๋ณต๋ฌธ ์—†์ด ๋ฐ์ดํ„ฐ ๋ฐฐ์—ด์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ์ง€์›ํ•œ๋‹ค

  • ์„ ํ˜•๋Œ€์ˆ˜์™€ ๊ด€๋ จ๋œ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค

  • C, C++, ํฌํŠธ๋ž€ ๋“ฑ์˜ ์–ธ์–ด์™€ ํ†ตํ•ฉ ๊ฐ€๋Šฅ

ndarray

import numpy as np
  • numpy์˜ ํ˜ธ์ถœ ๋ฐฉ๋ฒ•

  • ์ผ๋ฐ˜์ ์œผ๋กœ np๋ผ๋Š” ๋ณ„์นญ ์ด์šฉ

test_array = np.array([1, 4, 5, 8], float)
print(test_array)
type(test_array[3])
  • numpy๋Š” np.array ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•ด์„œ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๋Š”๋ฐ ์ด ๋ฐฐ์—ด์„ ndarray ๋ผ๊ณ  ํ•œ๋‹ค

  • numpy๋Š” ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…๋งŒ ๋ฐฐ์—ด์— ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.

    • ๋ฆฌ์ŠคํŠธ์™€์˜ ์ฐจ์ด์ 

    • dynamic typing์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•œ๋‹ค

  • C์˜ Array๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ๋‹ค

array creation

  • ํŒŒ์ด์ฌ์€ ์ž„์˜์˜ ์œ„์น˜์— ์ €์žฅ๋˜๋Š”๋ฐ ๋น„ํ•ด C์–ธ์–ด๋Š” ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅ๋œ๋‹ค.

    • c์–ธ์–ด์˜ ์ง€์—ญ์„ฑ

  • ๋˜, ํฌ๊ธฐ๊ฐ€ ๊ณ ์ •๋˜์–ด์žˆ๋‹ค.

  • ๊ทธ๋ž˜์„œ, ์†๋„๊ฐ€ ๋น ๋ฅธ๊ฒƒ

test_array = np.array([1, 4, 5, "8"], float) # String Type์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•ด๋„

print(test_array)
array([ 1., 4., 5., 8.])

print(type(test_array[3])) # Float Type์œผ๋กœ ์ž๋™ ํ˜•๋ณ€ํ™˜์„ ์‹ค์‹œ
numpy.float64

print(test_array.dtype) # Array(๋ฐฐ์—ด) ์ „์ฒด์˜ ๋ฐ์ดํ„ฐ Type์„ ๋ฐ˜ํ™˜ํ•จ
dtype('float64')

print(test_array.shape)
(4,)
  • shape : ndarr์˜ dimension ๊ตฌ์„ฑ์„ ๋ฐ˜ํ™˜

    • array์˜ ํฌ๊ธฐ, ํ˜•ํƒœ์— ๋Œ€ํ•œ ์ •๋ณด

  • dtype : ndarrr์˜ type์„ ๋ฐ˜ํ™˜

test_array = np.array([1, 4, 5, "8"], float) # String Type์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•ด๋„

test_array.ndim
1

test_array.size
4

test_array.nbytes
16
  • ndim : number of dimensions

  • size : data ์˜ ๊ฐœ์ˆ˜

  • nbytes : ndarray object์˜ ๋ฉ”๋ชจ๋ฆฌ ํฌ๊ธฐ๋ฅผ ๋ฐ˜ํ™˜ํ•จ

    • int๋Š” 1byte, float์€ 4 bytes

    • ํŒŒ์ด์ฌ์—์„œ float์€ 8bytes๊ฐ€ ๊ธฐ๋ณธ์ด๋‹ค. ์œ„๋Š” ๋„˜ํŒŒ์ด ๊ธฐ์ค€

Handling shape

reshape : Array์˜ shape์˜ ํฌ๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•จ. element์˜ ๊ฐฏ์ˆ˜๋Š” ๋™์ผ

test_matrix = [[1,2,3,4], [5,6,7,8]]
np.array(test_matrix).shape
>>> (2, 4)

np.array(test_matrix).reshape(8, )
>>> array([1,2,3,4,5,6,7,8])

np.array(test_matrix).reshape(-1, 2)
>>> array([[1, 2], [3, 4], [5, 6], [7, 8]]
  • -1 ์€ ์•Œ์•„์„œ ์ปดํ“จํ„ฐ๊ฐ€ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„์„ ์˜๋ฏธํ•œ๋‹ค

flatten : ๋‹ค์ฐจ์› array๋ฅผ 1์ฐจ์› array๋กœ ๋ณ€ํ™˜

  • (2, 2, 4) => (16, )

indexing for numpy array

  • list์™€ ๋‹ฌ๋ฆฌ ์ด์ฐจ์› ๋ฐฐ์—ด์—์„œ [0, 0] ํ‘œ๊ธฐ๋ฒ•์„ ์ œ๊ณตํ•œ๋‹ค

    • a[0, 0] == a[0][0]

    • ๋‘˜ ๋‹ค ๊ฐ€๋Šฅํ•˜๋‹ค

  • ๋˜, list์™€ ๋‹ฌ๋ฆฌ ํ–‰๊ณผ ์—ด ๋ถ€๋ถ„์„ ๋‚˜๋ˆ ์„œ slicing์ด ๊ฐ€๋Šฅํ•˜๋‹ค

a = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], int)

a[:, 2:]
>>> array([[3, 4, 5], [8, 9, 10]])

a[1, 1:3]
>>> array([7, 8])

Create Functions

arange

  • array์˜ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜์—ฌ ๊ฐ’์˜ list๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ช…๋ น์–ด

np.arrange(30)
>>> 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])

#์‹œ์ž‘, ๋, step => ์ด ๋•Œ step์€ ์ •์ˆ˜ํ˜•์ผ ํ•„์š”๋Š” ์—†์Œ
np.arange(0, 5, 0.5)
>>> array([0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5])

np.arange(30).reshape(5, 6)
>>> 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]])

zeros

  • 0์œผ๋กœ ๊ฐ€๋“์ฐฌ ndarr ์ƒ์„ฑ

>>> np.zeros((2, 5), int)
array([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]])
       
>>> np. zeros((3, ), float)
array([0., 0., 0.])

ones

  • 1๋กœ ๊ฐ€๋“์ฐฌ ndarr ์ƒ์„ฑ

>>> np.ones((1, 1, 1), int)
array([[[1]]])
>>> np.ones((2, 3, 2, 3), int)
array([[[[1, 1, 1],
         [1, 1, 1]],

        [[1, 1, 1],
         [1, 1, 1]],

        [[1, 1, 1],
         [1, 1, 1]]],


       [[[1, 1, 1],
         [1, 1, 1]],

        [[1, 1, 1],
         [1, 1, 1]],

        [[1, 1, 1],
         [1, 1, 1]]]])

empty

  • shape๋งŒ ์ฃผ์–ด์ง€๊ณ  ๋น„์–ด์žˆ๋Š” ndarr ์ƒ์„ฑ

    • memory initialization์ด ๋œ ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค

    • ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๊ฐ’์€ ์ด์ „์— ์‚ฌ์šฉํ•˜๋˜ ์“ฐ๋ ˆ๊ธฐ๊ฐ’์ด๋‹ค

>>> np.empty((3, 5))
array([[2.12199579e-314, 6.36598737e-314, 1.06099790e-313,
        1.48539705e-313, 1.90979621e-313],
       [2.33419537e-313, 2.75859453e-313, 3.18299369e-313,
        3.60739285e-313, 4.03179200e-313],
       [4.45619116e-313, 4.88059032e-313, 5.30498948e-313,
        5.72938864e-313, 6.15378780e-313]])

somthing_like

  • ๊ธฐ์กด ndarr์˜ shape ํฌ๊ธฐ๋งŒํผ 1 ๋˜๋Š” 0์˜ array ๋ฐ˜ํ™˜

>>> test = np.arange(12).reshape(3, 4)
>>> test
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
       
>>> np.zeros_like(test)
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])
>>> np.ones_like(test)
array([[1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1]])

identity

๋‹จ์œ„ํ–‰๋ ฌ ์ƒ์„ฑ

>>> np.identity(4, dtype=np.float32)
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])

eye

๋Œ€๊ฐ์„ ์ด 1์ธ ํ–‰๋ ฌ ์ƒ์„ฑ.

  • identity์™€ ๋‹ค๋ฅธ์ ์€ ์‹œ์ž‘์œ„์น˜๋ฅผ ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค

  • np.eye(3, 5, k=2) ๋ฉด 2๋งŒํผ ์ด๋™๋œ 3 * 5 ํ–‰๋ ฌ ์ƒ์„ฑ

>>> np.eye(3, 5, k=2)
array([[0., 0., 1., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.]])

diag

๋Œ€๊ฐ ํ–‰๋ ฌ์˜ ๊ฐ’์„ ์ถ”์ถœํ•จ

>>> test
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> np.diag(test)
array([ 0,  5, 10])
>>> np.diag(test, k=2)
array([2, 7])

random sampling

๋ฐ์ดํ„ฐ ๋ถ„ํฌ์— ๋”ฐ๋ฅธ sampling์œผ๋กœ array๋ฅผ ์ƒ์„ฑ

# ์‹œ์ž‘, ๋, ๊ฐœ์ˆ˜
>>> np.random.uniform(0, 1, 10)
array([0.46238198, 0.10681686, 0.0266183 , 0.60596315, 0.41614435,
       0.99164157, 0.4245724 , 0.87679971, 0.37257856, 0.31018757])

# ๊ท ๋“ฑ ๋ถ„ํฌ
>>> np.random.uniform(0, 1, 10).reshape(2, 5)
array([[0.16017842, 0.70721719, 0.79159583, 0.95743024, 0.77892252],
       [0.38001109, 0.21918138, 0.59308826, 0.56590121, 0.31633467]])

# ์ •๊ทœ ๋ถ„ํฌ
>>> np.random.normal(0, 1, 10).reshape(2, 5)
array([[-0.15451055,  0.35729475,  0.07026103, -0.68009187, -0.68631985],
       [ 0.37181644, -0.92405456,  0.50774203,  0.87155016,  1.48159822]])

Operation functions

sum

element๊ฐ„์˜ ํ•ฉ

axis

๋ชจ๋“  operation function์„ ์‹คํ–‰ํ•  ๋•Œ ๊ธฐ์ค€์ด ๋˜๋Š” dimension ์ถ•์ด๋‹ค.

mathematical functions

๋‹ค์–‘ํ•œ ์ˆ˜ํ•™ ์—ฐ์‚ฐ์ž

  • np.exp

  • np.sqrt

  • np.mean

  • np.std

Concatenate

vstack

  • numpy array๋ฅผ ์„ธ๋กœ๋กœ ๋ถ™์ž„

hstack

  • numpy array๋ฅผ ๊ฐ€๋กœ๋กœ ๋ถ™์ž„

concatenate

  • axis = 0 : vstack๊ณผ ๋™์ผ

  • axis = 1: hstack๊ณผ ๋™์ผ

newaxis

  • ์ถ•์„ ํ•˜๋‚˜ ๋Š˜๋ฆฐ๋‹ค

b = np.array([5, 6])
b = b[np.newaxis, :]
b
>>> array([[5, 6]])

Opertaions b/t arrays

  • ๊ธฐ๋ณธ์ ์œผ๋กœ numpy array๊ฐ„์˜ ๊ธฐ๋ณธ์ ์ธ ์‚ฌ์น™ ์—ฐ์‚ฐ์„ ์ง€์›ํ•œ๋‹ค

    • ์ด ๋•Œ element-wise operation์œผ๋กœ ์—ฐ์‚ฐ๋œ๋‹ค

dot product

  • ๋‚ด์  ํ•จ์ˆ˜

  • np.array.dot(np.array) ๊ผด๋กœ ์‚ฌ์šฉ

transpose

  • ์ „์น˜ ํ•จ์ˆ˜

  • np.array.T ์˜ ๊ผด๋กœ ์‚ฌ์šฉ

broadcasting

  • shape์ด ๋‹ค๋ฅธ ๋ฐฐ์—ด ๊ฐ„ ์—ฐ์‚ฐ์„ ์ง€์›ํ•˜๋Š” ๊ธฐ๋Šฅ

  • scalar - vector ์™€ vector - matrix ๊ฐ„์— ์ง€์›ํ•œ๋‹ค

timeit

  • jupyter ํ™˜๊ฒฝ์—์„œ ์ฝ”๋“œ์˜ ํผํฌ๋จผ์Šค๋ฅผ ์ฒดํฌํ•˜๋Š” ํ•จ์ˆ˜

  • ์ผ๋ฐ˜์ ์œผ๋กœ ์†๋„๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค

    • numpy > list comprehension > for loop

Comparisons

All & Any

Array์˜ ๋ฐ์ดํ„ฐ ์ „๋ถ€ ๋˜๋Š” ์ผ๋ถ€๊ฐ€ ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค

a = np.arange(10)
a
>>> array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

np.any(a>5), np.any(a<0)
>>> (True, False)

np.all(a>5), np.all(a<10)
>>> (False, True)

numpy๋Š” ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ๋™์ผํ•œ element๊ฐ„ ๋น„๊ต์˜ ๊ฒฐ๊ณผ๋ฅผ Boolean type์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

test_a = np.array([1, 3, 0], float)
test_b = np.array([5, 2, 1], float)
test_a > test_b
>>> array([False, True, False], dtype=bool)

where

a = np.arange(10)
np.where(a>5)
>>> (array([6, 7, 8, 9], dtype=int64),)

a = np.array([1, np.NaN, np.Inf], float)
np.isnan(a)
>>> array([False,  True, False])

np.isfinite(a)
>>> array([ True, False, False])

argmax & argmin

array๋‚ด ์ตœ๋Œ€๊ฐ’ ๋˜๋Š” ์ตœ์†Œ๊ฐ’์˜ index๋ฅผ ๋ฐ˜ํ™˜

๋˜ํ•œ, axis ๊ธฐ๋ฐ˜์˜ ๋ฐ˜ํ™˜์„ ํ•  ์ˆ˜ ์žˆ๋‹ค

a = np.arange(0, 20, 3)
a
>>> array([ 0,  3,  6,  9, 12, 15, 18])

np.argmax(a), np.argmin(a)
>>> (6, 0)

 a = np.array([[1,2,4,7],[9,88,6,45],[9,76,3,4]])
np.argmax(a, axis=1), np.argmax(a, axis=0)
>>> (array([3, 1, 1], dtype=int64), array([1, 1, 1, 1], dtype=int64))
np.argmin(a, axis=1), np.argmin(a, axis=0)
>>> (array([0, 2, 2], dtype=int64), array([0, 0, 2, 2], dtype=int64))

boolean index

ํŠน์ • ์กฐ๊ฑด์— ๋”ฐ๋ฅธ ๊ฐ’์„ ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ์ถ”์ถœํ•œ๋‹ค.

arr > 3
>>> array([False,  True, False, False, False,  True,  True,  True])

arr[arr > 3]
>>> array([4., 8., 9., 7.])

fancy index

numpy array๋ฅผ index value๋กœ ์‚ฌ์šฉํ•ด์„œ ๊ฐ’์„ ์ถ”์ถœํ•œ๋‹ค. ์ด ๋•Œ ์ธ๋ฑ์Šค๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฐ์—ด์€ ๋ฐ˜๋“œ์‹œ ์ •์ˆ˜๋กœ ์„ ์–ธ๋˜์–ด์•ผ ํ•œ๋‹ค.

a = np.array([2, 4, 6, 8], float)
b = np.array([0, 0, 1, 3, 2, 1], int)
a[b]
>>> array([2., 2., 4., 8., 6., 4.])
  • matrixํ˜•ํƒœ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค

    • a[b][c]

Last updated

Was this helpful?