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

📄 clusteringminingmodel.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.Models.Clustering;

import java.io.Reader;
import java.io.Writer;

import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.MiningMatrixElement;
import com.prudsys.pdm.Core.MiningModel;
import com.prudsys.pdm.Input.MiningInputStream;
import com.prudsys.pdm.Input.MiningStoredData;
import com.prudsys.pdm.Input.MiningVector;

/**
  * Description of data produced by a clustering mining function. <p>
  *
  * From PDM CWM extension. <p>
  *
  * Superclasses:
  * <ul>
  *   <li> MiningModel
  * </ul>
  */
public class ClusteringMiningModel extends MiningModel
{
    // -----------------------------------------------------------------------
    //  Constants defining compression types
    // -----------------------------------------------------------------------
    /** No compression. */
    public static final int COMPRESSION_TYPE_NONE = 0;

    /** Clusters with more than one elements remain. */
    public static final int COMPRESSION_TYPE_MULTI_CLUSTERS = 1;

    /** Clusters are replaced by representant, one-element clusters ignored. */
    public static final int COMPRESSION_TYPE_REPRESENTANT_WITHOUT_BACKGROUND = 2;

    /** Clusters are replaced by representant. */
    public static final int COMPRESSION_TYPE_REPRESENTANT_WITH_BACKGROUND = 3;

    /** Cluster representant is center vector. */
    public static final int REPRESENTANT_TYPE_CENTER = 0;

    /** Cluster representant is arbitrary vector from cluster. */
    public static final int REPRESENTANT_TYPE_RANDOM = 1;

    // -----------------------------------------------------------------------
    //  Variables declarations
    // -----------------------------------------------------------------------
    /** Distance between vectors to be clustered. */
    protected Distance distance = new Distance();

    /** Array of clusters. */
    protected Cluster clusters[];

    /** Compression type for createClusterStream method. */
    protected int compressType = COMPRESSION_TYPE_REPRESENTANT_WITHOUT_BACKGROUND;

    /** Representation vector for createClusterStream model. */
    protected int representType = REPRESENTANT_TYPE_CENTER;

    // -----------------------------------------------------------------------
    //  Constructor
    // -----------------------------------------------------------------------
    /**
     * Constructor sets function and algorithm parameters.
     */
    public ClusteringMiningModel()
    {
        function  = CLUSTERING_FUNCTION;
        algorithm = CENTER_BASED_CLUSTERING_ALGORITHM;
    }

    // -----------------------------------------------------------------------
    //  Getter and setter methods
    // -----------------------------------------------------------------------
    /**
     * Set distances definition. Distance between vectors is also
     * refered to as comparison measure (PMML) or aggregation function (JDM).
     *
     * @param distance new distances
     */
    public void setDistance(Distance distance)
    {
      this.distance = distance;
    }

   /**
    * Returns distances definition. Distance between vectors is also
    * refered to as comparison measure (PMML) or aggregation function (JDM).
    *
    * @return distances
    */
    public Distance getDistance()
    {
      return distance;
    }

    /**
     * Get cluster objects.
     *
     * @return cluster objects
     */
    public Cluster[] getClusters()
    {
        return clusters;
    }

    /**
     * Set cluster objects.
     *
     * @param clusters cluster objects
     */
    public void setClusters(Cluster[] clusters)
    {
        this.clusters = clusters;
    }

    /**
     * Returns number of clusters.
     *
     * @return number of clusters
     * @throws MiningException could not get cluster number
     */
    public int getNumberOfClusters() throws MiningException {

      int nClust = 0;
      if (clusters != null)
        nClust = clusters.length;

      return nClust;
    }

    /**
     * Returns compression type for method createClusterStream.
     *
     * @return compression type for method createClusterStream
     */
    public int getCompressType() {

      return compressType;
    }

    /**
     * Sets compression type for method createClusterStream.
     *
     * @param compressType compression type for method createClusterStream
     */
    public void setCompressType(int compressType) {

      this.compressType = compressType;
    }

    /**
     * Returns cluster center representation type for method createClusterStream.
     *
     * @return cluster center representation type for method createClusterStream
     */
    public int getRepresentType() {

      return representType;
    }

    /**
     * Sets cluster center representation typefor method createClusterStream.
     *
     * @param representType cluster center representation type for method createClusterStream
     */
    public void setRepresentType(int representType) {

      this.representType = representType;
    }

    // -----------------------------------------------------------------------
    //  Application of model to new data
    // -----------------------------------------------------------------------
    /**
     * Applies clustering model to mining vector returning the number
     * of the cluster it belongs to. Just calls applyModelFunction.
     *
     * @param miningVector mining vector to be assigned to a cluster
     * @return number of the cluster the vector belongs to
     * @throws MiningException caould not execute operation
     * @deprecated since version 1.1, use applyModelFunction instead
     */
    public int apply(MiningVector miningVector) throws MiningException
    {
      return (int) applyModelFunction(miningVector);
    }

    /**
     * Applies function of clustering mining model to a mining vector.
     * The meta data of the mining vector should be similar to the
     * metaData of this class. This ensures compatibility of training
     * and application data.
     * Not implemented.
     *
     * @param miningVector mining vector where the model should be applied
     * @return function value of the mining vector
     * @throws MiningException always thrown
     */
    public double applyModelFunction(MiningVector miningVector)
        throws MiningException {
      throw new MiningException("Not implemented.");
    }

