SMO的Python代码实现(仅供参考)

本文详细介绍了支持向量机(SVM)中的SMO(Sequential Minimal Optimization)算法,并提供了Python代码实现,帮助读者深入理解和支持向量机的学习。
摘要由CSDN通过智能技术生成

一起学ML之支持向量机 - SMO算法的Python实现 - 代码

 

 

import numpy as np
import matplotlib.pyplot as plt

from sklearn.datasets import make_blobs,make_circles,make_moons
from sklearn.preprocessing import StandardScaler

class SMOStruct:
    """ 按照John Platt的论文构造SMO的数据结构"""
    def __init__(self, X, y, C, kernel, alphas, b, errors, user_linear_optim):
        self.X = X              # 训练样本
        self.y = y              # 类别 label
        self.C = C              # regularization parameter  正则化常量,用于调整(过)拟合的程度
        self.kernel = kernel    # kernel function   核函数,实现了两个核函数,线性和高斯(RBF)
        self.alphas = alphas    # lagrange multiplier 拉格朗日乘子,与样本一一相对
        self.b = b              # scalar bias term 标量,偏移量
        self.errors = errors    # error cache  用于存储alpha值实际与预测值得差值,与样本数量一一相对
        
        self.m, self.n = np.shape(self.X)    # store size(m) of training set and the number of features(n) for each example  
                                             #训练样本的个数和每个样本的features数量

        self.user_linear_optim = user_linear_optim    # 判断模型是否使用线性核函数
        self.w = np.zeros(self.n)     # 初始化权重w的值,主要用于线性核函数
        #self.b = 0               


   

def linear_kernel(x,y,b=1):
    #线性核函数
    """ returns the linear combination of arrays 'x' and 'y' with
    the optional bias term 'b' (set to 1 by default). """
    result = x @ y.T + b
    return result # Note the @ operator for matrix multiplications


def gaussian_kernel(x,y, sigma=1):
    #高斯核函数
    """Returns the gaussian similarity of arrays 'x' and 'y' with
    kernel width paramenter 'sigma' (set to 1 by default)"""

    if np.ndim(x) == 1 and np.ndim(y) == 1:
        result = np.exp(-(np.linalg.norm(x-y,2))**2/(2*sigma**2))
    elif(np.ndim(x)>1 and np.ndim(y) == 1) or (np.ndim(x) == 1 and np.ndim(y)>1):
        result = np.exp(-(np.linalg.norm(x-y, 2, axis=1)**2)/(2*sigma**2))
    elif np.ndim(x) > 1 and np.ndim(y) > 1 :
        result = np.exp(-(np.linalg.norm(x[:, np.newaxis]- y[np.newaxis, :], 2, axis = 2) ** 2)/(2*sigma**2))
    return result


# 判别函数一,用于单一样本
def decision_function_output(model,i):
    if model.user_linear_optim:
        #Equation (J1)
        #return float(np.dot(model.w.T, model.X[i])) - model.b
        return float(model.w.T @ model.X[i]) - model.b
    else:
        #Equation (J10)
        return np.sum([model.alphas[j] * model.y[j] * model.kernel(model.X[j], model.X[i]) for j in range(model.m)]) - model.b


# 判别函数二,用于多个样本
def decision_function(
  • 7
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值