14.决策边界
约 198 字小于 1 分钟
2025-09-20

from sklearn.datasets import make_classification
import numpy as np
from sklearn.model_selection import train_test_split
from matplotlib import pyplot as pltX,y = make_classification(
n_samples=200,
n_features=2,
n_redundant=0,
n_informative=2,
)
x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=42)from sklearn.linear_model import LogisticRegression
lrg = LogisticRegression()
lrg.fit(x_train, y_train)
lrg.score(x_test, y_test)运行结果
0.9
plt.scatter(x_train[:,0],x_train[:,1],c=y_train)
plt.show()
lrg.coef_,lrg.intercept_运行结果
(array([[-0.77217001, 2.58603312]]), array([-0.10393696]))
w1x1+w2x2+b=0 转化得到x2=(−w1x1−b)/w2
# 绘制决策边界
x1 = np.linspace(-4,4,1000)
x2 = (-lrg.coef_[0][0]*x1 - lrg.intercept_[0])/lrg.coef_[0][1]
plt.plot(x1,x2)
plt.scatter(X[:,0],X[:,1],c=y)
plt.show()
