感知机学习算法的对偶形式——代码

《统计学习方法》2.3

import numpy as np

trainingSet = np.mat([[1, 1], [2, 2], [1, 3], [3, 3], [5, 3], [3, 4]])
y = [1, 1, 1, -1, -1, -1]

# 计算Gram矩阵
m = np.shape(trainingSet)[0]
GramMatrix = [None] * m
for i in range(m):
    GramMatrix[i] = [0] * m
    for j in range(m):
        GramMatrix[i][j] = int(np.dot(trainingSet[i], trainingSet[j].T))

# 参数初始化
alpha = [0] * m
b = 0
eta = 1

# 开始训练
isFound = False
while not isFound:
    for i in range(m):
        temp = 0
        for j in range(m):
            temp += alpha[j] * y[j] * GramMatrix[j][i]

        if y[i] * (temp + b) <= 0:
            alpha[i] += 1 * eta
            b += y[i] * eta
            break
        elif i == m - 1:
            isFound = True

w = [0, 0]
for i in range(m):
    w += alpha[i] * trainingSet[i] * y[i]
print(w, b)

分类
感知机学习算法的对偶形式——代码_第1张图片
参考文章:
https://blog.csdn.net/zuanfengxiao/article/details/79033800?ops_request_misc=%25257B%252522request%25255Fid%252522%25253A%252522161364812816780269868096%252522%25252C%252522scm%252522%25253A%25252220140713.130102334.pc%25255Fall.%252522%25257D&request_id=161364812816780269868096&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v29-1-79033800.first_rank_v2_pc_rank_v29_10&utm_term=%25E6%2584%259F%25E7%259F%25A5%25E6%259C%25BA%25E7%259A%2584%25E5%25AF%25B9%25E5%2581%25B6%25E5%25BD%25A2%25E5%25BC%258F%25E4%25BB%25A3%25E7%25A0%2581

你可能感兴趣的:(机器学习笔记,机器学习,python,算法)