import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
frame = pd.read_csv('lssvm.csv')
arr = frame.values[0:10]
x,y = arr[:,0:-1],arr[:,-1]
x_asis = np.random.random((1,2))
y_asis = np.random.random((1,2))
sample_nums = 50
def kernel(x,y,sigma):
a = x-y
result = np.exp(-a.dot(a.T)/(2*sigma**2))
return result
def matrixOmega(x,y,sigma):
length = len(x)
omega = np.zeros((length,length))
for i in range(length):
for j in range(length):
omega[i,j] = kernel(x[i],x[j],sigma)
return omega
def countParams(x,y,gamma,sigma):
length = len(x)+1
A = np.zeros((length,length))
A[0,1:] = np.ones((len(x),1)).T
A[1:length,0] = np.ones(len(x))
A[1:length,1:length] = matrixOmega(x,y,sigma) + np.eye(length-1)/gamma
B = np.zeros(length)
B[1:] = y
p = np.linalg.solve(A,B)
b,a = p[0],p[1:]
return a,b
def iterProcess(x_asis,y_asis):
position = []
smell = []
for num in range(sample_nums):
x_ = x_asis+2*np.random.rand()-1
y_ = y_asis+2*np.random.rand()-1
params = 1/np.sqrt(x_**2+y_**2)
gamma,sigma = params[0,0],params[0,1]
a,b = countParams(x,y,gamma,sigma)
predict = np.dot(a.T,matrixOmega(x,y,sigma))+b
error = np.sum((predict-y)**2,axis=0)/len(x)
position.append((x_,y_))
smell.append(error)
index = np.argmin(smell)
bestX,bestY,bestSmell = position[index][0],position[index][1],smell[index]
return bestX,bestY,bestSmell
bestX,bestY,bestSmell = iterProcess(x_asis,y_asis)
def searchBestParams(bestX,bestY,bestSmell):
position = []
smell = []
for time in range(100):
tmpX,tmpY,tmpSmell = iterProcess(bestX,bestY)
if tmpSmell < bestSmell:
bestX = tmpX
bestY = tmpY
bestSmell = tmpSmell
position.append((bestX,bestY))
smell.append(bestSmell)
plt.plot(smell,c='red')
plt.show()
params = 1/np.sqrt(bestX**2+bestY**2)
best_gamma,best_sigma = params[0,0],params[0,1]
return best_gamma,best_sigma
best_gamma,best_sigma = searchBestParams(bestX,bestY,bestSmell)
a,b = countParams(x,y,best_gamma,best_sigma)
def visualInTrainSet(a,b,x,y):
predict = np.dot(a.T,matrixOmega(x,y,best_sigma))+b
print(predict)
fig,ax = plt.subplots()
ax.plot(range(len(x)),y,c='purple')
ax.plot(range(len(x)),predict,c='blue')
ax.legend(['actual value','predict value'])
error = np.sum((predict-y)**2,axis=0)/len(x)
print(error)
def visualInTestSet(a,b):
test = frame.values[10:]
X,Y = test[:,0:-1],test[:,-1]
result = np.zeros(len(test))
for i in range(len(test)):
result[i] = b
for j in range(len(x)):
result[i] += a[j]*kernel(X[i],x[j],best_sigma)
print(result)
print(Y)
plt.plot(range(len(X)),Y,c='red')
plt.plot(range(len(X)),result,c='blue')
plt.legend(['actual value','predict value'])
plt.show()
error = np.sum((result-Y)**2,axis=0)/len(x)
print(error)
visualInTestSet(a,b)