⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 libsvmadapter.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
字号:
/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/**
 * Title: XELOPES Data Mining Library
 * Description: The XELOPES library is an open platform-independent and data-source-independent library for Embedded Data Mining.
 * Copyright: Copyright (c) 2002 Prudential Systems Software GmbH
 * Company: ZSoft (www.zsoft.ru), Prudsys (www.prudsys.com)
 * @author Michael Thess
 * @author Victor Borichev
 * @author Valentine Stepanenko (valentine.stepanenko@zsoft.ru)
 * @version 1.0
 */

package com.prudsys.pdm.Models.Regression.SVM.Algorithms.SparseSVM;

import com.prudsys.pdm.Models.Regression.SVM.Algorithms.libsvm.svm;
import com.prudsys.pdm.Models.Regression.SVM.Algorithms.libsvm.svm_model;
import com.prudsys.pdm.Models.Regression.SVM.Algorithms.libsvm.svm_node;
import com.prudsys.pdm.Models.Regression.SVM.Algorithms.libsvm.svm_parameter;
import com.prudsys.pdm.Models.Regression.SVM.Algorithms.libsvm.svm_problem;

/**
 * Adapter to LIBSVM.
 */
public class LIBSVMAdapter
{

  /**
   * Empty constructor.
   */
  public LIBSVMAdapter()
  {
  }

  /**
   * Training using the LIBSVM.
   *
   * @param m_prob SVM problem
   * @param m_param SVM parameter
   * @return SVM model
   */
  public static SVMModel svm_train(SVMProblem m_prob, SVMParameters m_param) {

    // Convert RegProblem to svm_problem:
    svm_problem s_prob = new svm_problem();
    s_prob.l = m_prob.l;
    s_prob.y = new double[ m_prob.y.length ];
    for (int i = 0; i < m_prob.y.length; i++)
      s_prob.y[i] = m_prob.y[i];
    s_prob.x = new svm_node[ m_prob.x.length ][];
    for (int i = 0; i < m_prob.x.length; i++) {
      s_prob.x[i] = new svm_node[ m_prob.x[i].length ];
      for (int j = 0; j < m_prob.x[i].length; j++) {
        s_prob.x[i][j] = new svm_node();
        s_prob.x[i][j].index = m_prob.x[i][j].index;
        s_prob.x[i][j].value = m_prob.x[i][j].value;
      };
    };

    // Convert SVMParameters to svm_parameter:
    svm_parameter s_param = svm2libParam(m_param);

    // Run training on LIBSVM:
    svm_model s_model = svm.svm_train(s_prob, s_param);

    // Convert svm_model to SVMModel:
    SVMModel model = new SVMModel();

    model.param = m_param;
    model.nr_class = s_model.nr_class;
    model.l = s_model.l;

    model.SV = new SVMNode[ s_model.SV.length ][];
    for (int i = 0; i < s_model.SV.length; i++) {
      model.SV[i] = new SVMNode[ s_model.SV[i].length ];
      for (int j = 0; j < s_model.SV[i].length; j++) {
        model.SV[i][j] = new SVMNode();
        model.SV[i][j].index = s_model.SV[i][j].index;
        model.SV[i][j].value = s_model.SV[i][j].value;
      };
    };

    model.sv_coef = s_model.sv_coef;
    model.rho = s_model.rho;

    model.label = s_model.label;
    model.nSV = s_model.nSV;

    return model;
  }

  /**
   * Prediction using the LIBSVM.
   *
   * @param model SVM model of svm_model type
   * @param vector as array of nodes
   * @return prediction score
   */
  public static double svm_predict(SVMModel model, SVMNode[] rNode) {

    // Convert SVMModel in svm_model:
    svm_model s_model = svm2libModel( model );

    // Convert array of SVMNode to array of svm_node:
    svm_node[] s_node = new svm_node[ rNode.length ];
    for (int i = 0; i < rNode.length; i++) {
      s_node[i] = new svm_node();
      s_node[i].index = rNode[i].index;
      s_node[i].value = rNode[i].value;
    };

    // Run predict on LIBSVM:
    double predict = svm.svm_predict(s_model, s_node);

    return predict;
  }

  /**
   * Save SVM model as file.
   *
   * @param fileName path to model file
   * @param model SVM model
   */
  public static void svm_save_model(String fileName, SVMModel model)
    throws Exception{

    // Convert SVMModel in svm_model:
    svm_model s_model = svm2libModel( model );

    // Save model as file:
    svm.svm_save_model(fileName, s_model);
  }

  /**
   * Convert svm to LIBSVM model.
   *
   * @param model model of SVM
   * @return model of LIBSVM
   */
  private static svm_model svm2libModel(SVMModel model) {

    svm_model s_model = new svm_model();

    s_model.param = svm2libParam( model.param );
    s_model.nr_class = model.nr_class;
    s_model.l = model.l;

    s_model.SV = new svm_node[ model.SV.length ][];
    for (int i = 0; i < model.SV.length; i++) {
      s_model.SV[i] = new svm_node[ model.SV[i].length ];
      for (int j = 0; j < model.SV[i].length; j++) {
        s_model.SV[i][j] = new svm_node();
        s_model.SV[i][j].index = model.SV[i][j].index;
        s_model.SV[i][j].value = model.SV[i][j].value;
      };
    };

    s_model.sv_coef = model.sv_coef;
    s_model.rho = model.rho;

    s_model.label = model.label;
    s_model.nSV = model.nSV;

    return s_model;
  }

  /**
   * Convert SVM to LIBSVM parameters.
   *
   * @param m_param parameters of SVM
   * @return parameters of LIBSVM
   */
  private static svm_parameter svm2libParam(SVMParameters m_param) {

    svm_parameter s_param = new svm_parameter();

    // Model parameters:
    s_param.svm_type = m_param.svm_type;
    s_param.kernel_type = m_param.kernel_type;
    s_param.degree = m_param.degree;
    s_param.gamma = m_param.gamma;
    s_param.coef0 = m_param.coef0;

    // Training parameters:
    s_param.cache_size = m_param.cache_size;
    s_param.eps = m_param.eps;
    s_param.C = m_param.C;
    s_param.nr_weight = m_param.nr_weight;
    s_param.weight_label = m_param.weight_label;
    s_param.weight = s_param.weight;
    s_param.nu = m_param.nu;
    s_param.p = m_param.p;
    s_param.shrinking = m_param.shrinking;

    return s_param;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -