简要说明在matlab中如何转化UCI数据为libsvm库可用数据并举例如何使用

来源:互联网 发布:家庭千兆网络组建方案 编辑:程序博客网 时间:2024/06/07 03:11


本文整合了几篇有关在matlab中结合uci数据库使用libsvm的文章,亲测可用,供大家参考。

1.读取UCI数据集iris.data中数据:

[attrib1, attrib2, attrib3, attrib4, class] = textread('iris.data', '%f%f%f%f%s', 'delimiter', ',');
attrib = [attrib1'; attrib2'; attrib3'; attrib4']';
a = zeros(150, 1);
a(strcmp(class, 'Iris-setosa')) = 1;
a(strcmp(class, 'Iris-versicolor')) = 2;
a(strcmp(class, 'Iris-virginica')) = 3;

至此,属性值均保存到attrib中,类别值保存到数组a中。


得到数据attrib和a之后:

2. 如何在libsvm中使用attirb和a数据呢?

举例:

提取120个数据作为训练样本,30个数据作为测试数据

%每类取40个数据作为训练,共120个训练数据
iris_train_label = a([1:40 51:90 101:140],end);
iris_train_data  = attrib([1:40 51:90 101:140],1:end);
%每类取10个数据作为测试,共30个测试数据
iris_test_label  = a([41:50 91:100 141:150],end);
iris_test_data   = attrib([41:50 91:100 141:150],1:end-1);

3.检测

model            = svmtrain(iris_train_label,iris_train_data);
svmpredict(iris_test_label,iris_test_data,model);

4.结果

*
optimization finished, #iter = 27
nu = 0.048556
obj = -2.012197, rho = 0.099724
nSV = 10, nBSV = 1
*
optimization finished, #iter = 24
nu = 0.045610
obj = -1.823956, rho = 0.212437
nSV = 9, nBSV = 0
*
optimization finished, #iter = 64
nu = 0.344129
obj = -20.289711, rho = 0.134442
nSV = 32, nBSV = 25
Total nSV = 42
Accuracy = 66.6667% (20/30) (classification)


参考:

http://blog.csdn.net/qiudw/article/details/8615830

http://wenku.baidu.com/link?url=NTTMm9uA5cK_r8b3d3WXG6LtKClc_YoyIrdxq9pZY37sEGvJNp3Ob_SSJTEWRk-1apQQWIAmhTteXf5EP1URO8pYyvjYR2qirv0K3jBbwK3

http://blog.csdn.net/abcjennifer/article/details/7370177

0 0