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

📄 attributevaluefilter.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;

/** The condition is fulfilled if an attribute as a value equal to, not equal to, 
 *  less than, ... a given value.
 *
 *  @version $Id: AttributeValueFilter.java,v 2.5 2004/08/27 11:57:31 ingomierswa Exp $
 */
public class AttributeValueFilter implements Condition {

    private static final String[] COMPARISON_TYPES={"<=", ">=", "!=", "=", "<", ">"};

    public static final int LEQ     = 0;
    public static final int GEQ     = 1;
    public static final int NEQ     = 2;
    public static final int EQUALS  = 3;
    public static final int LESS    = 4;
    public static final int GREATER = 5;

    private int comparisonType = EQUALS;
    private Attribute attribute;
    private double value;

    /** Creates a new AttributeValueFilter. If attribute is not nominal, value must be a number. */
    public AttributeValueFilter(Attribute attribute, int comparisonType, String value) {
	this.attribute      = attribute;
	this.comparisonType = comparisonType;
	setValue(value);
    }

    /** Constructs an AttributeValueFilter for a given {@link ExampleSet} from a parameter
     *  string
     *  @param parameterString Must be of the form attribute R value, where R is one out 
     *         of =, !=, &lt&, &gt;, &lt;=, and &gt;=. */
    public AttributeValueFilter(ExampleSet exampleSet, String parameterString) {
	if (parameterString == null)
	    throw new IllegalArgumentException("Parameter string must not be null!");

	int compIndex = -1;
	for (comparisonType = 0; comparisonType < COMPARISON_TYPES.length; comparisonType++) {
	    compIndex = parameterString.indexOf(COMPARISON_TYPES[comparisonType]);
	    if (compIndex != -1) break;
	}
	if (compIndex == -1) 
	    throw new IllegalArgumentException("Parameter string must have the form 'attribute {=|<|>|<=|>=|!=} value'");
	String attName  = parameterString.substring(0, compIndex).trim();
	String valueStr = parameterString.substring(compIndex+1).trim();
	if ((attName.length() == 0) || (valueStr.length() == 0))
	    throw new IllegalArgumentException("Parameter string must have the form 'attribute=value'");
	
	if (tryAttribute(exampleSet.getLabel(), attName)) this.attribute = exampleSet.getLabel();
	else if (tryAttribute(exampleSet.getPredictedLabel(), attName)) this.attribute = exampleSet.getPredictedLabel();
	else if (tryAttribute(exampleSet.getWeight(), attName)) this.attribute = exampleSet.getWeight();
	else if (tryAttribute(exampleSet.getIdAttribute(), attName)) this.attribute = exampleSet.getIdAttribute();
	else this.attribute = exampleSet.getAttribute(attName);
	if (this.attribute == null) {
	    if (attName.equals("label")) this.attribute = exampleSet.getLabel();
	    else if (attName.equals("prediction")) this.attribute = exampleSet.getPredictedLabel();
	    else if (attName.equals("weight")) this.attribute = exampleSet.getWeight();
	    else if (attName.equals("id")) this.attribute = exampleSet.getIdAttribute();
	}
	if (this.attribute == null) {
	    throw new IllegalArgumentException("Unknown attribute: '"+attName+"'");
	}
	setValue(valueStr);
    }

    private boolean tryAttribute(Attribute attribute, String name) {
	if (attribute == null) return false;
	return attribute.getName().equals(name);
    }

    private void setValue(String value) {
	if (attribute.isNominal()) {
	    if ((comparisonType != EQUALS) && (comparisonType != NEQ))
		throw new IllegalArgumentException("For nominal attributes only '=' and '!=' is allowed!");
	    this.value = attribute.mapString(value);	    
	} else {
	    try {
		this.value = Double.parseDouble(value);
	    } catch (NumberFormatException e) {
		throw new IllegalArgumentException("Value for attribute '"+attribute.getName()+
						   "' must be numerical!");
	    }
	}
    }

    public String toString() {
	return attribute.getName() + " " + COMPARISON_TYPES[comparisonType] + " "+
	    (attribute.isNominal() ? attribute.mapIndex((int)value) : ""+value);
    }

    public boolean conditionOk(Example e) {
	return e.getValue(attribute) == value;
    }

}

⌨️ 快捷键说明

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