随机梯度下降

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import SGDClassifier
from sklearn.datasets.samples_generator 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)
clf.fit(X, Y)

## 绘图
xx = np.linspace(-1, 5, 10)
yy = np.linspace(-1, 5, 10)

##生成二维矩阵
X1, X2 = np.meshgrid(xx, yy)
##生产一个与X1相同形状的矩阵
Z = np.empty(X1.shape)
##np.ndenumerate 返回矩阵中每个数的值及其索引
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'
##绘制等高线:Z分别等于levels
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=20)
plt.axis('tight')
plt.show()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值