svm的使用

 

1.首先从主页上下载libsvm软件。

      http://www.csie.ntu.edu.tw/~cjlin/

 

 

2.准备好数据,首先要把数据转换成Libsvm软件包要求的数据格式为:
      label index1:value1 index2:value2 ...
      其中对于分类来说label为类标识,指定数据的种类;对于回归来说label为目标值。(我主要要用到回归)
      Index是从1开始的自然数,value是每一维的特征值。

该过程可以自己使用excel或者编写程序来完成,也可以使用网络上的FormatDataLibsvm.xls来完成。
FormatDataLibsvm.xls使用说明:
先将数据按照下列格式存放(注意label放最后面):
value1 value2 „„ label
value1 value2 „„ label
„„
然后将以上数据粘贴到FormatDataLibsvm.xls中的最左上角单元格,接着工具->宏执行行FormatDataToLibsvm宏。就可以得到libsvm要求的数据格式。将该数据存放到文本文件中进行下一步的处理。

   

3.对数据进行归一化。

 

该过程要用到libsvm软件包中的svm-scale.exe
Svm-scale用法:
用法:svmscale [-l lower] [-u upper] [-y y_lower y_upper] [-s save_filename] [-r restore_filename] filename (缺省值: lower = -1,upper = 1,没有对y进行缩放) 其中, -l:数据下限标记;lower:缩放后数据下限; -u:数据上限标记;upper:缩放后数据上限; -y:是否对目标值同时进行缩放;y_lower为下限值,y_upper为上限值;(回归需要对目标进行缩放,因此该参数可以设定为 –y -1 1 ) -s save_filename:表示将缩放的规则保存为文件save_filename; -r restore_filename:表示将缩放规则文件restore_filename载入后按此缩放; filename:待缩放的数据文件(要求满足前面所述的格式)。
缩放规则文件可以用文本浏览器打开,看到其格式为:

y
lower upper min max x
lower upper
index1 min1 max1
index2 min2 max2
„„ 其中的lower 与upper 与使用时所设置的lower 与upper 含义相同;index 表 示特征序号;min 转换前该特征的最小值;max 转换前该特征的最大值。数据集的缩放结果在此情况下通过DOS窗口输出,当然也可以通过DOS的文件重定向符号“>”将结果另存为指定的文件。该文件中的参数可用于最后面对目标值的反归一化。反归一化的公式为:
(Value-lower)*(max-min)/(upper - lower)+lower
其中value为归一化后的值,其他参数与前面介绍的相同。
建议将训练数据集与测试数据集放在同一个文本文件中一起归一化,然后再将归一化结果分成训练集和测试集。

 

4.训练数据,生成模型。

用法: svmtrain [options] training_set_file [model_file]
其中, options(操作参数):可用的选项即表示的涵义如下所示 -s svm类型:设置SVM 类型,默认值为0,可选类型有(对于回归只能选3或4):
0 -- C- SVC 1 -- n - SVC 2 -- one-class-SVM 3 -- e - SVR 4 -- n - SVR -t 核函数类型:设置核函数类型,默认值为2,可选类型有: 0 -- 线性核:u'*v 1 -- 多项式核: (g*u'*v+ coef 0)deg ree 2 -- RBF 核:e( u v 2) g - 3 -- sigmoid 核:tanh(g*u'*v+ coef 0) -d degree:核函数中的degree设置,默认值为3;
-g g :设置核函数中的g ,默认值为1/ k ; -r coef 0:设置核函数中的coef 0,默认值为0; -c cost:设置C- SVC、e - SVR、n - SVR中从惩罚系数C,默认值为1; -n n :设置n - SVC、one-class-SVM 与n - SVR 中参数n ,默认值0.5; -p e :设置n - SVR的损失函数中的e ,默认值为0.1; -m cachesize:设置cache内存大小,以MB为单位,默认值为40; -e e :设置终止准则中的可容忍偏差,默认值为0.001; -h shrinking:是否使用启发式,可选值为0 或1,默认值为1; -b 概率估计:是否计算SVC或SVR的概率估计,可选值0 或1,默认0; -wi weight:对各类样本的惩罚系数C加权,默认值为1; -v n:n折交叉验证模式。
其中-g选项中的k是指输入数据中的属性数。操作参数 -v 随机地将数据剖分为n 部分并计算交叉检验准确度和均方根误差。以上这些参数设置可以按照SVM 的类型和核函数所支持的参数进行任意组合,如果设置的参数在函数或SVM 类型中没有也不会产生影响,程序不会接受该参数;如果应有的参数设置不正确,参数将采用默认值。training_set_file是要进行训练的数据集;model_file是训练结束后产生的模型文件,该参数如果不设置将采用默认的文件名,也可以设置成自己惯用的文件名。
本实验中的参数-s取3,-t取2(默认)还需确定的参数是-c,-g,-p

 

svmtrain 的語法大致就是:

The syntax of svmtrain is basically:

svmtrain [options] training_set_file [model_file]

training_set_file 就是之前的格式,而 model_file 如果不給就會 叫 [training_set_file].model。 options 可以先不要給。

The format of training_set_files is described above. If the model_file is not specified, it'll be [training_set_file].model by default. Options can be ignored at first.

下列程式執行結果會產生 heart_scale.model 檔:(螢幕輸出不是很重要,沒有錯誤就好了) The following command will generate the heart_scale.model file. The screen output may be ignored if there were no errors.

./svm-train heart_scale
optimization finished, #iter = 219
nu = 0.431030
obj = -100.877286, rho = 0.424632
nSV = 132, nBSV = 107
Total nSV = 132

 

svmpredict

svmpredict 的語法是 : The syntax to svm-predict is:

svmpredict test_file model_file output_file

test_file 就是我們要 predict 的資料。它的格式跟 svmtrain 的輸入,也就是 training_set_file 是一樣的! predict 完會順便拿 predict 出來的值跟 test_file 裡面寫的值去做比對,這代表: test_file 寫的 label 是真正的分類結果,拿來跟我們 predict 的結果比對就可以 知道 predict 有沒有猜對了。 test_file is the data the we are going to 'predict'. Its format is almost exactly the same as the training_set_file, which we fed as input to svmtrain. After predicting svm-predict will compare the predicted label with the label written in test_file. That means, test_file has the real (or correct) result of classification, and after comparing with our predicted result we can know whether the prediction is correct or not.

也所以,我們可以拿原 training set 當做 test_file再丟給 svmpredict 去 predict (因為格式一樣),看看正確率有多高, 方便後面調參數。 So we can use the original training_set_file as test_file and feed it to svmpredict for prediction (nothing different in file format) and see how high the accuracy is so we can optimize the arguments.

其它參數就很好理解了: model_file 就是 svmtrain 出來 的檔案, output_file 是存輸出結果的檔案。 Other arguments should be easy to figure out now: model_file is the model trained by svmtrain, and output_file is where we store the output result.

輸出的格式很簡單,每行一個 label,對應到你的 test_file 裡面的各行。 Format of output is simple. Each line contains a label corresponding to your test_file.

下列程式執行結果會產生 heart_scale.out: The following commands will generate heart_scale.out:

./svm-predict heart_scale heart_scale.model heart_scale.out
Accuracy = 86.6667% (234/270) (classification)
Mean squared error = 0.533333 (regression)
Squared correlation coefficient = 0.532639(regression)

As you can see,我們把原輸入丟回去 predict, 第一行的 Accuracy 就是預測的正確率了。 如果輸入沒有 label 的話,那就是真的 predict 了。 As you can see, after we 'predict'ed the original input, we got 'Accuracy=86.6667%" on first line as accuracy of prediction. If we don't put labels in input, the result is real prediction.

看到這裡,基本上你應該已經可以利用 svm 來作事了: 你只要寫程式輸出正確格式的資料,交給 svm 去 train, 後來再 predict 並讀入結果即可。

 

你可能感兴趣的:(File,input,output,Training,optimization,classification)