๐ Numerical Python - numpy
- '๋ค์ํ ํ๋ ฌ๊ณผ ๊ทธ ๊ณ์ฐ์ ์ฝ๊ฒ ํํํ ์ ์์๊น?' ๋ผ๋ ๊ณ ๋ฏผ์์ ์์๋์ด numpy๋ผ๋ ๋ชจ๋์ด ํ์
- ๊ณ ์ฑ๋ฅ ๊ณผํ ๊ณ์ฐ์ฉ ํจํค์ง๋ก ๊ณตํ, ์ดํ, ์ฆ๊ถ ๋ฑ์ ๋ถ์ผ์์ ๋ง์ด ์ฌ์ฉ๋จ
- Maxtrix์ Vector์ ๊ฐ์ Array ์ฐ์ฐ์ ์ฌ์ค์ ํ์ค
๐ฅ ํน์ง
1. ๋น ๋ฅด๊ณ , ๋ฉ๋ชจ๋ฆฌ ํจ์จ์
2. ๋ฐ๋ณต๋ฌธ์์ด ๋ฐ์ดํฐ ๋ฐฐ์ด์ ๋ํ ์ฒ๋ฆฌ๋ฅผ ์ง์ํจ.
3. ์ ํ ๋์์ ๊ด๋ จ๋ ๋ค์ํ ๊ธฐ๋ฅ์ ์ ๊ณต
4. ์ฌ์ฉํ๊ธฐ ์ํด์ numpy๋ชจ๋์ ์ค์นํด์ผ ํจ.(ex. in conda, conda install numpy) - ์ผ๋ฐ์ ์ผ๋ก np๋ผ๋ alias์ฌ์ฉ
๐ฅ ndarray
- ๋ณดํต array๋ผ๊ณ ๋ถ๋ฆ - numpy์์ ์ง์ํ๋ array๋ก np.array(List์ด๋ฆ, dtype(๋ฐ์ดํฐํ์ )) ์ผ๋ก ์ฐ์ด๊ณ ndarray๊ฐ์ฒด๋ผ ๋ถ๋ฆผ
- List์ ๋ค๋ฅด๊ฒ ํ๋์ ๋ฐ์ดํฐ ํ์ ๋ง ์ง์ํจ -> Dynamic Typing ์ง์ ํ์ง ์๋๋ค!!
-> List๋ ์ฃผ์๋ฅผ ํ์ฉํ์ฌ ๋ฐ์ดํฐ์ ์ ๊ทผํ๊ธฐ์ ๋ณ๊ฒฝ์ ์์ด์๋ ์ด์ ์ด ์์ง๋ง, ์ฐ์ฐ์ ํ ๋์๋ numpy์ ๊ฒฝ์ฐ๊ฐ ์ข๋ค
-> numpy์ ๊ฒฝ์ฐ๋ ๊ฐ๋ค์ด ๋ค ๋ถ์ด์๊ธฐ ๋๋ฌธ์ ์ฐ์ฐ์ด ๋ ๋น ๋ฆ, ๋ฉ๋ชจ๋ฆฌ์ ์ ๊ทผ์ฑ์ด ๋๊ณ ๋ฉ๋ชจ๋ฆฌ ์ ์ฅ ๊ณต๊ฐ์ ์ ํ๊ธฐ ์ฉ์ํ๋ค.
- shape(): numpy array์ dimension ๊ตฌ์ฑ(ํฌ๊ธฐ, ํํ)๋ฅผ ๋ฐํํจ - 3x4ํ๋ ฌ์ด๋ผ๋ฉด (3, 4)๋ฅผ ๋ฐํ
- dtype(): numpy array์ ๋ฐ์ดํฐ type๋ฅผ ๋ฐํ(int, float ๋ฑ)
- Array์ Rank(์ฐจ์)์ ๋ฐ๋ผ ๋ถ๋ฆฌ๋ ์ด๋ฆ์ด ๋ค๋ฆ
-> RANK0: scalar / RANK1: Vector / RANK2: Matrix / RANK3: 3-tensor / RANKn: n-tensor
๐ Handling Shape
๐ฅ reshape(): Array์ shape์ ํฌ๊ธฐ๋ฅผ ๋ณ๊ฒฝํจ, elements์ ๊ฐฏ์๋ ๋์ผ
ex. (4,2) -> (8, )์ผ๋ก ๋ณ๊ฒฝํ๊ธฐ ์ํด์๋ np.array(a).reshape(8, )์ผ๋ก ํด์ฃผ๋ฉด ๋ณ๊ฒฝ.
- reshape์์ -1์ ์ฌ์ฉํ๊ฒ๋๋ฉด ์๋์ผ๋ก ๊ณ์ฐํด ์ฑ์์ฃผ๋ ๊ฒ์ ์๋ฏธ
ex. np.array(a).reshape(2, -1) ์ ํ๊ฒ๋๋ฉด (2, 4) shape์ ๊ฐ์ง๋ array๋ก ๋ฐํ
- flatten(): ๋ค์ฐจ์ Array๋ฅผ 1์ฐจ์ Array๋ก ๋ณ๊ฒฝ
๐ฅ Indexing and Slicing
- index: List์ ๋ฌ๋ฆฌ [x, y]๋ก ํ๊ธฐ ๊ฐ๋ฅ -> test_list[x][y] = test_list[x, y]
Matrix์ ๊ฒฝ์ฐ [row, column]์
- slicing: List์ ๋ฌ๋ฆฌ ํ๊ณผ ์ด ๋ถ๋ถ์ ๋๋ ์ Slicing ๊ฐ๋ฅ
Matrix์ ๋ถ๋ถ ์งํฉ ์ถ์ถ์ ์ ์ฉ
ex. a = np.array([[1,2,3,4,5], [6,7,8,9,10]], int)
-> a[:, 2:] ==> ์ ์ฒด row์ 2 column์ด์์ ๋ถ๋ถ์งํฉ = [[3,4,5], [8,9,10]]
-> a[1, 1:3] ==> 1 row์ 1, 2 column = [7, 8]
-> a[1:3] ==> 1, 2 row ์ ์ฒด, But ์ธ๋ฑ์ค๊ฐ ๋์ด๊ฐ๊ธฐ ๋๋ฌธ์ Error
- arange(): array์ ๋ฒ์๋ฅผ ์ง์ ํด ๊ฐ์ ์์ฑํ list ์์ฑ
ex. np.arange(30) -> 0~29๊น์ง list ์์ฑ
np.arange(0, 5, 0.5) -> [0, 0.5, 1, 1.5 ...] list ์์ฑ
np.arange(30).reshape(5,6) -> 2์ฐจ์ list๋ก 0~29๊น์ง ์์ฑ
- Zeros: 0์ผ๋ก ๊ฐ๋์ฐฌ array -> np.zeros(shape = (5, ), dtype = np.int) -> [0, 0, 0, 0, 0]
- Ones: 1๋ก ๊ฐ๋์ฐฌ array -> np.ones(shape = (5,), dtype = np.int) -> [1, 1, 1, 1, 1]
- empy: ๊ฐ์ด ์๋ array ์์ฑ(Memory initialization ๋์ง ์์)
- something_like: ์๋ ์๋ array์ ํฌ๊ธฐ๊ฐ ๊ฐ์ 0, 1, empty array๋ฅผ ์์ฑ(ex. ones_like(a))
๐ฅ identity, eye, dia, and random sample
- identity: ๋จ์ ํ๋ ฌ(iํ๋ ฌ)์ ์์ฑ ex) np.identity(n=3, dtype = np.int) -> [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
- eye: ๋๊ฐ์ ์ด 1์ธ ํ๋ ฌ, k๊ฐ์ผ๋ก ์์ํจ(index ๋ณ๊ฒฝ์ด ๊ฐ๋ฅ)
ex. np.eye(3, 5, k=2) ==> [[0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]]
- diag: ๋๊ฐ ํ๋ ฌ์ ๊ฐ์ ์ถ์ถํ๋ ํจ์
- random sample: ๋ฐ์ดํฐ ๋ถํฌ์ ๋ฐ๋ฅธ sampling์ผ๋ก array ์์ฑ
ex. np.random.uniform(0, 1, 10).reshape(2, 5) => ๊ท ๋ฑ๋ถํฌ๋ก 0~1์ฌ์ด์ ๊ฐ์ ๋๋คํ๊ฒ ๋ฝ์๋
ex. np.random.normal(0, 1, 10).reshape(2, 5) => ์ ๊ท๋ถํฌ๋ก 0~1์ฌ์ด์ ๊ฐ์ ๋๋คํ๊ฒ ๋ฝ์๋
๐ฅ Operation Functions
- sum, mean, std: array์ ์์(elements)๋ค์ ํฉ, ํ๊ท , ํ์คํธ์ฐจ ๋ฑ์ ๊ตฌํ ์ ์์
** axis - ๊ธฐ์ค์ด ๋๋ dimension ์ถ์ ์ ํ ์ ์์(Matrix์ ๊ฒฝ์ฐ 0 - row, 1 - column)
- Concatenate: numpy array๋ฅผ ํฉ์น๋(๋ถ์ด๋) ํจ์(vstack, hstack, concatenate)
- numpy์์๋ array๊ฐ์ ๊ธฐ๋ณธ์ ์ธ ์ฌ์น์ฐ์ฐ์ ์ ๊ณต
- Element-wise operations: array๊ฐ shape๊ฐ ๊ฐ์ ๋๋ ๊ฐ์ ์ธ๋ฑ์ค์ ์์๋ผ๋ฆฌ ์ฐ์ฐ์ด ๊ฐ๋ฅํจ.
- Dot prduct: Matrix์ ๊ธฐ๋ณธ ์ฐ์ฐ, dotํจ์๋ฅผ ์ฌ์ฉ
- Transpose: ์ ์นํ๋ ฌ(T)์ผ๋ก transposeํจ์๋ T๋ฅผ ์ฌ์ฉํ์ฌ ์์ฑ
- Broadcasting: shape์ด ๋ค๋ฅธ ๋ฐฐ์ด ๊ฐ์ ์ฐ์ฐ์ ์ง์ํจ.
๐ฅ Comparisons
- All & any: Array์ ๋ฐ์ดํฐ ์ ๋ถ(all) ๋๋ ์ผ๋ถ(any)๊ฐ ์กฐ๊ฑด์ ๋ง์กฑํ๋์ง์ ๋ํ ์ฌ๋ถ๋ฅผ ๋ฐํ(True / False list)
ex. a = [1, 2, 3, 4, 5] a > 3 ==> array([False, False, False, True, True]) ๋ฐํ
np.any(a<3) => True np.all(a>3) => False
- np.where() => where(์กฐ๊ฑด, True์ ๋ฐํ ๊ฐ, False์ ๋ฐํ ๊ฐ)
ex. np.where(a < 3, 2, 3) ==> [2, 2, 3, 3, 3] ๋ฐํ
-> ๊ทธ ์ธ์ index๊ฐ์ ๋ฐํ(True์ธ index), isnan(none ๊ฐ ์ฐพ๊ธฐ), isfinite(finite number)๋ฑ์ด ์์
- argmax & argmin: array๋ด์ ์ต๋๊ฐ ๋๋ ์ต์๊ฐ์ index ๋ฐํ -> axis ๊ธฐ๋ฐ์ผ๋ก ๋ฐํ ๊ฐ๋ฅ
- argsort(): ์ ๋ ฌ ํ index๋ฅผ ๋ฐํ
- boolean index: ํน์ ์กฐ๊ฑด์ ๋ฐ๋ฅธ ๊ฐ์ ๋ฐฐ์ด ํํ๋ก ์ถ์ถ - Shape๊ฐ ์ค์!!
- fancy index: numpy๋ array๋ฅผ index value๋ก ์ฌ์ฉํด์ ๊ฐ ์ถ์ถ - index ๋ฒ์๊ฐ ์ค์!!
ex. a = [1., 2., 3., 4., 5.] b = [0, 0, 1, 2, 3, 4, 1, 1]
a[b] = a.take(b) ==> [1., 1., 2., 3., 4., 5., 2., 2.]๊ฐ ๋ฐํ
=> matrix ํํ๋ ๊ฐ๋ฅ(a[b, c])
- numpy I/O: loadtxt & savetxt: text type์ ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ณ ์ ์ฅ
ex. a = np.loadtxt("./test.txt", delimiter = "\t")
np.savetxt("a_test.csv", a, fmt = "%.2e", delimiter = ',')
npyํํ์ ํ์ผ๋ก๋ ์ฝ๊ณ , ์ ์ฅ ๊ฐ๋ฅ(pickleํํ)
ex. np.save("npy_test", arr = a) ==> npy_test.npy ํ์ผ ์์ฑ
a_test = np.load(file = "npy_test.npy")
'์ธ๊ณต์ง๋ฅ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Day 7] Math for AI - Gradient Descent (0) | 2021.01.26 |
---|---|
[Day 6] Math for AI - 2. Vector and Matrix (0) | 2021.01.25 |
[Day 5] Python (2) | 2021.01.22 |
[Day 4] Python (0) | 2021.01.21 |
[Day 3] Python (0) | 2021.01.20 |