a = np.array([0, 1, 2, 3, 4, 5])
μ€μ΅μ μν μμμ λ°°μ΄μ μμ±νκ³ μ΄ λ°°μ΄λ‘ μ΄κ²μ κ² ν΄λ³΄λ €κ³ νλ€.
shape/ndim/size
print("shape: ", a.shape) # μμ (6, 1)
print("ndim: ", a.ndim) # μμ 2
print("size: ", a.size) # μμ 6
β» shape : (ν, μ΄)μ λ€μ§μ ννλ‘ λνλ → (6, 1) = (1ν, 6μ΄)
β» ndim : λ°°μ΄ μ°¨μ
β» size : λ°°μ΄μ μμ κ°μ
μ΄μ΄ νλμΌ κ²½μ°λ shapeμμ 1μ΄ μ°νμ§ μλλ€λ κ²μ΄ νΉμ§! μμ§λ μ°¨μ κ°λ μ΄ λ무 μ΄λ ΅λ€ γ γ ν μ μ°¨μμ΄λ κ°κ² μκ°ν΄μ μ€μΉΌλΌκ° 1μ°¨μ, λ°°μ΄μ΄λκΉ 2μ°¨μμ΄λΌκ³ μκ°νλλ° 1μ°¨μμ΄μμ. κ·Έλ₯ νμ΄μ¬ μμ λ°°μ΄ κ°λ μ΄λ λκ°μλ€.
shape μΈλ±μ±
# print("shape[1]: ", a.shape[1]) IndexError
print("shape[0]: ", a.shape[0])
print("shape[-1]: ", a.shape[-1])
(6, 1)μ΄λκΉ shape[-1] νλ©΄ 1 λμ€κ² μ§ νλλ° λ μ¬λμ μμ μΌλ‘ μκ°ν΄λ²λ Έλ€!^^ νμ΄ νλμΌ λλ 1μ΄ μ°νμ§ μμΌλκΉ νν μμκ° ν κ°λΌμ μΈλ±μ€ 0λ² μμμ μΈλ±μ€ -1λ² μμκ° λμΌνλ€.
reshape
a_reshape = a.reshape(3, 2)
print("reshape: ", a_reshape) # μμ
# [[0, 1, 2], [3, 4, 5]
β» reshape : νλ ¬μ μ°¨μμ μνλ λͺ¨μμΌλ‘ λ°κΏ μ μμΌλ©° μΈμ μμλλ‘ ν, μ΄μ΄λ€.
β» reshape λ°©λ² : κΈ°μ‘΄ λ°°μ΄.reshape(ν, μ΄) / np.reshape(κΈ°μ‘΄ λ°°μ΄, (ν, μ΄))
1ν 6μ΄μ λ°°μ΄μ΄ 3ν 2μ΄λ‘ λ°λμλ€. μμ κ²°κ³Όλ reshape κ²μν΄λ³΄κ³ μ μ 건λ°λ νλ¦Ό
axis
print("axis=0 sum: ", a_reshape.sum(axis=0)) # ν
print("axis=1 sum: ", a_reshape.sum(axis=1)) # μ΄
sum ν¨μ μΈμκ° axis=0μ΄λ©΄ ν λ°©ν₯μΌλ‘ μμλ₯Ό ν©νκ³ axis=1μ΄λ©΄ μ΄ λ°©ν₯μΌλ‘ μμλ₯Ό ν©νλ€. μΆμ νμ λ°°μ΄λ³΄λ€ μμ κ°μ κ°λλ€(λ°°μ΄ 3μ°¨μμ΄λ©΄ axis μ΅λκ°μ 2).
expand_dims
expand_arr = np.array([1, 3])
# expand_dims: μ°¨μ μ¦κ°
expand_zero = np.expand_dims(expand_arr, axis=0) # [[1, 3]]
expand_one = np.expand_dims(expand_arr, axis=1) # [[1], [3]]
print(f"expand_arr shape: {expand_arr.shape}, expand_arr ndim: {expand_arr.ndim}")
print("axis expand_dims=0: ", expand_zero)
print("axis expand_dims=1: ", expand_one)
print(f"expand_zero shape: {expand_zero.shape}, expand_zero ndim: {expand_zero.ndim}")
print(f"expand_one shape: {expand_one.shape}, expand_one ndim: {expand_one.ndim}")
λͺ¨λ₯Ό λλ μμ μ°μ΄λ³΄λ κ²μ΄ κ°μ₯ μ’μ λ°©λ²μ΄λΌκ³ μκ°νλ€. μμ μ½λλ₯Ό νλμ© νμ΄μ μ 리ν΄λ³΄λ©΄!
- expand_arr : 1μ°¨μ λ°°μ΄(ndim=1), shape (2, )
- expand_zero : axis=0μ΄λ―λ‘ μ²« λ²μ§Έ μΆμ μ°¨μ μΆκ°, 1ν 2μ΄μ 2μ°¨μ λ°°μ΄
- expand_one : axis=1μ΄λ―λ‘ λ λ²μ§Έ μΆμ μ°¨μ μΆκ°, 2ν 1μ΄μ 2μ°¨μ λ°°μ΄
- expand_zero shape : 첫 λ²μ§Έ μΆμ μ°¨μμ μΆκ°νμΌλ―λ‘ (1, 2)
- expand_one shape : λ λ²μ§Έ μΆμ μ°¨μμ μΆκ°νμΌλ―λ‘ (2, 1)
β expand_dimsμ μΆ μ°¨μ μΆκ° κ°λ μ μ΄ κ³³μ μ°Έκ³ νλ€.
'Artificial Intelligence > π' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[μ 리] Numpy β‘ : squeeze (0) | 2022.04.20 |
---|---|
[μ 리] train_test_splitμ μ΄μ©ν λ°μ΄ν°μ λΆν (0) | 2022.03.17 |
[κ°λ ] ν¬μ νν / λ°μ§ νν (0) | 2022.03.10 |
[μ 리] Encoding κ΄λ ¨ API (0) | 2022.01.28 |
[κ°λ ] object detection κ΄λ ¨ μ©μ΄ μ 리 (1) (0) | 2021.11.16 |