numpy
numpy
: ๋ํ์ด(Numpy)๋ ์์น ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃจ๋ ํ์ด์ฌ ํจํค์ง๋ก์จ, ์ฃผ๋ก ๋ฐฐ์ด์ด๋ ํ๋ ฌ ๊ณ์ฐ์ ์ฌ์ฉ๋๋ค. Numpy์ ํต์ฌ์ด๋ผ๊ณ ๋ถ๋ฆฌ๋ ๋ค์ฐจ์ ํ๋ ฌ ์๋ฃ๊ตฌ์กฐ์ ndarray๋ฅผ ํตํด ๋ฒกํฐ ๋ฐ ํ๋ ฌ์ ์ฌ์ฉํ๋ ์ ํ ๋์ ๊ณ์ฐ์์ ์ฃผ๋ก ์ฌ์ฉ. ๋ํ์ผ ๋ฐฐ์ด์ N์ฐจ์ ๋ฐฐ์ด์ ์์ฑํ ์ ์์ต๋๋ค. 1์ฐจ์ ๋ฐฐ์ด, 2์ฐจ์ ๋ฐฐ์ด, 3์ฐจ์ ๋ฐฐ์ด ์ฒ๋ผ ์ํ๋ ์ฐจ์์ ๋ฐฐ์ด์ ๋ง๋ค์ ์๋ค. ์ํ์์๋ 1์ฐจ์ ๋ฐฐ์ด์ ๋ฒกํฐ vector, 2์ฐจ์ ๋ฐฐ์ด์ ํ๋ ฌ matrix์ด๋ผ๊ณ ๋ถ๋ฅธ๋ค. ๋ ๋ฒกํฐ์ ํ๋ ฌ์ ์ผ๋ฐํํ ๊ฒ์ ํ ์(tensor)๋ผ ํ๋ค. ์ผ๋ฐ์ ์ผ๋ก 2์ฐจ์ ๋ฐฐ์ด์ ํ๋ ฌ, 3์ฐจ์ ์ด์์ ๋ฐฐ์ด์ ๋ค์ฐจ์ ๋ฐฐ์ด์ด๋ผ ํ๋ค.
import numpy as np
Numpy์ ์ฃผ์ ๋ชจ๋
๋ชจ๋
๊ธฐ๋ฅ
np.array()
๋ฆฌ์คํธ, ํํ, ๋ฐฐ์ด๋ก ๋ถํฐ adarray๋ฅผ ์์ฑ
np.asarray()
๊ธฐ์กด์ array๋ก ๋ถํฐ adarray๋ฅผ ์์ฑ
np.arange()
range์ ๋น์ท
np.linspace(start, end, num)
[start, end] ๊ท ์ผํ ๊ฐ๊ฒฉ์ผ๋ก num๊ฐ ์์ฑ
np.logspace(start, end, num)
[start, end] log scale ๊ฐ๊ฒฉ์ผ๋ก num๊ฐ ์์ฑ
np.array()
np.array() ๋ ํ์ด์ฌ์ ๋ฆฌ์คํธ๋ฅผ ์ธ์๋ก ๋ฐ์ ๋ํ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ ๊ณตํ๋ ํน์ํ ํํ์ ๋ฐฐ์ด (numpy.ndarray)์ ๋ฐํ
x = np.array([1.0, 2.0, 3.0])
print(x) #[1, 2, 3]
type(x) <class 'numpy.ndarray'>
๋ํ์ด N์ฐจ์ ๋ฐฐ์ด
: ๋ํ์ด๋ 1์ฐจ์ ๋ฐฐ์ด(1์ค๋ก ๋์ด์ ๋ฐฐ์ด)๋ฟ ์๋๋ผ ๋ค์ฐจ์ ๋ฐฐ์ด๋ ์์ฑํ ์ ์๋ค
shape : ํ๋ ฌ์ ํ์ (์. 2 x 2)
ndim : ํ๋ ฌ์ ์ฐจ์ ์ถ๋ ฅ
dtype : ํ๋ ฌ์ ๋ด๊ธด ์์์ ์๋ฃํ
import numpy as np
x = np.array([[1, 2], [3, 4]])
print(x)
# [[1 2]
# [3 4]]
print(x.shape) # (2, 2)
print(x.ndim) # 2
print(x.dtype) # int32
y = np.array([[3, 0], [0, 6]])
print(x + y)
# [[ 4 2]
# [ 3 10]]
print(x * y)
# [[ 3 0]
# [ 0 24]]
๋ํ์ด ์ฐ์ ์ฐ์ฐ
import numpy as np
x = np.array([1.0, 2.0, 3.0])
y = np.array([4.0, 5.0, 6.0])
print(x + y) # [5. 7. 9.]
print(x - y) # [-3. -3. -3.]
print(x * y) # [ 4. 10. 18.]
print(x / y) # [0.25 0.4 0.5 ]
Numpy์์ ๋ฒกํฐ์ ํ๋ ฌ์ ๊ณฑ ๋๋ ํ๋ ฌ๊ณฑ์ ์ํด์๋ dot()์ ์ฌ์ฉ
a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
c = np.dot(a, b) print(c)
# [[19 22]
# [43 50]]
ndnumpy์ ์ด๊ธฐํ : zeros(), ones(), full(), eye()
zeros(): ํด๋น ๋ฐฐ์ด์ ๋ชจ๋ 0์ ์ฝ์
ones() : ํด๋น ๋ฐฐ์ด์ ๋ชจ๋ 1์ ์ฝ์
full() : ๋ฐฐ์ด์ ์ฌ์ฉ์๊ฐ ์ง์ ํ ๊ฐ์ ๋ฃ๋๋ฐ ์ฌ์ฉ
eye() : ๋๊ฐ์ ์ผ๋ก๋ 1์ด๊ณ ๋๋จธ์ง๋ 0์ธ ๋ฐฐ์ด์ ์์ฑ
import numpy as np
a = np.zeros((2, 3))
print(a)
# [[0. 0. 0.]
# [0. 0. 0.]]
a = np.ones((2, 3))
print(a)
# [[1. 1. 1.]
# [1. 1. 1.]]
a = np.full((2, 3), 7)
print(a)
# [[7 7 7]
# [7 7 7]]
a = np.eye(2)
print(a)
# [[1. 0.]
# [0. 1.]]
np.arange()
numpy.arange(start, stop, step, dtype)
np.arange(n) # 0๋ถํฐ n-1๊น์ง ๋ฒ์์ ์ง์ .
np.arange(i, j, k) # i๋ถํฐ j-1๊น์ง k์ฉ ์ฆ๊ฐํ๋ ๋ฐฐ์ด.
a = np.arange(10) #0๋ถํฐ 9๊น์ง
print(a) # [0 1 2 3 4 5 6 7 8 9]
a = np.arange(1, 10, 2) #1๋ถํฐ 9๊น์ง +2์ฉ ์ ์ฉ๋๋ ๋ฒ์
print(a) # [1 3 5 7 9]
reshape()
: arange(n) ํจ์์ ๋ฐฐ์ด์ ๋ค์ฐจ์์ผ๋ก ๋ณํํ๋ reshape()๋ฅผ ํตํด ๋ฐฐ์ด์ ์์ฑ
import numpy as np
a = np.array(np.arange(1, 30, 2)).reshape((3,5))
# ์ฌ๊ธฐ์ reshape์ ๋ฒ์๋ arange์ ๋์ผํด์ผ ํ๋ค
print(a)
# [[ 1 3 5 7 9]
# [11 13 15 17 19]
# [21 23 25 27 29]]
๋ธ๋ก๋ ์บ์คํธ
: ์์๋ณ ๊ณ์ฐ๋ฟ ์๋๋ผ ๋ํ์ผ ๋ฐฐ์ด๊ณผ ์์น ํ๋(์ค์นผ๋ผ๊ฐ)์ ์กฐํฉ์ผ๋ก ๋ ์ฐ์ ๋ก ์ฐ์ฐ์ด ๊ฐ๋ฅํ๋ค
import numpy as np
x = np.array([[1, 2], [3, 4]])
y = np.array([10, 20])
print(x + y)
๋ํ์ด ์์ ์ ๊ทผ
import numpy as np
x = np.array([[1, 2], [3, 4], [5, 6]])
print(x)
print(x[0]) # 0ํ์ ์์, [1 2]
print(x[0][1]) # (0, 1)์ ์์, 2
for row in x:
print(row)
# [1 2]
# [3 4]
# [5 6]
# x๋ฅผ 1์ฐจ์ ๋ฐฐ์ด๋ก ๋ณํ(ํํํ)
y = x.flatten()
print(y) # [1 2 3 4 5 6]
# ์ธ๋ฑ์ค๊ฐ 0, 2, 4์ธ ์์ ์ป๊ธฐ
print(y[np.array([0, 2, 4])]) # [1 3 5]
Last updated
Was this helpful?