📄 hierarchicalcluster.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.Models.Clustering.Hierarchical;
import java.util.Vector;
import com.prudsys.pdm.Models.Clustering.Cluster;
import com.prudsys.pdm.Utils.IntVector;
/**
* Class representing cluster for hierarchical clustering.
*/
public class HierarchicalCluster extends Cluster
{
// -----------------------------------------------------------------------
// Variables declarations
// -----------------------------------------------------------------------
/** Child cluster 1. */
private HierarchicalCluster child1;
/** Child cluster 2. */
private HierarchicalCluster child2;
/** Is leaf cluster. */
private boolean leaf = false;
/** Distance of merging both child cluster into this cluster. */
private double mergingDistance;
/** Weight of the cluster. */
private double weight = 1.0;
/** Absolute cluster index. */
private int index = -1;
// -----------------------------------------------------------------------
// Constructors
// -----------------------------------------------------------------------
/**
* Empty constructor.
*/
public HierarchicalCluster()
{
}
/**
* Hierarchical cluster from agglomeration of two clusters.
*
* @param child1 cluster 1
* @param child2 cluster 2
* @param mergingDistance merging distance
*/
public HierarchicalCluster(HierarchicalCluster child1, HierarchicalCluster child2,
double mergingDistance) {
// Set children and distance:
this.child1 = child1;
this.child2 = child2;
this.mergingDistance = mergingDistance;
// Set contained vectors and indexes:
this.containedVectors = new Vector();
for (int i = 0; i < child1.getContainedVectors().size(); i++)
containedVectors.addElement( child1.getContainedVectors().elementAt(i) );
for (int i = 0; i < child2.getContainedVectors().size(); i++)
containedVectors.addElement( child2.getContainedVectors().elementAt(i) );
}
// -----------------------------------------------------------------------
// Getter and setter methods
// -----------------------------------------------------------------------
/**
* Returns first child.
*
* @return first child
*/
public HierarchicalCluster getChild1()
{
return child1;
}
/**
* Sets first child.
*
* @param child1 first child
*/
public void setChild1(HierarchicalCluster child1)
{
this.child1 = child1;
}
/**
* Returns second child.
*
* @return second child
*/
public HierarchicalCluster getChild2()
{
return child2;
}
/**
* Sets second child.
*
* @param child2 second child
*/
public void setChild2(HierarchicalCluster child2)
{
this.child2 = child2;
}
/**
* Is leaf cluster?
*
* @return true if leaf cluster, otherwise false
*/
public boolean isLeaf()
{
return leaf;
}
/**
* Sets leaf child.
*
* @param leaf set leaf node
*/
public void setLeaf(boolean leaf)
{
this.leaf = leaf;
}
/**
* Returns distance of merging.
*
* @return distance of merging
*/
public double getMergingDistance()
{
return mergingDistance;
}
/**
* Sets distance of merging.
*
* @param mergingDistance new distance of merging
*/
public void setMergingDistance(double mergingDistance)
{
this.mergingDistance = mergingDistance;
}
/**
* Returns weight of the cluster.
*
* @return weight of the cluster
*/
public double getWeight()
{
return weight;
}
/**
* Sets weight of the cluster.
*
* @param weight new weight of the cluster
*/
public void setWeight(double weight)
{
this.weight = weight;
}
/**
* Returns cluster index.
*
* @return cluster index
*/
public int getIndex()
{
return index;
}
/**
* Sets cluster index.
*
* @param index new cluster index
*/
public void setIndex(int index)
{
this.index = index;
}
/**
* Returns vector of indexes of all contained vectors.
*
* @return indexes of all vectors of the cluster
*/
public IntVector getAllVectorIndexes() {
IntVector childVec = new IntVector();
if (leaf)
childVec.addElement(index);
else {
IntVector vc1 = child1.getAllVectorIndexes();
childVec.addAll(vc1);
IntVector vc2 = child2.getAllVectorIndexes();
childVec.addAll(vc2);
};
return childVec;
}
// -----------------------------------------------------------------------
// Other export methods
// -----------------------------------------------------------------------
/**
* Returns string representation of hierarchical cluster.
*
* @return string representation of hierarchical cluster
*/
public String toString() {
StringBuffer text = new StringBuffer();
if (getName() != null)
text.append("name: " + getName() );
if (centerVec != null) {
text.append(" centerVec: " + centerVec.toString());
text.append(" weight = " + weight);
};
if (leaf)
text.append(" leaf cluster");
else
text.append(" aggl. cluster. child1 = " + child1.getIndex() +
" child2 = " + child2.getIndex() + " mdist = " + mergingDistance);
IntVector cv = getAllVectorIndexes();
text.append(" contVect(" + cv.size() + "):");
for (int i = 0; i < cv.size(); i++)
text.append(cv.IntegerAt(i) + " ");
return text.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -