1 분 소요

원본 사이트: https://scikit-learn.org/stable/auto_examples/linear_model/plot_logistic.html

로지스틱 함수

이 합성 데이터 세트로 로지스틱 회귀가 로지스틱 곡선을 사용하여 값을 0 또는 1, 즉 클래스 1 또는 2로 분류하는 방법이 그래프에 표시됩니다.

image.png

# Code source: Gael Varoquaux
# License: BSD 3 clause

import numpy as np
import matplotlib.pyplot as plt

from sklearn.linear_model import LogisticRegression, LinearRegression
from scipy.special import expit     # expit(x) = 1/(1+exp(-x))

# 약간의 가우스 잡음이 있는 직선인 토이 데이터 세트를 생성:
xmin, xmax = -5, 5
n_samples = 100
np.random.seed(0)   # 매 실행마다 같은 결과를 내기 위한 seed 설정
X = np.random.normal(size=n_samples)    # (100,) 정규분포 추출
y = (X > 0).astype(float)               # (100,) 0 또는 1
X[X > 0] *= 4
X += 0.3 * np.random.normal(size=n_samples) # 잡음 추가

X = X[:, np.newaxis]                    # (100,) -> (100, 1)

# 분류기 학습
clf = LogisticRegression(C=1e5)         # 규제가 있는 로지스틱 회귀 분류기
clf.fit(X, y)

# 결과 출력
plt.figure(1, figsize=(4, 3))
plt.clf()
plt.scatter(X.ravel(), y, color="black", zorder=20)
X_test = np.linspace(-5, 10, 300)      # (300,)

loss = expit(X_test * clf.coef_ + clf.intercept_).ravel()
plt.plot(X_test, loss, color="red", linewidth=3)

ols = LinearRegression()
ols.fit(X, y)
plt.plot(X_test, ols.coef_ * X_test + ols.intercept_, linewidth=1)
plt.axhline(0.5, color=".5")

plt.ylabel("y")
plt.xlabel("X")
plt.xticks(range(-5, 10))
plt.yticks([0, 0.5, 1])
plt.ylim(-0.25, 1.25)
plt.xlim(-4, 10)
plt.legend(
    ("Logistic Regression Model", "Linear Regression Model"),
    loc="lower right",
    fontsize="small"
)
plt.tight_layout()
plt.show()
<Figure size 288x216 with 1 Axes>

ⓒ 2007 - 2021, scikit-learn developers (BSD License). Show this page source

댓글남기기