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

📄 miningvector.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 Valentine Stepanenko (valentine.stepanenko@zsoft.ru)
  * @version 1.0
  */

package com.prudsys.pdm.Input;

import java.util.Vector;

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.MiningMatrixElement;

/**
 * Represents vector of the data matrix. To every mining vector
 * the meta data is attached. <p>
 *
 * In mathematical formulation, a mining vector mv is a vector in the
 * linear space spanned by the mining attributes ma_i, i = 1, ..., n
 * (having in mind that the term 'linear space' is not fully correct
 * due to the ambivalent nature of categorical attributes).
 * Hence the the meta data defines the basis and the values v_i are the
 * vector coordinates: mv = sum_{i=1}^n v_i * ma_i. <p>
 *
 * The values of the vector are stored as a double array. For categorical
 * attributes, the category keys are stored and the categories
 * are available through the meta data.
 */
public class MiningVector extends com.prudsys.pdm.Cwm.Instance.Object implements MiningMatrixElement, Cloneable
{
    // -----------------------------------------------------------------------
    //  Variables declarations
    // -----------------------------------------------------------------------
    /** Meta data of mining vector. */
    protected MiningDataSpecification metaData;

    /** The vector's attribute values. */
    protected double[] values;

    /** The vector's weight. */
    protected double weight;
    
    //<<Frank J. Xu, 17/03/2005
    //Here is not the final solution. If the value of a numeric attribute
    //happens to be Double.MIN_VALUE, bug appears.
    public static double INVALID_INDEX = Double.MIN_VALUE;
    //Frank J. Xu, 17/03/2005>>

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

   /**
    * Constructor that inititalizes mining vector with given
    * values. Reference to the meta data is set to null. (I.e. the vector
    * doesn't have access to information about the attribute types.)
    *
    * @param values a vector of attribute values
    */
    public MiningVector( double[] values )
    {
        this.values = values;
        this.weight = 1;
        this.metaData = null;
    }

    // -----------------------------------------------------------------------
    //  Getter and setter methods
    // -----------------------------------------------------------------------
    /**
     * Returns meta data of mining vector.
     *
     * @return meta data of mining vector
     */
    public MiningDataSpecification getMetaData()
    {
        return metaData;
    }

    /**
     * Sets meta data of mining vector.
     *
     * @param metaData new meta data of mining vector
     */
    public void setMetaData(MiningDataSpecification metaData)
    {
        this.metaData = metaData;
    }

    /**
     * Returns the weight of the vector.
     *
     * @return vector weight
     */
    public double getWeight()
    {
        return weight;
    }

    /**
     * Set weight of the vector.
     *
     * @param weight new vector weight
     */
    public void setWeight(double weight)
    {
        this.weight = weight;
    }

    /**
     * Get value array of the mining vector.
     *
     * @return value array of the vector (as copy)
     */
    public double[] getValues()
    {
      double[] v = new double[ values.length ];
      for( int i = 0; i < values.length; i++ )
      {
          v[i] = values[i];
      }
      return v;
    }

    // -----------------------------------------------------------------------
    //  Value access methods
    // -----------------------------------------------------------------------
    /**
     * Sets value at specified position.
     *
     * @param attributeIndex attribute index for new value
     * @param newValue new value
     */
    public void setValue( int attributeIndex, double newValue )
    {
        values[attributeIndex] = newValue;
    }

   /**
    * Returns vector's attribute value in internal format.
    *
    * @param attributeIndex attribute index for value to read
    * @return the specified value as a double (If the corresponding
    * attribute is categorical then it returns the value's index as a
    * double).
    */
    public double getValue( int attributeIndex )
    {
        return values[attributeIndex];
    }

    /**
     * Returns value at specified attribute.
     *
     * @param attribute mining attribute
     * @return value of mining attribute
     */
    public double getValue( MiningAttribute attribute )
    {
        int index = metaData.getAttributeIndex( attribute );
        return getValue(index);
    }

    /**
     * Returns value at specified attribute name.
     *
     * @param attributeName mining attribute name
     * @return value of mining attribute
     */
    public double getValue( String attributeName )
    {
        //<<15/03/2005, Frank J. Xu
    	//return -1 for non-exist attribute.
    	if(metaData.getMiningAttribute( attributeName ) != null)
    		return getValue( metaData.getMiningAttribute( attributeName ) );
    	else
    		return INVALID_INDEX;
    	//return getValue( metaData.getMiningAttribute( attributeName ) );
    	//15/03/2005, Frank J. Xu>>
    }

    /**
     * Returns category of value for categorical attribute.
     *
     * @param attributeIndex index of (categoryical) attribute
     * @return category of value of categorical attribute
     */
    public Category getValueCategory( int attributeIndex ) {

        MiningAttribute ma = metaData.getMiningAttribute(attributeIndex);
        if ( !(ma instanceof CategoricalAttribute) )
            //<<tyleung 12/4/2005 change the index of attribute (start with 1 instead of 0)
          throw new IllegalArgumentException("attribute " + Integer.toString(attributeIndex+1) + " must be categorical");
        	//tyleung 12/4/2005>>
        return ((CategoricalAttribute) ma).getCategory( getValue(attributeIndex) );
    }

    /**
     * Returns category of value of specified categorical attribute.
     *
     * @param attribute categoryical attribute
     * @return category of value of categorical attribute
     */
    public Category getValueCategory( CategoricalAttribute catAttribute ) {

        return catAttribute.getCategory( getValue(catAttribute) );
    }

    /**
     * Returns category of value of specified categorical attribute name.
     *
     * @param attribute categorical attribute name
     * @return category of value of categorical attribute
     */
    public Category getValueCategory( String attributeName ) {

      MiningAttribute ma = metaData.getMiningAttribute(attributeName);
      if ( !(ma instanceof CategoricalAttribute) )
        throw new IllegalArgumentException("attribute " + attributeName + " must be categorical");

      return getValueCategory( (CategoricalAttribute) ma );
    }

    /**
     * Does the mining vector contain a missing value
     * at specified position?
     *
     * @param index given attribute index
     * @return true if missing value, else false
     */
    public boolean isMissing( int index )
    {
        boolean missing = false;
        if ( Category.isMissingValue( getValue(index) ) )
        {
            missing = true;
        }
        return missing;
    }

    // -----------------------------------------------------------------------
    //  Export methods
    // -----------------------------------------------------------------------
    /**
     * Returns mining vector as vector of strings. For categorical attributes
     * the category objects are contained, for numeric attributes
     * the double value wrapped as Double.
     *
     * @return mining vector as Java vector
     */
    public Vector toVector()
    {
        Vector vector = new Vector();
        for (int i = 0; i < values.length; i++) {
            MiningAttribute attr = metaData.getMiningAttribute(i);
            if(attr instanceof CategoricalAttribute)
            {
                vector.add( ((CategoricalAttribute)attr).getCategory( values[i] ) );
            }
            else
            {
                vector.add( new Double(values[i]) );
            }
        }
        return vector;
    }

    /**
     * Returns mining vector as HTML string.
     *
     * @return mining vector as HTML string
     */
    public String toHtmlString()
    {
        String description = "";
        for (int i = 0; i < values.length; i++) {
            MiningAttribute attr = metaData.getMiningAttribute(i);
            if(attr instanceof CategoricalAttribute)
            {
                description = description +
                "<a href=http://this?miningVector>" + attr.getName() + "&nbsp;=&nbsp;" +  "<font color=blue><b>" + ((CategoricalAttribute)attr).getCategory( values[i] ) + "</b></font></a><br>";
            }
            else
            {
                description = description +
                "<a href=http://this?miningVector>" + attr.getName() + "&nbsp;=&nbsp;" +  "<font color=blue><b>" + Double.toString(values[i]) + "</b></font></a><br>";
            }
        }
        return description;
    }

    // -----------------------------------------------------------------------
    //  java.lang.Object methods
    // -----------------------------------------------------------------------
    /**
     * Copies mining vector inclding its meta data. Shallow copy.
     *
     * @return copy of mining vector
     */
    public Object clone() {

      MiningVector minVec = new MiningVector(values);
      minVec.setWeight( this.getWeight() );
      minVec.setMetaData( this.getMetaData() );

      return minVec;
    }

    //<<Frank Xu, 01/12/2004
    //	Add single quotation mark to the attribute and the data, 
    //	when converting data from SQL data source into ARFF format.
    /**
    * Returns the dataset as a string in ARFF format. Strings
    * are quoted if they contain whitespace characters, or if they
    * are a question mark.
    *
    * @return the dataset in ARFF format as a string
    */
    public String toString()
    {
        String description = "";
        for (int i = 0; i < values.length; i++) {
            if ( Category.isMissingValue(values[i]) ) {
              description = description + "?" + ",";
              continue;
            };
            if (metaData == null) {
              description = description + Double.toString(values[i]) + ",";
              continue;
            };

            MiningAttribute attr = metaData.getMiningAttribute(i);
            if(attr instanceof CategoricalAttribute)
            {
              Category category = ((CategoricalAttribute)attr).getCategory( values[i] );
              description = description + ((category != null)?"\""+category.toString()+"\"":"?") + ",";
            }
            else
            {
              description = description + Double.toString(values[i])+",";
            }
        };
        if (description.length() > 0)
          description = description.substring( 0, description.lastIndexOf( "," ) );

        return description;
    }
    //Frank Xu, 01/12/2004>>
}

⌨️ 快捷键说明

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