๐๐ป ์ ๋ฒ ์๊ฐ์ Logistic Regression Model์ด ๋ฌด์์ธ์ง, ๊ธฐ์ด ๊ฐ๋ ์ ๋ํด์ ํ์ตํ๋ค. (↓↓↓↓↓↓)
Logistic Regression Model (concepts)
** ML ๊ฐ์ ํฌ์คํ ์์ ๋ค๋ฃฌ 'supervised learning'์ ์๋์ ๊ฐ์ ์ ์ฐจ๋ฅผ ๋ฐ๋ฅธ๋ค๊ณ ํ๋ค (↓↓↓↓ํ๋จ ํฌ์คํ ์ฐธ์กฐ ↓↓↓↓) ML Supervised Learning → Regression → Linear Regression 1. ML ๊ธฐ๋ฒ ๊ตฌ๋ถ ๐..
sh-avid-learner.tistory.com
๐ ์ด์ python์ผ๋ก model์ ์ง์ ๊ตฌํํด๋ณด๋ ค ํ๋ค!
- binary classification problem solving์ ์ํด ํ์ํ ๋ชจ๋ธ์ด๋ผ ๋ฐฐ์ -
++ scikit-learn ์ฌ์ฉ ++
¶ sklearn.linear_model.LogisticRegression docu() ¶
class sklearn.linear_model.LogisticRegression(penalty='l2', *, dual=False, tol=0.0001, C=1.0, fit_intercept=True, intercept_scaling=1, class_weight=None, random_state=None, solver='lbfgs', max_iter=100, multi_class='auto', verbose=0, warm_start=False, n_jobs=None, l1_ratio=None)
> logistic regression ๋ชจ๋ธ์ ๋ค์ด๊ฐ๋ ์ฌ๋ฌ hyperparamter ์กฐํฉ์ ์ถํ ์์๋ณด์! (์ด๋ ต๋ค;; ๐ข) <
์ผ๋ถ๋ง ์์๋ณด์๋ฉด.
→ class_weight(default None): ๋ก์ง์คํฑ ํ๊ท์ data๋ฅผ ํ์ต์ํฌ ๋ target ๋ ๋ฒ์ฃผ ๋น์จ์ด ๋งค์ฐ ๋ถ๊ท ํํ ๊ฒฝ์ฐ ๊ท ์ผํ๊ฒ ํ์ต์์ผ ์ฑ๋ฅ์ ๋์ด๋ ๋ฐฉ๋ฒ์ด๋ค. 'balanced'๊ฐ์ ๋ฃ๊ฑฐ๋ ๊ฐ class ๋ณ ํ์ต์ํค๊ณ ์ถ์ ์ํ๋ ๊ฐ์ค์น๋ฅผ ๋ฃ์ผ๋ฉด ๋๋ค.
→ solver(default lbfgs): ๋ฌธ์ ๋ฅผ ์ต์ ํํ ๋ ์ฐ์ด๋ ์๊ณ ๋ฆฌ์ฆ์ ์ข
๋ฅ์ด๋ค. default๋ก๋ lbfgs๋ผ๋ ๊ธฐ์กด BFGS ์๊ณ ๋ฆฌ์ฆ์ limited-memory ๋ฒ์ ์ธ๋ฐ ๋ด๊ฐ ์ํ๋ ์๊ณ ๋ฆฌ์ฆ ์ข
๋ฅ๋ฅผ ์ ํํด ์ต์ ์ ๋ชจ๋ธ๋งํ ์ ์๊ฒ ๋ง๋ค์ด ์ค ์ ์๋ค. ์ด ๋ lbfgs๋ l2 ๊ท์ ๋ฅผ ์ฌ์ฉํ๋ค. ๋๋ฌด ์ด๋ ค์ด ์๊ณ ๋ฆฌ์ฆ์ด ๋ง์์ ์ ์ skip. (๋ ๊น๊ฒ ํ ์ง๋ ๋์ค์ ๊ฐ๋ฉด ์ ๊ฒ ๊ฐ๋ค?)
→ warm_start(default False): True๋ก ์ค์ ํ๋ฉด ์ด๋ฏธ ํ์ตํ๋ ๋ชจ๋ธ์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ์ผ๋ก ๋ค์์ ๋ ํ์ต ์ ํ์ต ์๋ฃ๋ก ์ ๊ณต์ด ๋๋ค. ์ฆ ๋์ ํด์ ํ์ต์์ผ์ฃผ๋ ๊ธฐ๋ฅ
→ max_iter(default 100): ์์์ ์ ํ solver algorithm์ ๋ฐํ์ผ๋ก ์๊ณ ๋ฆฌ์ฆ์ด ๊ฒฐ๊ณผ๋ฅผ ๋ด๊ธฐ ์ํด ๊ณ์ํด์ ๋์๊ฐ๋๋ฐ ์ด ๋ช ๋ฒ ๋๋ฆด ์ง ๊ทธ ๊ฐ์๋ฅผ ์ค์ ํ๋ ํ๋ผ๋ฏธํฐ. 100๋ฒ๋ณด๋ค ์กฐ๊ธ ๋ ๋ง์ด ๋๋ฆฌ๋ฉด ์คํ๋ ค ์ฑ๋ฅ์ด ์ฌ๋ผ๊ฐ๋ case๊ฐ ๋๋ฌ ์๋ค!
→ verbose: ๋ชจ๋ธ ๋๋ฆฌ๋ ๊ณผ์ ์์๋ณด๊ณ ์ถ์ ๋ ๊ณผ์ ๋ค ์ถ๋ ฅํด์ฃผ๋ ์ธ์
> ์ฃผ์ method <
→ ํ ๋ชจ๋ธ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก fitting - predicting์ ์ํด fit & predict method ์กด์ฌ
→ score method๋ก accuracy๋ฅผ ์ถ๋ ฅํด์ค๋ค (classification evaluation metrics์ค ํ๋)
→ predict_proba method๋ก feature๋ฅผ ์ง์ด๋ฃ์ผ๋ฉด ๊ฐ binary class์ ์ํ ํ๋ฅ ์ ๋ณด์ฌ์ค๋ค! ์ฆ, ๋ ํฐ ํ๋ฅ ๋ก ๋์จ ๊ฐ์ class๋ก ์์ธก๋จ
<Logistic Regression ์์ ์ค์ต (w/scikit-learn) (๊ธฐ์กด ์ ์ฐจ ์ฝ๊ฐ ๋ณ๊ฒฝ)>
โ 'Choose a class of model by importing the appropriate estimator class from Scikit-Learn.'
→ ์ด๋ค model ๋ถ๋ฅ๋ฅผ ์ฌ์ฉํ ์ง ๋ชจ๋ธ ๋ถ๋ฅ๋ฅผ ์ ํ๋ค!
(ex) ์ ํ๋ชจ๋ธ์ ๋ง๋ค๊ณ ์ถ์ผ๋ฉด sklearn์ linear_model์ importํ๋ฉด ๋จ!)
โก 'Choose model hyperparameters by instantiating this class with desired values.'
→ ๋ชจ๋ธ์ ์ธ์๋ฅผ ์ค์ ํ๋๊ฒ โ (ML term์์ ํต์ฉ๋๋ ๋ถ๋ถ - ํท๊ฐ๋ฆฌ์ง ๋ง๊ธฐ)
→ model ๋ถ๋ฅ(class)๋ฅผ ์ ํํ๋ค๋ฉด ํด๋น ๋ถ๋ฅ์ ์ด๋ค ๋ชจ๋ธ์ ์ ํํ ์ง ๊ณ ๋ฅด๋ ๋จ๊ณ
(ex) ์ ํ๋ชจ๋ธ์ import ํ๋ค๋ฉด ๋ค์ํ ์ ํ ๋ชจ๋ธ ์ค ํ ์ข ๋ฅ์ model์ import ํ๋ค! - ์๋ฅผ ๋ค์ด import LinearRegression)
→ ์ฌ๊ธฐ์ model์ ๊ณ ๋ฅด๋ ๊ณผ์ ์์ cross-validation ๊ธฐ๋ฒ์ ์ฌ์ฉํ๊ธฐ๋ ํจ
โข 'Arrange data into a features matrix and target vectorfollowing the discussion above.(+ ์ถ๊ฐ๋ก train/val/test ๋ถ๋ฆฌํ๊ธฐ)'
→ ์ 1.์์ ๋ฐฐ์ด ๋๋ก ๋ชจ๋ธ์ ๋ค์ด๊ฐ data ๋ ์ข ๋ฅ X์ y ์ค๋น!
โฃ 'baseline model์ ๊ธฐ๋ฐ์ผ๋ก metrics ์์น ํ์ธํ๊ธฐ'
→ ํ์ ๋ง๋ค์ด์ง ๋ชจ๋ธ ์ฑ๋ฅ์ด ๋ ์๋์์ผ ํ๋ฏ๋ก ๊ธฐ์ค์ด ๋๋ baseline model์ ๋ง๋ค๊ณ ์ฑ๋ฅ์ ํ์ธํ์
โค 'Fit the model to your data by calling the fit() method of the model instance.'
→ ์ด์ ์ฃผ์ด์ง X์ y๋ฅผ model์ fittingํด์ model ์์ฑ!
โฅ 'validation data ์ฑ๋ฅ์ ๋์ด๊ธฐ ์ํด โค ๊ณผ์ hyperparameter-tuning ๋ฌดํ๋ฐ๋ณต'
→ ์ต์ข ์ ์ผ๋ก ์๋ก์ด data๋ test data๋ฅผ model์ ๋ฃ๊ธฐ ์ ์ ์ต์ ์ ์ฑ๋ฅ์ ๋ด๋ model์ ๋ง๋ค๊ธฐ ์ํด hyperparameter-tuning ๊ณผ์ ๋ง์กฑํ ๋๊น์ง ๊ณ์ ์งํ
โฆ 'Apply the Model to new data (test data)'
โซ For supervised learning, often we predict labels for unknown data using the predict() method.
โซ For unsupervised learning, we often transform or infer properties of the data using the transform() or predict() method.
→ ์ง๋ํ์ต, ๋น์ง๋ํ์ต ํ์ต ์ข ๋ฅ์ ๋ฐ๋ผ ์ฝ๊ฐ ๋ค๋ฅด๋ค! - ํํผ ์๋ก์ด data๋ฅผ ์ง์ด๋ฃ์ด ์์ฑํ ๋ชจ๋ธ์ ์๊ฑฐํด ์์ธก๊ฐ์ ์์ฑํ์!
<์์>
Q. ์ ์ธ NBA ๋๊ตฌ์ ์์ ์ฌ๋ฌ ์คํฏ์ ํ์ ํด ์ด๋ฅผ ๋ฐํ์ผ๋ก ํฅํ 5๋ ๋ด์ ์ถ์ ์ด ๊ฐ๋ฅํ ์ง์ ์ฌ๋ถ๋ฅผ ํ๋ณํ๋ ๋ก์ง์คํฑ ํ๊ท ๋ถ๋ฅ๊ธฐ๋ฅผ ๊ตฌ์ถํ์
A. STEP-BY-STEP
โ โก ๋ก์ง์คํฑ ํ๊ท ๋ชจ๋ธ์ด๋ผ ์ ํ์!
โข ๋ฐ์ดํฐ - google dataset ์ฐธ์กฐ
> feature๋ ๋๊ตฌ ๊ด๋ จ ์ฌ๋ฌ ์์นํ data (์ด๋ฆ column๋ง ์ ์ธ)
> target์ 5๋ ๋ด์ ์ถ์ ์์ํ๋ค๋ฉด 1, ์๋๋ฉด 0์ธ binary class ํํ
> preprocessing + train/test ๋ถ๋ฆฌ + X์ y๋ถ๋ฆฌ
(๊ฐ๋จ ์์ ๋ผ validation data ์ ๋ง๋ฆ)
dataset = pd.read_csv('./data/nba_logreg.csv')
dataset.dropna(inplace=True) #๊ฒฐ์ธก์น ์๋ ํ ์ญ์
dataset.drop(columns=['Name'],inplace=True) #์ด๋ฆ column ์ญ์
#train, test
from sklearn.model_selection import train_test_split
train, test = train_test_split(dataset, random_state=2)
target = 'TARGET_5Yrs'
X_train = train.drop(columns=target)
y_train = train[target]
X_test = test.drop(columns=target)
y_test = test[target]
> scaling - StandardScaler ์ฌ์ฉ
๐ฝ scaling ์ถ์ฒ ์ด์ - logistic ํ๊ท์ ๊ฒฝ์ฐ ๋ชจ๋ feature๋ค์ ๋จ์ unit์ด ์ ๊ฐ๊ฐ ๋ค๋ฅด๋ฉด unit์ด ํฐ feature์ ์์ feature์ target ๊ธฐ์ฌ๋์ ์ฐจ์ด๊ฐ ๋ ์ ๋ฐ์ ์์ผ๋ฉฐ, ์์์ ์ ํด์ค solver๋ก optimizeํ๋ ๊ณผ์ ์์ converge, ์ฆ gradient descent๊ฐ converge ์๋ ดํ๋ ์๋๊ฐ ๋ชจ๋ feature๊ฐ ๋จ์๊ฐ ์ ๊ฐ๊ฐ์ด๋ฏ๋ก ๋ชจ๋ธ ์ฑ๋ฅ ์ธก์ ์ ์ํฅ์ ์ค ์ ๋ฐ์ ์๋ค.
๐คธโ๏ธ ๋ฐ๋ผ์ ์ฐ๋ฆฌ๋ scaling์ ํตํด ๋ชจ๋ feature๋ฅผ ๋์ผ unit ๋ฒ์๋ก ์ค์ ํ์ฌ ๋งค์ฐ ๋น ๋ฅด๊ณ ์ ํํ๊ฒ ๋ก์ง์คํฑ ๋ชจ๋ธ์ด ๋์๊ฐ๋๋ก ์๊ณ ๋ฆฌ์ฆ์ ์ต์ ํ๋ ์กฐ๊ฑด์ ์ ๊ณตํด์ค์ผ ํจ!
-- ๋ ์์ธํ ์ฌํญ์ normalization, standardization, regularization posting ์ฐธ๊ณ ํ๊ธฐ --
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
โฃ baseline model ์ฑ๋ฅ ํ์ธํ๊ธฐ - ๋ถํฌ ๋์ class ๊ธฐ์ค
#baseline ๋ง๋ค๊ธฐ
major = y_train.mode()[0]
y_pred_baseline = [major] * len(y_train)
from sklearn.metrics import accuracy_score
print("baseline model accuracy: ", accuracy_score(y_train, y_pred_baseline))
#baseline model accuracy: 0.6204819277108434
โค fitting
> ์ธ์ ํ๋์ ํน๋ณํ ํ์ง ์์๋ค. (โฅ hyperparameter tuning - validation ์๋ตํจ)
#LR
from sklearn.linear_model import LogisticRegression
logistic = LogisticRegression()
# ํ์ต
logistic.fit(X_train_scaled, y_train)
โฆ predicting - testing
# ์์ธก
pred_lr = logistic.predict(X_test_scaled)
print("LR accuracy: ", accuracy_score(y_test, pred_lr))
#LR accuracy: 0.7027027027027027
๐คน modelling ๊ฒฐ๊ณผ baseline accuracy๋ ์ฝ 0.62์๋๋ฐ modeling accuracy๋ ์ฝ 0.7์ด ๋์ด (์ฑ๋ฅ ํฅ์!)
- ๋ก์ง์คํฑ ๋ชจ๋ธ ํจ๊ณผ ์ฆ๋ช ! -
++ logistic coefficients ๋ถ์ ++
# plot logistic model coefficients
coefficients = pd.Series(logistic.coef_[0], X_train.columns)
plt.figure(figsize=(6,6))
coefficients.sort_values().plot.barh()
plt.show()
- logistic coefficients -
โน๏ธโ๏ธ logistic์ coefficients ํด์ - ํด๋น X feature ๋ณ์๊ฐ unit 1์ฉ ์ฆ๊ฐํ๋ฉด Odds ๋น(p/(1-p))๊ฐ e์ coefficients์น์ฉ ๋ณํํ๋ค.
(scaling์ ๊ฑฐ์น๊ณ ๋ ๋ค์ด๋ฏ๋ก feature๋ณ๋ก ๋ชจ๋ ๋จ์๊ฐ ๊ฐ์์ coefficients ๊ฐ ๋น๊ต๊ฐ ๊ฐ๋ฅ!)
๐ ๋ฐ๋ผ์ coefficient๊ฐ์ด 0์ด์ ์์์ด๋ฉด Odds ๋น๊ฐ 1์ด์ = ์ฆ p > (1-p)๋ก ์ฑ๊ณตํ๋ฅ ์ ๋ ๊ธฐ์ฌํ๋ feature
๐ ๋ฐ๋๋ก 0 ๋ฏธ๋ง ์์์ธ feature๋ Odds ๋น๊ฐ 1์ดํ = ์ฆ p < (1-p)๋ก ์คํจํ๋ฅ , ์ฆ class 0์ ๋ ๊ธฐ์ฌํ๋ feature
→ ๋ฐ๋ผ์ ์ ๋ชจ๋ธ์ ๊ฒฝ์ฐ 3P Made, GP, FGA feature๊ฐ ์ ์ธ ํ๋ ์ด์ด๊ฐ 5๋ ๋์ ์ ๋๊ฐ๋ค๋ ์ ์ ํฐ ๊ธฐ์ฌ. ํนํ 3P Made, ์ฆ 3์ ์ฑ๊ณต์ฌ๋ถ๊ฐ ์ง๋ํ ์ํฅ์ ๋ฏธ์น๋ค๋ ๊ฑธ ํ์ธํ ์ ์์!
→ 3PA๋ 5๋
์ปค๋ฆฌ์ด ์ฌ๋ถ์ ์ ์ผ ๋ฐ๋๋ก ์ํฅ์ ๋ฏธ์น๋ ํน์ฑ์(์ํฅ์ ๋ฏธ์น์ง ์๋๊ฒ ์๋. ์ํฅ์ ๋ฐ๋๋ก ๋ฏธ์นจ)
* ๋ฐ์ดํฐ ์ถ์ฒ) https://data.world/exercises/logistic-regression-exercise-1
* ์ถ์ฒ1) coefficients https://soobarkbar.tistory.com/12 / https://dive-into-ds.tistory.com/44
* ์ถ์ฒ2) scaling ํ์์ฑ https://stats.stackexchange.com/questions/48360/is-standardization-needed-before-fitting-logistic-regression
'Machine Learning > Models (with codes)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
K-Means Clustering (concepts + w/code) (0) | 2022.06.08 |
---|---|
Decision Trees (concepts) (0) | 2022.04.27 |
Polynomial Regression Model (0) | 2022.04.24 |
Logistic Regression Model (concepts) (0) | 2022.04.24 |
(L2 Regularization) โ Ridge Regression (w/scikit-learn) (0) | 2022.04.20 |
๋๊ธ