一.下载LibSVM
传送门:https://www.csie.ntu.edu.tw/~cjlin/libsvm/
二.使用LibSVM制作鸢尾花数据集
将下载的压缩文件解压如下:  该文件夹中有多种语言的代码,可自行根据自己的需要进行使用。
点击windows文件夹,在文件夹中找到名为svm-toy.exe的运行程序并运行
手动绘制数据集的点: 使用时,在程序运行框内点击鼠标左键就能打点,点击Change后能够换颜色,最后点击Save将数据保存为train.txt。
在原有基础上再次添加一点数据,保存为test.txt。
三.利用上述数据集实现模型训练并写出决策函数的数学公式
打开IDEA,新建java项目,将压缩文件下java文件夹下的libsvm文件夹所有内容拷贝到项目src中,并将
如下文件也拷贝过去:  并新建一个Test类。
IDEA项目结构如下:  为Test添加代码:
package Demo;
import java.io.IOException;
class test {
public static void main(String args[]) throws IOException {
String filepath = "D:\\libdemo\\";
String[] arg = {"-s","0","-c","10","-t","0",filepath+"test2.txt",filepath+"line.txt"};
String[] arg1 = {filepath+"test.txt",filepath+"line.txt",filepath+"predict1.txt"};
System.out.println("----------------线性-----------------");
svm_train.main(arg);
svm_predict.main(arg1);
arg[5]="1";
arg[7]=filepath+"poly.txt";
arg1[1]=filepath+"poly.txt";
arg1[2]=filepath+"predict2.txt";
System.out.println("---------------多项式-----------------");
svm_train.main(arg);
svm_predict.main(arg1);
arg[5]="2";
arg[7]=filepath+"RBF.txt";
arg1[1]=filepath+"RBF.txt";
arg1[2]=filepath+"predict3.txt";
System.out.println("---------------高斯核-----------------");
svm_train.main(arg);
svm_predict.main(arg1);
}
}
运行项目,结果如下:    决策函数:
公式:f(x)=SV*x+rho
SV的值在生成的txt文件内。 在路径D:\libdemo下可以看到生成了如下文件:  它们分别是使用LibSVM工具进行线性,多项式,高斯核这三种分类训练后得到的数据文件,任意打开一个文件如下:
四.参考文章
LibSVM工具实现决策树训练 LIBSVM使用说明、简介及感悟
|