
์ด๋ฒ ์ฃผ ํ์ต ์ ๋ฆฌ
์ด๋ฒ ์ฃผ์๋ Titanic ๋ฐ์ดํฐ์
๊ณผ MNIST ๋ฐ์ดํฐ์
์ ํตํด
๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ(Pandas & Numpy)์ MLP(๋ค์ธต ํผ์
ํธ๋ก ) ๊ธฐ๋ณธ๊ธฐ๋ฅผ ์ตํ
| ๋ฐฉ๋ฒ | ์์ ์ฝ๋ | ํน์ง |
|---|---|---|
| Boolean Index | df[df['Age']>30] | ์กฐ๊ฑด True/False๋ก ํํฐ๋ง |
| loc | df.loc[df['Age']>30, ['Name','Age']] | ์กฐ๊ฑด + ํน์ ์ด๋ง ์ ํ |
| isin | df[df['Name'].isin(['John','Mary'])] | ๋ฆฌ์คํธ ๊ฐ ํํฐ๋ง |
| isin+loc | df.loc[df['Name'].isin(my_condition), 'Age'] | ์กฐ๊ฑด + ํน์ ์ด |
df['Age'].isna() # True/False
df['Age'].isnull().sum() # NaN ๊ฐ์
df['Age'].notnull() # ๊ฒฐ์ธก ์๋ ๊ฐ
df['Cabin'] = df['Cabin'].fillna('U')
| ํญ๋ชฉ | map() | apply() |
|---|---|---|
| ๋์ | iterable ์ ์ฒด | Series/DataFrame |
| ๋ฐํ | map ๊ฐ์ฒด | Series/DataFrame |
| ์ ๋ ฅ์ผ๋ก ํ์ฉ | ํจ์, dict, Series(๋งคํ ๊ฐ๋ฅ ๊ฐ์ฒด) | ํจ์๋ง (dict๋ Series ๋งคํ X) |
pd.to_numeric(errors='coerce') โ ๋ณํ ๋ถ๊ฐ ์ NaNastype()pd.cut()df.describe(), df['col'].mean() ๋ฑ| ๊ตฌ๋ถ | NumPy ๋ฐฐ์ด (ndarray) | Pandas ๋ฐ์ดํฐํ๋ ์ (DataFrame) |
|---|---|---|
| ๊ตฌ์กฐ | ๋ค์ฐจ์ ๋ฐฐ์ด (๋ฒกํฐยทํ๋ ฌยทํ ์) | 2์ฐจ์ ํ ์ด๋ธ ๊ตฌ์กฐ |
| ์๋ฃํ | ๋์ผํ ์๋ฃํ(int, float)๋ง | ์ด๋ง๋ค ์๋ก ๋ค๋ฅธ ์๋ฃํ ๊ฐ๋ฅ |
| ์ธ๋ฑ์ฑ | ์ ์ ๊ธฐ๋ฐ | ๋ ์ด๋ธ ๊ธฐ๋ฐ(ํ/์ด) |
| ํน์ง | ๋น ๋ฅธ ์์น ์ฐ์ฐ, ๋ธ๋ก๋์บ์คํ | groupby, merge, join, pivot ๋ฑ ๊ณ ๊ธ ๊ธฐ๋ฅ |
arange + reshapeimport numpy as np
d = np.arange(12).reshape(3,4)
print(d, d.shape)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]] (3, 4)
1์ฐจ์
a = np.arange(0,5,2)
print(a, a.shape)
2์ฐจ์
m = np.array([np.arange(0,4,3),
np.arange(1,5,3),
np.arange(2,6,3)])
print(m, m.shape)
# [[0 3]
# [1 4]
# [2 5]] (3,2)
3์ฐจ์
m = np.array([[[0,1,2],
[3,4,5]],
[[0,1,2],
[3,4,5]]])
print(m.shape)
# (2,2,3)
๐ก Python range() vs NumPy arange()
โ arange()๋ ์ค์ ๊ฐ๊ฒฉ๋ ๊ฐ๋ฅ, NumPy ๋ฐฐ์ด ๋ฐํ.
m1 = np.arange(6)
m2 = m1.reshape(6,1) # 6ํ 1์ด
m3 = m1.reshape(2,3) # 2ํ 3์ด
m4 = m1.reshape(2,2,3) # 2๋ฉด 2ํ 3์ด
ํ ์๋ฅผ -1๋ก ์ฃผ๋ฉด ์๋ ๊ณ์ฐ:
m2 = m1.reshape(-1,2) # 2์ด, ํ์ ์๋
flat = m4.flatten()
arr[::-1] # ํ ๋ฐฉํฅ ๋ค์ง๊ธฐ
arr[:, ::-1] # ์ด ๋ฐฉํฅ ๋ค์ง๊ธฐ
| ํ ๋ฐฉํฅ | ์ด ๋ฐฉํฅ |
|---|---|
vstack(), row_stack(), concatenate(..., axis=0) | hstack(), column_stack(), concatenate(..., axis=1) |
| ํ ๋จ์ ๋ถํ | ์ด ๋จ์ ๋ถํ |
|---|---|
vsplit(arr, n) | hsplit(arr, n) |
split(arr, n, axis=0) | split(arr, n, axis=1) |
๋ธ๋ก๋์บ์คํ : ์ฐจ์์ ํฌ๊ธฐ๊ฐ ๋ค๋ฅธ ๋ฐฐ์ด๋ผ๋ฆฌ ์ฐ์ ์ฐ์ฐ์ด ๊ฐ๋ฅํ๋๋ก ํ๋ ๊ฒ.
A = np.array([[1,2,3],
[4,5,6]])
B = np.array([[2],
[4]])
print(A+B)
[[ 3 4 5]
[ 8 9 10]]
Softmax๋ฅผ ์จ์ ํ๋ฅ ๋ถํฌ๋ก ๋ณํ.Sigmoid ๋ฅผ ์ฌ์ฉํ์ฌ 0~1 ํ๋ฅ ๋ก ๋ณํ์์ ํ(Forward)๋ก ์ ๋ ฅโ์ถ๋ ฅ, ์ญ์ ํ(Backpropagation)๋ก ๊ฐ์ค์น ์ ๋ฐ์ดํธ
๐น Iris ๋ฐ์ดํฐ
from sklearn.neural_network import MLPClassifier
mlp = MLPClassifier(hidden_layer_sizes=(10,),
activation='logistic',
solver='lbfgs')
mlp.fit(X_train, y_train) # ํ์ต
# ์์ ๋ฐ์ดํฐ์
(150๊ฐ) โ ์์ ์๋์ธต์ผ๋ก๋ ๋์ ์ ํ๋
๐น MNIST ๋ฐ์ดํฐ
from sklearn.datasets import fetch_openml
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
mnist = fetch_openml('mnist_784', version=1, as_frame=False)
X, y = mnist.data / 255.0, mnist.target.astype(int)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
mlp = MLPClassifier(hidden_layer_sizes=(128,64),
activation='relu',
solver='adam',
max_iter=100)
mlp.fit(X_train, y_train)
fetch_openml('mnist_784') : 28ร28 ํฝ์
โ 784 ๋ฒกํฐ
์ ๊ทํ: /255.0 โ 0~1 ์ค์ผ์ผ๋ง(scaling).
๋ผ๋ฒจ ๋ณํ: .astype(int)
์๋์ธต: ๊ณ ์ฐจ์ ๋ฐ์ดํฐ โ ํฐ ๋คํธ์ํฌ(128โ64) ์ค์
hidden_layer_sizes ์ ์ ๋ฌํ๋ ํํ(tuple) ์ ๊ฐ ์์๋ ๊ฐ ์๋์ธต์ ๋ด๋ฐ(์ ๋) ๊ฐ์๋ฅผ ์๋ฏธ.
(100, 50) ์
(3,) ์ ์๋์ธต 1๊ฐ, ๋ด๋ฐ 3๊ฐ. (5, 3, 2) ๋ ์๋์ธต 3๊ฐ(5โ3โ2).
๋ก์ง์คํฑ ํ๊ท โ "์ฑ๋ณ์ด ์์กด์ ๋ฏธ์น๋ ์ํฅ์ด ์ผ๋ง๋ ํฐ๊ฐ?" (๊ณ์ ํด์, ์ค์ฆ๋น Odds Ratio, AUC/Recall ์ค์ ): ์ ํ ๋ถ๋ฅ ๋ชจ๋ธ, ์์ธก ํ๋ฅ ์ ์ง์ ์ ๊ณต
๐ ์ค์ํ ํ๊ฐ์งํ
๋๋ค ํฌ๋ ์คํธ โ "์ฑ๋ณ, ์๊ธ, ๋์ด, ๊ฐ์กฑ ์ ์ค ์ด๋ค ๋ณ์๊ฐ ๊ฐ์ฅ ์ค์ํ๊ฐ?" (Feature Importance, Accuracy ์ค์ ): ์์๋ธ ํธ๋ฆฌ ๊ธฐ๋ฐ ๋ชจ๋ธ, ๋ณ์ ์ค์๋๋ฅผ ์ฐ์ถ
๐ ์ค์ํ ํ๊ฐ์งํ
๐ ๋ฐ๋ผ์,
โ๏ธ ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ:
[Pandas] ์กฐ๊ฑด ๊ฒ์, ๊ฒฐ์ธก์น ์ฒ๋ฆฌ, ๋ฐ์ดํฐํ ๋ณํ [Numpy] ๋ฐฐ์ด ์กฐ์, ๋ฐฐ์ด ์ฐ์ฐ(๋ธ๋ก๋์บ์คํ
)โ๏ธ ์ ๊ฒฝ๋ง ๊ธฐ์ด: [์
๋ ฅ์ธต]์
๋ ฅโ [์๋์ธต] ๊ฐ์ค์นโํ์ฑํ โ [์ถ๋ ฅ์ธต] ์ถ๋ ฅ ์์
โ๏ธ ์ฌ์ดํท๋ฐ์ ํตํ ์ ๊ฒฝ๋ง ๊ตฌํ:
mlp = MLPClassifier(hidden_layer_sizes=(128, 64), # ์๋์ธต 2๊ฐ (128 โ 64)
activation='relu',
solver='adam', #๋ชจ๋ธ ํ์ต ๋ฐฉ๋ฒ
max_iter=100, # ๋ฐ๋ณต ํ์ (20 โ 100 ์คํ ๊ฐ๋ฅ)
random_state=42, #์ด๊ธฐ ๊ฐ์ค์น ๊ฐ ๊ณ ์
verbose=True)
โ๏ธ ๋ก์ง์คํฑ ํ๊ท vs ๋๋คํฌ๋ ์คํธ: ํด์๋ ฅ vs ์์ธก๋ ฅ
โ๏ธ ์ดํ: ์ค์ตํด๋ณด๋ฉด์ ๊ณ์ ์์ฃผ ์ฐ์ด๋ ํจ์/ํจํค์ง๋ค์ ์ตํ๊ณ ๋ชจ๋ธ๋ค์ ํน์ฑ์ ์ตํ์ ์ด๋จ ๋ ์ฌ์ฉํ๋ฉด ์ข์์ง ๋ฐ๋ก ์๋ ๋ฅ๋ ฅ์ ๊ธฐ๋ฅด๋ ๊ฒ ์ค์ํ ๋ฏ. ๋ค์์ฃผ๋ ํ์ดํ ~