LSSVM分类和回归

LSSVM-回归

gamma = 100;
sig2 = 13;
type = 'function estimation';

%训练模型
[alpha,b] = trainlssvm({x_train,y_train,type,gamma,sig2,'RBF_kernel'});

%模型预测
predict_train = simlssvm({x_train,y_train,type,gamma,sig2,'RBF_kernel'},{alpha,b},x_train);
train_RMSE = sqrt(sum((predict_train-y_train).^2)/size(y_train,1));
predict_test = simlssvm({x_train,y_train,type,gamma,sig2,'RBF_kernel'},{alpha,b},x_test);
test_RMSE = sqrt(sum((predict_test-y_test).^2)/size(y_test,1));
disp(['train RMSE = ',num2str(train_RMSE),' test RMSE = ',num2str(test_RMSE)]);

%结果可视化
plotlssvm({x_train,y_train,type,gamma,sig2,'RBF_kernel'},{alpha,b});

figure(1)
plot(y_test,'-r')
hold on
plot(predict_test,'--k')

 

LSSVM-分类

%设置参数
gamma = 10;     %惩罚系数C
sig2 = 0.2;     %RBF核函数参数
type = 'classification';

%训练模型
[alpha,b] = trainlssvm({x,y,type,gamma,sig2,'RBF_kernel'});
%[alpha,b] = trainlssvm({x,y,type,gamma,sig2,'RBF_kernel','original'});
%[alpha,b] = trainlssvm({x,y,type,gamma,sig2,'RBF_kernel','preprocess'});

%模型预测
predict_train = simlssvm({x,y,type,gamma,sig2,'RBF_kernel'},{alpha,b},x);
acc = length(find(predict_train==y))/size(y,1);
disp(['train acc = ',num2str(acc)]);

predict_test = simlssvm({x,y,type,gamma,sig2,'RBF_kernel'},{alpha,b},X);
acc = length(find(predict_test==y))/size(X,1);
disp(['test acc = ',num2str(acc)]);

 

你可能感兴趣的:(matlab)