๐Ÿš€ Languege 39

python - numpy(11) 2์ฐจ์› ๊ทธ๋ฆฌ๋“œ ํฌ์ธํŠธ

๊ทธ๋ฆฌ๋“œ๋ž€? 2์ฐจ์› ์˜์—ญ์— ๋Œ€ํ•œ (x,y)์˜ ์ขŒํ‘œ๊ฐ’ ์Œ. metplotlib ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋ณ€์ˆ˜๊ฐ€ 2๊ฐœ์ธ 2์ฐจ์› ํ•จ์ˆ˜์˜ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ฆฌ๊ฑฐ๋‚˜ ํ‘œ๋ฅผ ๊ทธ๋ฆฌ๊ณ ์ž ํ•˜๋Š” ๊ฒฝ์šฐ metplotlib ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉํ•œ๋‹ค. import numpy as np import matplotlib.pyplot as plt x = np.arange(3) y = np.arange(5) X, Y = np.meshgrid(x, y) print(X) print(Y) plt.title('Grid Points') plt.scatter(X, Y, linewidths=1) # ์‚ฐ์ ๋„ plt.show() [์ฝ”๋“œ ์‹คํ–‰ ๊ฒฐ๊ณผ] ๋ญ”๊ฐ€๊ฐ€ ํŒ์—…๋œ๋‹ค..ใ…‹ [[0 1 2] [0 1 2] [0 1 2] [0 1 2] [0 1 2]] [[0 0 0] [1 1..

python - numpy(8) ๋ฐฐ์—ด ์ƒ์„ฑ ๋ช…๋ น

zeros, ones, empty(๊ถŒ์žฅ), arange, linspace ๋“ฑ์˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค! import numpy as np # zeros() : ํฌ๊ธฐ๊ฐ€ ์ •ํ•ด์ ธ ์žˆ๋Š” ๋ชจ๋“  ๊ฐ’์ด 0์ธ ๋ฐฐ์—ด ์ƒ์„ฑ, ์ธ์ˆ˜๋Š” ๋ฐฐ์—ด์˜ ํฌ๊ธฐ z_ar = np.zeros(5) print('zeros array 1d \n', z_ar, z_ar.dtype) # ๋‹ค์ฐจ์› ๋ฐฐ์—ด: ์ธ์ˆ˜์˜ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํŠœํ”Œ ์‚ฌ์šฉ z_2dar = np.zeros((2, 3)) print('zeros array 2d \n', z_2dar, z_2dar.dtype) # ์ž๋ฃŒํ˜• ์ž…๋ ฅ ์ง€์ • dt_z_ar = np.zeros((5, 2), dtype='i') print('zeros array dt \n', dt_z_ar, dt_z_a..

python -numpy(7) ๋ฐฐ์—ด์˜ ์ž๋ฃŒํ˜•

๋ฐฐ์—ด ndarrayํด๋ž˜์Šค๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๊ฐ™์€ ์ž๋ฃŒํ˜•์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค! array๋ช…๋ น์œผ๋กœ ๋ฐฐ์—ด ์ƒ์„ฑ ์‹œ ๋ช…์‹œ์ ์œผ๋กœ ์ง€์ •ํ•˜๋ ค๋ฉด dtype์ธ์ˆ˜ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. import numpy as np # ---------------------------------------------------------- # ๋ฐฐ์—ด์— ํƒ€์ž… ํ™•์ธ x = np.array([1, 2, 3]) print(x.dtype) x = np.array([1.2, 2.2, 3.1]) print(x.dtype) x = np.array(['A', 'B', 'C']) print(x.dtype) # Unicode x = np.array([True, False]) print(x.dtype) # ------------------------------------------..

python - numpy(5) ๋ฐฐ์—ด์˜ ์ธ๋ฑ์‹ฑ

