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

📄 miningarraystream.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *    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 org.omg.cwm.analysis.transformation.ClassifierMap;
import org.omg.cwm.analysis.transformation.FeatureMap;
import org.omg.cwm.analysis.transformation.TransformationMap;
import org.omg.cwm.analysis.transformation.TransformationPackage;

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 eti.bi.util.ValueValidator;

/**
 * Extension of MiningInputStreams for arrays. Can only be
 * used for data arrays that fit completely into memory. <p>
 *
 * Two basic array types are supported: an array of double
 * values (for categorical attributes these are the keys) and
 * an array of objects (for numeric attributes the double
 * values are wrapped as Double).
 */
public class MiningArrayStream extends MiningInputStream
{
    // -----------------------------------------------------------------------
    //  Variables declarations
    // -----------------------------------------------------------------------
    /** Array of doubles that could be used for fast access. */
    protected double[][] miningArray = null;

    /** Array of mining objects for fast access. */
    protected Object[][] miningObjectArray = null;

    /** Use array of objects (miningObjectArray) instead of array of doubles (miningArray). */
    protected boolean useObjectArray = false;

    /** Length of mining array **/
    protected int miningArrayLength = -1;

    // -----------------------------------------------------------------------
    //  Constructors
    // -----------------------------------------------------------------------
    /**
     * Constructs mining array stream from array of doubles.
     * All attributes are treated as numeric and the meta data
     * is constructed automatically.
     *
     * @param miningArray array of double values
     * @exception MiningException could not create array stream
     */
    public MiningArrayStream( double[][] miningArray ) throws MiningException
    {
        this.useObjectArray = false;
        this.miningArray    = miningArray;

        // Create meta data:
        this.metaData       = new MiningDataSpecification();
        this.metaData.setRelationName("Created for MiningArrayStream at: " + new java.util.Date());
        int numbAtt         = miningArray[0].length;
        for (int i = 0; i < numbAtt; i++)
          metaData.addMiningAttribute( new NumericAttribute("numAtt"+String.valueOf(i)) );

        miningArrayLength   = getMiningArrayLength();
        cursorPosition      = -1;
    }

    /**
     * Constructs mining array stream from array of doubles whose meta data
     * is specified in metaData. In case of categorical attributes, the
     * double values are the keys.
     *
     * @param miningArray array of double values
     * @param metaData meta data of attributes
     * @exception MiningException could not create array stream
     */
    public MiningArrayStream( double[][] miningArray, MiningDataSpecification metaData ) throws MiningException
    {
        this.useObjectArray = false;
        this.miningArray    = miningArray;
        this.metaData       = metaData;

        miningArrayLength   = getMiningArrayLength();
        cursorPosition      = -1;
    }

    /**
     * Constructs mining array stream from array of objects whose meta data
     * is specified in metaData. For numeric attributes, the objects are
     * Doubles wrappers. In case of categorical attributes, the
     * objects are of the type Category.
     *
     * @param miningObjectArray array of objects
     * @param metaData meta data of attributes
     * @exception MiningException could not create array stream
     */
    public MiningArrayStream( Object[][] miningObjectArray, MiningDataSpecification metaData ) throws MiningException
    {
      this.useObjectArray    = true;
      this.miningObjectArray = miningObjectArray;
      this.metaData          = metaData;

      miningArrayLength   = getMiningArrayLength();
      cursorPosition      = -1;
    }

    /**
     * Creates a mining array from any mining input stream.
     * Requires two passes through the data: one to determine the number
     * of rows of the input stream and one to copy the data to the
     * mining array.
     *
     * @param inputStream mining input stream to read in
     * @exception IllegalArgumentException empty input stream
     * @exception MiningException could not create array stream
     */
    public MiningArrayStream( MiningInputStream inputStream ) throws IllegalArgumentException, MiningException
    {
        this.useObjectArray = false;
        this.metaData       = inputStream.getMetaData();

        // No input stream => exception:
        if( inputStream == null )
        {
            throw new IllegalArgumentException( "MiningInputStream can't be null." );
        };

        // Determine length of input stream and allocate array:
        inputStream.reset();
        miningArrayLength = 0;
        while( inputStream.next() )
        {
          miningArrayLength = miningArrayLength + 1;
        };
        miningArray = new double[ miningArrayLength] [ metaData.getAttributesNumber() ];

        // Copy data to mining array:
        int iRow = 0;
        inputStream.reset();
        while (inputStream.next()) {
          MiningVector vec = inputStream.read();
          for (int i = 0; i < metaData.getAttributesNumber(); i++)
            miningArray[iRow][i] = vec.getValue(i);
          iRow = iRow + 1;
        };

        cursorPosition      = -1;
    }

    /**
     * Returns length of mining array. If useObjectArray is true,
     * the miningObjectArray is used, else miningArray.
     *
     * @return length of mining array, -1 if array is null
     */
    private int getMiningArrayLength() {

       if (useObjectArray) {
         if (miningObjectArray == null)
           return -1;
         else
           return miningObjectArray.length;
       }
       else {
         if (miningArray == null)
           return -1;
         else
           return miningArray.length;
       }
    }

    // -----------------------------------------------------------------------
    //  Getter and setter methods
    // -----------------------------------------------------------------------
    /**
     * Returns supported stream methods.
     *
     * @return supported stream methods
     */
    public java.util.Enumeration getSupportedStreamMethods() {

      java.util.Vector suppmeth = new java.util.Vector();
      suppmeth.addElement("reset");
      suppmeth.addElement("move");

      return suppmeth.elements();
    }

    /**
     * Finds physical array model (CWM Resource Package "Object").
     * This is just the meta data wrapped in a Package.
     *
     * @exception MiningException couldn't obtain physical model
     */
     public void findPhysicalModel() throws MiningException {

       org.omg.cwm.objectmodel.core.CorePackage cp = com.prudsys.pdm.Cwm.CWMCompletePackage.getCWMCompletePackage().getCore();
       org.omg.cwm.objectmodel.core.Package dataPackage = cp.getPackage().createPackage("StoredData package", null);
       dataPackage.addOwnedElement(metaData);
       physicalModel = dataPackage;
     }

    /**
     * Returns the CWM mapping from the physical to the logical data model.
     * This is the identical transformation.
     *
     * @return transformation of physical to logical data model
     * @throws MiningException couldn't get transformation
     */
    public org.omg.cwm.analysis.transformation.TransformationMap getPhysicalToLogicalModelTransformation()
        throws MiningException {

      com.prudsys.pdm.Cwm.CWMCompletePackage cwmFactory =
          com.prudsys.pdm.Cwm.CWMCompletePackage.getCWMCompletePackage();
      TransformationPackage tpg = cwmFactory.getTransformation();

      TransformationMap tm = tpg.getTransformationMap().createTransformationMap();
      ClassifierMap cm = tpg.getClassifierMap().createClassifierMap();
      tm.addOwnedElement(cm);
      for (int i = 0; i < metaData.getAttributesNumber(); i++) {
        FeatureMap fm = tpg.getFeatureMap().createFeatureMap();
        fm.addSource( metaData.getMiningAttribute(i) );
        fm.addTarget( metaData.getMiningAttribute(i) );
        cm.addFeatureMap(fm);
      };

      metaData.addOwnedElement(tm);
      physicalModel.addOwnedElement(tm);

      return tm;
    }

    /**
     * Returns number of vectors of mining array.
     *
     * @return number of vectors
     */
    public int getVectorsNumber() {

      return miningArrayLength;
    }

    // -----------------------------------------------------------------------
    //  General stream methods
    // -----------------------------------------------------------------------
    /**
     * Opens mining array stream. This method can be left out because
     * does nothing.
     *
     * @exception MiningException if a mining source access error occurs
     */
    public void open() throws MiningException
    {
    }

    /**
     * Closes mining array stream. This method can be left out because
     * does nothing.
     *
     * @exception MiningException if a mining source access error occurs
     */
    public void close() throws MiningException
    {
    }

    /**
     * Recognizes meta data of array.
     *
     * @return meta data of array
     * @throws MiningException could not recognize meta data
     */
    public MiningDataSpecification recognize() throws MiningException
    {
        return metaData;
    }

⌨️ 快捷键说明

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