(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?