
์์ฆ 1๋ ๊ฐ ๊ฑฐ์ ๋งค์ผ ์งํํด์จ ์ํ ๊ณต๋ถ๊ฐ ๊ฒฐ์ค์ ๋งบ๊ณ ์๋ ๊ฒ ๊ฐ์ ๊ธฐ๋ถ์ด ์ข๋ค. ๋จธ์ ๋ฌ๋ ๊ณต๋ถ๋ฅผ ์ต๊ทผ์ ๋ณธ๊ฒฉ์ ์ผ๋ก ์์ํ๋ฉด์ ์ํ ๋๋ฌธ์ ๋งํ ์ ์ ํฌ๊ฒ ์๋ ๊ฒ ๊ฐ๋ค (IQ๊ฐ ๋ช ์ ๋ถ์กฑํ์ฌ ๋ฐ์ํ๋ ๋ฌธ์ ๋ ๋น๋ฒํ๋ค).
์๋ฌดํผ, Numpy๋ 'multidimensional arrays'์ ๋ํ ์ฐ์ฐ์ ์ฉ์ดํ๊ฒ ํด์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค. ๊ทธ๋ฅ ๊ธฐ๋ณธ ๋ฆฌ์คํธ ํน์ ๋์ ๋๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ๋ณด๋ค ํจ์ฌ ๋น ๋ฅด๋ค. ํนํ 'gradient descent'๋ฅผ ์๊ฐํ๋ค๋ฉด for loop์ ๋๋ ค parameter๋ฅผ ์ ๋ฐ์ดํธ ํด์ฃผ๋ ๊ฒ๋ณด๋ค np.dot ํน์ np.matmul ๋ฑ์ ๊ธฐ๋ฅ์ ํ์ฉํ๋ฉด ํจ์ฌ ๋น ๋ฅด๊ฒ ํ๋ ฌ ์ฐ์ฐ์ ์งํํ ์ ์๋ค. ์ด๋ฐ ์๊ธฐ๋ ์ถํ machine learning ๊ด๋ จ ํฌ์คํ ์์ ๋ ์์ธํ ํ๋๋ก ํ๊ฒ ๋ค.
'vectorization'์ ๋จธ์ ๋ฌ๋์ ์๊ณ ๋ฆฌ์ฆ์ ๋งค์ฐ ์ค์ํ๋ค. ์๋ ์ฝ๋๋ฅผ ์ดํด๋ณด์.
import numpy
import time
size = 1000000
list1 = range(size)
list2 = range(size)
array1 = numpy.arange(size)
array2 = numpy.arange(size)
initialTime = time.time()
resultantList = [(a * b) for a, b in zip(list1, list2)]
print("Time taken by Lists :",
(time.time() - initialTime),
"seconds")
initialTime = time.time()
resultantArray = array1 * array2
print("Time taken by NumPy Arrays :",
(time.time() - initialTime),
"seconds")
> Time taken by Lists : 1.1984527111053467 seconds
Time taken by NumPy Arrays : 0.13434123992919922 seconds
๋ฆฌ์คํธ๋ฅผ 'vectorize'ํ์ฌ ํ๋ ฌ์ฒ๋ผ ๋ํ๋ฉด ํจ์ฌ ๋น ๋ฅด๊ฒ ๊ฒฐ๊ณผ๋ฅผ ์ฐ์ถํ ์ ์๋ค.
B = np.array([[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11]])
print(B.shape)
> (3, 4)
print(np.zeroes((3,4)))
print(np.eye(3))
[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]]
[[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]

> Z= np.array([[0,1,2,3,4,5],
[10,11,12,13,14,15],
[20,21,22,23,24,25],
[30,31,32,33,34,35],
[40,41,42,43,44,45],
[50,51,52,53,54,55]])
# Construct `Z_green`, `Z_red`, `Z_orange`, and `Z_cyan`:
Z_green = Z[(2,4), ::2]
Z_red = Z[:, 2]
Z_orange = Z[0, 3:5]
Z_cyan = Z[(4,5), 4:6]
ํฌ๊ฒ ์ด๋ ค์ธ ๊ฑด ์๋ค. ๋ฆฌ์คํธ ์ธ๋ฑ์ฑ๊ณผ ๋น์ทํ๋ค๊ณ ์๊ฐํ๋ฉด ๋๋ค.
๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ๊ณ ๋ คํ์๋ Z_green ๋ฑ์ ๊ทธ๋ฅ 'view'์ด๋ค. Slicing์ ํ์ฌ ๋ณ์๋ฅผ ์ ์ธํ๋ค๊ณ ์๋ก์ด ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ด ํ ๋น ๋๋ ๊ฒ์ ์๋๋ค. ๋ง์ฐฌ๊ฐ์ง๋ก ์๋ก์ด ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ์ถ๋ค๋ฉด Z[:, 2].copy() ๋ฅผ ์ ์ธํ๋ฉด ๋๋ค.
'Boolean Mask' ๋๋ 'Indices'๋ก ๊ตฌ์ฑ๋ array๋ฅผ ํตํด indxing์ ํ ์๋ ์๋ค.
from numpy.random import default_rng
rng = default_rng(12345)
x = rng.integers(0, 20, 15)
print(x)
> [13 4 15 6 4 15 12 13 19 7 16 6 11 11 4]
inds = np.array([3, 7, 7, 12])
print(x[inds])
> [6 13 19 11]
mask_mult_3 = (x > 0) & (x % 3 ==0)
print("x:", x)
print("mask_mult_3:", mask_mult_3)
print("==> x[mask_mult_3]:", x[mask_mult_3])
>x: [13 4 15 6 4 15 12 13 19 7 16 6 11 11 4]
>mask_mult_3: [False False True True False True True False False False False True
False False False]
>==> x[mask_mult_3]: [15 6 15 12 6]
20๊น์ง์ ์์๋ฅผ ๋ชจ๋ ์ฐพ๋ ์๊ณ ๋ฆฌ์ฆ์ ์์ฑํด๋ณด์. ์๋ผํ ์คํ ๋ค์ค์ ์ฒด๋ฅผ numpy๋ฅผ ํ์ฉํ์ฌ ์์ฑํ ์ ์๋ค. ์ฌ์ค ๋ถํ์ํ๋ฉฐ ์ฝ๋ฉํ ์คํธ์์๋ ๊ทธ๋ฅ ๋ฆฌ์คํธ๋ฅผ ํ์ฉํ ๊ฒ ๊ฐ๋ค.
from math import sqrt
def sieve(n):
is_prime = np.empty(n+1, dtype=bool) # the "sieve"
# Initial values
is_prime[0:2] = False # {0, 1} are _not_ considered prime
is_prime[2:] = True # All other values might be prime
m = int(sqrt(n)) + 1
for i in range(2, m):
if is_prime[i] == True:
for j in range(i+i, n+1, i):
is_prime[j] = False
return is_prime
# Prints your primes
print("==> Primes through 20:\n", np.nonzero(sieve(20))[0])
>==> Primes through 20:
[2 3 5 7 11 13 17 19]