📄 attributeweightedexampleset.java
字号:
/*
* YALE - Yet Another Learning Environment
* Copyright (C) 2001-2004
* Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,
* Katharina Morik, Oliver Ritthoff
* Artificial Intelligence Unit
* Computer Science Department
* University of Dortmund
* 44221 Dortmund, Germany
* email: yale-team@lists.sourceforge.net
* web: http://yale.cs.uni-dortmund.de/
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package edu.udo.cs.yale.example;
import edu.udo.cs.yale.tools.Ontology;
import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;
/** An implementation of ExampleSet that allows the weighting of the attributes. Weights can be queried by
* the method {@link #getWeight(int)}. */
public class AttributeWeightedExampleSet extends ExampleSetAdapter {
private static class Weight {
private double weight = 1.0d;
private Weight(double weight) {
this.weight = weight;
}
}
/** Flags indicating the weight of each attribute. */
private Map weightMap = new HashMap();
/** This weight applier is used for the examples created by a example reader. */
private WeightApplier weightApplier = new ScalingWeightApplier();
/** Clone constructor. */
public AttributeWeightedExampleSet(AttributeWeightedExampleSet exampleSet) {
super((ExampleSetAdapter)exampleSet);
this.weightApplier = exampleSet.weightApplier; // TODO: deep
Iterator i = exampleSet.weightMap.keySet().iterator();
while (i.hasNext()) {
String name = (String)i.next();
Weight weight = (Weight)exampleSet.weightMap.get(name);
this.weightMap.put(name, new Weight(weight.weight));
}
}
/** Constructs a new AttributeWeightedExampleSet. Initially all attributes are weighted with 1.0. */
public AttributeWeightedExampleSet(ExampleSet exampleSet) {
this(exampleSet, null);
}
/** Constructs a new AttributeWeightedExampleSet. The attributes are weighted with the given weights.
* Attributes which are not stored in the map are weighted with 1.0. */
public AttributeWeightedExampleSet(ExampleSet exampleSet, AttributeWeights weights) {
super(exampleSet);
if (weights == null) {
for (int i = 0; i < getNumberOfAttributes(); i++) {
weightMap.put(getAttribute(i).getName(), new Weight(1.0d));
}
} else {
for (int i = 0; i < getNumberOfAttributes(); i++) {
double weight = weights.getWeight(getAttribute(i).getName());
weightMap.put(getAttribute(i).getName(), new Weight(Double.isNaN(weight) ? 1.0d : weight));
}
}
}
/** Sets the weight applier for this example set. */
public void setWeightApplier(WeightApplier weightApplier) {
this.weightApplier = weightApplier;
}
/** Returns the weight of the attribute. */
public double getWeight(Attribute attribute) {
if (!contains(attribute)) {
throw new RuntimeException("Ask weight for att not in this set: " + attribute.getName());
}
Weight weight = (Weight)weightMap.get(attribute.getName());
if (weight == null) throw new RuntimeException("Attribute " + attribute.getName() +
" is not a regular attribute in this example set!");
else return weight.weight;
}
/** Sets the weight of the attribute. If the weight is 0, the attribute is completely removed. */
public void setWeight(Attribute attribute, double weightValue) {
Weight weight = (Weight)weightMap.get(attribute.getName());
weight.weight = weightValue;
clearUserData();
}
/** Returns the number of selected attributes. */
public int getNumberOfUsedAttributes() {
int counter = 0;
for (int i = 0; i < getNumberOfAttributes(); i++) {
Attribute attribute = getAttribute(i);
if ((getWeight(i) != 0.0d) && (attribute.isNominal() || (attribute.getMinimum() != getAttribute(i).getMaximum())))
counter++;
}
return counter;
}
// -------------------- wrapper methods --------------------
/** Returns the weight of the attribute with the given index. */
public double getWeight(int i) {
return getWeight(getAttribute(i));
}
/** Sets the weight of the attribute with the given index. */
public void setWeight(int index, double weight) {
setWeight(getAttribute(index), weight);
}
/** Sets the weights of all attributes to 1.0. */
public void selectAll() {
setAll(1.0d);
}
/** Sets the weights of all attributes to 0.0. */
public void deselectAll() {
setAll(0.0d);
}
/** Sets all flags to the given value. */
private void setAll(double weight) {
for (int i = 0; i < getNumberOfAttributes(); i++) {
setWeight(getAttribute(i), weight);
}
}
/** Sets the weight of the attribute with the given index. Wrapper method to
* {@link #setWeight(Attribute attribute, double weight)}. */
public int setWeightForBlock(int index, double weight) {
return setWeightForBlock(getAttribute(index), weight);
}
/** If a block starts with this attribute the weight of all attributes
* in the whole block will be set to the given weight.
* Returns the index of the attribute which is the last one in the block or the index of the given
* attribute itself if it is not the start attribute of a block. */
public int setWeightForBlock(Attribute attribute, double weight) {
if (attribute.getBlockNr() != Attribute.UNDEFINED_BLOCK_NR) {
if (!attribute.isSeries()) {
throw new RuntimeException("Attribute " + attribute.getName() +
" is a single value and should not have a blocknumber!");
}
if (attribute.isBlockStart()) {
int startIndex = attribute.getIndex();
int endIndex = getExampleTable().getBlockEndIndex(startIndex);
for (int i = startIndex; i <= endIndex; i++) {
Attribute blockAtt = getExampleTable().getAttribute(i);
if (!blockAtt.isSeries()) {
throw new RuntimeException("Attribute " + blockAtt.getName() +
" is a single value and should not have a blocknumber!");
}
setWeight(blockAtt, weight);
}
clearUserData();
return endIndex;
} else {
return attribute.getIndex();
}
} else {
setWeight(attribute, weight);
clearUserData();
return attribute.getIndex();
}
}
// -------------------- selection methods --------------------
/** Returns the selection state of the attribute. */
public boolean isAttributeUsed(Attribute attribute) {
return getWeight(attribute) != 0.0d;
}
/** Sets the selection state of the attribute. */
public void setAttributeUsed(Attribute attribute, boolean selected) {
setWeight(attribute, (selected ? 1.0d : 0.0d));
clearUserData();
}
/** Flips the selection state of the attribute with the given index. (Convenience
* method for evolutionary algorithms). Wrapper method to
* {@link #flipAttributeUsed(Attribute attribute)}. */
public int flipAttributeUsed(int index) {
return flipAttributeUsed(getAttribute(index));
}
/** Flips the selection state of the attribute. (Convenience method for evolutionary algorithms).
* If a block starts with this attribute the whole block will be switched.
* Returns the index of the attribute which is the last one in the block or the index of the given
* attribute itself if it is not the start attribute of a block. */
public int flipAttributeUsed(Attribute attribute) {
return setWeightForBlock(attribute, isAttributeUsed(attribute) ? 0.0d : 1.0d);
}
// -------------------- selection wrapper methods --------------------
/** Returns the selection state of the attribute with the given index. */
public boolean isAttributeUsed(int i) {
return isAttributeUsed(getAttribute(i));
}
/** Sets the selection state of the attribute with the given index. */
public void setAttributeUsed(int index, boolean selected) {
setAttributeUsed(getAttribute(index), selected);
}
// -------------------- overridden methods --------------------
public void addAttribute(Attribute attribute) {
super.addAttribute(attribute);
weightMap.put(attribute.getName(), new Weight(1.0d));
}
public void removeAttribute(Attribute attribute) {
super.removeAttribute(attribute);
weightMap.remove(attribute.getName());
}
public boolean equals(Object o) {
if (!super.equals(o)) return false;
if (!(o instanceof AttributeWeightedExampleSet)) return false;
AttributeWeightedExampleSet es = (AttributeWeightedExampleSet)o;
for (int i = 0; i < getNumberOfAttributes(); i++) {
if (es.getWeight(i) != this.getWeight(i))
return false;
}
return true;
}
public String toString() {
StringBuffer buffer = new StringBuffer(super.toString());
buffer.append("\nWeights: ");
for (int i = 0; i < getNumberOfAttributes(); i++) {
if (i != 0) buffer.append(", ");
if (i > 50) { buffer.append("..."); break; }
buffer.append(getAttribute(i).getName() + ":" + getWeight(i));
}
return buffer.toString();
}
// ---------------------------------------------------------
/** Creates a new ExampleSet without attributes with weight 0. A
* {@link WeightingExampleReader} is returned which creates examples
* which can handle the attribute weights. */
public ExampleReader getExampleReader() {
ExampleSet selectionSet = createExampleSetFromSelection();
ExampleReader reader = selectionSet.getExampleReader();
AttributeWeights weights = new AttributeWeights();
for (int i = 0; i < selectionSet.getNumberOfAttributes(); i++) {
Attribute attribute = selectionSet.getAttribute(i);
weights.setWeight(attribute.getName(), getWeight(attribute));
}
return new WeightingExampleReader(selectionSet.getExampleTable().getDataReader(),
selectionSet,
weights,
weightApplier);
}
/** Creates a new example set without the deselected attributes. The result again is a
* AttributeWeightedExampleSet, all not deselected attributes will use the weights of this example set.
* Please note that this method does not alter the data in the example table, it simply removes the not
* used attributes and the next {@link ExampleReader} returned by the method {@link #getExampleReader()}
* returns a weighted example reader based on the weights and the given {@link WeightApplier}.
* Please make sure that the correct {@link WeightApplier} is set before you invoke this method.
* The default is {@link ScalingWeightApplier}. */
public AttributeWeightedExampleSet createCleanExampleSet() {
AttributeWeightedExampleSet result = (AttributeWeightedExampleSet)this.clone();
result.setWeightApplier(weightApplier);
for (int i = result.getNumberOfAttributes()-1; i >= 0; i--) {
Attribute attribute = result.getAttribute(i);
if (result.getWeight(attribute) == 0.0d) {
result.removeAttribute(attribute);
} else {
if (attribute.isNumerical() && (attribute.getMinimum() == attribute.getMaximum()))
result.removeAttribute(attribute);
}
}
return result;
}
/** Creates a new simple ExampleSet without attributes with weight 0. The result is a simple example set without
* weights. */
public ExampleSet createExampleSetFromSelection() {
ExampleSet eSet = (ExampleSet)delegate.clone();
for (int i = eSet.getNumberOfAttributes()-1; i >= 0; i--) {
Attribute attribute = eSet.getAttribute(i);
if (getWeight(attribute) == 0.0d) eSet.removeAttribute(attribute);
}
return eSet;
}
/** Creates a new ExampleSet without attributes with weight 0. All numeric attributes are weighted with the
* given weight applier. Invoke this method only if you really want to change the data in the example table.
* In all other cases the use of {@link #createCleanExampleSet()} is recommended. */
public ExampleSet createExampleSetFromWeights() {
ExampleSet eSet = (ExampleSet)delegate.clone();
for (int i = eSet.getNumberOfAttributes()-1; i >= 0; i--) {
Attribute attribute = eSet.getAttribute(i);
double weight = getWeight(attribute);
if (weight == 0.0d) eSet.removeAttribute(attribute);
else {
if (attribute.isNumerical()) {
ExampleReader reader = eSet.getExampleReader();
while (reader.hasNext()) {
Example example = reader.next();
example.setValue(attribute, weightApplier.applyWeight(example.getValue(attribute), weight));
}
}
}
}
eSet.recalculateAllAttributeStatistics();
return eSet;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -