LibSVM MATLAB 使用

[转载]
这里给出加州住房的例子,大家转载时请尊重版权,本例已列入新书《 支持向量机的工程应用》,李应红,尉询楷编著,新书已正式开编,希望大家多提意见,
获得统计参数
for ii=1:100
cal_housing_data;
start=cputime;
%!svmtrain -s 0 -t 2 -g num2str(sigma) training.dat model.dat
dos(['svmtrain -s 3 -c 500 cal_housing_train.dat model.dat'])
train_time(ii)=cputime-start
params = libsvm2mat('model.dat');
No_sv(ii)=params.total_sv
dos(['svm-predict cal_housing_train.dat model.dat validation.txt'])
load validation.txt;
train_accuracy(ii)=sqrt(validation(end-1))
start1=cputime;
dos(['svm-predict cal_housing_test.dat model.dat validation1.txt'])
load validation1.txt
test_time(ii)=cputime-start1
test_accuracy(ii)=sqrt(validation1(end-1))
clear validation validation1 start start1;
end
av_train_time=mean(train_time);
dev_train_time=std(train_time);
av_test_time=mean(test_time);
dev_test_time=std(test_time);
av_train_accuracy=mean(train_accuracy);
dev_train_accuracy=std(train_accuracy);
av_test_accuracy=mean(test_accuracy);
dev_test_accuracy=std(test_accuracy);
av_No_sv=mean(No_sv);
save test1.mat;

%%%%%%%%%%%%%%%%%%%%
子程序
function params = libsvm2mat(fname)
%LIBSVM2MAT LibSVM to MATLAB conversion
% usage:
% params = libsvm2mat(model_file_name)
% or if you have a data file you would like to load
% params = libsvm2mat(data_file_name)

fid = fopen(fname,'r');
if (fid == -1), error(sprintf('%s not found!',fname')); end
while 1,
tline = fgetl(fid);
if ~ischar(tline), break, end
I = [findstr(tline,' ') length(tline)+1];
switch tline(1(1)-1),
case 'total_sv',
params.total_sv = sscanf(tline(I(1):end),'%f');
otherwise, % assume this to be a data file, double check if its true:
if isempty(str2num(tline(1(1)-1))),
disp([tline(1(1)-1) ' ignored!']);
else
k = 0;
while 1,
k = k + 1;
if ~ischar(tline), break, end
tline(find(tline == ':')) = ' ';
values = sscanf(tline, '%f')';
% params.y(k,1) = values(1);
%params.X(k, values(2:2:end)) = values(3:2:end);
tline = fgetl(fid);
end
end
end
end
fclose(fid);
%%%%%%%%%%%%%%%%%%%%%%%%%%
数据格式的生成
function cal_housing_data
%
%This file is used to load boston original data and generate random
%validation and test data.
% Created by skyhawk,2004.7.24
%
% function califonia_data
% %
%This file is used to load boston original data and generate random
%validation and test data.
% Created by skyhawk,2004.7.24
%

load cal_housing.data;
who
dataset=cal_housing;
rand_sequence=randperm(size(dataset,1));
temp_dataset=dataset;
dataset=temp_dataset(rand_sequence, ;
%预处理
for i=1:size(dataset,2)
if max(dataset(:,i))~=min(dataset(:,i))
dataset(:,i)= (dataset(:,i)-min(dataset(:,i)))/(max(dataset(:,i))-min(dataset(:,i)));
% dataset(:,i)=(dataset(:,i)-mean(dataset(:,i)))/(std(dataset(:,i)));
else
dataset(:,i)=0;
end
end

P1=dataset(1:8000,1:size(dataset,2)-1);
T1=dataset(1:8000,size(dataset,2));

%Obtain Random Validation Matrix

X=dataset(8001:size(dataset,1),1:size(dataset,2)-1);
Y=dataset(8001:size(dataset,1),size(dataset,2));



fid1 = fopen('cal_housing_train.dat','w');

for i=1:size(P1,1)
fprintf(fid1,'%2.8f ',T1(i,1));

for j=1:size(P1,2)
fprintf(fid1,' %d:%2.8f',j, P1(i,j)); % for SVM

end
fprintf(fid1,'/n');

end
fclose(fid1);

fid1 = fopen('cal_housing_test.dat','w');

for i=1:size(X,1)
fprintf(fid1,'%2.8f ',Y(i,1));

for j=1:size(X,2)
fprintf(fid1,' %d:%2.8f',j, X(i,j)); % for SVM

end
fprintf(fid1,'/n');

end
fclose(fid1);


%%%%%%%%%%
加州的数据网上查的到,大家可以验证
 
来源: http://genial.yculblog.com/post.104136.html 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值