(Python 6๊ฐ) numpy
210804
Numpy
Numerical Python
ํ์ด์ฌ์ ๊ณ ์ฑ๋ฅ ๊ณผํ ๊ณ์ฐ์ฉ ํจํค์ง
Matrix์ Vector์ ๊ฐ์ Array์ฐ์ฐ์ ํ์ค
ํ๊ธ๋ก ๋ํ์ด๋ก ์ฃผ๋ก ํต์นญ
ํน์ง
์ผ๋ฐ List์ ๋นํด ๋น ๋ฅด๊ณ ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ ์ด๋ค
๋ฐ๋ณต๋ฌธ ์์ด ๋ฐ์ดํฐ ๋ฐฐ์ด์ ๋ํ ์ฒ๋ฆฌ๋ฅผ ์ง์ํ๋ค
์ ํ๋์์ ๊ด๋ จ๋ ๋ค์ํ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค
C, C++, ํฌํธ๋ ๋ฑ์ ์ธ์ด์ ํตํฉ ๊ฐ๋ฅ
ndarray
numpy์ ํธ์ถ ๋ฐฉ๋ฒ
์ผ๋ฐ์ ์ผ๋ก np๋ผ๋ ๋ณ์นญ ์ด์ฉ
numpy๋ np.array ํจ์๋ฅผ ํ์ฉํด์ ๋ฐฐ์ด์ ์์ฑํ๋๋ฐ ์ด ๋ฐฐ์ด์ ndarray ๋ผ๊ณ ํ๋ค
numpy๋ ํ๋์ ๋ฐ์ดํฐ ํ์ ๋ง ๋ฐฐ์ด์ ๋ฃ์ ์ ์๋ค.
๋ฆฌ์คํธ์์ ์ฐจ์ด์
dynamic typing์ ์ง์ํ์ง ์๋๋ค๊ณ ํ๋ค
C์ Array๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐฐ์ด์ ์์ฑํ๋ค
array creation
ํ์ด์ฌ์ ์์์ ์์น์ ์ ์ฅ๋๋๋ฐ ๋นํด C์ธ์ด๋ ์์๋๋ก ์ ์ฅ๋๋ค.
c์ธ์ด์ ์ง์ญ์ฑ
๋, ํฌ๊ธฐ๊ฐ ๊ณ ์ ๋์ด์๋ค.
๊ทธ๋์, ์๋๊ฐ ๋น ๋ฅธ๊ฒ
shape : ndarr์ dimension ๊ตฌ์ฑ์ ๋ฐํ
array์ ํฌ๊ธฐ, ํํ์ ๋ํ ์ ๋ณด
dtype : ndarrr์ type์ ๋ฐํ
ndim : number of dimensions
size : data ์ ๊ฐ์
nbytes : ndarray object์ ๋ฉ๋ชจ๋ฆฌ ํฌ๊ธฐ๋ฅผ ๋ฐํํจ
int๋ 1byte, float์ 4 bytes
ํ์ด์ฌ์์ float์ 8bytes๊ฐ ๊ธฐ๋ณธ์ด๋ค. ์๋ ๋ํ์ด ๊ธฐ์ค
Handling shape
reshape : Array์ shape์ ํฌ๊ธฐ๋ฅผ ๋ณ๊ฒฝํจ. element์ ๊ฐฏ์๋ ๋์ผ
-1
์ ์์์ ์ปดํจํฐ๊ฐ ๊ณ์ฐํ ์ ์๋ ๋ถ๋ถ์ ์๋ฏธํ๋ค
flatten : ๋ค์ฐจ์ array๋ฅผ 1์ฐจ์ array๋ก ๋ณํ
(2, 2, 4) => (16, )
indexing for numpy array
list์ ๋ฌ๋ฆฌ ์ด์ฐจ์ ๋ฐฐ์ด์์ [0, 0] ํ๊ธฐ๋ฒ์ ์ ๊ณตํ๋ค
a[0, 0] == a[0][0]
๋ ๋ค ๊ฐ๋ฅํ๋ค
๋, list์ ๋ฌ๋ฆฌ ํ๊ณผ ์ด ๋ถ๋ถ์ ๋๋ ์ slicing์ด ๊ฐ๋ฅํ๋ค
Create Functions
arange
array์ ๋ฒ์๋ฅผ ์ง์ ํ์ฌ ๊ฐ์ list๋ฅผ ์์ฑํ๋ ๋ช ๋ น์ด
zeros
0์ผ๋ก ๊ฐ๋์ฐฌ ndarr ์์ฑ
ones
1๋ก ๊ฐ๋์ฐฌ ndarr ์์ฑ
empty
shape๋ง ์ฃผ์ด์ง๊ณ ๋น์ด์๋ ndarr ์์ฑ
memory initialization์ด ๋ ๊ฒ์ ์๋๋ค
์ด๋ฏธ ์กด์ฌํ๋ ๊ฐ์ ์ด์ ์ ์ฌ์ฉํ๋ ์ฐ๋ ๊ธฐ๊ฐ์ด๋ค
somthing_like
๊ธฐ์กด ndarr์ shape ํฌ๊ธฐ๋งํผ 1 ๋๋ 0์ array ๋ฐํ
identity
๋จ์ํ๋ ฌ ์์ฑ
eye
๋๊ฐ์ ์ด 1์ธ ํ๋ ฌ ์์ฑ.
identity์ ๋ค๋ฅธ์ ์ ์์์์น๋ฅผ ์ ํ ์ ์๋ค
np.eye(3, 5, k=2)
๋ฉด 2๋งํผ ์ด๋๋ 3 * 5 ํ๋ ฌ ์์ฑ
diag
๋๊ฐ ํ๋ ฌ์ ๊ฐ์ ์ถ์ถํจ
random sampling
๋ฐ์ดํฐ ๋ถํฌ์ ๋ฐ๋ฅธ sampling์ผ๋ก array๋ฅผ ์์ฑ
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
์ถ์ ํ๋ ๋๋ฆฐ๋ค
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์ ๋ฐ์ดํฐ ์ ๋ถ ๋๋ ์ผ๋ถ๊ฐ ์กฐ๊ฑด์ ๋ง์กฑํ๋์ง์ ๋ํ ์ฌ๋ถ๋ฅผ ๋ฐํํ๋ค
numpy๋ ๋ฐฐ์ด์ ํฌ๊ธฐ๊ฐ ๋์ผํ element๊ฐ ๋น๊ต์ ๊ฒฐ๊ณผ๋ฅผ Boolean type์ผ๋ก ๋ฐํํ๋ค.
where
argmax & argmin
array๋ด ์ต๋๊ฐ ๋๋ ์ต์๊ฐ์ index๋ฅผ ๋ฐํ
๋ํ, axis ๊ธฐ๋ฐ์ ๋ฐํ์ ํ ์ ์๋ค
boolean index
ํน์ ์กฐ๊ฑด์ ๋ฐ๋ฅธ ๊ฐ์ ๋ฐฐ์ด ํํ๋ก ์ถ์ถํ๋ค.
fancy index
numpy array๋ฅผ index value๋ก ์ฌ์ฉํด์ ๊ฐ์ ์ถ์ถํ๋ค. ์ด ๋ ์ธ๋ฑ์ค๋ก ์ฌ์ฉ๋๋ ๋ฐฐ์ด์ ๋ฐ๋์ ์ ์๋ก ์ ์ธ๋์ด์ผ ํ๋ค.
matrixํํ๋ ๊ฐ๋ฅํ๋ค
a[b][c]
Last updated
Was this helpful?