조금씩 꾸준히 완성을 향해

[Sklearn] 로지스틱 회귀(Logistic Regression) 본문

AI/Machine Learning

[Sklearn] 로지스틱 회귀(Logistic Regression)

all_sound 2022. 10. 29. 23:32

Logistic Regression


# 데이터 준비
import pandas as pd
fish = pd.read_csv('http://bit.ly/fish_csv_data')
fish.head()

  • z = a*무게 + b*길이 + c*대각선 + d*높이 + e두께 + f
from sklearn.model_selection import train_test_split

fish_input = fish[['Weight', 'Length', 'Diagonal', 'Height', 'Width']].to_numpy()
fish_target = fish['Species'].to_numpy()
train_input, test_input, train_target, test_target = train_test_split(fish_input, fish_target, stratify=fish_target, random_state=42)
# 표준화 전처리
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
sc.fit(train_input)
train_scaled = sc.transform(train_input)
test_scaled = sc.transform(test_input)

 

▶ 이진 분류

# 데이터 선택
bream_smelt_indexes = (train_target == 'Bream') | (train_target =='Smelt')
train_bream_smelt = train_scaled[bream_smelt_indexes]
target_bream_smelt = train_target[bream_smelt_indexes]
# 모델 생성, 학습, 예측
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression()
lr.fit(train_bream_smelt, target_bream_smelt)

print(lr.predict(train_bream_smelt[:5]))
print(lr.predict_proba(train_bream_smelt[:5]))

print(lr.coef_, lr.intercept_) # 가중치와 절편 출력
# [[-0.4235112  -0.61604834 -0.70216369 -0.97498265 -0.7403996 ]] [-2.46732659]

 

  • Sigmoid 함수
# z 값 출력 (양성클래스에 대한 값만 선택)
decisions = lr.decision_function(train_bream_smelt[:5])
print(decisions)
# [-3.7167051  -7.86053208 -5.18626807 -2.30377249 -7.35578257]
# z값을 sigmoid 함수에 넣어 출력
from scipy.special import expit
print(expit(decisions))
# [0.02373681 0.00038552 0.00556173 0.09081101 0.00063848]

 

▶ 다중 분류

  • 2진 분류를 여러번 실행(OvR : One vs Rest)
lr = LogisticRegression(C=20, max_iter=1000)
# max_iter : 반복횟수(default=100),
# C : L2규제 강도(defalut=1, 숫자와 규제는 반비례)

lr.fit(train_scaled, train_target)
print(lr.score(train_scaled, train_target)) # 0.9243697478991597
print(lr.score(test_scaled, test_target)) # 0.925
proba = lr.predict_proba(test_scaled[:5])
print(np.round(proba, decimals=3))

print(lr.coef_.shape, lr.intercept_.shape)
# (7, 5) (7,)

 

  • SoftMax 함수
decision = lr.decision_function(test_scaled[:5])
print(np.round(decision, decimals=2))

from scipy.special import softmax

proba = softmax(decision, axis=1)
print(np.round(proba, 3))
# predict_proba 함수와 같은 결과를 확인 할 수 있음.

 

 

 

 

  • 참고 - [한빛미디어] 혼자 공부하는 머신러닝+딥러닝, 박해선 저