    /**
     * General function of applying the clustering mining model to some data.
     * Not implemented.
     *
     * @param miningData mining data where the model should be applied
     * @return data resulting from the model application
     * @throws MiningException always thrown
     */
    public MiningMatrixElement applyModel(MiningMatrixElement miningData)
        throws MiningException {
      throw new MiningException("Not implemented.");
    }

    // -----------------------------------------------------------------------
    //  Methods of PMML handling
    // -----------------------------------------------------------------------
    /**
     * Read clustering model from PMML document. Not supported, exception is
     * always thrown.
     *
     * @param reader reader for the PMML document
     * @exception MiningException always thrown
     */
    public void readPmml( Reader reader ) throws MiningException
    {
        throw new MiningException( "Not realized yet." );
    }

    /**
     * Write clustering model to PMML document. Not supported, exception is
     * always thrown.
     *
     * @param writer writer for the PMML document
     * @exception MiningException always thrown
     */
    public void writePmml( Writer writer ) throws MiningException
    {
        throw new MiningException( "Not realized yet." );
    }

    /**
     * Write clustering model to PMML element. Not supported, exception is
     * always thrown.
     *
     * @return PMML element of clustering model
     * @exception MiningException always thrown
     */
    public Object createPmmlObject() throws MiningException
    {
        throw new MiningException( "Not realized yet." );
    }

    /**
     * Read clustering model from PMML element. Not supported, exception is
     * always thrown.
     *
     * @param pmmlObject PMML element to read in
     * @exception MiningException always thrown
     */
    public void parsePmmlObject( Object pmmlObject ) throws MiningException
    {
        throw new MiningException( "Not realized yet." );
    }

    // -----------------------------------------------------------------------
    //  Export as mining input stream, compression
    // -----------------------------------------------------------------------
    /**
     * Creates mining input stream formed by the representants
     * of the clusters. Can be used for data compression.
     *
     * Stream is controled by the variable compressType and
     * representType.
     *
     * @return stream of cluster representants
     * @exception MiningException no clusters contained in model
     */
    public MiningInputStream toMiningInputStream() throws MiningException {

      if (clusters == null)
        throw new MiningException("No clusters contained in model");

      MiningStoredData msd = new MiningStoredData();

      // Use first vector of first cluster to assign meta data to stream:
      for (int i = 0; i < clusters.length; i++) {
        boolean found = false;
        for (int j = 0; j < clusters[i].containedVectors.size(); j++) {
            MiningVector mv = (MiningVector) clusters[i].getContainedVectors().elementAt(j);
            msd.setMetaData( mv.getMetaData() );
            found = true;
            break;
        };
        if (found) break;
      }

      // Add cluster vectors to stream:
      if (compressType == COMPRESSION_TYPE_NONE) {
        for (int i = 0; i < clusters.length; i++)
          for (int j = 0; j < clusters[i].containedVectors.size(); j++)
            msd.add( clusters[i].getContainedVectors().elementAt(j) );
      }
      else if (compressType == COMPRESSION_TYPE_MULTI_CLUSTERS) {
         for (int i = 0; i < clusters.length; i++) {
           int size = clusters[i].containedVectors.size();
           if (size > 1)
             for (int j = 0; j < clusters[i].containedVectors.size(); j++)
               msd.add( clusters[i].getContainedVectors().elementAt(j) );
         };
      }
      else if (compressType == COMPRESSION_TYPE_REPRESENTANT_WITHOUT_BACKGROUND) {
         for (int i = 0; i < clusters.length; i++) {
           int size = clusters[i].containedVectors.size();
           if (size > 1) {
             if (representType == REPRESENTANT_TYPE_CENTER)
               msd.add( clusters[i].getCenterVec() );
             else if (representType == REPRESENTANT_TYPE_RANDOM)
               msd.add( clusters[i].getContainedVectors().elementAt(0) );
           }
         };
      }
      else if (compressType == COMPRESSION_TYPE_REPRESENTANT_WITH_BACKGROUND) {
         for (int i = 0; i < clusters.length; i++) {
           int size = clusters[i].containedVectors.size();
           if (representType == REPRESENTANT_TYPE_CENTER)
             msd.add( clusters[i].getCenterVec() );
           else if (representType == REPRESENTANT_TYPE_RANDOM)
             msd.add( clusters[i].getContainedVectors().elementAt(0) );
         };
      }
      else
        throw new MiningException("Unknown compression type");

      return msd;
    }

    /**
     * Creates mining input stream formed by the representants
     * of the clusters. Can be used for data compression.
     *
     * Stream is controled by the variable compressType and
     * representType.
     *
     * @return stream of cluster representants
     * @exception MiningException no clusters contained in model
     * @deprecated use toMiningInputStream instead
     */
    public MiningInputStream createClusterStream() throws MiningException {

      return toMiningInputStream();
    }

    // -----------------------------------------------------------------------
    //  Other export methods
    // -----------------------------------------------------------------------
    /**
     * Write clustering model as plain text. Not supported, exception is
     * always thrown.
     *
     * @param writer writer for plain text
     * @exception MiningException always thrown
     */
    public void writePlainText( Writer writer ) throws MiningException
    {
        throw new MiningException( "Not realized yet." );
    }

    /**
     * Returns string representation (just few words).
     *
     * @return string representation
     */
    public String toString()
    {
        return "Clustering mining model";
    }

    /**
     * Returns HTML representation of clustering model.
     *
     * @return HTML string representation of clustering model
     */
    public String toHtmlString()
    {
        return toString();
    }
}

⌨️ 快捷键说明

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