文档章节

LibSVM在Java中的简单应用

晨曦断罪
 晨曦断罪
发布于 2016/12/28 12:48
字数 1549
阅读 53
收藏 0

首先,在这里首先感谢台湾林智仁先生的开源工具包libsvm。使SVM算法更加普及。大家可以到下面的libsvm官网去了解相关的信息。
Libsvm官方网站->https://www.csie.ntu.edu.tw/~cjlin/libsvm/
其次,我在使用过程中发现,先生svm_scale文件中无法将经过规约的文件输出到本地txt文件中,只能在控制台重定向,而我并不想在程序运行中打开控制台进行较为繁琐的操作。
所以我改造了svm_scale文件,实现了文件的写入,在这里可以和大家分享一下。
改造后新增参数“-z [filename]” 参数filename表示输出文件的路径。

public static void main(String args[])
{
            String[] testArgs_Test = {"-l","0", "-u","1","-z","C:\\db\\svm\\scale_testData.txt","-s","C:\\db\\svm\\chao-test-scale.txt","C:\\db\\svm\\UCI-breast-cancer-tra"};
            //-l [] 规约文件属性值的下限
            //-u [] 规约文件属性值的下限
            //-s [] 规约标准存放的位置
            //-z [] 对训练样本进行规约后输出到本地的文件
            //C:\\db\\svm\\UCI-breast-cancer-tra 参数最后的训练样本的Path路径
            String[] testArgs_Predict = {"-l","0", "-u","1","-z","C:\\db\\svm\\scale_predictData.txt","-s","C:\\db\\svm\\chao-predict-scale.txt","C:\\db\\svm\\UCI-breast-cancer-test"};
                getTest_scale(testArgs_Test);
                getPredict_scale(testArgs_Predict);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e2) {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                }
                String[] arg = { "C:\\db\\svm\\scale_testData.txt", // 存放SVM训练模型用的数据的路径
                "C:\\db\\svm\\\\model_r.txt" };
                String[] parg = { "C:\\db\\svm\\scale_predictData.txt", // 这个是存放测试数据
                        "C:\\db\\svm\\model_r.txt", // 调用的是训练以后的模型
                        "C:\\db\\svm\\out_r.txt" }; // 生成的结果的文件的路径
                System.out.println("........SVM运行开始..........");
                // 创建一个训练对象
                svm_train t = new svm_train();
                // 创建一个预测或者分类的对象
                svm_predict p = new svm_predict();
                try {
                    t.main(arg);// 调用
                    p.main(parg); // 调用
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } 

            }
        });

    }
    /** 这里使用多线程使训练样本和预测样本进行规约并输出至本地路径 * 方便svm_model和svm_predict文件调用 * */
    public void  getTest_scale(String[] args) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    svm_scaleUpdate.main(args);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();
    }
    public void getPredict_scale(String[] args) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    svm_scaleUpdate.main(args);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

运行结果:

……..SVM运行开始……….
*
optimization finished, #iter = 50
nu = 0.11329630723694926
obj = -58.4674213525147, rho = -0.8102576113199965
nSV = 76, nBSV = 70
Total nSV = 76
Accuracy = 92.3076923076923% (36/39) (classification)

找了找没发现怎么上传文件,直接将代码复制出来吧!
svm_scaleUpdate.java—–>>>>>>

package demo;


import java.io.*;
import java.util.*;
/** * 此类升级至svm_scaleUpdate * 新增参数“-z [filename]” 表示将输出文件输出至本地方便训练及预测使用 * @author Administrator * */
class svm_scaleUpdate
{
    private String line = null;
    private double lower = -1.0;
    private double upper = 1.0;
    private double y_lower;
    private double y_upper;
    private boolean y_scaling = false;
    private double[] feature_max;
    private double[] feature_min;
    private double y_max = -Double.MAX_VALUE;
    private double y_min = Double.MAX_VALUE;
    private int max_index;
    private long num_nonzeros = 0;
    private long new_num_nonzeros = 0;

    private FileWriter fWriter = null;



    private static void exit_with_help()
    {
        System.out.print(
         "Usage: svm-scale [options] data_filename\n"
        +"options:\n"
        +"-l lower : x scaling lower limit (default -1)\n"
        +"-u upper : x scaling upper limit (default +1)\n"
        +"-y y_lower y_upper : y scaling limits (default: no y scaling)\n"
        +"-s save_filename : save scaling parameters to save_filename\n"
        +"-r restore_filename : restore scaling parameters from restore_filename\n"
        );
        System.exit(1);
    }

