* ์ด ๊ธ์ ๋ค์ด๋ฒ ๋ถ์คํธ ์ฝ์ค์ ์ธ๊ณต์ง๋ฅ(AI) ๊ธฐ์ด ๋ค์ง๊ธฐ ๊ฐ์๋ฅผ ์๊ฐํ๋ฉฐ ์ ๋ฆฌํ ๊ธ์ ๋๋ค.
0. numpy๋?
ํ์ด์ฌ์ ๊ณ ์ฑ๋ฅ ๊ณผํ ๊ณ์ฐ์ฉ ํจํค์ง
์ผ๋ฐ ๋ฆฌ์คํธ์ ๋นํด ๋น ๋ฅด๊ณ , ๋ฉ๋ชจ๋ฆฌ ํจ์จ์
1. numpy์ ๊ธฐ๋ณธ์ ๋จ์
ndarray
2. ndarray creation
- array
import numpy as np
np.array(๋ฆฌ์คํธ, ํ์
)
Dynamic typing not supported
* python์ ๊ฐ์ฅ ํฐ ํน์ง์ค ํ๋์ธ dynamic typing(์คํ๋ ๋ ๋ฐ์ดํฐ type์ ์ธ์ํ๋ฏ๋ก ๋ค์ํ ๋ฐ์ดํฐ ํ์ ์ ์ง์)์ ์ง์ํ์ง ์๊ณ , ํ๋์ ๋ฐ์ดํฐ type๋ง ๋ฐฐ์ด์ ๋ฃ์ ์ ์๋ค.
- arrange
np.arange(start, end, step)
or
np.arange(๋ฒ์)
reshape์ ํจ๊ป ์ฌ์ฉํ ์ ์๋ค.
- zeros : 0์ผ๋ก ๊ฐ๋์ฐฌ ndarray ์์ฑ
np.zeros(shape, dtype, order)
- ones : 1๋ก ๊ฐ๋์ฐฌ ndarray ์์ฑ
np.ones(shape, dtype, order)
- empty : shape๋ง ์ฃผ์ด์ง๊ณ ๋น์ด์๋ ndarray ์์ฑ(์ด๊ธฐํ ๋์ง ์๊ณ ์ฐ๋ ๊ธฐ๊ฐ์ด ์ฝ์ )
np.empty(shape, dtype)
- something_like : ๊ธฐ์กด ndarray์ shape ํฌ๊ธฐ ๋งํผ 0, 1, ๋๋ ๋น array ๋ฐํ
- zeros_like
- ones_like
- empty_like
np.somethings_like(ndarray)
- identity : ๋จ์ ํ๋ ฌ์ ์์ฑ
np.identity(n=์ซ์, dtype)
or
np.identity(N=row์ซ์, M=col์ซ์, dtype)
- eye : ๋๊ฐ์ ์ด 1์ธ ํ๋ ฌ์ ์์ฑ
np.eye(N=row์ซ์, M=col์ซ์, dtype, k=๋๊ฐ์ ์์index)
- ramdom sampling : ๋ฐ์ดํฐ ๋ถํฌ์ ๋ฐ๋ฅธ sampling์ผ๋ก array๋ฅผ ์์ฑ
- ๊ท ๋ฑ๋ถํฌ
- ์ ๊ท๋ถํฌ
#๊ท ๋ฑ๋ถํฌ
np.random.uniform(start, end, ๊ฐฏ์)
#์ ๊ท๋ถํฌ
np.random.normal(start, end, ๊ฐฏ์)
- ndarray ๊ธฐ๋ณธ์ ๋ณด
- axis : ์ถ (์๋ก ์๊ธฐ๋ ์ถ์ 0 ๊ฐ ๋ถ์ฌ)
- dtype : ๋ฐฐ์ด ์ ์ฒด์ ๋ฐ์ดํฐ Type์ ๋ฐํ
- shape : ๋ฐฐ์ด์ dimension ๊ตฌ์ฑ์ ๋ฐํ
- vector (b, )
- matrix (a, b)
- 3rd order tensor (depth, row, col)
- ndim : number of dimension
- size : ์ ์ฒด ํฌ๊ธฐ
- nbytes : ๋ฉ๋ชจ๋ฆฌ์ ํฌ๊ธฐ๋ฅผ ๋ฐํ
3. shape handling
๋ฐ์ดํฐ์ ๊ฐ์๋ ์ ์งํ๋, ํํ๋ง ๋ฐ๊พธ๋ ๊ฒ
- reshape : ๋ฐ์ดํฐ์ ๊ฐ์๋ ์ ์งํ๋, ํํ๋ง ๋ฐ๊ฟ์ค
- -1์ด ๊ฐ์ผ๋ก ๋ค์ด๊ฐ ๊ฒฝ์ฐ ๋ค๋ฅธ ๊ฐ์ ๊ธฐ๋ฐ์ผ๋ก ํด๋น ์์ฑ์ ์ฑ์์ค
- flatten : ๋ค์ฐจ์ array๋ฅผ ์ผ์ฐจ์ array๋ก ๋ณํ์์ผ์ค
4. ๊ฐ ์ผ๋ถ ์ถ์ถํ๊ธฐ
- Indexing
ndarray[row, col]
or
ndarray[row][col]
- Slicing : ํ๊ณผ ์ด ๋ถ๋ถ์ ๋๋ ์ ์ฌ๋ผ์ด์ฑ์ ํ ์ ์์
ndarray[row_slicing, col_slicing]
slicing์ ๊ฒฝ์ฐ [start:end:step] ํํ๋ก ํ์
- diag : ๋๊ฐ ํ๋ ฌ์ ๊ฐ์ ์ถ์ถ
np.diag(ndarray, k=start_index)
5. ํจ์์ฐ์ฐ
- sum : ์์๋ค์ ํฉ์ ๊ตฌํ๋ ํจ์
ndarray.sum(dtype=๋ฐ์ดํฐํ์
)
- mean : ์์๋ค์ ํ๊ท ์ ๊ตฌํ๋ ํจ์
- std : ์์๋ค์ ํ์คํธ์ฐจ๋ฅผ ๊ตฌํ๋ ํจ์
- ๊ทธ ์ธ์ ๋ค์ํ ์ํ ์ฐ์ฐ์๋ฅผ ์ ๊ณต (exponential, trigonometric, hyperbolic ๋ฑ)
- ๋ณํฉ : ndarray๋ฅผ ํฉ์น๋ ํจ์
- vstack : row ๊ธฐ์ค ๋ณํฉ
- hstack : col ๊ธฐ์ค ๋ณํฉ
- concatenate
np.vstack((ndarray1, ndarray2))
or
np.hstack((ndarray1, ndarray2))
or
np.concatenate((ndarray1, ndarray2), axis=์ซ์)
6. ๊ธฐ๋ณธ์ฐ์ฐ
- transpose : ์ ์นํ๋ ฌ
- broadcasting : shape์ด ๋ค๋ฅธ ๋ฐฐ์ด ๊ฐ ์ฐ์ฐ์ ์ง์ํ๋ ๊ธฐ๋ฅ
- matrix X scalar(์ซ์)
- matrix X vector
- ์ฌ์น์ฐ์ฐ : ๊ฐ์ ์์น์ ์๋ ๊ฐ๋ค๋ผ๋ฆฌ ์ฐ์ฐ
- +, -, *
- dot : ํ๋ ฌ๊ณฑ ์ฐ์ฐ
ndarray1.dot(ndarray2)
7. ๋น๊ต์ฐ์ฐ
- all : and ์ฐ์ฐ๊ณผ ๋์ผ
np.all(๋น๊ต์ฐ์ฐ)
- any : or ์ฐ์ฐ๊ณผ ๋์ผ
np.any(๋น๊ต์ฐ์ฐ)
- ndarray๊ฐ ๋น๊ต์ฐ์ฐ : ๊ฐ element๊ฐ ๋น๊ต์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ
ndarray1 ๋น๊ต์ฐ์ฐ ndarray2
- logical_and
- logical_not : boolean์ ๋ฐ๋๋ก ๋ฐํ
- logical_or
- np.where : ๋ฐํ๊ฐ์ ์ง์ ํ์ง ์์ผ๋ฉด ์ธ๋ฑ์ค๊ฐ์ด ๋ฐํ๋จ
np.where(์กฐ๊ฑด, True์ผ ๊ฒฝ์ฐ ๋ฐํ๊ฐ, False์ผ ๊ฒฝ์ฐ ๋ฐํ๊ฐ)
- isnan : nan ๊ฐ์ธ์ง boolean์ผ๋ก ๋ฐํ
- isfinite : ์ ํํ ์์ธ์ง boolean์ผ๋ก ๋ฐํ
- argmax : ์ต๋๊ฐ์ ์ธ๋ฑ์ค๋ฅผ ๋ฐํ (axis๋ฅผ ์ง์ ํ ์ ์์)
- argmin : ์ต์๊ฐ์ ์ธ๋ฑ์ค๋ฅผ ๋ฐํ (axis๋ฅผ ์ง์ ํ ์ ์์)
8. boolean / fancy index
- Boolean Index : ํน์ ์กฐ๊ฑด์ ๋ฐ๋ฅธ ๊ฐ์ ๋ฐฐ์ด ํํ๋ก ์ถ์ถ
ndarray[์กฐ๊ฑด]
or
ndarray[boolean_list]#boolean list๋ ์กฐ๊ฑด์ ์ํด ์ฐ์ถ๋ ๋ฐฐ์ด
- Fancy Index : index value๋ฅผ ์ฌ์ฉํด ๊ฐ์ ์ถ์ถํ๋ ๋ฐฉ๋ฒ (๊ทธ๋ฅ ์ธ๋ฑ์ค๋ ๋น์ท)
ndarray1[index_ndarray]
or
ndarray1.take(index_ndarray)
9. ๋ฐ์ดํฐ ์ ์ถ๋ ฅ
numpy object(pickle) ํํ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ณ ๋ถ๋ฌ์ด
Binary ํ์ผ ํํ๋ก ์ ์ฅ
- ๋ถ๋ฌ์ค๊ธฐ
np.load(file="ํ์ผ์ด๋ฆ.npy")
- ์ ์ฅํ๊ธฐ
np.save("ํ์ผ์ด๋ฆ", arr=ndarray์ด๋ฆ)