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

📄 examplesetgenerator.java

📁 一个很好的LIBSVM的JAVA源码。对于要研究和改进SVM算法的学者。可以参考。来自数据挖掘工具YALE工具包。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *  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;

import edu.udo.cs.yale.operator.parameter.*;
import edu.udo.cs.yale.example.Attribute;
import edu.udo.cs.yale.example.ExampleSet;
import edu.udo.cs.yale.example.MemoryExampleTable;
import edu.udo.cs.yale.example.DoubleArrayDataRow;
import edu.udo.cs.yale.example.ListDataRowReader;
import edu.udo.cs.yale.tools.Ontology;
import edu.udo.cs.yale.tools.RandomGenerator;

import java.util.List;
import java.util.LinkedList;

/** Generates a random example set for testing purposes. Uses a subclass of {@link TargetFunction} to create
 *  the examples from the attribute values. Possible target functions are: random, sum (of all attributes), 
 *  polynomial (of the first three attributes, degree 3), non linear, sinus, 
 *  sinus frequency (like sinus, but with frequencies in the argument), random classification, 
 *  sum classification (like sum, but positive for positive sum and negative for negative sum), 
 *  interaction classification (positive for negative x  or positive y and negative z), sinus classification (positive for 
 *  positive sinus values).
 *
 *  @version $Id: ExampleSetGenerator.java,v 2.10 2004/09/04 20:07:03 ingomierswa Exp $
 */
public class ExampleSetGenerator extends Operator {

    // ================================================================================
    // super classes for known target functions
    // ================================================================================

    private static abstract class RegressionFunction implements TargetFunction {

	private double lower = -10.0d;
	private double upper =  10.0d;

	public void setLowerArgumentBound(double lower) { this.lower = lower; }
	public void setUpperArgumentBound(double upper) { this.upper = upper; }

	public Attribute getLabel() {
	    return new Attribute("label", Ontology.REAL, Ontology.SINGLE_VALUE, 
				 Attribute.UNDEFINED_BLOCK_NR, null);
	}

	public double[] createArguments(int dimension) {
	    double[] args = new double[dimension];
	    for (int i = 0; i < args.length; i++)
		args[i] = RandomGenerator.getGlobalRandomGenerator().nextDoubleInRange(lower, upper);
	    return args;
	}
    }

    private static abstract class ClassificationFunction implements TargetFunction {

	private double lower = -10.0d;
	private double upper =  10.0d;
	private Attribute label = new Attribute("label", Ontology.NOMINAL, Ontology.SINGLE_VALUE, 
						Attribute.UNDEFINED_BLOCK_NR, null);

	public ClassificationFunction() {
	    label.mapString("positive");
	    label.mapString("negative");
	}

	public void setLowerArgumentBound(double lower) { this.lower = lower; }
	public void setUpperArgumentBound(double upper) { this.upper = upper; }

	public Attribute getLabel() {
	    return label;
	}

	public double[] createArguments(int dimension) {
	    double[] args = new double[dimension];
	    for (int i = 0; i < args.length; i++)
		args[i] = RandomGenerator.getGlobalRandomGenerator().nextDoubleInRange(lower, upper);
	    return args;
	}
    }




    // ================================================================================
    // regression
    // ================================================================================    

    private static class RandomFunction extends RegressionFunction {
	public double calculate(double[] args) {
	    return RandomGenerator.getGlobalRandomGenerator().nextDouble();
	}
    }

    private static class SumFunction extends RegressionFunction {
	public double calculate(double[] args) {
	    double sum = 0.0d;
	    for (int i = 0; i < args.length; i++) sum += args[i];
	    return sum;
	}
    }

    private static class PolynomialFunction extends RegressionFunction {
	public double calculate(double[] att) throws FunctionException {
	    if (att.length < 3)
		throw new FunctionException("Polynomial function needs at least 3 attributes!");
	    return (att[0]*att[0]*att[0] + att[1]*att[1] + att[2]);
	}
    }

    private static class NonLinearFunction extends RegressionFunction {
	public double calculate(double[] att) throws FunctionException {
	    if (att.length < 3)
		throw new FunctionException("Non linear function needs at least 3 attributes!");
	    return (att[0]*att[1]*att[2] + att[0]*att[1] + att[1]*att[1]);
	}
    }

    private static class SinusFunction extends RegressionFunction {
	public double calculate(double[] att) throws FunctionException {
	    if (att.length < 2)
		throw new FunctionException("Sinus function needs at least 2 attributes!");
	    return (Math.sin(att[0]*att[1]) + Math.sin(att[0]+att[1]));
	}
    }

    private static class SinusFrequencyFunction extends RegressionFunction {
	public double calculate(double[] att) throws FunctionException {
	    if (att.length < 2)
		throw new FunctionException("Sinus frequency function needs at least 2 attributes!");
	    return 
		10 * Math.sin(3 * att[0]) + 12 * Math.sin(7 * att[0]) + 
		11 * Math.sin(5 * att[1]) + 9 * Math.sin(10 * att[1]) +
		10 * Math.sin(8 * (att[0] + att[1]));
	}
    }



    // ================================================================================
    // classification
    // ================================================================================

    private static class RandomClassificationFunction extends ClassificationFunction {
	public double calculate(double[] args) {
	    if (RandomGenerator.getGlobalRandomGenerator().nextBoolean()) return getLabel().mapString("positive");
	    else return getLabel().mapString("negative");
	}
    }

    private static class SumClassificationFunction extends ClassificationFunction {
	public double calculate(double[] args) {
	    double sum = 0.0d;
	    for (int i = 0; i < args.length; i++) sum += args[i];
	    return (sum > 0 ? getLabel().mapString("positive") : getLabel().mapString("negative"));
	}
    }

    private static class InteractionClassificationFunction extends ClassificationFunction {
	public double calculate(double[] att) throws FunctionException {
	    if (att.length < 3)
		throw new FunctionException("Interactive classification function needs at least 3 attributes!");
	    if ((att[0] < 0.0d) || (att[1] > 0.0d) && (att[2] < 0.0d))
		return getLabel().mapString("positive");
	    else 
		return getLabel().mapString("negative");
	}
    }

    private static class SimpleNonLinearClassificationFunction extends ClassificationFunction {
	public double calculate(double[] att) throws FunctionException {
	    if (att.length < 2)
		throw new FunctionException("Simple non linear classification function needs at least 2 attributes!");
	    if ((att[0]*att[1] > 100.0d) && (att[0]*att[1] < 150.0d))
		return getLabel().mapString("positive");
	    else 
		return getLabel().mapString("negative");
	}
    }

    private static class PolynomialClassificationFunction extends ClassificationFunction {
	public double calculate(double[] att) throws FunctionException {
	    if (att.length < 4)
		throw new FunctionException("Polynomial classification function needs at least 4 attributes!");
	    if ((att[0]*att[0]*att[0] + att[1]*att[1] - att[2]*att[2] + att[3]) > 0)
		return getLabel().mapString("positive");
	    else 
		return getLabel().mapString("negative");
	}
    }

    private static class IrrelevantSumClassificationFunction extends ClassificationFunction {
	public double calculate(double[] att) throws FunctionException {
	    double sum = 0.0d;
	    for (int i = 0; i < att.length; i++) 
		if ((i % 2) == 0)
		    sum += 1000 * att[i];
	    return (sum > 0 ? getLabel().mapString("positive") : getLabel().mapString("negative"));
	}
    }

    private static class SinusClassificationFunction extends ClassificationFunction {
	public double calculate(double[] att) throws FunctionException {
	    if (att.length < 2)
		throw new FunctionException("Sinus classification function needs at least 2 attributes!");
	    if (Math.sin(att[0]*att[1]) + Math.sin(att[0]+att[1]) > 0)
		return getLabel().mapString("positive");
	    else 
		return getLabel().mapString("negative");
	}
    }

    private static class MultiClassificationFunction extends ClassificationFunction {	
	private Attribute label = new Attribute("label", Ontology.NOMINAL, Ontology.SINGLE_VALUE, 
						Attribute.UNDEFINED_BLOCK_NR, null);
	public MultiClassificationFunction() {
	    getLabel().mapString("one");

⌨️ 快捷键说明

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