我正在尝试在Python / Pulp中实现以下LP,以便与Sklearn进行比较:
#universe
from pulp import *
import numpy as np
from sklearn.metrics.pairwise import rbf_kernel
#define the function to regresse
N = 100
x = np.linspace(0, 2 * np.pi, N)
y = np.sin(x) + np.random.random(N) * 1
x.resize(N, 1)
K = rbf_kernel(x, gamma=5)
# define de problem for Pulp
# define the hyper parameters
C = 10
eps = 0.1
# define the variables
RANGE = list(range(N))
alpha = pulp.LpVariable.dicts("alpha", (RANGE,), cat='Continuous')
b = pulp.LpVariable("b", cat='Continuous')
a = pulp.LpVariable.dicts("a", (RANGE,), lowBound=0, cat='Continuous')
Eta = pulp.LpVariable.dicts("Eta", (RANGE,), lowBound=0, cat='Continuous')
prob = LpProblem("Constrained SVM", LpMinimize)
# implementing the constrains to Pulp
for i in range(N):
prob += lpDot(alpha, K[i]) + b - y[i] >= -Eta[i]
prob += lpDot(alpha, K[i]) + b - y[i] <= Eta[i]
prob += eps >= 0 #useless constraint
prob += eps <= Eta[i]
prob += alpha[i] <= a[i]
prob += alpha[i] >= -a[i]
# call the objective function
prob += lpSum(a) + C * lpSum(Eta)
#output file
prob.writeLP("MySVM.lp")
#launch solver and read result
prob.solve()
print("Status:", LpStatus[prob.status])
我的问题是求解器不对 alpha 和 a 做任何事情。它们最后等于零。是由于编码错误还是我的LP设置错误?
答案 0 :(得分:0)
将numpy导入为np 将matplotlib.pyplot导入为plt
来自sklearn.svm import SVR 来自sklearn.metrics.pairwise导入rbf_kernel 从纸浆进口*
def CompPulpSlk():
N = 100
x = np.linspace(0, 2 * np.pi, N)
y = np.sin(x) + np.random.random(N) * 1
x.resize(N, 1)
K = rbf_kernel(x, gamma=5)
# perform SVR
svr_rbf = SVR(kernel='rbf', C=1e3, gamma=2)
y_rbf = svr_rbf.fit(x, y).predict(x)
# define de problem for Pulp
# define the hyper parameters
C = 10
eps = 0.1
# define the variables
Alpha = pulp.LpVariable.matrix("alpha", list(range(N)), lowBound=-1000, upBound=1000, cat='Continuous')
b = pulp.LpVariable("b", cat='Continuous')
a = pulp.LpVariable.matrix("a", list(range(N)), lowBound=0, cat='Continuous')
Eta = pulp.LpVariable.matrix("Eta", list(range(N)), lowBound=0, cat='Continuous')
prob = LpProblem("Constrained SVM", LpMinimize)
# implementing the constrains to Pulp
for i in range(N):
prob += lpDot(K[i], Alpha) + b - y[i] >= -Eta[i]
prob += lpDot(K[i], Alpha) + b - y[i] <= Eta[i]
prob += eps >= 0
prob += eps <= Eta[i]
prob += Alpha[i] <= a[i]
prob += Alpha[i] >= -a[i]
# call the objective function
prob += lpSum(a[i] for i in range(N)) + C * lpSum(Eta[i] for i in range(N))
#LpSolverDefault.msg = 1
prob.writeLP("MySVM.lp")
prob.solve()
print("Status:", LpStatus[prob.status])
alp = np.zeros(N)
for v in prob.variables():
if v.name == 'b':
b = v.varValue
elif v.name[:5] == 'alpha':
alp[int(v.name[6:])] = v.varValue
#clean up PULP variables
Alpha.clear()
Eta.clear()
a.clear()
#Projection of the solution from Pulp and Sci-learn
y_pulp = np.zeros(N)
for i in range(N):
y_pulp[i] = np.dot(alp, K[i, :]) + b
#display result
lw = 2
plt.scatter(x, y, color='darkorange', label='data')
plt.hold('on')
plt.plot(x, y_rbf, color='navy', lw=lw, label='SciLearn')
plt.plot(x, y_pulp, color='cornflowerblue', lw=lw, label='Pulp')
plt.xlabel('data')
plt.ylabel('target')
plt.title('Support Vector Regression')
plt.legend()
plt.show()
CompPulpSlk()