📄 horizontalpartitionminingstream.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.0
*/
package com.prudsys.pdm.Transform.Special;
import com.prudsys.pdm.Core.MiningDataSpecification;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Input.MiningInputStream;
/**
* Partitiones a given mining input stream horizontally
* into some mining input stream with same meta data
* and user-defined size.
*
* @see MiningInputStream
*/
public class HorizontalPartitionMiningStream extends com.prudsys.pdm.Cwm.Core.Class
{
// -----------------------------------------------------------------------
// Variables declarations
// -----------------------------------------------------------------------
/** Number of vectors per partition. */
private int[] partVectNumb = null;
// -----------------------------------------------------------------------
// Constructor
// -----------------------------------------------------------------------
/**
* Empty constructor.
*/
public HorizontalPartitionMiningStream() {
}
// -----------------------------------------------------------------------
// Getter and setter methods
// -----------------------------------------------------------------------
/**
* Returns number of vectors per partition (in same order like
* new streams in transform method).
*
* @return number of vectors per partition, null if equal partitions
*/
public int[] getPartVectNumb()
{
return partVectNumb;
}
/**
* Sets number of vectors per partition (in same order like
* new streams in transform method).
*
* @param partVectNumb array of vector numbers, null for equal partitions
*/
public void setPartVectNumb(int[] partVectNumb)
{
this.partVectNumb = partVectNumb;
}
// -----------------------------------------------------------------------
// Transformation methods
// -----------------------------------------------------------------------
/**
* Partitions mining input and copies partitions to target streams.
* The targetStreams must be updatable mining streams.
*
* @param sourceStream mining stream used as source to partition
* @param targetStreams mining streams used as targets of copy
* @return number of partitions
* @exception MiningException could not partition stream
*/
public int transform( MiningInputStream sourceStream, MiningInputStream[] targetStreams )
throws MiningException {
int nStreams = targetStreams.length;
int nVec = sourceStream.getVectorsNumber();
// If partition sizes are set, check whether they match with streams:
if (partVectNumb != null) {
if (partVectNumb.length != targetStreams.length)
throw new MiningException("# " + partVectNumb.length +
" partition sizes given, but " +
"# " + targetStreams.length + " partition streams");
int vectNumb = 0;
for (int i = 0; i < partVectNumb.length; i++)
vectNumb = vectNumb + partVectNumb[i];
if (vectNumb != nVec)
throw new MiningException("# " + vectNumb +
" total vector number given, but " +
"# " + nVec + " in source stream");
};
// Copy metadata:
MiningDataSpecification metaDataSource = sourceStream.getMetaData();
for (int i = 0; i < nStreams; i++)
targetStreams[i].updateSetMetaData( metaDataSource );
// Partition stream:
int nPartVec = nVec / nStreams;
sourceStream.reset();
for (int i = 0; i < nStreams; i++) {
targetStreams[i].updateRemoveAllVectors();
// Vector numbers of next partition:
int nPart = nPartVec;
if (partVectNumb == null) {
if (i == nStreams - 1) nPart = nVec - (nStreams-1)*nPartVec;
}
else
nPart = partVectNumb[i];
// Add vectors to new stream:
for (int j = 0; j < nPart; j++) {
sourceStream.next();
targetStreams[i].updateAppendVector( sourceStream.read() );
};
};
return nStreams;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -