标签:ace 归一化 fit 本质 int 模拟 pre __init__ standard
import numpy as np import matplotlib.pyplot as plt np.random.seed(666) X = np.random.normal(0, 1, size=(200, 2)) y = np.array(X[:,0]**2 + X[:,1]**2 < 1.5, dtype=‘int‘) plt.scatter(X[y==0,0], X[y==0,1]) plt.scatter(X[y==1,0], X[y==1,1]) plt.show()
from playML.LogisticRegression import LogisticRegression log_reg = LogisticRegression() log_reg.fit(X, y) 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, 4, -4, 4]) plt.scatter(X[y==0,0], X[y==0,1]) plt.scatter(X[y==1,0], X[y==1,1]) plt.show()
# 使用管道:Pipeline(list),list 内的每一个元素为为管道的一步,每一步是一个元组, # 元组的第一个元素是一个字符串,是一个实例对象,描述这一步的内容或功能,第二个元素是一个类的对象 from sklearn.pipeline import Pipeline from sklearn.preprocessing import PolynomialFeatures from sklearn.preprocessing import StandardScaler def PolynomialLogisticRegression(degree): return Pipeline([ # 管道第一步:给样本特征添加多形式项; (‘poly‘, PolynomialFeatures(degree=degree)), # 管道第二步:数据归一化处理; (‘std_scaler‘, StandardScaler()), (‘log_reg‘, LogisticRegression()) ]) poly_log_reg = PolynomialLogisticRegression(degree=2) poly_log_reg.fit(X, y) plot_decision_boundary(poly_log_reg, axis=[-4, 4, -4, 4]) plt.scatter(X[y==0,0], X[y==0,1]) plt.scatter(X[y==1,0], X[y==1,1]) plt.show()
标签:ace 归一化 fit 本质 int 模拟 pre __init__ standard
原文地址:https://www.cnblogs.com/volcao/p/9385930.html