Logistic Regression 3-class Classifier
원본 사이트: https://scikit-learn.org/stable/auto_examples/linear_model/plot_iris_logistic.html
로지스틱 회귀 3가지 클래스 분류
다음은 iris 데이터 세트의 처음 2개 특성(꽃받침 길이 및 너비)에 대한 로지스틱 회귀 분류기 결정 경계를 나타냅니다. 데이터 포인트는 레이블에 따라 색상이 지정됩니다.
# Code source: Gaël Varoquaux
# Modified for documentation by Jaques Grobler
# License: BSD 3 clause
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn import datasets
# 다룰 데이터를 불러옵니다.
iris = datasets.load_iris()
X = iris.data[:, :2] # 처음 2개의 특성만을 다룹니다.
Y = iris.target
# 로지스틱 회귀 객체를 생성하고 데이터를 학습시킵니다.
logreg = LogisticRegression(C=1e5)
logreg.fit(X, Y)
# 결정 경계를 나타내고 각 레이블에 맞는 색을 할당합니다.
# [x_min, x_max] x [y_min, y_max]에 해당하는 모든 좌표를 xx, yy에 할당합니다.
x_min, x_max = X[:, 0].min() - 0.5, X[:, 0].max() + 0.5
y_min, y_max = X[:, 1].min() - 0.5, X[:, 1].max() + 0.5
h = 0.02 # 좌표 간 간격
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
Z = logreg.predict(np.c_[xx.ravel(), yy.ravel()])
# 그래프에 결과(결정 경계) 표시
Z = Z.reshape(xx.shape)
plt.figure(1, figsize=(4, 3))
plt.pcolormesh(xx, yy, Z, cmap=plt.cm.Paired)
# 학습 데이터 표시
plt.scatter(X[:, 0], X[:, 1], c=Y, edgecolors="k", cmap=plt.cm.Paired)
plt.xlabel("Sepal length")
plt.ylabel("Sepal width")
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.xticks(())
plt.yticks(())
plt.show()
<Figure size 288x216 with 1 Axes>
ⓒ 2007 - 2021, scikit-learn developers (BSD License). Show this page source
댓글남기기