随机梯度下降

随机梯度下降

随机梯度下降是一种用于解决使用凸损失函数的线性分类器的方法,如,支持向量机和逻辑回归。它在大规模学习的背景下,吸引了大量的注意。

随机梯度下降的优点:

  • 快速
  • 易实现

随机梯度下降的缺点:

  • 需要大量的超参,如,正则化参数和迭代次数
  • 对特征缩放敏感
分类

在拟合模型时,确保在每次迭代后,打乱训练数据。

SGDClassifier实现了一个基本的随机梯度学习函数,支持不同的损失函数和惩罚。

损失函数包括:

  • loss='hinge:(soft-margin) linear Support Vector Machine
  • loss='modified_huber':smoothed hinge loss
  • loss='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

SGDClassifierone versus all (OVA)方式组合多个二元分类器来支持多类别分类。对 K K K个类别中的每一类,学习这个类和其它 K − 1 K-1 K1个类的差别得到一个二元分类器。在测试时,计算每类距超平面的带符号的距离,选择具有最大距离的类。

# 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=
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值