正则线性模型(回归)
- Ridge
- Lasso
- 早期停止法(分类)
概率模型
- LogisticRegression逻辑回归
- Softmax回归
Softmax只不过y值变为categorial
#logistic只取一个特征 X = iris["data"][:, 3:] # petal width y = (iris["target"] == 2).astype(np.int) # 1 if Iris-Virginica, else 0 from sklearn.linear_model import LogisticRegression log_reg = LogisticRegression(random_state=42) log_reg.fit(X, y)
#logistic取多个特征 from sklearn.linear_model import LogisticRegression X = iris['data'][:, (2,3)] #length, width y = (iris['target']==2).astype(np.int) log_reg = LogisticRegression(C=10**10, random_state=42) log_reg.fit(X, y)LogisticRegression默认使用一对多的训练方式,
不过将超参数multi_class设置为"multinomial"可以切换到softmax回归。
还得指定softmax回归的求解器,比如“lbfgs”求解器,
默认使用里l2正则化,可通过超参数C控制
#softmax 取多特征,输出多类别的分类 X = iris['data'][:, (2,3)] y = iris['target'] softmax_reg = LogisticRegression(multi_class='multinomial', solver='lbfgs', C=10, random_state=42) softmax_reg.fit(X, y) softmax_reg.predict([[5, 2]]) #array([2]) softmax_reg.predict_proba([[5, 2]]) #array([[6.38014896e-07, 5.74929995e-02, 9.42506362e-01]])
再次区分model.predict()和model.predict_proba()
技巧(用于画图。。)
- C是正则化参数,越大正则程度越高
- 逻辑回归鸢尾花里,写到了
- 如何把两个特征合并
- (200, 500)是为了取不同值方便展示
- (200, 500)* (200, 500)变成
- (100000, 2)
x0, x1 = np.meshgrid(
)np.linspace(0, 8, 500).reshape(-1, 1), np.linspace(0, 3.5, 200).reshape(-1, 1),
X_new = np.c_[x0.ravel(), x1.ravel()]
y_proba = softmax_reg.predict_proba(X_new)
y_predict = softmax_reg.predict(X_new)
绘图做出contour图
X = iris["data"][:, (2, 3)] # petal length, petal width y = iris["target"] softmax_reg = LogisticRegression(multi_class="multinomial",solver="lbfgs", C=10, random_state=42) softmax_reg.fit(X, y) x0, x1 = np.meshgrid( np.linspace(0, 8, 500).reshape(-1, 1), np.linspace(0, 3.5, 200).reshape(-1, 1), ) X_new = np.c_[x0.ravel(), x1.ravel()] y_proba = softmax_reg.predict_proba(X_new) y_predict = softmax_reg.predict(X_new) zz1 = y_proba[:, 1].reshape(x0.shape) zz = y_predict.reshape(x0.shape) plt.figure(figsize=(10, 4)) plt.plot(X[y==2, 0], X[y==2, 1], "g^", label="Iris-Virginica") plt.plot(X[y==1, 0], X[y==1, 1], "bs", label="Iris-Versicolor") plt.plot(X[y==0, 0], X[y==0, 1], "yo", label="Iris-Setosa") from matplotlib.colors import ListedColormap custom_cmap = ListedColormap(['#fafab0','#9898ff','#a0faa0']) plt.contourf(x0, x1, zz, cmap=custom_cmap) contour = plt.contour(x0, x1, zz1, cmap=plt.cm.brg) plt.clabel(contour, inline=1, fontsize=12) plt.xlabel("Petal length", fontsize=14) plt.ylabel("Petal width", fontsize=14) plt.legend(loc="center left", fontsize=14) plt.axis([0, 7, 0, 3.5]) save_fig("softmax_regression_contour_plot") plt.show()