📄 nontransactionaltotransactional.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.Category;
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.Core.OrdinalAttribute;
import com.prudsys.pdm.Input.MiningInputStream;
import com.prudsys.pdm.Input.MiningSparseVector;
import com.prudsys.pdm.Input.MiningVector;
import com.prudsys.pdm.Transform.MiningStreamTransformer;
/**
* Transforms a mining input stream from non-transactional into transactional
* format.
*/
public class NontransactionalToTransactional implements MiningStreamTransformer
{
// -----------------------------------------------------------------------
// Variables declarations
// -----------------------------------------------------------------------
/** Categories (transaction IDs) of vectornr ID not stored . */
private boolean unstoredVectorNrId = true;
/** Categories of attribute value not stored. */
private boolean unstoredAttributeValue = false;
// -----------------------------------------------------------------------
// Constructor
// -----------------------------------------------------------------------
/**
* Empty constructor.
*/
public NontransactionalToTransactional()
{
}
// -----------------------------------------------------------------------
// Getter and setter methods
// -----------------------------------------------------------------------
/**
* Categories of vectornr ID are not stored (default: true).
*
* @return true if categories not stored, otherwise false
*/
public boolean isUnstoredVectorNrId()
{
return unstoredVectorNrId;
}
/**
* Set categories of vectornr ID unstored.
*
* @param unstoredVectorNrId new store status
*/
public void setUnstoredVectorNrId(boolean unstoredVectorNrId)
{
this.unstoredVectorNrId = unstoredVectorNrId;
}
/**
* Categories of attribute value are not stored (default: false).
*
* @return true if categories not stored, otherwise false
*/
public boolean isUnstoredAttributeValue()
{
return unstoredAttributeValue;
}
/**
* Set categories of attribute value unstored.
*
* @param unstoredAttributeValue new store status
*/
public void setUnstoredAttributeValue(boolean unstoredAttributeValue)
{
this.unstoredAttributeValue = unstoredAttributeValue;
}
// -----------------------------------------------------------------------
// Transformation methods
// -----------------------------------------------------------------------
/**
* Transforms non-transactional into transactional mining input stream.
* The targetStream must contain an updatable mining stream.
*
* @param sourceStream mining stream used as source of transformation
* @param targetStream mining stream used as target of transformation
* @return number of vectors of transformation
* @exception MiningException can't run transformation
*/
public int transform( MiningInputStream sourceStream, MiningInputStream targetStream )
throws MiningException {
// Transform metadata:
MiningDataSpecification metaDataSource = sourceStream.getMetaData();
MiningDataSpecification targetMetaData = new MiningDataSpecification();
targetMetaData.setRelationName( "Transactional: " + metaDataSource.getRelationName() );
// OrdinalAttribute vectorNrId = new OrdinalAttribute("vectorNrId");
// NumericAttribute attributeIndex = new NumericAttribute("attributeIndex");
// CategoricalAttribute attributeValue = new CategoricalAttribute("attributeValue");
//<<tyleung 11/3/2005
OrdinalAttribute vectorNrId = new OrdinalAttribute("Transaction id");
CategoricalAttribute attributeIndex = new CategoricalAttribute("Item id");
CategoricalAttribute attributeValue = new CategoricalAttribute("Attribute value");
// tyleung 11/3/2005>>
vectorNrId.setUnstoredCategories(unstoredVectorNrId);
attributeValue.setUnstoredCategories(unstoredAttributeValue);
targetMetaData.addMiningAttribute(vectorNrId);
targetMetaData.addMiningAttribute(attributeIndex);
targetMetaData.addMiningAttribute(attributeValue);
targetStream.updateSetMetaData(targetMetaData);
// First scan to fill metadata if required:
if (!unstoredVectorNrId || !unstoredAttributeValue) {
int nVec = 0;
//<<tyleung 17/3/2005
int nAttr = 0;
//tyleung 17/3/2005>>
sourceStream.reset();
while (sourceStream.next()) {
// Transform nVec in category of vectorNrId attribute:
if (!unstoredVectorNrId) {
Category dCat = new Category( String.valueOf(nVec) );
double dVec = vectorNrId.getKey(dCat);
if ( Category.isMissingValue(dVec) )
dVec = vectorNrId.addCategory( dCat );
};
// Read current vector and transform anyway into sparse form:
if (!unstoredAttributeValue) {
MiningSparseVector msv = new MiningSparseVector( sourceStream.read() );
msv.setMetaData(metaDataSource);
//<<tyleung 17/3/2005
nAttr = 0;
//tyleung 17/3/2005>>
for (int i = 0; i < msv.getValues().length; i++ ) {
// Get current attribute value, if numeric transform into category:
MiningAttribute mAtt = metaDataSource.getMiningAttribute(i);
double val = msv.getValue(i);
//<<tyleung 17/3/2005
Category dCat = new Category( String.valueOf(nAttr) );
double dAttr = attributeIndex.getKey(dCat);
if ( Category.isMissingValue(dAttr) )
dAttr = attributeIndex.addCategory( dCat );
//tyleung 17/3/2005>>
if ( !(Category.isMissingValue(val) || Category.isInvalidValue(val)) ) {
Category cat;
if (mAtt instanceof NumericAttribute)
cat = new Category(String.valueOf(val));
else
cat = ( (CategoricalAttribute) mAtt).getCategory(val);
// Search for given category, otherwise add as new value:
double dVal = attributeValue.getKey(cat);
if (Category.isMissingValue(dVal))
attributeValue.addCategory(cat);
};
//<<tyleung 17/3/2005
nAttr = nAttr + 1;
//tyleung 17/3/2005>>
};
};
nVec = nVec + 1;
};
};
// Transform stream data:
int nVec = 0;
sourceStream.reset();
targetStream.updateRemoveAllVectors();
while (sourceStream.next()) {
double dVec = nVec;
// Transform nVec in category of vectorNrId attribute:
Category dCat = new Category( String.valueOf(nVec) );
dVec = vectorNrId.getKey(dCat);
if ( Category.isMissingValue(dVec) )
dVec = vectorNrId.addCategory(dCat);
// Read current vector and transform anyway into sparse form:
MiningSparseVector msv = new MiningSparseVector( sourceStream.read() );
msv.setMetaData(metaDataSource);
for (int i = 0; i < msv.getValues().length; i++ ) {
// Get current attribute value, if unstored att value into category:
MiningAttribute mAtt = metaDataSource.getMiningAttribute(i);
double val = msv.getValue(i);
double dVal = Category.MISSING_VALUE;
// Transform in category of attributeValue attribute:
if ( !(Category.isMissingValue(val) || Category.isInvalidValue(val)) ) {
Category cat;
if (mAtt instanceof NumericAttribute)
cat = new Category(String.valueOf(val));
else
cat = ( (CategoricalAttribute) mAtt).getCategory(val);
// Search for given category, otherwise add as new value:
dVal = attributeValue.getKey(cat);
if (Category.isMissingValue(dVal))
dVal = attributeValue.addCategory(cat);
};
// Compose transactional vector and write to stream:
double[] values = new double[3];
values[0] = dVec;
values[1] = i;
values[2] = dVal;
MiningVector mv = new MiningVector(values);
mv.setMetaData(targetMetaData);
targetStream.updateAppendVector( mv );
};
nVec = nVec + 1;
};
return nVec;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -