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

📄 regularizationnetworksalgorithm.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.RegularizationNetworks;

import com.prudsys.pdm.Core.ApplicationAttribute;
import com.prudsys.pdm.Core.AttributeType;
import com.prudsys.pdm.Core.CategoricalAttribute;
import com.prudsys.pdm.Core.MiningDataSpecification;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.MiningModel;
import com.prudsys.pdm.Input.MiningArrayStream;
import com.prudsys.pdm.Input.MiningSparseVector;
import com.prudsys.pdm.Models.Regression.SVM.SupportVectorMiningModel;
import com.prudsys.pdm.Models.Supervised.Classifier;
import com.prudsys.pdm.Models.Supervised.SupervisedMiningAlgorithm;
import com.prudsys.pdm.Transform.Special.ReplaceMissingValueStream;

public class RegularizationNetworksAlgorithm extends SupervisedMiningAlgorithm {
  // Regularization network type:
  public static final int C_SVC          = 0;
  public static final int NU_SVC         = 1;
  public static final int ONE_CLASS      = 2;
  public static final int EPSILON_SVR    = 3;
  public static final int NU_SVR         = 4;
  public static final int SPARSE_GRIDS   = 5;

  // Kernel type:
  public static final int LINEAR         = 0;
  public static final int POLY           = 1;
  public static final int RBF            = 2;
  public static final int SIGMOID        = 3;

  // Grid type (only for sparse grids):
  public static final int TENSOR_PRODUCT = 0;
  public static final int SIMPLICIAL     = 1;

  RegMethod regMethod;
  private int regNetworkType;
  private int kernelType;
  private int gridType;
  private double degree;
  private double gamma;
  private double kernelCoefficient;
  private int gridLevel;
  private double cost;
  private double nu;
  private double lossEpsilon;
  private double cacheSize;
  private double terminationEpsilon;
  private int shrinking;
  private double weight;

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

  /**
   * Run algorithm. Result is written in variable regMethod.
   */
  protected void runAlgorithm() throws com.prudsys.pdm.Core.MiningException {

    System.out.print("Building classifier...");
    regMethod = new RegMethod();

    // Types of regularization network:
    regMethod.m_param.reg_type = regNetworkType;        // type of regularization network
    regMethod.m_param.kernel_type = kernelType;     // kernel type
    regMethod.m_param.grid_type = gridType;       // grid type of sparse grids

    // Parameters of the approximation functions:
    regMethod.m_param.degree = degree;	    // for poly
    regMethod.m_param.gamma = gamma;	    // for poly/rbf/sigmoid
    regMethod.m_param.coef0 = kernelCoefficient;	    // for poly/sigmoid
    regMethod.m_param.level = gridLevel;        // level of sparse grids

    // These parameters are for training only:
    regMethod.m_param.cache_size = cacheSize;   // in MB
    regMethod.m_param.eps = terminationEpsilon;	    // stopping criteria
    regMethod.m_param.C = cost;	    // for C_SVC, EPSILON_SVR, NU_SVR, SPARSE GRIDS
//	public int nr_weight;       // for C_SVC
//	public int[] weight_label;  // for C_SVC
    if (regMethod.m_param.nr_weight!=0)
      regMethod.m_param.weight[0] = weight;	    // for C_SVC
    regMethod.m_param.nu = nu;	    // for NU_SVC, ONE_CLASS, and NU_SVR
    regMethod.m_param.p = lossEpsilon;	    // for EPSILON_SVR
//    System.out.println("loss eps: "+lossEpsilon);
    regMethod.m_param.shrinking = shrinking;	    // use the shrinking heuristics
//    regMethod.m_param.resetParameters();

    // Run algorithm:
    regMethod.buildClassifier(miningInputStream,target);
    System.out.println("\tok");
  }

    /**
     * Creates SupportVectorMiningModel.
     *
     * @return SV mining model
     */
    public MiningModel buildModel() throws MiningException
    {
      ReplaceMissingValueStream rep = new ReplaceMissingValueStream(miningInputStream);
      miningInputStream             = new MiningArrayStream( rep.createReplaceMissingValueStream() );

      runAlgorithm();

      SupportVectorMiningModel model = new SupportVectorMiningModel();
      model.setMiningSettings( miningSettings );
      model.setInputSpec( applicationInputSpecification );
      model.setMiningTransform( rep.getMts() );

      ApplicationAttribute[] appAtt = applicationInputSpecification.getInputAttribute();
      double[] repVal = rep.getRepValues();
      for (int i = 0; i < appAtt.length; i++) {
        if (appAtt[i].getAttributeType().getType() == AttributeType.NUMERICAL) {
          appAtt[i].setMissingValueTreatment(
            ApplicationAttribute.MISSING_VALUE_TREATMENT_METHOD_asMean);
          appAtt[i].setMissingValueReplacement( String.valueOf(repVal[i]) );
        };
        if (appAtt[i].getAttributeType().getType() == AttributeType.CATEGORICAL) {
          appAtt[i].setMissingValueTreatment(
            ApplicationAttribute.MISSING_VALUE_TREATMENT_METHOD_asMode);
          appAtt[i].setMissingValueReplacement(
            ((CategoricalAttribute) metaData.getMiningAttribute(i)).getCategory( repVal[i] ).getDisplayValue() );
        };
      };

      model.setClassifier( getClassifier() );
      ApplicationAttribute target = applicationInputSpecification.getTargetApplicationAttribute();
      model.setTarget( target );

      model.setKernelType(regMethod.m_param.kernel_type);
      model.setSvmType(regMethod.m_param.reg_type);
      model.setCoef0(regMethod.m_param.coef0);
      model.setDegree(regMethod.m_param.degree);
      model.setGamma(regMethod.m_param.gamma);

      RegNode[][] sv = regMethod.m_model.SV;
      MiningSparseVector[] msv = new MiningSparseVector[sv.length];
      MiningDataSpecification metaData = miningSettings.getDataSpecification();
      for(int i=0;i<sv.length;i++)
      {
        double[] values = new double[sv[i].length];
        int[] indices = new int[sv[i].length];
        for(int j=0;j<sv[i].length;j++)
        {
          values[j] = sv[i][j].value;
          indices[j] = sv[i][j].index;
        }
        msv[i] = new MiningSparseVector(0.f,values,indices);
        msv[i].setMetaData(metaData);
      }
      model.setSupportVectors(msv);
      model.setCoefficients(regMethod.m_model.sv_coef[0]);
      model.setAbsoluteCoefficient(regMethod.m_model.rho[0]);

      return model;
    }

  /**
   * Returns classifier.
   *
   * @return classifier
   */
  public Classifier getClassifier() {

    return regMethod;
  }

  public void setRegNetworkType(int regNetworkType) {
    this.regNetworkType = regNetworkType;
  }
  public int getRegNetworkType() {
    return regNetworkType;
  }
  public void setKernelType(int kernelType) {
    this.kernelType = kernelType;
  }
  public int getKernelType() {
    return kernelType;
  }
  public void setGridType(int gridType) {
    this.gridType = gridType;
  }
  public int getGridType() {
    return gridType;
  }
  public void setDegree(double degree) {
    this.degree = degree;
  }
  public double getDegree() {
    return degree;
  }
  public void setGamma(double gamma) {
    this.gamma = gamma;
  }
  public double getGamma() {
    return gamma;
  }
  public void setKernelCoefficient(double kernelCoefficient) {
    this.kernelCoefficient = kernelCoefficient;
  }
  public double getKernelCoefficient() {
    return kernelCoefficient;
  }
  public void setGridLevel(int gridLevel) {
    this.gridLevel = gridLevel;
  }
  public int getGridLevel() {
    return gridLevel;
  }
  public void setCost(double cost) {
    this.cost = cost;
  }
  public double getCost() {
    return cost;
  }
  public void setNu(double nu) {
    this.nu = nu;
  }
  public double getNu() {
    return nu;
  }
  public void setLossEpsilon(double lossEpsilon) {
    this.lossEpsilon = lossEpsilon;
  }
  public double getLossEpsilon() {
    return lossEpsilon;
  }
  public void setCacheSize(double cacheSize) {
    this.cacheSize = cacheSize;
  }
  public double getCacheSize() {
    return cacheSize;
  }
  public void setTerminationEpsilon(double terminationEpsilon) {
    this.terminationEpsilon = terminationEpsilon;
  }
  public double getTerminationEpsilon() {
    return terminationEpsilon;
  }
  public void setShrinking(int shrinking) {
    this.shrinking = shrinking;
  }
  public int getShrinking() {
    return shrinking;
  }
  public void setWeight(double weight) {
    this.weight = weight;
  }
  public double getWeight() {
    return weight;
  }
}

⌨️ 快捷键说明

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