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

📄 replacemissingattributevalue.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.OneToOne;

import java.util.ArrayList;

import com.prudsys.pdm.Core.CategoricalAttribute;
import com.prudsys.pdm.Core.Category;
import com.prudsys.pdm.Core.MiningAttribute;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Models.Statistics.Group;
import com.prudsys.pdm.Transform.OneToOneMapping;

/**
 * Realization of missing value replacement. By default, the
 * 0-value replacement is used.
 *
 * The replacement value can be set via the
 * corresponding setter method. If a statistics model
 * object is passed, the mean value is taken from this.
 *
 * Use identical transformation; hence target name of attribute is
 * identical to source name and therefore ignored.
 *
 * No PMML convertation implemented because this is usually done
 * via the PMML element MiningSchema.
 */
public class ReplaceMissingAttributeValue extends OneToOneMapping
{
  // -----------------------------------------------------------------------
  //  Variables declarations
  // -----------------------------------------------------------------------
  /** Replacement value. */
  private double repValue = 0.0;

  /** Replacement category for categorical attributes, if defined. */
  private Category repCateg = null;

  /** Key of replacement category, if defined. */
  private double repKey = Category.MISSING_VALUE;

  /** Attribute is categorical. */
  private boolean categType = false;
  
  //<<tyleung 8/3/2005
  /** Transformed attribute. */
  private MiningAttribute transformedAttribute = null;
  // tyleung 8/3/2005>>
  // -----------------------------------------------------------------------
  //  Constructor
  // -----------------------------------------------------------------------
  /**
   * Empty constructor.
   */
  public ReplaceMissingAttributeValue()
  {
  }

  // -----------------------------------------------------------------------
  //  Getter and setter methods
  // -----------------------------------------------------------------------
  /**
   * Returns replacement value.
   *
   * @return replacement value
   */
  public double getRepValue()
  {
    return repValue;
  }

  /**
   * Sets replacement value.
   *
   * @param repValue new replacement value
   */
  public void setRepValue(double repValue)
  {
    this.repValue = repValue;
  }

  /**
   * Returns replacement category.
   *
   * @return replacement category
   */
  public Category getRepCateg()
  {
    return repCateg;
  }

  /**
   * Sets replacement category.
   *
   * @param repCateg new replacement category
   */
  public void setRepCateg(Category repCateg)
  {
    this.repCateg = repCateg;
  }

  // -----------------------------------------------------------------------
  //  Transformation methods
  // -----------------------------------------------------------------------
  /**
   * Transforms the source attribute. The result is the target attribute.
   * Identity operator.
   *
   * @return transformed (normalized) attribute
   * @exception MiningException could not transform attribute
   */
  public MiningAttribute transformAttribute() throws MiningException
  {
    if (getSourceAttribute() == null)
        throw new MiningException("Could not find source attribute: " + sourceName);

    //<<tyleung 8/3/2005
    MiningAttribute sourceAttribute = getSourceAttribute();
    if (sourceAttribute instanceof CategoricalAttribute) {
      categType = true;
      
      transformedAttribute = new CategoricalAttribute();
      //<<Frank J. Xu, 14/03/2005
      //Set the transformedAttribute's name to original sourceAttribute's name
      //instead of temporary attribute's name.
      //transformedAttribute.setName( getTargetNameDynamic() );
      transformedAttribute.setName( sourceAttribute.getName());
      //Frank J. Xu, 14/03/2005>>
            
      ArrayList sourceCategs = ((CategoricalAttribute)sourceAttribute).getValues();
      ArrayList categs = new ArrayList();
      for (int i = 0; i < sourceCategs.size(); i++) {
        Category value = (Category) sourceCategs.get(i);
        if (! categs.contains(value))
          categs.add(value);
      };

      try {
        ((CategoricalAttribute)transformedAttribute).setValues(categs);
      }
      catch (MiningException ex) {;};
      
      repKey = ((CategoricalAttribute)transformedAttribute).getKey(repCateg);
      if(Double.isNaN(repKey)){
          ((CategoricalAttribute)transformedAttribute).addCategory(repCateg);
          repKey = ((CategoricalAttribute)transformedAttribute).getKey(repCateg);
      }
    }
    else{
      categType = false;
      transformedAttribute = sourceAttribute;
    }
    if (statisticsMiningModel != null) {
      Group group = statisticsMiningModel.getRootGroup();
      repValue    = group.getMean();
    };

   
    return transformedAttribute;
    //tyleung 8/3/2005>>
   
  }

  /**
   * Transforms attribute value. The result is also a value.
   *
   * @param attributeValue value of attribute to be transformed
   * @return tranformed (normalized) value
   * @exception MiningException could not transform attribute value
   */
  public double transformAttributeValue( double attributeValue ) throws MiningException
  {
      double transformedValue = attributeValue;

      // Is missing value => replace:
      if (Category.isMissingValue( attributeValue )) {
          transformedValue = repValue;

          // Categorical attribute => key of replacement category:
          if (categType && ! Category.isMissingValue(repKey))
            transformedValue = repKey;
      };

      return transformedValue;
  }

  // -----------------------------------------------------------------------
  //  Methods of PMML handling
  // -----------------------------------------------------------------------
  /**
   * Creates PMML object DerivedField of this object.
   *
   * @return DerivedField element
   * @see com.prudsys.pdm.Adapters.PmmlVersion20.TransformationDictionary
   * @exception MiningException could not create PMML object
   */
  public Object createPmmlObject() throws MiningException
  {
    /**@todo Implement this com.prudsys.pdm.DataMining.PmmlPresentable method*/
      throw new java.lang.UnsupportedOperationException("Method createPmmlObject() not yet implemented.");
  }

  /**
   * Creates this object from PMML.
   * Currently not supported.
   *
   * @param pmml pmml element
   * @exception MiningException always thrown
   */
  public void parsePmmlObject(Object pmml) throws MiningException
  {
      /**@todo Implement this com.prudsys.pdm.DataMining.PmmlPresentable method*/
      throw new java.lang.UnsupportedOperationException("Method parsePmmlObject() not yet implemented.");
  }
}

⌨️ 快捷键说明

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