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

📄 examplesetgenerator.java

📁 一个很好的LIBSVM的JAVA源码。对于要研究和改进SVM算法的学者。可以参考。来自数据挖掘工具YALE工具包。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	    getLabel().mapString("two");
	    getLabel().mapString("three");
	    getLabel().mapString("four");
	}
	public Attribute getLabel() { return label; }

	public double calculate(double[] args) throws FunctionException {
	    double sumD = 0.0d;
	    for (int i = 0; i < args.length; i++) sumD += args[i];
	    int sum = Math.abs((int)Math.round(sumD));
	    if ((sum % 2) == 0) return getLabel().mapString("one");
	    else if ((sum % 3) == 0) return getLabel().mapString("two");
	    else if ((sum % 5) == 0) return getLabel().mapString("three");
	    else return getLabel().mapString("four");
	}
    }


    // ================================================================================
    // clustering
    // ================================================================================

    private static class RingClusteringFunction implements TargetFunction {
	
	private double bound =  10.0d;

	private Attribute label = new Attribute("label", Ontology.NOMINAL, Ontology.SINGLE_VALUE, 
						Attribute.UNDEFINED_BLOCK_NR, null);

	public RingClusteringFunction() {
	    label.mapString("core");
	    label.mapString("first_ring");
	    label.mapString("second_ring");
	}

	/** Since circles are used the upper and lower bounds must be the same. */
 	public void setLowerArgumentBound(double lower) { this.bound = Math.max(this.bound, Math.abs(lower)); }
 	public void setUpperArgumentBound(double upper) { this.bound = Math.max(this.bound, Math.abs(upper)); }

	public Attribute getLabel() { return label; }

	public double calculate(double[] att) throws FunctionException {
	    if (att.length != 2)
		throw new FunctionException("Clustering function must have 2 attributes!");
	    double label = 0.0d;
	    if (RandomGenerator.getGlobalRandomGenerator().nextDouble() < 0.05) {
		int type = RandomGenerator.getGlobalRandomGenerator().nextInt(3);
		switch (type) {
		case 0: return getLabel().mapString("core");
		case 1: return getLabel().mapString("first_ring");
		case 2: return getLabel().mapString("second_ring");
		default: return getLabel().mapString("core");
		}
	    } else {
		double radius = Math.sqrt(att[0]*att[0] + att[1]*att[1]);
		if (radius < bound / 3.0d) return getLabel().mapString("core");
		else if (radius < 2 * bound / 3.0d) return getLabel().mapString("first_ring");
		else return getLabel().mapString("second_ring");
	    }
	}

	public double[] createArguments(int number) throws FunctionException {
	    if (number != 2)
		throw new FunctionException("Ring clustering function must have 2 attributes!");
	    double[] args = new double[number];
	    RandomGenerator random = RandomGenerator.getGlobalRandomGenerator();
	    int type = random.nextInt(3);
	    double radius = 0.0d;
	    switch (type) {
	    case 0: radius = random.nextGaussian(); break;
	    case 1: radius = bound / 2.0d + random.nextGaussian(); break;
	    case 2: radius = bound + random.nextGaussian(); break;
	    default: radius = random.nextGaussian(); break;
	    }
	    double angle  = random.nextDouble() * 2 * Math.PI;
	    args[0] = radius * Math.cos(angle);
	    args[1] = radius * Math.sin(angle);
	    return args;
	}
    }


    private static class GaussianFunction implements TargetFunction {
	
	private double bound =  10.0d;

	/** Since circles are used the upper and lower bounds must be the same. */
 	public void setLowerArgumentBound(double lower) { this.bound = Math.max(this.bound, Math.abs(lower)); }
 	public void setUpperArgumentBound(double upper) { this.bound = Math.max(this.bound, Math.abs(upper)); }

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

	public double calculate(double[] att) throws FunctionException {
	    if (att.length != 2)
		throw new FunctionException("Gaussian function must have 2 attributes!");
	    return 0.0d;
	}

	public double[] createArguments(int number) throws FunctionException {
	    if (number != 2)
		throw new FunctionException("Ring clustering function must have 2 attributes!");
	    double[] args = new double[number];
	    RandomGenerator random = RandomGenerator.getGlobalRandomGenerator();
	    args[0] = random.nextGaussian() * bound;
	    args[1] = random.nextGaussian() * bound;
	    return args;
	}
    }









    // ================================================================================

    private static final String[] KNOWN_FUNCTION_NAMES = new String[] {
	"random", "sum", "polynomial", "non linear", "sinus", "sinus frequency",	
	"random classification", "sum classification", "simple non linear classification", 
	"interaction classification", "polynomial classification", 
	"irrelevant sum classification", "sinus classification",
	"multi classification",
	"three ring clusters", "single gaussian cluster"
    };
    
    private static final TargetFunction[] KNOWN_FUNCTIONS = new TargetFunction[] {
	new RandomFunction(), new SumFunction(), new PolynomialFunction(), 
	new NonLinearFunction(), new SinusFunction(), new SinusFrequencyFunction(), 
	new RandomClassificationFunction(), new SumClassificationFunction(),
	new SimpleNonLinearClassificationFunction(),
	new InteractionClassificationFunction(), new PolynomialClassificationFunction(),
	new IrrelevantSumClassificationFunction(), new SinusClassificationFunction(),
	new MultiClassificationFunction(),
	new RingClusteringFunction(), new GaussianFunction()
    };

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

    public IOObject[] apply() throws OperatorException {

	// init
	int numberOfExamples   = getParameterAsInt("number_examples");
	int numberOfAttributes = getParameterAsInt("number_of_attributes");
	double lower = getParameterAsDouble("attributes_lower_bound");
	double upper = getParameterAsDouble("attributes_upper_bound");
	String functionName = getParameterAsString("target_function");

 	TargetFunction function = getFunctionForName(functionName);
 	if (function == null) {
 	    try {
 		Class clazz = Class.forName(functionName);
 		function = (TargetFunction)clazz.newInstance();
 	    } catch (Exception e) {
 		throw new UserError(this, e, 904, new Object[] { functionName, e});
 	    }
 	}

	function.setLowerArgumentBound(lower);
	function.setUpperArgumentBound(upper);

	// create table
	List attributes = new LinkedList();
	for (int m = 0; m < numberOfAttributes; m++)
	    attributes.add(new Attribute("att" + (m+1), Ontology.REAL, Ontology.SINGLE_VALUE, Attribute.UNDEFINED_BLOCK_NR, null));
	Attribute label = function.getLabel();
	if (label != null)
	    attributes.add(label);
	MemoryExampleTable table = new MemoryExampleTable(attributes);

	// create data
	List data = null;
	try {
	    data = new LinkedList();
	    for (int n = 0; n < numberOfExamples; n++) {
		double[] features = function.createArguments(numberOfAttributes);
		double[] example = features;
		if (label != null) {
		    example = new double[numberOfAttributes + 1];
		    System.arraycopy(features, 0, example, 0, features.length);
		    example[example.length - 1] = function.calculate(features);
		}
		data.add(new DoubleArrayDataRow(example));
	    }
	} catch (TargetFunction.FunctionException e) {
	    throw new OperatorException(e.getMessage());
	}

	// fill table with data
	table.readExamples(new ListDataRowReader(data.iterator()));


	// create example set and return it
	ExampleSet result = table.createCompleteExampleSet(label, null, null, null);

	return new IOObject[] { result };
    }


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

    public List getParameterTypes() {
	List types = super.getParameterTypes();
	ParameterType type = new ParameterTypeStringCategory("target_function", "Specifies the target function of this example set",
							     KNOWN_FUNCTION_NAMES);
	type.setExpert(false);
	types.add(type);
	type = new ParameterTypeInt("number_examples", "The number of generated examples.", 1, Integer.MAX_VALUE, 100);
	type.setExpert(false);
	types.add(type);
	type = new ParameterTypeInt("number_of_attributes", "The number of attributes.", 0, Integer.MAX_VALUE, 5);
	type.setExpert(false);
	types.add(type);

	types.add(new ParameterTypeDouble("attributes_lower_bound", "The minimum value for the attributes.", 
					  Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, -10));
	types.add(new ParameterTypeDouble("attributes_upper_bound", "The maximum value for the attributes.", 
					  Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 10));
	return types;
    }


    // ================================================================================

    public static TargetFunction getFunctionForName(String functionName) {
 	for (int i = 0; i < KNOWN_FUNCTION_NAMES.length; i++) {
 	    if (KNOWN_FUNCTION_NAMES[i].equals(functionName))
 		return KNOWN_FUNCTIONS[i];
 	}
 	return null;
    }
}

⌨️ 快捷键说明

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