📄 miningbinarysparsevector.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.Input;
/**
* Represents binary sparse mining vectors. These are sparse vectors
* whose non-zero elements are always one
*/
public class MiningBinarySparseVector extends MiningSparseVector
{
// -----------------------------------------------------------------------
// Constructors
// -----------------------------------------------------------------------
/**
* Empty constructor.
*/
protected MiningBinarySparseVector()
{
}
/**
* Constructor that generates a binary sparse vector from the given
* mining vector.
*
* @param vector the vector from which the attribute values
* and the weight are to be copied
*/
public MiningBinarySparseVector(MiningVector vector) {
// Init:
this.weight = vector.getWeight();
// Vector is of sparse type:
if (vector instanceof MiningSparseVector) {
this.values = null;
this.indexes = ((MiningSparseVector) vector).indexes;
}
// Vector is just of mining vector type:
else {
// Get all non-zero values of vector:
int nAtt = vector.getValues().length;
int[] tempIndexes = new int[nAtt];
int vals = 0;
for (int i = 0; i < nAtt; i++) {
if (vector.getValue(i) != 0) {
tempIndexes[vals] = i;
vals = vals + 1;
};
};
// Copy values to sparse vector:
values = null;
indexes = new int[vals];
System.arraycopy(tempIndexes, 0, indexes, 0, vals);
};
}
/**
* Creates MiningBinarySparseVector from its weight and
* the indexes.
*
* @param weight binary sparse vector's weight
* @param indexes attribute indexes of non-zero elements
*/
public MiningBinarySparseVector( double weight, int[] indexes )
{
this.values = null;
this.indexes = indexes;
this.weight = weight;
}
// -----------------------------------------------------------------------
// Getter and setter methods
// -----------------------------------------------------------------------
/**
* Get value array of the binary sparse mining vector.
*
* @return value array of the vector
*/
public double[] getValues()
{
int size = metaData.getAttributesNumber();
double[] v = new double[ size ];
for( int i = 0; i < indexes.length; i++ )
{
v[indexes[i]] = 1;
}
return v;
}
// -----------------------------------------------------------------------
// Value access methods
// -----------------------------------------------------------------------
/**
* Returns a vector's attribute value in internal format,
* always 1.
*
* @param indexOfIndex the index of the attribute's index
* @return 1
*/
public double getValueSparse(int indexOfIndex) {
return 1;
}
/**
* Returns vector's attribute value in internal format.
*
* @param attributeIndex the attribute's index
* @return the specified value as a double (If the corresponding
* attribute is categorical then it returns the value's index as a
* double).
*/
public double getValue(int attributeIndex)
{
int index = locateIndex(attributeIndex);
if ((index >= 0) && (indexes[index] == attributeIndex))
{
return 1.0;
}
else
{
return 0.0;
}
}
// -----------------------------------------------------------------------
// java.lang.Object methods
// -----------------------------------------------------------------------
/**
* Copies binary sparse mining vector inclding its meta data. Shallow copy.
*
* @return copy of mining sparse vector
*/
public Object clone() {
MiningSparseVector minBinarySparseVec = new MiningBinarySparseVector(this);
minBinarySparseVec.setMetaData( this.getMetaData() );
return minBinarySparseVec;
}
/**
* Returns string representation of binary sparse vector.
*
* @return string representation of binary sparse vector.
*/
public String toStringBinarySparse()
{
String description = "";
for (int i = 0; i < getNumValuesSparse(); i++) {
int ind = getIndex(i);
description = description + String.valueOf(ind) + ": " + ",";
}
description = description.substring( 0, description.lastIndexOf( "," ) );
return description;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -