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

📄 replacemissingvaluestream.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
  * @version 1.0
  */
package com.prudsys.pdm.Transform.Special;

import com.prudsys.pdm.Core.CategoricalAttribute;
import com.prudsys.pdm.Core.Category;
import com.prudsys.pdm.Core.MiningAttribute;
import com.prudsys.pdm.Core.MiningDataSpecification;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.NumericAttribute;
import com.prudsys.pdm.Input.MiningInputStream;
import com.prudsys.pdm.Models.Statistics.SimpleStats;
import com.prudsys.pdm.Transform.MiningTransformationFactory;
import com.prudsys.pdm.Transform.MiningTransformationStep;
import com.prudsys.pdm.Transform.OneToOne.ReplaceMissingAttributeValue;

/**
 * Realization of missing value replacement for a given mining input
 * stream. By default, the mean values are used for numeric attributes
 * and the mode values for categorical attributes.
 */
public class ReplaceMissingValueStream extends VectorTransformationStream
{
  // -----------------------------------------------------------------------
  //  Variables declarations
  // -----------------------------------------------------------------------
  /** Array of replacement values for all attributes. */
  private double[] repValues = null;

  /** Replace all values with zero. */
  private boolean zeroValues = false;

  // -----------------------------------------------------------------------
  //  Constructors
  // -----------------------------------------------------------------------
  /**
   * Empty constructor.
   */
  public ReplaceMissingValueStream()
  {
  }

  /**
   * Constructor for given stream.
   *
   * @param inputStream mining input stream for calculating replacement parameters
   */
  public ReplaceMissingValueStream(MiningInputStream inputStream) {

    this.inputStream = inputStream;
  }

  // -----------------------------------------------------------------------
  //  Getter and setter methods
  // -----------------------------------------------------------------------
  /**
   * Replace all values with zero?
   *
   * @return true if all values replaced by zero, false otherwise
   */
  public boolean isZeroValues()
  {
    return zeroValues;
  }

  /**
   * Set replace all values by zero.
   *
   * @param zeroValues replace all values by zero
   */
  public void setZeroValues(boolean zeroValues)
  {
    this.zeroValues = zeroValues;
  }

  /**
   * Returns array of all replacement values.
   *
   * @return array of all replacement values
   */
  public double[] getRepValues()
  {
    return repValues;
  }

  // -----------------------------------------------------------------------
  //  Transformation methods
  // -----------------------------------------------------------------------
  /**
   * Calculates replacement values using the mean for numeric
   * and the mode for categorical attributes.
   *
   * @exception MiningException error while calculating replacement values
   */
  private void calcRepValues() throws MiningException {

    // Init:
    int nAtt = inputStream.getMetaData().getAttributesNumber();
    repValues = new double[nAtt];

    // Zero values => return:
    if (zeroValues) return;

    // Calculate simple statistics:
    SimpleStats sist = new SimpleStats();
    sist.setInputStream(inputStream);
    sist.runCalculation();

    // Fill array of replacement values:
    MiningDataSpecification metaData = inputStream.getMetaData();
    for (int i = 0; i < nAtt; i++) {
      MiningAttribute att = metaData.getMiningAttribute(i);
      if (att instanceof NumericAttribute)
        repValues[i] = sist.getCalculatedValue(att, SimpleStats.STAT_MEAN);
      else
        repValues[i] = sist.getCalculatedValue(att, SimpleStats.STAT_MODE);
    };
  }

  /**
   * Creates mining transformation step for replacing missing values
   * by valid ones.
   *
   * @return mining transformation step
   * @exception MiningException no input stream defined
   */
  public MiningTransformationStep createMiningTransformationStep() throws MiningException  {

    // No mining input stream defined => exception:
    if (inputStream == null)
      throw new MiningException("No mining input stream defined");

    // Get mean and modes of all attributes:
    calcRepValues();

    // Mining transformation factory:
    MiningTransformationFactory mtf = new MiningTransformationFactory();

    MiningDataSpecification metaData = inputStream.getMetaData();
    for (int i = 0; i < metaData.getAttributesNumber(); i++) {
      // Get attribute and name:
      MiningAttribute mAtt = metaData.getMiningAttribute(i);
      String attName = mAtt.getName();

      // Don't use excluded attributes, if defined:
      if ( excludedAttributeNames != null && excludedAttributeNames.indexOf(attName) > -1)
        continue;

      // Add replace missing value transformation:
      ReplaceMissingAttributeValue rep = new ReplaceMissingAttributeValue();
      rep.setSourceName( attName );
      rep.setRepValue( repValues[i] );
      if (mAtt instanceof CategoricalAttribute) {
        Category cat = ((CategoricalAttribute) mAtt).getCategory(repValues[i]);
        if (cat != null) rep.setRepCateg(cat);
      };

      mtf.addOneToOneMapping(rep);
    };

    // Create transformation step for replacement:
    mts = mtf.createMiningTransformationStep();

    return mts;
  }

  // -----------------------------------------------------------------------
  //  Other methods
  // -----------------------------------------------------------------------
  /**
   * Displays replacement values.
   *
   * @returns replacement values
   */
  public String toString() {

    String mess = "Replace missing value stream. Replacement: ";
    if (repValues == null)
      mess = mess + "means and modes.";
    else {
      for (int i = 0; i < repValues.length; i++)
        mess = mess + String.valueOf( repValues[i] ) + " ";
    };

    return mess;
  }

  // -----------------------------------------------------------------------
  //  Old methods. Should no longer be used
  // -----------------------------------------------------------------------
  /**
   * Creates mining transformation step for replacing missing values
   * by valid ones. Deprecated, use createMiningTransformationStep instead.
   *
   * @return mining transformation step
   * @exception MiningException no input stream defined
   */
  public MiningTransformationStep createReplaceMissingValueTransformationStep() throws MiningException  {

    return createMiningTransformationStep();
  }

  /**
   * Returns mining transformation step. Deprecated, use
   * getMiningTransformationStep instead.
   *
   * @return mining transformation step
   */
  public MiningTransformationStep getMts()
  {
    return getMiningTransformationStep();
  }

  /**
   * Creates mining input stream where missing values are replaced
   * by valid ones. Uses mining filter stream. Deprecated, use
   * createMiningTransformationStep instead.
   *
   * @return mining filter stream which replaces missing values
   * @exception MiningException cannot create transformed stream
   */
  public MiningInputStream createReplaceMissingValueStream() throws MiningException  {

      return createTransformedStream();
  }
}

⌨️ 快捷键说明

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