๐Ÿ“•Numpy Study

๊ฐ•๊ธฐํ™˜ยท2022๋…„ 12์›” 13์ผ

Numpy๋Š” ๋น…๋ฐ์ดํ„ฐ ๋ถ„์„์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๋Œ€ํ‘œ์ ์ธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ค‘ ํ•˜๋‚˜์ด๋‹ค. Numpy์˜ ํ•ต์‹ฌ์ ์ธ ์š”์†Œ๋Š” ndarray object(N์ฐจ์› ๋ฐฐ์—ด ๊ฐ์ฒด)์ด๋‹ค.
ํ–‰๋ ฌ, ๋ฒกํ„ฐ ์—ฐ์‚ฐ(์„ ํ˜•๋Œ€์ˆ˜)์„ ์œ„ํ•œ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ๋น ๋ฅธ ์ฒ˜๋ฆฌ์†๋„๊ฐ€ ์žฅ์ ์ด๋‹ค.

1. ndarray

np.array() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆœ์ฐจ์ ์ธ ๊ฐ์ฒด๋ฅผ ๋ฐ›์•„ np.array๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

  1. ndim: ์ฐจ์› ์ˆ˜
  2. shape: ๊ฐ ์ฐจ์›์˜ ํฌ๊ธฐ
  3. size: ๋ชจ๋“  ์š”์†Œ์˜ ์ˆ˜
  4. dtype: ์›์†Œ์˜ data type
  5. itemsize: ์›์†Œ์˜ byte ํฌ๊ธฐ

2. ์—ฐ์‚ฐ

shape๊ฐ€ ๊ฐ™์€ ndarray ์‚ฌ์น™์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
EX) +, -, /, *

import numpy as np

# ํ–‰๋ ฌ ๊ณฑ
np.matmul(a, b)
a @ b

# ๊ฐ€์žฅ ํฐ ๊ฐ’ ๋ฐ˜ํ™˜
a.max()

# ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’ ๋ฐ˜ํ™˜
a.min()

# ํ‰๊ท  ๊ฐ’ ๋ฐ˜ํ™˜
a.mean()

# ๋ฐฐ์—ด์˜ ํ‰ํƒ„ํ™”
a.flatten()

#์—ด ๋ฐฉํ–ฅ ์›์†Œ
(axis = 0)

#ํ–‰ ๋ฐฉํ–ฅ ์›์†Œ
(axis = 1) 

3. ๋ฉ”์†Œ๋“œ

# 1์ฐจ์›์œผ๋กœ ๋ง๋ถ™์ด๊ธฐ
np.append(a,b)

# ๋‚œ์ˆ˜ ์ƒ์„ฑ

# (3,3) ๋‚œ์ˆ˜ ์ƒ์„ฑ
np.random.rand(3,3)

#  0~10 ๊นŒ์ง€ 10๊ฐœ์˜ ๋‚œ์ˆ˜ ์ƒ์„ฑ
np.random.randint(0, 10, size = 10)

4. ํ–‰๋ ฌ ์ƒ์„ฑ

# 0ํ–‰๋ ฌ
np.zeros((2,3))

# ๋ชจ๋“  ๊ฐ’์ด 1์ธ ํ–‰๋ ฌ
np.ones((2,3))

# ๋ชจ๋“  ๊ฐ’์ด 100์ธ ํ–‰๋ ฌ
np.full((2,3), 100)

# ๋‹จ์œ„ ํ–‰๋ ฌ
np.eye(3)

5. reshape

# 0~10 ๊นŒ์ง€ ๋ฐฐ์—ด ์žฌ๊ตฌ์„ฑ
np.arrange(0,10).reshape(2,5)

np.arrange(0,10).reshape(5,2)

# ์ „์น˜ํ–‰๋ ฌ
np.transpose(a)
a.transpose()

6. ์ธ๋ฑ์‹ฑ, ์Šฌ๋ผ์ด์‹ฑ

print(a[0])

# ์Œ์ˆ˜ ์ธ๋ฑ์‹ฑ
print(a[-1])

# ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์›์†Œ ์ธ๋ฑ์‹ฑ
print(a[np.array([0,1])])

# ๋ฆฌ์ŠคํŠธ ์Šฌ๋ผ์ด์‹ฑ
a_list[1:5]
profile
๋ฐฑ์—”๋“œ๊ฐœ๋ฐœ์ž ๊ฟˆ๋‚˜๋ฌด

0๊ฐœ์˜ ๋Œ“๊ธ€