#实现逻辑回归
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
iris = datasets.load_iris()
X = iris.data
y = iris.target
X = X[y<2,:2]
y = y[y<2]
plt.scatter(X[y==0,0],X[y==0,1],color = 'red')
plt.scatter(X[y==1,0],X[y==1,1],color = 'blue')
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=666)
from sklearn.linear_model import LogisticRegression
log_reg = LogisticRegression()
log_reg.fit(X_train,y_train)
log_reg.score(X_test,y_test)
1.0
print(log_reg.predict_proba(X_test))
[[0.09391473 0.90608527]
[0.07585788 0.92414212]
[0.79176992 0.20823008]
[0.93705398 0.06294602]
[0.89573287 0.10426713]
[0.95218244 0.04781756]
[0.86675827 0.13324173]
[0.01504783 0.98495217]
[0.02499892 0.97500108]
[0.29394692 0.70605308]
[0.8294444 0.1705556 ]
[0.9617504 0.0382496 ]
[0.74466265 0.25533735]
[0.89573287 0.10426713]
[0.11837917 0.88162083]
[0.35916592 0.64083408]
[0.24085869 0.75914131]
[0.68831525 0.31168475]
[0.73479882 0.26520118]
[0.79387392 0.20612608]
[0.91851748 0.08148252]
[0.68279272 0.31720728]
[0.02499892 0.97500108]
[0.01448875 0.98551125]
[0.89810141 0.10189859]]
print(y_test)
[1 1 0 0 0 0 0 1 1 1 0 0 0 0 1 1 1 0 0 0 0 0 1 1 0]
print(log_reg.predict(X_test))
[1 1 0 0 0 0 0 1 1 1 0 0 0 0 1 1 1 0 0 0 0 0 1 1 0]
print(log_reg.coef_)
[[ 2.78090985 -2.71686283]]
def x2(x1):
return (-log_reg.coef_[0][0] * x1 - log_reg.intercept_) / log_reg.coef_[0][1]
x1_plot = np.linspace(4,8,1000)
x2_plot = x2(x1_plot)
#print(x2_plot)
plt.scatter(X[y==0,0],X[y==0,1],color = 'red')
plt.scatter(X[y==1,0],X[y==1,1],color = 'blue')
plt.plot(x1_plot,x2_plot)
plt.scatter(X_test[y_test==0,0],X_test[y_test==0,1],color = 'red')
plt.scatter(X_test[y_test==1,0],X_test[y_test==1,1],color = 'blue')
plt.plot(x1_plot,x2_plot)
绘制决策边界
#绘制决策边界
def plot_decision_boundary(model, axis):
x0, x1 = np.meshgrid(
np.linspace(axis[0], axis[1], int((axis[1]-axis[0])*100)).reshape(-1, 1),
np.linspace(axis[2], axis[3], int((axis[3]-axis[2])*100)).reshape(-1, 1),
)
X_new = np.c_[x0.ravel(), x1.ravel()]
y_predict = model.predict(X_new)
zz = y_predict.reshape(x0.shape)
from matplotlib.colors import ListedColormap
custom_cmap = ListedColormap(['#EF9A9A','#FFF59D','#90CAF9'])
plt.contourf(x0, x1, zz, linewidth=5, cmap=custom_cmap)
plot_decision_boundary(log_reg,axis=[4,7.5,1.5,4.5])
plt.scatter(X[y==0,0],X[y==0,1],color = 'red')
plt.scatter(X[y==1,0],X[y==1,1],color = 'blue')
#KNN决策边界
from sklearn.neighbors import KNeighborsClassifier
knn_clf = KNeighborsClassifier()
knn_clf.fit(X_train,y_train)
plot_decision_boundary(knn_clf,axis=[4,7.5,1.5,4.5])
plt.scatter(X[y==0,0],X[y==0,1],color = 'red')
plt.scatter(X[y==1,0],X[y==1,1],color = 'blue')
三种分类
knn_clf_all = KNeighborsClassifier()
knn_clf_all.fit(iris.data[:,:2],iris.target)
plot_decision_boundary(knn_clf_all,axis=[4,8.5,1.5,4.5])
plt.scatter(iris.data[iris.target==0,0],iris.data[iris.target==0,1],color = 'red')
plt.scatter(iris.data[iris.target==1,0],iris.data[iris.target==1,1],color = 'green')
plt.scatter(iris.data[iris.target==2,0],iris.data[iris.target==2,1],color = 'blue')
#增大n
knn_clf_all = KNeighborsClassifier(n_neighbors=50)
knn_clf_all.fit(iris.data[:,:2],iris.target)
plot_decision_boundary(knn_clf_all,axis=[4,8.5,1.5,4.5])
plt.scatter(iris.data[iris.target==0,0],iris.data[iris.target==0,1],color = 'red')
plt.scatter(iris.data[iris.target==1,0],iris.data[iris.target==1,1],color = 'green')
plt.scatter(iris.data[iris.target==2,0],iris.data[iris.target==2,1],color = 'blue')
#1、 模型的复杂与简单与决策边界的直接练习。
#通过决策边界可视化看到模型是否存在过拟合。