📄 cluster.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.util.StringTokenizer;
import java.util.Vector;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.MiningMatrixElement;
import com.prudsys.pdm.Core.PmmlPresentable;
import com.prudsys.pdm.Input.MiningVector;
/**
* A cluster as part of clustering. <p>
*
* From PDM CWM extension. <p>
*
* In addition, some functionality from PMML was added.
* It corresponds to the PMML element Cluster.
*
* @see com.prudsys.pdm.Adapters.PmmlVersion20.Cluster
*/
public class Cluster extends com.prudsys.pdm.Cwm.Core.Class implements PmmlPresentable, MiningMatrixElement
{
// -----------------------------------------------------------------------
// Variables declarations
// -----------------------------------------------------------------------
/** Name of cluster. Now inherited from ModelElement. */
// protected String name;
/** Center vector of cluster. */
protected MiningVector centerVec;
/** Set of center vectors of cluster. */
protected Vector centerVectors;
/** Vectors contained in the cluster. */
protected Vector containedVectors;
// -----------------------------------------------------------------------
// Constructor
// -----------------------------------------------------------------------
/**
* Empty constructor.
*/
public Cluster()
{
}
// -----------------------------------------------------------------------
// Getter and setter methods
// -----------------------------------------------------------------------
/**
* Sets the center vector of the cluster.
*
* @param centerVec new center vector of the cluster
*/
public void setCenterVec(MiningVector centerVec)
{
this.centerVec = centerVec;
}
/**
* Returns the center vector of the cluster.
*
* @return centerVec center vector of the cluster
*/
public MiningVector getCenterVec()
{
return centerVec;
}
/**
* Sets a set of center vectors (may be in the case of
* transformation of categorical attributes more vectors
* can appear).
*
* @param centerVectors new set of center vectors
*/
public void setCenterVectors(Vector centerVectors)
{
this.centerVectors = centerVectors;
}
/**
* Returns a set of center vectors (may be in the case of
* transformation of categorical attributes more vectors
* can appear).
*
* @return set of center vectors
*/
public Vector getCenterVectors()
{
return centerVectors;
}
/**
* Sets vector of vectors contained in this cluster.
*
* @param containedVectors vectors contained in this cluster
*/
public void setContainedVectors(Vector containedVectors)
{
this.containedVectors = containedVectors;
}
/**
* Returns vector of vectors contained in this cluster.
*
* @return vectors contained in this cluster
*/
public Vector getContainedVectors()
{
return containedVectors;
}
// -----------------------------------------------------------------------
// Methods of PMML handling
// -----------------------------------------------------------------------
/**
* Write clustering model to PMML element.
*
* @return PMML element of clustering model
* @exception MiningException
*/
public Object createPmmlObject() throws MiningException
{
// Create cluster:
com.prudsys.pdm.Adapters.PmmlVersion20.Cluster cluster =
new com.prudsys.pdm.Adapters.PmmlVersion20.Cluster();
// Set cluster name:
cluster.setName( getName() );
// Set center vector:
com.prudsys.pdm.Adapters.PmmlVersion20.Array arr =
new com.prudsys.pdm.Adapters.PmmlVersion20.Array();
double[] values = centerVec.getValues();
arr.setN( String.valueOf(values.length) );
arr.setType( "real" );
String arrSt = "";
for (int i = 0; i < values.length; i++) {
arrSt = arrSt + String.valueOf( values[i] );
if (i < values.length-1)
arrSt = arrSt + " ";
};
arr.setText( arrSt );
cluster.setArray( arr );
return cluster;
}
/**
* 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
{
com.prudsys.pdm.Adapters.PmmlVersion20.Cluster cluster =
(com.prudsys.pdm.Adapters.PmmlVersion20.Cluster) pmmlObject;
// Get cluster name:
setName( cluster.getName() );
// Get center vector:
com.prudsys.pdm.Adapters.PmmlVersion20.Array arr =
cluster.getArray();
String arrSt = arr.getText();
int nArr = Integer.parseInt( arr.getN() );
double[] values = new double [nArr];
StringTokenizer stg = new StringTokenizer(arrSt);
int i = 0;
while (stg.hasMoreTokens()) {
values[i] = Double.parseDouble( stg.nextToken() );
i = i + 1;
};
centerVec = new MiningVector( values );
}
// -----------------------------------------------------------------------
// Other export methods
// -----------------------------------------------------------------------
/**
* Returns string representation of cluster.
*
* @return string representation of cluster
*/
public String toString() {
StringBuffer text = new StringBuffer();
if (getName() != null)
text.append("name: " + getName());
if (centerVec != null)
text.append(" centerVec: " + centerVec.toString());
if (containedVectors != null) {
text.append(" contVect: ");
for (int i = 0; i < containedVectors.size(); i++)
text.append( containedVectors.elementAt(i) + " " );
};
return text.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -