import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs, make_circles, make_moons
from sklearn.preprocessing import StandardScaler
plt.rcParams['font.sans-serif'] = ['SimHei'] # 中文显示
plt.rcParams['axes.unicode_minus'] = False # 负号显示
class SMO:
def __init__(self, X, y, C, kernel, alphas, b,errors, user_linear_optim):
# model = SMO(X, y, C, linear_kernel, initial_alphas, initial_b, np.zeros(m), user_linear_optim=True)
self.X = X # 训练样本
self.y = y # 类别 label
self.C = C # r 正则化常量,用于调整(过)拟合的程度
self.kernel = kernel # kernel function 核函数,实现了两个核函数,线性和高斯(RBF)
self.alphas = alphas # lagrange multiplier 拉格朗日乘子,与样本一一相对
self.b = b# scalar bias term 标量,偏移量# self.b = 0
self.errors = errors # E cache 用于存储alpha值实际与预测值得差值,与样本数量一一相对
self.m, self.n = np.shape(self.X) # 训练样本的个数和每个样本的w维度
self.user_linear_optim = user_linear_optim # 判断模型是否使用线性核函数
self.w = np.zeros(self.n) # 初始化权重w的值,主要用于线性核函数
# self.b = 0
def linear_kernel(x, y, b=1):
end = np.dot(x,y.T)-b
return end
def gaussian_kernel(x, y, sigma=1):
# 高斯核函数
if np.ndim(x) == 1 and np.ndim(y) == 1:
end = 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):
end = np.exp(-(np.linalg.norm(x - y, 2, axis=1) ** 2) / (2 * sigma ** 2))
elif np.ndim(x) > 1 and np.ndim(y) > 1:
end = np.exp(-(np.linalg.norm(x[:, np.newaxis] - y[np.newaxis, :], 2, axis=2) ** 2) / (2 * sigma ** 2))
return end
#主要用于绘图
def decision_function_plot(alphas, y, kernel, x1 ,x2, b):
end = np.dot(alphas * y,kernel(x1, x2))-b # *,@ 两个Operators的区别?
return end
def decision_f_output(model, i):
if model.user_linear_optim:
# Equation (J1)
f_xi=float(np.dot(model.w.T, model.X[i])) - model.b
return f_xi
else:
# Equation (J10)
for j in range(model.m):
f_xi=np.sum(model.alphas[j] * model.y[j] * model.kernel(model.X[j], model.X[i])) - model.b
return f_xi
def get_error(model, i):
if 0 < model.alphas[i] < model.C:#为支持向量时
return model.errors[i]
else:
return decision_f_output(model, i) - model.y[i]
#train中寻找a2之后在examine中寻找a1之后送到takestep中训练
def fit(model):
numChanged = 0#判断优化是否成功0代表失败
examineAll = 1#代表从0号元素开始优化
loopnum = 0# 计数器,记录优化时的循环次数
loopnum1 &#
SVM分类实战(线性)
最新推荐文章于 2025-03-09 11:21:08 发布