Statistical Learning Method 统计学习方法 https://pypi.org/project/slmethod/
1 | import numpy as np |
生成数据
1 | # X 为样本特征,y 为样本类别输出,共 30 个样本,每个样本 2 个特 |
处理 y 值,取值为范围 {-1, 1}
1 | y = np.array([1 if i == 1 else -1 for i in y]) |
1 | X |
array([[ 1.26654361, 1.20833252],
[ 2.06535223, -2.13177734],
[ 0.11811139, 0.76083319],
[ 0.90184139, -0.93177662],
[ 0.96240122, -0.99933393],
[ 0.32421018, -0.79732491],
[ 0.16465073, -0.67426018],
[-0.8393532 , 0.02946355],
[ 1.50737475, -1.62701845],
[ 1.81169336, -1.46844229],
[ 2.63399788, 0.97604937],
[ 1.59518059, -1.52110826],
[ 1.74505037, 1.46262955],
[ 1.86201667, 0.93060442],
[ 0.11905276, -0.52595242],
[ 1.48508147, -0.97794231],
[-0.92705557, 0.39367018],
[ 3.42983834, -2.72193889],
[ 1.99481682, 1.60244273],
[ 2.59197055, 1.29968242],
[ 1.60598522, -1.13518763],
[ 1.52118196, 1.12174498],
[ 0.13097699, -0.58481754],
[ 0.84318571, 0.65777867],
[ 1.0940501 , -0.02064728],
[ 0.36579428, 1.32704784],
[ 1.46113021, 1.11893705],
[ 0.42126239, -0.25448586],
[ 0.97213672, 0.63569881],
[ 0.81731191, 0.7569019 ],
[ 1.85348049, -1.36379487],
[ 0.81830809, 0.89762419],
[ 0.9559071 , -1.13200362],
[ 1.19788365, 0.24486292],
[ 0.43394253, 1.2190042 ],
[ 0.75245696, -1.00391468],
[ 0.26120942, -0.49444024],
[ 0.23915671, 1.72744475],
[ 1.33104015, 1.73132756],
[ 2.37528241, 1.47588251]])
1 | y |
array([ 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1,
-1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, -1, 1,
1, -1, -1, 1, 1, 1])
1 | plt.scatter(X[:, 0], X[:, 1], c=y, s=40, marker='o') |
<matplotlib.collections.PathCollection at 0x129908d68>
sklearn 里面的 Perceptron
1 | from sklearn.linear_model import Perceptron |
1 | clf = Perceptron(fit_intercept=True, max_iter=1000, shuffle=False) |
Perceptron(alpha=0.0001, class_weight=None, early_stopping=False, eta0=1.0,
fit_intercept=True, max_iter=1000, n_iter_no_change=5, n_jobs=None,
penalty=None, random_state=0, shuffle=False, tol=0.001,
validation_fraction=0.1, verbose=0, warm_start=False)
1 | # Weights 权重(值) |
[[2.06165026 2.89632885]]
1 | # bias 截距 |
[-1.]
1 | plt.scatter(X[:, 0], X[:, 1], c=y, s=40, marker='o') |
[<matplotlib.lines.Line2D at 0x12bdbe320>]
原始感知机
1 | from slmethod.perceptron import Perceptron |
1 | origin_clf = Perceptron(dual=False) |
1 | print(origin_clf.w) |
[0.20616503 0.28963289]
1 | print(origin_clf.b) |
-0.1
对偶形式
1 | dual_clf = Perceptron(dual=True) |
1 | print(dual_clf.w) |
[0.20616503 0.28963289]
1 | print(dual_clf.b) |
-0.1
绘图
1 | plt.scatter(X[:, 0], X[:, 1], c=y, s=40, marker='o') |
slmethod 绘图
slmethod 库的 估计器有一个 show2D() 方法,可以绘制动态图。
从上图可以看到 sklearn,原始类型和对偶形式的线几乎重合了。