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

📄 noiseoperator.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.operator.preprocessing;

import edu.udo.cs.yale.operator.Operator;
import edu.udo.cs.yale.operator.IOObject;
import edu.udo.cs.yale.operator.OperatorException;
import edu.udo.cs.yale.operator.parameter.*;
import edu.udo.cs.yale.tools.LogService;
import edu.udo.cs.yale.tools.Ontology;
import edu.udo.cs.yale.tools.RandomGenerator;
import edu.udo.cs.yale.example.Example;
import edu.udo.cs.yale.example.ExampleReader;
import edu.udo.cs.yale.example.ExampleSet;
import edu.udo.cs.yale.example.Attribute;
import edu.udo.cs.yale.generator.*;

import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;
import java.util.Map;
import java.util.HashMap;

/** This operator adds random attributes and white noise to the data.
 *
 *  @version $Id: NoiseOperator.java,v 1.4 2004/09/09 12:00:53 ingomierswa Exp $
 */
public class NoiseOperator extends Operator {

    private static final Class[] INPUT_CLASSES  = { ExampleSet.class };
    private static final Class[] OUTPUT_CLASSES = { ExampleSet.class };

    public Class[] getInputClasses() { return INPUT_CLASSES; }
    public Class[] getOutputClasses() { return OUTPUT_CLASSES; }


    public IOObject[] apply() throws OperatorException {
	ExampleSet exampleSet = (ExampleSet)getInput(ExampleSet.class);
	
	// read noise values from list
	Map noiseMap = new HashMap();
	List noises = getParameterList("noise");
	Iterator i = noises.iterator();
	while (i.hasNext()) {
	    Object[] pair = (Object[])i.next();
	    noiseMap.put((String)pair[0], (Double)pair[1]);
	}

	// add noise to existing attributes
	double defaultNoise = getParameterAsDouble("default_noise");
	ExampleReader reader = exampleSet.getExampleReader();	
	while (reader.hasNext()) {
	    Example example = reader.next();
	    for (int j = 0; j < exampleSet.getNumberOfAttributes(); j++) {
		Attribute attribute = exampleSet.getAttribute(j);
		Double noiseObject = (Double)noiseMap.get(attribute.getName());
		double noise = noiseObject == null ? defaultNoise : noiseObject.doubleValue();
		double noiseValue = 
		    RandomGenerator.getGlobalRandomGenerator().nextGaussian() * noise * 
		    Math.abs(attribute.getMaximum() - attribute.getMinimum());
		example.setValue(attribute, example.getValue(attribute) + noiseValue);
	    }
	}

	// add new noise attributes
	int numberOfNewAttributes = getParameterAsInt("random_attributes");
	List newAttributes = new LinkedList();
	for (int j = 0; j < numberOfNewAttributes; j++) {
	    newAttributes.add(new Attribute(Attribute.createName("random"), 
					    Ontology.REAL, Ontology.SINGLE_VALUE, 
					    Attribute.UNDEFINED_BLOCK_NR, null));
	}
	exampleSet.getExampleTable().addAttributes(newAttributes);
	exampleSet.addAllAttributes(newAttributes);
	reader = exampleSet.getExampleReader();
	while (reader.hasNext()) {
	    Example example = reader.next();
	    i = newAttributes.iterator();
	    while (i.hasNext()) {
		example.setValue((Attribute)i.next(), RandomGenerator.getGlobalRandomGenerator().nextDouble());
	    }
	}

	exampleSet.recalculateAllAttributeStatistics();
	return new IOObject[] { exampleSet };
    }


    public List getParameterTypes() {
	List types = super.getParameterTypes();
	ParameterType type = new ParameterTypeInt("random_attributes", "Adds this number of random attributes.", 
						  0, Integer.MAX_VALUE, 0);
	type.setExpert(false);
	types.add(type);
	type = new ParameterTypeDouble("default_noise", "The standard deviation of the default noise.", 
				       0.0d, Double.POSITIVE_INFINITY, 0.1d);
	type.setExpert(false);
	types.add(type);
	types.add(new ParameterTypeList("noise", "List of noises for each attributes.", 
					new ParameterTypeDouble("noise", "Names of attributes and noises to use.", 0.0d, 
								Double.POSITIVE_INFINITY, 0.1d)));
	return types;
    }

}

⌨️ 快捷键说明

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