ํฐ์คํ ๋ฆฌ ๋ทฐ

๐น Numpy ๋ฐฐ์ด ndarray์ ์ฃผ์ ์์ฑ
Numpy ๋ฐฐ์ด์ ๋ค์๊ณผ ๊ฐ์ ์์ฑ๋ค์ ์ ๊ณตํ๋ค.
| ์์ฑ | ์ค๋ช |
| ndarray.ndim | ๋ฐฐ์ด์ ์ฐจ์ ์ (์ถ์ ๊ฐ์) |
| ndarray.shape | ๋ฐฐ์ด์ ๊ตฌ์กฐ๋ฅผ ๋ํ๋ด๋ ํํ (๊ฐ ์ฐจ์์ ํฌ๊ธฐ) |
| ndarray.reshape | ๋ฐฐ์ด์ ๋ชจ์์ ๋ณ๊ฒฝ (๋จ, ์์ ์๋ ์ ์งํด์ผ ํจ) |
| ndarray.size | ์ ์ฒด ์์ ๊ฐ์ |
| ndarray.dtype | ๋ฐฐ์ด์ ๋ฐ์ดํฐ ํ์ (์: int32, float64, bool ๋ฑ) |
| ndarray.itemsize | ํ๋์ ์์๊ฐ ์ฐจ์งํ๋ ๋ฐ์ดํธ ํฌ๊ธฐ |
| ndarray.T | ๋ฐฐ์ด์ ์ ์น(transpose) ๊ฒฐ๊ณผ |
| ndarray.tolist() | ๋ฐฐ์ด์ list๋ก ๋ฐํ |
์์ ์ฝ๋:
import numpy as np
ex_arr = np.array([[[1, 2, 3], [3, 4, 5]],
[[10, 20, 30], [30, 40, 50]]])
print("์ฐจ์:", ex_arr.ndim) # 3
print("๋ชจ์:", ex_arr.shape) # (2, 2, 3)
print("ํฌ๊ธฐ:", ex_arr.size) # 12
print("๋ฐ์ดํฐ ํ์
:", ex_arr.dtype) # int32
print("์์ ํฌ๊ธฐ:", ex_arr.itemsize)# 4 (bytes)
print("์ ์น ํ๋ ฌ:\n", ex_arr.T)
๐ธndarray.ndim
"Number of array dimensions(๋ฐฐ์ด ์ฐจ์์ ์)"์ ์๋ฏธ์ด๋ค. ์ฆ, ๋ฐฐ์ด์ ์ฐจ์ ์๋ฅผ ๋ํ๋ธ๋ค.
import numpy as np
scalar = np.array(1) # 0์ฐจ์
vector = np.array([1, 2, 3]) # 1์ฐจ์
matrix = np.array([[1, 2], [3, 4]]) # 2์ฐจ์
tensor = np.array([[[1,2,3],[4,5,6]],
[[7,8,9],[10,11,12]]]) # 3์ฐจ์
print(scalar.ndim) # 0
print(vector.ndim) # 1
print(matrix.ndim) # 2
print(tensor.ndim) # 3
- Scalar : 0์ฐจ์ (๋จ์ผ ๊ฐ)
- Vector : 1์ฐจ์ (Scalar๊ฐ ๋ชจ์ธ ๋ฐฐ์ด)
- matrix(๋งคํธ๋ฆญ์ค) : 2์ฐจ์, (Vector๊ฐ ๋ชจ์ธ ๋ฐฐ์ด)
- tensor(ํ ์) : 3์ฐจ์์ด์, (๋ฐฐ์ด ์ผ๋ฐ์ ๋ํ๋ด๋ ํํ)
๐ธ ndarray.shape
"Tuple of array dimensions(๋ฐฐ์ด ์ฐจ์์ ํํ)"๋ก, ๋ฐฐ์ด์ ๊ตฌ์กฐ(๋ชจ์)์ด๋ค.
๊ฐ ์ฐจ์์ ํฌ๊ธฐ๋ฅผ ํํ ํํ๋ก ๋ฐํํ๋ค.
scalar = np.array(1)
vector = np.array([1, 2, 3])
matrix = np.array([[1, 2], [3, 4]])
tensor = np.array([[[1,2,3],[4,5,6]],
[[7,8,9],[10,11,12]]])
print(scalar.shape) # ()
print(vector.shape) # (3,)
print(matrix.shape) # (2, 2)
print(tensor.shape) # (2, 2, 3)
- scalar์ ๊ฒฝ์ฐ, 0์ฐจ์์ด๋ฏ๋ก, ๋ฐฐ์ด์ ๋ชจ์์ด ์์
- vector ๊ฒฝ์ฐ, 3๊ฐ์ง๋ฆฌ 1์ฐจ์ ๋ฐฐ์ด์ด๋ผ๋ ์๋ฏธ๋ก (3, )
- matrix์ ๊ฒฝ์ฐ, 2ํ 2์ด์ง๋ฆฌ ๋ฐฐ์ด์ด ์์ผ๋ (2,2)
- tensor์ ๊ฒฝ์ฐ, 2๊ฐ์ 2ํ 3์ด์ง๋ฆฌ ๋ฐฐ์ด์ด ์์ผ๋ฏ๋ก (2,2,3)
๐ธ ndarray.reshape()
shape ์์ฑ์ ํ์ฉํ๋ ๋ฉ์๋๊ฐ reshape()์ด๋ฉฐ, ๋ฐฐ์ด์ ํํ๋ฅผ ๋ฐ๊ฟ ๋ ์ฌ์ฉํ๋ค. ์ด๋, ์์์ ๊ฐ์๋ ์ ์งํด์ผ ํ๋ค.
a = np.array([[1,2,3], [4,5,6], [7,8,9]])
print(a.shape) # (3, 3)
b = np.linspace(11, 19, 9)
print(b) # [11. 12. 13. 14. 15. 16. 17. 18. 19.]
b = b.reshape(a.shape)
print(b) # [[11. 12. 13.]
# [14. 15. 16.]
# [17. 18. 19.]]
print("result: ", a + b)
์์์ ๊ฐ์๊ฐ ์ ์ ํ ๊ฒฝ์ฐ, ํ๋ ฌ์ ๋ชจ์์ ๋ฐ๊ฟ ์ฐ์ฐ ์ฒ๋ฆฌ๋ฅผ ํ๋ ์์ผ๋ก ํ์ฉํ ์ ์๋ค.
๐ธndarray.size
์ ์ฒด ์์ ๊ฐ์๋ฅผ ์๋ฏธํ๋ฉฐ, ๋ฐฐ์ด ์ฐจ์๋ค์ ๊ณฑ๊ณผ ๊ฐ๋ค.
scalar = np.array(1)
vector = np.array([1, 2, 3])
matrix = np.array([[1, 2], [3, 4]])
tensor = np.array([[[1,2,3],[4,5,6]],
[[7,8,9],[10,11,12]]])
print(scalar.size) # 1
print(vector.size) # 3
print(matrix.size) # 4
print(tensor.size) # 12
scalar_np : ํ๋์ ์ ์ = 1
vector_np : 3๊ฐ์ ์์ * 1์ฐจ์ = 3
matrix_np : 2๊ฐ์ ์์ ์์ * 2๊ฐ์ ์์ = 4
tensor_np : 2๊ฐ์ ์์ ์์ * 2๊ฐ์ ์์, ๊ทธ๋ฆฌ๊ณ ๊ทธ์์ * 3๊ฐ์ ์์ = 12
๐ธ ndarray.dtype, itemsize
Numpy๋ ๋์ฉ๋ ๋ฐ์ดํฐ๋ฅผ ํจ๊ณผ์ ์ผ๋ก ์ฒ๋ฆฌํ๊ธฐ ์ํด ๋ฐ์ดํฐ ํ์ ์ค์ ๋ฐ ์ต์ ํ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค. ๋ฐฐ์ด์ ๊ฐ ์์๊ฐ ์ด๋ค ์๋ฃํ์ ๊ฐ์ง๋์ง๋ฅผ dtype์ผ๋ก ์ง์ ํ ์ ์์ผ๋ฉฐ, ์ด์ ๋ฐ๋ผ ๊ฐ ์์๊ฐ ์ฐจ์งํ๋ ๋ฉ๋ชจ๋ฆฌ ํฌ๊ธฐ(itemsize)๊ฐ ๊ฒฐ์ ๋๋ค.
int8_np = np.array([1,2,3], dtype=np.int8)
int16_np = np.array([1,2,3], dtype=np.int16)
uint8_np = np.array([1,2,3], dtype=np.uint8)
uint16_np = np.array([1,2,3], dtype=np.uint16)
float32_np = np.array([1,2,3], dtype=np.float32)
float64_np = np.array([1,2,3], dtype=np.float64)
print("Integer: {}/{}/{}/{}".format(int8_np.dtype, int16_np.dtype)) # int8 int16
print("Unsigned Integer: {}/{}/{}/{}".format(uint8_np.dtype, uint16_np.dtype)) # uint8 uint16
print("Floating Point: {}/{}".format(float32_np.dtype, float64_np.dtype)) # float32 float64
๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ ํ์ธ์ ๋ค์๊ณผ ๊ฐ๋ค.
ones = np.ones(shape=(10, 5, 3, 2)) #๊ธฐ๋ณธ dtype์ float64
print("size:", ones.size) #์ ์ฒด ์์ ์: 10*5*3*2 = 300
print("dtype/itemsize: {} / {}\n".format(ones.dtype, ones.itemsize)) # float64 / 8
total_bytes = ones.size * ones.itemsize
print("๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋:")
print(f" - {total_bytes} B") # 2400
๐ ndarray.nbytes๋ ์ ๊ณ์ฐ์ ํ ์ค๋ก ์์ฝํ ์์ฑ
print(ones.nbytes) # 2400 (bytes)
๐ธndarray.T
arr = np.array([[1, 2],
[3, 4]])
print(arr.T)
#[[1 3]
# [2 4]]
๐ธ ndarray.tolist()
print(arr.tolist()) # [[1, 2], [3, 4]]
๋ง๋ฌด๋ฆฌ

'ํ์ด์ฌ > Numpy' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Numpy] ndarray ํํ ๋ฐ๊พธ๊ธฐ (reshape() ,flatten(), copy(), view()) (3) | 2025.08.08 |
|---|---|
| [Numpy] random ๋์ ์์ฑ (rand, randint, randn, normal,seed) (2) | 2025.08.06 |
| [Numpy] ndarray์ axis ์ถ ๊ฐ๋ (2) | 2025.08.05 |
| [Numpy] Numpy์ ๊ฐ์ ๋ฐ ์ค์น (0) | 2024.07.03 |
| [Python ํ์ด์ฌ] Numpy ๋ฐฐ์ด ์์ฑ ๋ฐฉ๋ฒ (0) | 2024.07.03 |
- Total
- Today
- Yesterday
- *
- ndarray
- ํ์ด์ฌ
- Andrew Ng
- baekjoon
- ML Process
- ๋จธ์ ๋ฌ๋
- **kwargs
- ๊ฐํํ์ต
- ๊ฒฝ์ฌํ๊ฐ๋ฒ
- ML
- *args
- ๋ฅ๋ฌ๋
- NumPy
- ๋น์ฉํจ์
- ML ์ข ๋ฅ
- **
- ์ค๋๋ฅ์
- sorted
- Action spaces
- ML ํ๋ก์ธ์ค
- python
- ๋ฐฑ์ค
- ํด๋์ค ์ด ์ ๋ฆฌ
- ์์ฝ๋ฉ
- ๋ก์ง์คํฑ ํ๊ท
- ๊ฐ์๋ ธํธ ์ ๋ฆฌ
- Sort
- ์์คํจ์
- cnn
| ์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