import numpy as np # ๋ฐฐ์—ด ์ธ๋ฑ์‹ฑ # ์ธ๋ฑ์‹ฑ์€? ํŠน์ • ์œ„์น˜์˜ ๊ฐ’์„ ํ™•์ธํ•˜๋Š”๊ฒƒ.. a = np.array([0, 1, 2, 3, 4]) print(a[2]) print(a[-1]) a = np.array([[1, 2, 3],[4, 5, 6]]) print(a) print(a[1, 0]) # 1๋ฒˆ ํ–‰์˜ 0๋ฒˆ ์—ด print(a[1][0]) # 1๋ฒˆ ํ–‰์˜ 0๋ฒˆ ์—ด print(a[-1, -1]) # ๋งˆ์ง€๋ง‰ ํ–‰, ๋งˆ์ง€๋ง‰ ์—ด # ๋ฐฐ์—ด ์Šฌ๋ผ์ด์‹ฑ # ๋ฐฐ์—ด ๊ฐ์ฒด๋กœ ๊ตฌํ˜„ํ•œ ๋‹ค์ฐจ์› ๋ฐฐ์—ด์˜ ์›์†Œ์ค‘ ๋ณต์ˆ˜๊ฐœ์— ์ ‘๊ทผํ•˜๋Š” ๊ฒฝ์šฐ # Slicing ๊ณผ comma๋ฅผ ์‚ฌ์šฉ b = np.array([[0, 1, 2, 3], [4, 5, 6, 7]]) print(b) print(b[0, :]) # 0๋ฒˆ์งธ ํ–‰ ์ „์ฒด print(b[..

python - numpy(4) ๋ฐฐ์—ด์˜ ์†์„ฑ๊ฐ’ ํ™•์ธ

import numpy as np # 1์ฐจ์› ๋ฐฐ์—ด์˜ ์†์„ฑ๊ฐ’ a = np.array([1, 2, 3]) print(a.ndim) # 1 print(a.shape) # (3,) # 2์ฐจ์› ๋ฐฐ์—ด์˜ ์†์„ฑ๊ฐ’ c = np.array([[1, 2, 3], [4, 5, 6]]) print(c.ndim) # 2 print(c.shape) # (2, 3) # 3์ฐจ์› ๋ฐฐ์—ด์˜ ์†์„ฑ๊ฐ’ d = np.array([ [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], [[11, 12, 13, 14], [15, 16, 17, 18], [19, 20, 21, 22]] ]) print(d.ndim) # 3 print(d.shape) # (2, 3, 4) [์‹คํ–‰ ๊ฒฐ๊ณผ] 1 (3,) 2 (2, 3) 3 (2,..

python - numpy(2) 2์ฐจ์› ๋ฐฐ์—ด

2์ฐจ์› ๋ฐฐ์—ด์ด๋ž€, ํ‘œ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค! 2์ฐจ์› ๋ฐฐ์—ด import numpy as np # 2X3 ํ–‰๋ ฌ c = np.array([[0, 1, 2], [3, 4, 5]]) # ๋‚ด๋ถ€ ์Šคํ€˜์–ด๋ธŒ๋ผ์ผ“์ด ํ–‰, ์š”์†Œ๊ฐ€ ์—ด print(' 2x3 ํ–‰๋ ฌ\n', c) print('ํ–‰์˜ ๊ฐœ์ˆ˜ :', len(c)) print('์—ด์˜ ๊ฐœ์ˆ˜ :', len(c[1])) print('c[0]์˜ ๊ฐ’ :', c[0]) print('c[1]์˜ ๊ฐ’ :', c[1]) [์‹คํ–‰ ๊ฒฐ๊ณผ] 2x3 ํ–‰๋ ฌ [[0 1 2] [3 4 5]] ํ–‰์˜ ๊ฐœ์ˆ˜ : 2 ์—ด์˜ ๊ฐœ์ˆ˜ : 3 c[0]์˜ ๊ฐ’ : [0 1 2] c[1]์˜ ๊ฐ’ : [3 4 5]

python - numpy(1) 1์ฐจ์› ๋ฐฐ์—ด

numpy๋ž€, ์ˆ˜์น˜ ํ•ด์„์šฉ ํŒŒ์ด์ฌ ํŒจํ‚ค์ง€! 1์ฐจ์›~3์ฐจ์› ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 1์ฐจ์› ๋ฐฐ์—ด import numpy as np # numpy์˜ 1์ฐจ์› ๋ฐฐ์—ด------------------------------- # arrryํ•จ์ˆ˜์— ๋ฆฌ์ŠคํŠธ๋ฅผ ๋„ฃ์œผ๋ฉด ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ ar = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # ndarray ์ž๋ฃŒํ˜•์ด๊ณ  , ()์•ˆ์— ์‹œํ€€์Šค ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค. print(ar) print('ar ์ž๋ฃŒํ˜• :', type(ar)) # ๋ฐฑํ„ฐํ™” ์—ฐ์‚ฐ-------------------------------------- data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # 1.for๋ฌธ์„ ์ด์šฉํ•œ ์—ฐ์‚ฐ๋ฐฉ์‹ answer = [] for di in d..

728x90