    private BufferedReader rewind(BufferedReader fp, String filename) throws IOException
    {
        fp.close();
        return new BufferedReader(new FileReader(filename));
    }

    private void output_target(double value)
    {
        if(y_scaling)
        {
            if(value == y_min)
                value = y_lower;
            else if(value == y_max)
                value = y_upper;
            else
                value = y_lower + (y_upper-y_lower) *
                (value-y_min) / (y_max-y_min);
        }

        System.out.print(value + " ");
        try {
            fWriter.write(value + " ");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void output(int index, double value)
    {
        /* skip single-valued attribute */
        if(feature_max[index] == feature_min[index])
            return;

        if(value == feature_min[index])
            value = lower;
        else if(value == feature_max[index])
            value = upper;
        else
            value = lower + (upper-lower) * 
                (value-feature_min[index])/
                (feature_max[index]-feature_min[index]);

        if(value != 0)
        {
            System.out.print(index + ":" + value + " ");
            try {
                fWriter.write(index + ":" + value + " ");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            new_num_nonzeros++;
        }
    }

    private String readline(BufferedReader fp) throws IOException
    {
        line = fp.readLine();
        return line;
    }

    private void run(String []argv) throws IOException
    {
        int i,index;
        BufferedReader fp = null, fp_restore = null;
        String save_filename = null;
        String restore_filename = null;
        String data_filename = null;
        String save_file = null;


        for(i=0;i<argv.length;i++)
        {
            if (argv[i].charAt(0) != '-')   break;
            ++i;
            switch(argv[i-1].charAt(1))
            {
                case 'l': lower = Double.parseDouble(argv[i]);  break;
                case 'u': upper = Double.parseDouble(argv[i]);  break;
                case 'y':
                      y_lower = Double.parseDouble(argv[i]);
                      ++i;
                      y_upper = Double.parseDouble(argv[i]);
                      y_scaling = true;
                      break;
                case 's': save_filename = argv[i];  break;
                case 'r': restore_filename = argv[i];   break;
                case 'z': save_file = argv[i];  break;
                default:
                      System.err.println("unknown option");
                      exit_with_help();
            }
        }

        if(!(upper > lower) || (y_scaling && !(y_upper > y_lower)))
        {
            System.err.println("inconsistent lower/upper specification");
            System.exit(1);
        }
        if(restore_filename != null && save_filename != null)
        {
            System.err.println("cannot use -r and -s simultaneously");
            System.exit(1);
        }

        if(argv.length != i+1)
            exit_with_help();

        data_filename = argv[i];
        try {
            fp = new BufferedReader(new FileReader(data_filename));
        } catch (Exception e) {
            System.err.println("can't open file " + data_filename);
            System.exit(1);
        }

        File save_file_data = new File(save_file);
        fWriter = new FileWriter(save_file_data);

        /* assumption: min index of attributes is 1 */
        /* pass 1: find out max index of attributes */
        max_index = 0;

        if(restore_filename != null)
        {
            int idx, c;

            try {
                fp_restore = new BufferedReader(new FileReader(restore_filename));
            }
            catch (Exception e) {
                System.err.println("can't open file " + restore_filename);
                System.exit(1);
            }
            if((c = fp_restore.read()) == 'y')
            {
                fp_restore.readLine();
                fp_restore.readLine();      
                fp_restore.readLine();      
            }
            fp_restore.readLine();
            fp_restore.readLine();

            String restore_line = null;
            while((restore_line = fp_restore.readLine())!=null)
            {
                StringTokenizer st2 = new StringTokenizer(restore_line);
                idx = Integer.parseInt(st2.nextToken());
                max_index = Math.max(max_index, idx);
            }
            fp_restore = rewind(fp_restore, restore_filename);
        }

        while (readline(fp) != null)
        {
            StringTokenizer st = new StringTokenizer(line," \t\n\r\f:");
            st.nextToken();
            while(st.hasMoreTokens())
            {
                index = Integer.parseInt(st.nextToken());
                max_index = Math.max(max_index, index);
                st.nextToken();
                num_nonzeros++;
            }
        }

        try {
            feature_max = new double[(max_index+1)];
            feature_min = new double[(max_index+1)];
        } catch(OutOfMemoryError e) {
            System.err.println("can't allocate enough memory");
            System.exit(1);
        }

        for(i=0;i<=max_index;i++)
        {
            feature_max[i] = -Double.MAX_VALUE;
            feature_min[i] = Double.MAX_VALUE;
        }

        fp = rewind(fp, data_filename);

        /* pass 2: find out min/max value */
        while(readline(fp) != null)
        {
            int next_index = 1;
            double target;
            double value;

            StringTokenizer st = new StringTokenizer(line," \t\n\r\f:");
            target = Double.parseDouble(st.nextToken());
            y_max = Math.max(y_max, target);
            y_min = Math.min(y_min, target);

            while (st.hasMoreTokens())
            {
                index = Integer.parseInt(st.nextToken());
                value = Double.parseDouble(st.nextToken());

                for (i = next_index; i<index; i++)
                {
                    feature_max[i] = Math.max(feature_max[i], 0);
                    feature_min[i] = Math.min(feature_min[i], 0);
                }

                feature_max[index] = Math.max(feature_max[index], value);
                feature_min[index] = Math.min(feature_min[index], value);
                next_index = index + 1;
            }

            for(i=next_index;i<=max_index;i++)
            {
                feature_max[i] = Math.max(feature_max[i], 0);
                feature_min[i] = Math.min(feature_min[i], 0);
            }
        }

        fp = rewind(fp, data_filename);

        /* pass 2.5: save/restore feature_min/feature_max */
        if(restore_filename != null)
        {
            // fp_restore rewinded in finding max_index 
            int idx, c;
            double fmin, fmax;

            fp_restore.mark(2);             // for reset
            if((c = fp_restore.read()) == 'y')
            {
                fp_restore.readLine();      // pass the '\n' after 'y'
                StringTokenizer st = new StringTokenizer(fp_restore.readLine());
                y_lower = Double.parseDouble(st.nextToken());
                y_upper = Double.parseDouble(st.nextToken());
                st = new StringTokenizer(fp_restore.readLine());
                y_min = Double.parseDouble(st.nextToken());
                y_max = Double.parseDouble(st.nextToken());
                y_scaling = true;
            }
            else
                fp_restore.reset();

            if(fp_restore.read() == 'x') {
                fp_restore.readLine();      // pass the '\n' after 'x'
                StringTokenizer st = new StringTokenizer(fp_restore.readLine());
                lower = Double.parseDouble(st.nextToken());
                upper = Double.parseDouble(st.nextToken());
                String restore_line = null;
                while((restore_line = fp_restore.readLine())!=null)
                {
                    StringTokenizer st2 = new StringTokenizer(restore_line);
                    idx = Integer.parseInt(st2.nextToken());
                    fmin = Double.parseDouble(st2.nextToken());
                    fmax = Double.parseDouble(st2.nextToken());
                    if (idx <= max_index)
                    {
                        feature_min[idx] = fmin;
                        feature_max[idx] = fmax;
                    }
                }
            }
            fp_restore.close();
        }

        if(save_filename != null)
        {
            Formatter formatter = new Formatter(new StringBuilder());
            BufferedWriter fp_save = null;

            try {
                fp_save = new BufferedWriter(new FileWriter(save_filename));
            } catch(IOException e) {
                System.err.println("can't open file " + save_filename);
                System.exit(1);
            }

            if(y_scaling)
            {
                formatter.format("y\n");
                formatter.format("%.16g %.16g\n", y_lower, y_upper);
                formatter.format("%.16g %.16g\n", y_min, y_max);
            }
            formatter.format("x\n");
            formatter.format("%.16g %.16g\n", lower, upper);
            for(i=1;i<=max_index;i++)
            {
                if(feature_min[i] != feature_max[i]) 
                    formatter.format("%d %.16g %.16g\n", i, feature_min[i], feature_max[i]);
            }
            fp_save.write(formatter.toString());
            fp_save.close();
        }

        /* pass 3: scale */
        while(readline(fp) != null)
        {
            int next_index = 1;
            double target;
            double value;

            StringTokenizer st = new StringTokenizer(line," \t\n\r\f:");
            target = Double.parseDouble(st.nextToken());
            output_target(target);
            while(st.hasMoreElements())
            {
                index = Integer.parseInt(st.nextToken());
                value = Double.parseDouble(st.nextToken());
                for (i = next_index; i<index; i++)
                    output(i, 0);
                output(index, value);

                next_index = index + 1;
            }

            for(i=next_index;i<= max_index;i++)
                output(i, 0);

            System.out.print("\n");
            fWriter.write("\r\n");
        }
        if (new_num_nonzeros > num_nonzeros)
            System.err.print(
             "WARNING: original #nonzeros " + num_nonzeros+"\n"
            +" new #nonzeros " + new_num_nonzeros+"\n"
            +"Use -l 0 if many original feature values are zeros\n");

        fp.close();
        fWriter.flush();
        fWriter.close();
    }

    public static void main(String argv[]) throws IOException
    {
        svm_scaleUpdate s = new svm_scaleUpdate();
        s.run(argv);
    }
}

© 著作权归作者所有

晨曦断罪
粉丝 0
博文 16
码字总数 12398
作品 0
石家庄
程序员
私信 提问
MATLAB安装libsvm工具箱的方法

支持向量机(support vector machine,SVM)是机器学习中一种流行的学习算法,在分类与回归分析中发挥着重要作用。基于SVM算法开发的工具箱有很多种,下面我们要安装的是十分受欢迎的libsvm工...

东聃
2018/08/12
0
0
LibSvm使用说明和LibSvm源码解析

kernel_type rbf //训练采用的核函数类型,此处为RBF核gamma 0.0769231 //RBF核的参数γnr_class 2 //类别数,此处为两分类问题total_sv 132 //支持向量总个数rho 0.424462 //判决函数的偏置...

haoji007
2018/05/13
0
0
机器学习算法 Java 库 Smile 1.5.0 发布,引入新特性

机器学习算法 Java 库 Smile 1.5.0 已发布。该版本引入了新特性和修复 bug,改进了对 Windows 的支持,具体如下: DataFrame New Shell for Mac and Linux Shell improvement for Windows Ou...

局长
2017/11/23
1K
4
sparkmlib的sample_binary_classification_data.txt 和sample_libsvm_data.txt内容怎么换成实际项目内容

sample_binary_classification_data.txt 和sample_libsvm_data.txt的内容怎么理解和使用 sparkmlib的sample_binary_classification_data.txt 和sample_libsvm_data.txt内容怎么换成实际项目内......

知行合一1
2017/09/30
176
0
【毕设进行时-工业大数据,数据挖掘】LIBSVM 初步测试

正文之前 打摆子的日子很快就要一去不复返了。想想有点悲伤。今天做了下LibSVM的初步运用,也写了个从数据库读取数据,然后改造成LibSVM需要的数据格式的类,需要的自取。 正文 这是个从前面...

HustWolf
2018/04/23
0
0

没有更多内容

加载失败,请刷新页面

加载更多

Spring Boot + Mybatis-Plus 集成与使用(二)

前言: 本章节介绍MyBatis-Puls的CRUD使用。在开始之前,先简单讲解下上章节关于Spring Boot是如何自动配置MyBatis-Plus。 一、自动配置 当Spring Boot应用从主方法main()启动后,首先加载S...

伴学编程
昨天
7
0
用最通俗的方法讲spring [一] ──── AOP

@[TOC](用最通俗的方法讲spring [一] ──── AOP) 写这个系列的目的(可以跳过不看) 自己写这个系列的目的,是因为自己是个比较笨的人,我曾一度怀疑自己的智商不适合干编程这个行业.因为在我...

小贼贼子
昨天
7
0
Flutter系列之在 macOS 上安装和配置 Flutter 开发环境

本文为Flutter开发环境在macOS下安装全过程: 一、系统配置要求 想要安装并运行 Flutter,你的开发环境需要最低满足以下要求: 操作系统:macOS(64位) 磁盘空间:700 MB(不包含 IDE 或其余...

過愙
昨天
6
0
OSChina 周六乱弹 —— 早上儿子问我他是怎么来的

Osc乱弹歌单(2019)请戳(这里) 【今日歌曲】 @凉小生 :#今日歌曲推荐# 少点戾气,愿你和这个世界温柔以待。中岛美嘉的单曲《僕が死のうと思ったのは (曾经我也想过一了百了)》 《僕が死の...

小小编辑
昨天
2.7K
16
Excption与Error包结构,OOM 你遇到过哪些情况,SOF 你遇到过哪些情况

Throwable 是 Java 中所有错误与异常的超类,Throwable 包含两个子类,Error 与 Exception 。用于指示发生了异常情况。 Java 抛出的 Throwable 可以分成三种类型。 被检查异常(checked Exc...

Garphy
昨天
42
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部