模拟逻辑回归

  1. import numpy as np
  2. from matplotlib import pyplot as plt
  3. ef sigmod(t):
  4. return 1 / (1 + np.exp(-t))
  5. plt.plot(x, y)

image.png

  1. from sklearn import datasets
  2. iris = datasets.load_iris()
  3. X = iris.data
  4. y = iris.target
  5. X=X[y<2,:2]
  6. y=y[y<2]
  7. plt.scatter(X[y==0, 0], X[y==0, 1])
  8. plt.scatter(X[y==1, 0], X[y==1, 1])

image.png

  1. from sklearn.model_selection import train_test_split
  2. from sklearn.linear_model import LogisticRegression
  3. X_train, X_test, y_train, y_test = train_test_split(X, y)
  4. log_reg = LogisticRegression()
  5. log_reg.fit(X_train, y_train)
  6. log_reg.coef_ # array([[ 1.95815223, -3.28951035]])
  7. def x2(x1):
  8. return (-log_reg.coef_[0][0] * x1 - log_reg.intercept_) / log_reg.coef_[0][1]
  9. x1_plot = np.linspace(4, 8, 1000)
  10. x2_plot = x2(x1_plot)
  11. plt.scatter(X[y==0, 0], X[y==0, 1])
  12. plt.scatter(X[y==1, 0], X[y==1, 1])
  13. plt.plot(x1_plot, x2_plot)

image.png

决策边界

  1. def plot_decision_boundary(model, axis):
  2. x0, x1 = np.meshgrid(
  3. np.linspace(axis[0], axis[1], int((axis[1]-axis[0])*100)).reshape(-1, 1),
  4. np.linspace(axis[2], axis[3], int((axis[3]-axis[2])*100)).reshape(-1, 1),
  5. )
  6. X_new = np.c_[x0.ravel(), x1.ravel()]
  7. y_predict = model.predict(X_new)
  8. zz = y_predict.reshape(x0.shape)
  9. from matplotlib.colors import ListedColormap
  10. custom_cmap = ListedColormap(['#EF9A9A','#FFF59D','#90CAF9'])
  11. plt.contourf(x0, x1, zz, linewidth=5, cmap=custom_cmap)
  12. plot_decision_boundary(log_reg, axis=[4, 7.5, 1.5, 4.5])
  13. plt.scatter(X[y==0, 0], X[y==0, 1])
  14. plt.scatter(X[y==1, 0], X[y==1, 1])

image.png

  1. from sklearn.neighbors import KNeighborsClassifier
  2. knn = KNeighborsClassifier(n_neighbors=3)
  3. knn.fit(X_train, y_train)
  4. plot_decision_boundary(knn, axis=[4, 7.5, 1.5, 4.5])
  5. plt.scatter(X[y==0, 0], X[y==0, 1])
  6. plt.scatter(X[y==1, 0], X[y==1, 1])

image.png

  1. knn2 = KNeighborsClassifier(n_neighbors=5)
  2. knn2.fit(iris.data[:, :2], iris.target)
  3. plot_decision_boundary(knn2, axis=[4, 10, 1.5, 4.5])
  4. plt.scatter(iris.data[iris.target==0, 0], iris.data[iris.target==0, 1])
  5. plt.scatter(iris.data[iris.target==1, 0], iris.data[iris.target==1, 1])
  6. plt.scatter(iris.data[iris.target==2, 0], iris.data[iris.target==2, 1])

image.png

  1. knn3 = KNeighborsClassifier(n_neighbors=50)
  2. knn3.fit(iris.data[:, :2], iris.target)
  3. plot_decision_boundary(knn3, axis=[4, 8, 1.5, 4.5])
  4. plt.scatter(iris.data[iris.target==0, 0], iris.data[iris.target==0, 1])
  5. plt.scatter(iris.data[iris.target==1, 0], iris.data[iris.target==1, 1])
  6. plt.scatter(iris.data[iris.target==2, 0], iris.data[iris.target==2, 1])

image.png

  1. np.random.seed(6666)
  2. X = np.random.normal(0, 1, size=(300, 2))
  3. y = np.array(X[:, 0] ** 2 + X[:, 1] ** 2 < 1.5, dtype='int')
  4. plt.scatter(X[y==0, 0], X[y==0, 1])
  5. plt.scatter(X[y==1, 0], X[y==1, 1])
  6. log_reg2 = LogisticRegression()
  7. log_reg2.fit(X, y)
  8. plot_decision_boundary(log_reg2, axis=[-4, 4, -4, 4])
  9. plt.scatter(X[y==0, 0], X[y==0, 1])
  10. plt.scatter(X[y==1, 0], X[y==1, 1])

image.png

  1. from sklearn.preprocessing import PolynomialFeatures, StandardScaler
  2. from sklearn.pipeline import Pipeline
  3. def PolynomialLogisticRegression(degree, C=1):
  4. return Pipeline([
  5. ('poly', PolynomialFeatures(degree=degree)),
  6. ('std',StandardScaler()),
  7. ('logistic', LogisticRegression(C=C))
  8. ])
  9. poly_reg = PolynomialLogisticRegression(2)
  10. poly_reg.fit(X, y)
  11. plot_decision_boundary(poly_reg, axis=[-4, 4, -4, 4])
  12. plt.scatter(X[y==0, 0], X[y==0, 1])
  13. plt.scatter(X[y==1, 0], X[y==1, 1])

image.png

正则化

  1. poly_reg2 = PolynomialLogisticRegression(8,0.01)
  2. poly_reg2.fit(X, y)
  3. plot_decision_boundary(poly_reg2, axis=[-4, 4, -4, 4])
  4. plt.scatter(X[y==0, 0], X[y==0, 1])
  5. plt.scatter(X[y==1, 0], X[y==1, 1])

image.png

二分类-多分类—OVR-OVO

  1. from sklearn.multiclass import OneVsRestClassifier, OneVsOneClassifier
  2. ovr = OneVsRestClassifier(log_reg)
  3. ovo = OneVsOneClassifier(log_reg)