Я пытаюсь сделать прогноз с помощью алгоритма wls-svm, но он показывает следующую ошибку:
Файл "", строка 236, в альфа-выражениях, b, e = lessSquares (dataMat , labelMat, C, kTup)
Файл "", строка 95, в наименьших квадратах rightmat = vstack ((ноль, oS.labelMat))
Файл "C: \ Users \ behnia \ Anaconda3 \ lib \ site-packages \ numpy \ core \ shape_base.py ", строка 283, в vstack, возвращает _nx.concatenate ([atleast_2d (_m) для _m в tup], 0)
ValueError : все размеры входного массива, кроме оси конкатенации, должны точно совпадать
это код:
from numpy import *
def loadDataSet(filename):
'''
Import Data
input: filename: filename
'''
dataMat = []
labelMat = []
fr = open(filename)
for line in fr.readlines():
lineArr = line.strip().split('\t')
dataMat.append([float(lineArr[0]), float(lineArr[1]), float(lineArr[2]), float(lineArr[3]), float(lineArr[4]), float(lineArr[5]), float(lineArr[6]), float(lineArr[7])])
labelMat.append([float(lineArr[8]), float(lineArr[9])])
return mat(dataMat),mat(labelMat)
def kernelTrans(X,A,kTup):
'''
The kernel function value of each data vector and A in the data set
input: X--feature data set
A--input vector
kTup--Definition of kernel function parameters
output: K--a matrix of each data vector in the data set and the kernel function value of A
'''
X = mat(X)
m,n = shape(X)
K = mat(zeros((m,1)))
if kTup[0] == 'lin':
K = X * A.T
elif kTup[0] == 'rbf':
for j in range(m):
deltaRow = X[j] - A
K[j] = deltaRow * deltaRow.T
K = exp(K/(-1 * kTup[1] ** 2))
else: raise NameError('Houston We Have a Problem ,That Kernel is not recognized')
return K
class optStruct:
def __init__(self,dataMatIn,classLabels,C,kTup):
self.X = dataMatIn
self.labelMat = classLabels
self.C = C
self.m = shape(dataMatIn)[0]
self.alphas = mat(zeros((self.m,2)))
self.b = 0
self.K = mat(zeros((self.m,self.m)))
for i in range(self.m):
self.K[:,i] = kernelTrans(self.X, self.X[i,:], kTup)
def leastSquares(dataMatIn,classLabels,C,kTup):
'''
Least square method to solve alpha sequence
input: dataMatIn: feature data set
classLabels: classification label set
C: Parameters, (slack variables, allowing some data points to be on the wrong side of the separation surface)
kTup: Kernel function type and parameter selection
output: b--w.T * x + b = b in y
alphas: alphas sequence
'''
##1.
oS = optStruct(dataMatIn,classLabels,C,kTup)
unit = mat(ones((oS.m,1))) #[1,1,...,1].T
I = eye(oS.m)
zero = mat(zeros((1,1)))
upmat = hstack((zero,unit.T))
downmat = hstack((unit,oS.K + I/float(C)))
##2.
completemat = vstack((upmat,downmat))
rightmat = vstack((zero,oS.labelMat))
b_alpha = completemat.I * rightmat
##3.
oS.b = b_alpha[0,0]
for i in range(oS.m):
oS.alphas[i,0] = b_alpha[i+1,0]
e = oS.alphas/C
return oS.alphas,oS.b,e
def weights(e):
'''
Calculate weight sequence
input: e (mat): LSSVM error matrix
output: v (mat): weight matrix
'''
##1.
c1 = 2.5
c2 = 3
m = shape(e)[0]
v = mat(zeros((m,1)))
v1 = eye(m)
q1 = int(m/4.0)
q3 = int((m*3.0)/4.0)
e1 = []
shang = mat(zeros((m,1)))
##2.
for i in range(m):
e1.append(e[i,0])
e1.sort()
##3.
IQR = e1[q3] - e1[q1]
##4.
s = IQR/(2 * 0.6745)
##5.
for j in range(m):
shang[j,0] = abs(e[j,0]/s)
for x in range(m):
if shang[x,0] <= c1:
v[x,0] = 1.0
if shang[x,0] > c1 and shang[x,0] <= c2:
v[x,0] = (c2 - shang[x,0])/(c2 - c1)
if shang[x,0] > c2:
v[x,0] = 0.0001
v1[x,x] = 1/float(v[x,0])
return v1
def weightsleastSquares(dataMatIn,classLabels,C,kTup,v1):
'''
output:b--w.T*x+b=y中的b
alphas:alphas序列
Least square method to solve alpha sequence
input: dataMatIn: feature data set
classLabels: classification label set
C: Parameters, (slack variables, allowing some data points to be on the wrong side of the separation surface)
kTup: Kernel function type and parameter selection
output: b--w.T * x + b = b in y
alphas: alphas sequence
'''
##1.
oS = optStruct(dataMatIn,classLabels,C,kTup)
unit = mat(ones((oS.m,1))) #[1,1,...,1].T
#I = eye(oS.m)
gamma = kTup[1]
zero = mat(zeros((1,1)))
upmat = hstack((zero,unit.T))
downmat = hstack((unit,oS.K + v1/float(C)))
##2.
completemat = vstack((upmat,downmat))
rightmat = vstack((zero,oS.labelMat))
b_alpha = completemat.I * rightmat
##3.
oS.b = b_alpha[0,0]
for i in range(oS.m):
oS.alphas[i,0] = b_alpha[i+1,0]
e = oS.alphas/C
return oS.alphas,oS.b
def predict(alphas,b,dataMat):
'''
forecast result
input: alphas (mat): Lagrange multiplier sequence of WLSSVM model
b (float): Bias of regression equation of WLSSVM model
dataMat (mat): test sample set
output: predict_result (mat): test result
'''
m,n = shape(dataMat)
predict_result = mat(zeros((m,1)))
for i in range(m):
Kx = kernelTrans(dataMat,dataMat[i,:],kTup)
predict_result[i,0] = Kx.T * alphas + b
return predict_result
def predict_average_error(predict_result,label):
'''
Calculate the average forecast error
input: predict_result (mat): prediction result
label (mat): actual result
output: average_error (float): average error
'''
m,n = shape(predict_result)
error = 0.0
for i in range(m):
error += abs(predict_result[i,0] - label[i,0])
average_error = error / m
return average_error
if __name__ == '__main__':
##1.data import
print('--------------------Load Data------------------------')
dataMat,labelMat = loadDataSet('data12.txt')
##2. parameter settings
print('--------------------Parameter Setup------------------')
C = 0.6
k1 = 0.3
kernel = 'rbf'
kTup = (kernel,k1)
##3.Solve the LSSVM model
print('-------------------Save LSSVM Model-----------------')
alphas,b,e = leastSquares(dataMat,labelMat,C,kTup)
##4. Calculate error weight
print('----------------Calculate Error Weights-------------')
v1 = weights(e)
##5.Solve the WLSSVM model
print('------------------Save WLSSVM Model--------------- -')
alphas1,b1 = weightsleastSquares(dataMat,labelMat,C,kTup,v1)
##6.forecast result
print('------------------Predict Result------------------ -')
predict_result = predict(alphas1,b1,dataMat)
##7.average error
print('-------------------Average Error------------------ -')
average_error = predict_average_error(predict_result,labelMat)