⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 attributeweights.java

📁 一个很好的LIBSVM的JAVA源码。对于要研究和改进SVM算法的学者。可以参考。来自数据挖掘工具YALE工具包。
💻 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.math.Averagable;
import edu.udo.cs.yale.tools.math.AverageVector;
import edu.udo.cs.yale.operator.ResultObjectAdapter;
import edu.udo.cs.yale.operator.Saveable;

import java.io.*;
import java.util.*;

/** AttributeWeights holds the information about the weights of attributes of an example set. It is delivered by
 *  several feature weighting algorithms or learning schemes. The use of a linked hash map ensures that the added 
 *  features are stored in the same sequence they were added.
 *
 *  @version $Id: AttributeWeights.java,v 2.9 2004/08/30 15:08:28 ingomierswa Exp $
 */
public class AttributeWeights extends AverageVector implements Saveable {
    
    public static final int ASCENDING  =  1;
    public static final int DESCENDING = -1;
    
    private class WeightComparator implements Comparator {
	private int direction;
	private AttributeWeights weights;

	public WeightComparator(AttributeWeights weights, int direction) {
	    this.weights   = weights;
	    this.direction = direction;
	}

	public int compare(Object o1, Object o2) {
	    int result = 0;
	    if ((o1 instanceof String) && (o2 instanceof String))
		result = ((AttributeWeight)weights.weightMap.get((String)o1)).compareTo((AttributeWeight)weights.weightMap.get((String)o2)) * 
		    (-1) * direction;
	    return result;
	}
    }

    private class AttributeWeight extends Averagable { //implements Comparable {
	private String name;
	private double weight;
	public AttributeWeight(String name, double weight) {
	    this.name   = name;
	    this.weight = weight;
	}
	public String getName() { return name; }
	public double getWeight() { return weight; }
	public void setWeight(double weight) {
	    this.weight = weight;
	}
	public double getVariance() { return getMakroVariance(); }
	public double getValue() { return getWeight(); }
	public int compareTo(Object o) {
	    if (o instanceof AttributeWeight) return Double.compare(this.weight, ((AttributeWeight)o).weight);
	    else return -1;
	}
	public boolean equals(Object o) {
	    if (o instanceof AttributeWeight) return false;
	    else {
		AttributeWeight w = (AttributeWeight)o;
		return this.name.equals(w.name) && (this.weight == w.weight);
	    }
	}
	public void cloneAveragable(Averagable avg) {
	    AttributeWeight weight = (AttributeWeight)avg;
	    this.name   = weight.name;
	    this.weight = weight.weight; 
	}
    }
 
    private Map weightMap = new LinkedHashMap();

    public AttributeWeights() {}

    private AttributeWeights(AttributeWeights weights) {
	Iterator i = weights.getAttributeNames().iterator();
	while (i.hasNext()) {
	    String name = (String)i.next();
	    this.setWeight(name, weights.getWeight(name));
	}
    }

    public String getName() { 
	return "AttributeWeights";
    }

    public void setWeight(String name, double weight) {
	AttributeWeight oldWeight = (AttributeWeight)weightMap.get(name);
	if (Double.isNaN(weight)) {
	    weightMap.remove(name);
	    super.removeAveragable(oldWeight);
	}
	if (oldWeight == null) {
	    AttributeWeight attWeight = new AttributeWeight(name, weight);
	    super.addAveragable(attWeight);
	    weightMap.put(name, attWeight);
	} else {
	    oldWeight.setWeight(weight);
	}
    }

    /** Returns the weight for the attribute with the given name. Returns Double.NaN if the weight for the 
     *  queried attribute is not known. */
    public double getWeight(String name) {
	AttributeWeight weight = (AttributeWeight)weightMap.get(name);
	if (weight == null) return Double.NaN;
	else return weight.getWeight();
    }

    /** Returns the number of features in this map. */
    public int size() {
	return weightMap.size();
    }

    /** Returns an set of attribute names in this map ordered by their insertion time. */
    public Set getAttributeNames() {
	return weightMap.keySet();
    }


    public int compareTo(Object o) {
	return 0;
    }

    /** Sorts the given array of attribute names according to their weight and the sorting direction
     *  (ascending or descending). */
    public void sortByWeight(String[] attributeNames, int direction) {
	Arrays.sort(attributeNames, new WeightComparator(this, direction));
    }

    public void save(File file) throws IOException {	
	PrintWriter out = new PrintWriter(new FileWriter(file));
	Iterator i = weightMap.keySet().iterator();
	while (i.hasNext()) {
	    String key    = (String)i.next();
	    double weight = ((AttributeWeight)weightMap.get(key)).getWeight();
	    out.println(key + ": \t" + weight);
	}
	out.close();
    }

    public String toString() {
	StringBuffer result = new StringBuffer("");
	Iterator i = weightMap.keySet().iterator();
	while (i.hasNext()) {
	    String key = (String)i.next();
	    result.append(key + ": " + ((AttributeWeight)weightMap.get(key)).getWeight() + "\n");
	}
	return result.toString();
    }

    public static AttributeWeights load(File file) throws IOException {
	AttributeWeights result = new AttributeWeights();
	BufferedReader in = new BufferedReader(new FileReader(file));
	String line = null;
	while ((line = in.readLine()) != null) {
	    String name = line.substring(0, line.indexOf(":")).trim();
	    String weightString = line.substring(line.indexOf(":") + 1).trim();
	    double weight = 1.0d;
	    try {
		weight = Double.parseDouble(weightString);
	    } catch (NumberFormatException e) {
		throw new IOException("Cannot parse weight from weight file " + file + ": " + e.getMessage());
	    }
	    result.setWeight(name, weight);
	}
	in.close();
	return result;
    }

    /** Returns a deep clone of the attribute weights which provides the same sequence of attribute names. */
    public Object clone() {
	return new AttributeWeights(this);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -