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

📄 blockvectorstream.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.Special;

import com.prudsys.pdm.Core.CategoricalAttribute;
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.MiningFilterStream;
import com.prudsys.pdm.Input.MiningInputStream;
import com.prudsys.pdm.Input.MiningVector;
import com.prudsys.pdm.Transform.MiningStreamTransformer;

/**
 * Transforms blocks of given lengths from a mining stream to
 * assembled vectors or a new mining stream.
 *
 * Used in multivariate time-series prediction.
 *
 * @see MiningInputStream
 * @see MiningFilterStream
 */
public class BlockVectorStream implements MiningStreamTransformer
{
    // -----------------------------------------------------------------------
    //  Variables declarations
    // -----------------------------------------------------------------------
    /** Length of the moved block. */
    private int blockLength = 1;

    /** Step size for moving the block. */
    private int stepSize = 1;

    /** Direction of block movement. */
    private boolean horizontalOrVertical = true;

    // -----------------------------------------------------------------------
    //  Constructor
    // -----------------------------------------------------------------------
    /**
     * Empty constructor.
     */
    public BlockVectorStream() {

    }

    // -----------------------------------------------------------------------
    //  Getter and setter methods
    // -----------------------------------------------------------------------
    /**
     * Returns length of block
     *
     * @return length of block
     */
    public int getBlockLength()
    {
      return blockLength;
    }

    /**
     * Sets length of block.
     *
     * @param blockLength new length of block
     */
    public void setBlockLength(int blockLength)
    {
      this.blockLength = blockLength;
    }

    /**
     * Returns type of vector assembly (default: horizontal).
     *
     * @return true for horizontal vector assembly, else false
     */
    public boolean isHorizontalOrVertical()
    {
      return horizontalOrVertical;
    }

    /**
     * Sets type of vector assembly (default: horizontal).
     *
     * @param horizontalOrVertical true for horizontal vector assembly, else false
     */
    public void setHorizontalOrVertical(boolean horizontalOrVertical)
    {
      this.horizontalOrVertical = horizontalOrVertical;
    }

    /**
     * Returns size of horizontal steps.
     *
     * @return size of horizontal steps
     */
    public int getStepSize()
    {
      return stepSize;
    }

    /**
     * Sets size of horizontal steps.
     *
     * @param stepSize new size of horizontal steps
     */
    public void setStepSize(int stepSize)
    {
      this.stepSize = stepSize;
    }

    // -----------------------------------------------------------------------
    //  Transformation methods
    // -----------------------------------------------------------------------
    /**
     * Copies attribute and labels it with a given number.
     *
     * @param att attribute to be copies
     * @param tarNumber number for labeling the new attribute
     * @return new attribute
     * @throws MiningException if something went wrong
     */
    private MiningAttribute copyAttribute(MiningAttribute att, int tarNumber)
      throws MiningException {

      MiningAttribute ma = null;

      if (att instanceof NumericAttribute) {
        ma = new NumericAttribute();
        ma.setName( att.getName() + "_" + String.valueOf(tarNumber) );
      }
      else {
        ma = new CategoricalAttribute();
        ma.setName( att.getName() + "_" + String.valueOf(tarNumber) );
        ((CategoricalAttribute) ma).setValues( ((CategoricalAttribute)att).getValues() );
      };

      return ma;
    }

    /**
     * Transforms a block in a given mining input stream into
     * a mining vector.
     *
     * @param stream given mining input stream
     * @param startInd start line of mining input stream
     * @return mining vector of size nAttributes*blockLength
     * @exception MiningException can't transform block
     */
    public MiningVector transformBlockToVector(MiningInputStream stream, int startInd)
      throws MiningException {

      // Construct meta data of vector:
      MiningDataSpecification metaData = stream.getMetaData();
      int nAtt = metaData.getAttributesNumber();
      MiningDataSpecification metaDataVec = new MiningDataSpecification();
      metaDataVec.setRelationName( "Block transformed_ " + metaData.getRelationName() );
      if (horizontalOrVertical) {
        for (int i = 0; i < blockLength; i++)
          for (int j = 0; j < nAtt; j++)
            metaDataVec.addMiningAttribute(
                copyAttribute(metaData.getMiningAttribute(j), i*nAtt+j)
            );
      }
      else {
        for (int i = 0; i < nAtt; i++)
          for (int j = 0; j < blockLength; j++)
            metaDataVec.addMiningAttribute(
                 copyAttribute(metaData.getMiningAttribute(i), j*blockLength+i)
            );
      };
      int nAttVec = metaDataVec.getAttributesNumber();

      // Copy data to vector:
      double[] vec = new double[nAttVec];
      try {
        stream.move(startInd);
      }
      catch (MiningException ex) {            // Move method not supported
        stream.reset();
        for (int i = 0; i < startInd + 1; i++)
          stream.next();
      };
      for (int i = 0; i < blockLength; i++) {
        MiningVector svec = stream.read();
        if (horizontalOrVertical) {
          for (int j = 0; j < nAtt; j++)
            vec[i*nAtt+j] = svec.getValue(j);
        }
        else {
          for (int j = 0; j < nAtt; j++)
            vec[j*blockLength+i] = svec.getValue(j);
        };
        stream.next();
      };
      MiningVector vector = new MiningVector(vec);
      vector.setMetaData(metaDataVec);

      return vector;
    }

  /**
   * Copies mining input stream to another one by block-vector
   * transformations. The targetStream must contain an updatable mining stream.
   *
   * @param sourceStream mining stream used as source to copy
   * @param targetStream mining strem used as target of copy
   * @return number of vectors of transformation
   * @exception MiningException can't transform stream
   */
  public int transform( MiningInputStream sourceStream, MiningInputStream targetStream )
    throws MiningException {

    // Transform first vector for getting its metadata:
    MiningVector vector = transformBlockToVector(sourceStream, 0);

    // Copy metadata:
    targetStream.updateSetMetaData( vector.getMetaData() );

    // Copy stream:
    int nVec = sourceStream.getVectorsNumber();
    targetStream.updateRemoveAllVectors();
    for (int i = 0; i < nVec-blockLength+1; i++) {
      MiningVector mv = transformBlockToVector(sourceStream, i);
      targetStream.updateAppendVector( mv );
    };

    return nVec-blockLength+1;
  }

  /**
   * Block transformation with target attribute. Required
   * for time-series analysis. The targetStream must contain
   * an updatable mining stream.
   *
   * @param sourceStream mining stream used as source to copy
   * @param targetAtt target attribute
   * @param targetStream mining strem used as target of copy
   * @return number of vectors of transformation
   * @exception MiningException can't transform block
   */
  public int transformTarget( MiningInputStream sourceStream, MiningAttribute targetAtt, MiningInputStream targetStream )
    throws MiningException {

    // Transform first vector for getting its metadata:
    MiningVector vector = transformBlockToVector(sourceStream, 0);

    // Create metadata:
    MiningDataSpecification metaDataVec = vector.getMetaData();
    MiningDataSpecification targetMetaData = new MiningDataSpecification();
    targetMetaData.setRelationName("block_transformed_with_targetAtt_" +
                                   sourceStream.getMetaData().getRelationName());
    for (int i = 0; i < metaDataVec.getAttributesNumber(); i++)
      targetMetaData.addMiningAttribute( metaDataVec.getMiningAttribute(i) );
    targetMetaData.addMiningAttribute(
        copyAttribute(targetAtt, metaDataVec.getAttributesNumber() )
    );
    targetStream.updateSetMetaData( targetMetaData );

    // Copy stream:
    int nVec = sourceStream.getVectorsNumber();
    targetStream.updateRemoveAllVectors();
    for (int i = 0; i < nVec-blockLength; i++) {
      MiningVector mv = transformBlockToVector(sourceStream, i);
      double tarVal = sourceStream.readAttributeValue(i+blockLength, targetAtt);
      double[] tvec = new double[targetMetaData.getAttributesNumber()];
      for (int j = 0; j < metaDataVec.getAttributesNumber(); j++)
        tvec[j] = mv.getValue(j);
      tvec[metaDataVec.getAttributesNumber()] = tarVal;
      MiningVector tmv = new MiningVector(tvec);
      tmv.setMetaData(targetMetaData);
      targetStream.updateAppendVector( tmv );
    };

    return nVec-blockLength;
  }
}

⌨️ 快捷键说明

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