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

📄 transtypecateg.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.1
  */
package com.prudsys.pdm.Transform.OneToOne;

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.Transform.OneToOneMapping;

/**
 * Transforms categorical attribute to target data type.
 *
 * Transforms a categorical into another categorical attribute
 * by transforming categories to target data type. Data type of
 * categorical attribute is used.
 */
public class TransTypeCateg extends OneToOneMapping
{
  // -----------------------------------------------------------------------
  //  Variables declarations
  // -----------------------------------------------------------------------
  /** Data type of source attribute. */
  private int sourceDataType = CategoricalAttribute.DEFAULT;

  /** Data type of target attribute. */
  private int targetDataType = CategoricalAttribute.DEFAULT;

  /** Transformed attribute. */
  private CategoricalAttribute transformedAttribute = null;

  // -----------------------------------------------------------------------
  //  Constructor
  // -----------------------------------------------------------------------
  /**
   * Empty constructor.
   */
  public TransTypeCateg()
  {
  }

  // -----------------------------------------------------------------------
  //  Getter and setter methods
  // -----------------------------------------------------------------------
  /**
   * Returns target attribute data type.
   *
   * @return target attribute data type
   */
  public int getTargetDataType() {

    return targetDataType;
  }

  /**
   * Sets target attribute data type.
   *
   * @param targetDataType new data type of target attribute
   */
  public void setTargetDataType(int targetDataType) {

    this.targetDataType = targetDataType;
  }

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

      if (! (getSourceAttribute() instanceof CategoricalAttribute))
        throw new MiningException("Source attribute '" + sourceName + "' must be categorical");

      CategoricalAttribute sourceAttribute = (CategoricalAttribute) getSourceAttribute();
      transformedAttribute                 = new CategoricalAttribute();

      transformedAttribute.setName( getTargetNameDynamic() );
      transformedAttribute.setDataType( targetDataType );
      transformedAttribute.setUnboundedCategories( sourceAttribute.isUnboundedCategories() );
      transformedAttribute.setUnstoredCategories( sourceAttribute.isUnstoredCategories() );

      sourceDataType = sourceAttribute.getDataType();

      return transformedAttribute;
  }

  /**
   * Transforms key of a category of source attribute into the
   * key of the transformed category.
   *
   * @param attributeValue key of category to be mapped
   * @return key of mapped category, missing value if no category for key found
   * @exception MiningException could not transform attribute value
   */
  public double transformAttributeValue( double attributeValue ) throws MiningException
  {
    // Identic tranformation => return:
    if (sourceDataType == targetDataType)
      return attributeValue;

    // Get source attribute and category:
    CategoricalAttribute sourceAttribute  = (CategoricalAttribute) getSourceAttribute();
    Category cat = sourceAttribute.getCategory(attributeValue);

    // Transformation:
    double transformedValue = Category.MISSING_VALUE;
    if (sourceDataType == CategoricalAttribute.STRING) {
      transformedValue = transformFromString(cat);
    }
    else if (targetDataType == CategoricalAttribute.STRING) {
      transformedValue = transformToString(cat);
    }
    else {
      transformedValue = transformToString(cat);
      cat = transformedAttribute.getCategory(transformedValue);
      transformedValue = transformFromString(cat);
    }

    return transformedValue;
  }

  /**
   * Transforms string category into target type. The transformed attribute
   * is extended by the new transformed category.
   *
   * @param cat source category to transform
   * @return key of transformed category
   * @throws MiningException target data type not supported
   */
  private double transformFromString(Category cat) throws MiningException
  {
    if (cat == null)
      return Category.MISSING_VALUE;

    // Get category object:
    Object oval = cat.getValue();
    if ( ! (oval instanceof String) )  // should never happen!!
      return transformedAttribute.getKey(cat);

    // Init:
    Object catOb = null;
    if ( targetDataType == CategoricalAttribute.DOUBLE )
      catOb = new Double(0);
    else if ( targetDataType == CategoricalAttribute.FLOAT )
      catOb = new Float(0);
    else if ( targetDataType == CategoricalAttribute.INTEGER )
      catOb = new Integer(0);
    else if ( targetDataType == CategoricalAttribute.BOOLEAN )
      catOb = new Boolean(false);
    else if ( targetDataType == CategoricalAttribute.DATETIME_UNIX )
      catOb = new java.sql.Timestamp( 0 );
    else
      throw new MiningException("target data type " + targetDataType + " not supported");

    // Transform:
    try {
      String cval  = (String) oval;
      Object co = null;
      if ( targetDataType == CategoricalAttribute.DOUBLE )
        co = new Double(cval);
      else if ( targetDataType == CategoricalAttribute.FLOAT )
        co = new Float(cval);
      else if ( targetDataType == CategoricalAttribute.INTEGER )
        co = new Integer(cval);
      else if ( targetDataType == CategoricalAttribute.BOOLEAN )
        co = new Boolean(cval);
      else if ( targetDataType == CategoricalAttribute.DATETIME_UNIX )
        catOb = java.sql.Timestamp.valueOf(cval);
      catOb = co;
    }
    catch (Exception ex) {
    }
    Category catNew = new Category(catOb);

    // Recalculate key:
    double key = transformedAttribute.getKey(catNew);
    if ( Category.isMissingValue(key) )
      key = transformedAttribute.addCategory(catNew);

    return key;
  }

  /**
   * Transforms source category into string type. The transformed attribute
   * can be extended by the new transformed category.
   *
   * The string representation of the category object is used, not the
   * displayValue.
   *
   * @param cat source category to transform
   * @return key of transformed category
   * @throws MiningException target data type not supported
   */
  private double transformToString(Category cat) throws MiningException
  {
    if (cat == null)
      return Category.MISSING_VALUE;

    // Transform:
    String catOb = cat.getValue().toString();
    if (catOb == null)
      return Category.MISSING_VALUE;
    Category catNew = new Category(catOb);

    // Recalculate key:
    double key = transformedAttribute.getKey(catNew);
    if ( Category.isMissingValue(key) )
      key = transformedAttribute.addCategory(catNew);

    return key;
  }

  // -----------------------------------------------------------------------
  //  Methods of PMML handling
  // -----------------------------------------------------------------------
  /**
   * Creates PMML object DerivedField of this object. Not supported.
   *
   * @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. 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 + -