随机梯度下降
随机梯度下降是一种用于解决使用凸损失函数的线性分类器的方法,如,支持向量机和逻辑回归。它在大规模学习的背景下,吸引了大量的注意。
随机梯度下降的优点:
- 快速
- 易实现
随机梯度下降的缺点:
- 需要大量的超参,如,正则化参数和迭代次数
- 对特征缩放敏感
分类
在拟合模型时,确保在每次迭代后,打乱训练数据。
SGDClassifier
实现了一个基本的随机梯度学习函数,支持不同的损失函数和惩罚。
损失函数包括:
loss='hinge
:(soft-margin) linear Support Vector Machineloss='modified_huber'
:smoothed hinge lossloss='log'
:logistic regression- and all regression losses below
惩罚包括:
penalty='l2'
:L2范数penalty='l1'
:L1范数penalty='elasticnet'
:L2和L1范数的凸组合;(1 - l1_ratio) * L2 + l1_ratio * L1
SGDClassifier
以one versus all (OVA)
方式组合多个二元分类器来支持多类别分类。对 K K K个类别中的每一类,学习这个类和其它 K − 1 K-1 K−1个类的差别得到一个二元分类器。在测试时,计算每类距超平面的带符号的距离,选择具有最大距离的类。
# coding: utf-8
# SGD: Maximum margin separating hyperplane
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import SGDClassifier
from sklearn.datasets import make_blobs
X, Y = make_blobs(n_samples=50, centers=2, random_state=0, cluster_std=0.60)
clf = SGDClassifier(loss='hinge', alpha=0.01, max_iter=2000)
clf.fit(X, Y)
xx = np.linspace(-1, 5, 10)
yy = np.linspace(-1, 5, 10)
X1, X2 = np.meshgrid(xx, yy)
Z = np.empty(X1.shape)
# np.ndenumerate: Return an iterator yielding pairs of array coordinates and values
for (i, j), val in np.ndenumerate(X1):
x1 = val
x2 = X2[i, j]
p = clf.decision_function([[x1, x2]])
Z[i, j] = p[0]
levels = [-1.0, 0.0, 1.0]
linestyles = ['dashed', 'solid', 'dashed']
colors = 'k'
plt.contour(X1, X2, Z, levels, colors=colors, linestyles=linestyles)
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired, edgecolor='black', s=