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

📄 featuregenerator.java

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

    /** Returns the i-th selected argument (the true index as used in the example set's
     *  example table). */
    public Attribute getArgument(int i) {
	return arguments[i]; 
    }

    /** Checks if the arguments are compatible with the attributes specified by getInputAttributes(). */
    private boolean argumentsOk(ExampleTable input) {
	Attribute[] inputA = getInputAttributes();
	for (int i = 0; i < inputA.length; i++) {
	    if (!arguments[i].compatible(inputA[i])) return false;
	}
	return true;
    }

    // --------------------------------------------------------------------------------

    /** Creates a new FeatureGenerator for a given function name. */
    public static FeatureGenerator createGeneratorForFunction(String functionName) {
	Class genClass = (Class)generatorMap.get(functionName);
	if (genClass == null) {
	    if (functionName.startsWith(ConstantGenerator.FUNCTION_NAME)) {
		FeatureGenerator gen = new ConstantGenerator();
		gen.setFunction(functionName);
		return gen;
	    } else {
		LogService.logMessage("Unknown feature generator: '"+functionName+"'", LogService.ERROR);
		return null;
	    }
	} else {
	    try {
		FeatureGenerator gen = (FeatureGenerator)genClass.newInstance();
		gen.setFunction(functionName);
		return gen;
	    } catch (Exception e) {
		LogService.logMessage("Cannot instanciate '"+genClass.getName()+"'", LogService.ERROR);
		return null;
	    }
	}
    }

    // --------------------------------------------------------------------------------

    /** Randomly selects a generator from the generator list. The probability of a generator
     *  to be selected is proportional to its number of attribute combinations as delivered by
     *  {@link #getInputCandidates(ExampleSet, int, String[])} method. Returns null if no
     *  generators are applicable.
     *
     *  @param generators List of {@link FeatureGenerator}s  */
    public static FeatureGenerator selectGenerator(ExampleSet exampleSet, List generators, int maxDepth, String[] functions) {
	int combinationSum = 0;
	Iterator i = generators.iterator();
	double[] probs = new double[generators.size()];
	int k = 0;
	while ((i.hasNext())) {
	    FeatureGenerator generator = (FeatureGenerator)i.next();
	    //probs[k] = generator.getNumberOfApplicableGenerations(exampleSet);
	    probs[k] = generator.getInputCandidates(exampleSet, maxDepth, functions).size();
	    combinationSum += probs[k];
	    k++;
	}
	if (combinationSum == 0) return null;
	else {
	    for (k = 0; k < probs.length; k++)
		probs[k] /= (double)combinationSum;
	    
	    int index = RandomGenerator.getGlobalRandomGenerator().randomIndex(probs);
	    FeatureGenerator selected = (FeatureGenerator)generators.get(index);
	    return selected;
	}
    }


    // --------------------------------------------------------------------------------

    /** Generates all new attributes and updates the ExampleTable.
     *  Returns a list of Attributes for the newly generated attributes. 
     *  @param exampleTable the source example table
     *  @param generatorList List of FeatureGenerators 
     *  @return A list of AttributeReferences */
    public static List generateAll(ExampleTable exampleTable, Collection generatorList) throws GenerationException {
	LogService.logMessage("Starting feature generation with " + 
			      generatorList.size() + " generators.", LogService.OPERATOR);

	Iterator gi = generatorList.iterator();
	while (gi.hasNext()) 
	    ((FeatureGenerator)gi.next()).setExampleTable(exampleTable);
		
	// for performance reasons convert the list to an array
	FeatureGenerator[] generators = new FeatureGenerator[generatorList.size()];
	generatorList.toArray(generators);
	
	List newAttributeList = newAttributes(generators, exampleTable);
	// add the attributes to the example table and ensure length of the DataRows
	exampleTable.addAttributes(newAttributeList);
	LogService.logMessage("Generator list: " + generatorList, LogService.TASK);
	LogService.logMessage("Input set has " + exampleTable.getAttributeCount() + 
			      " features, " + exampleTable.getSize() + " examples.", LogService.OPERATOR);

	// initialize calculation of statistics
	double[] minimum    = new double[newAttributeList.size()];
	double[] maximum    = new double[newAttributeList.size()];
	double[] average    = new double[newAttributeList.size()];
	double[] squaredSum = new double[newAttributeList.size()];
	
	for (int n = 0; n < newAttributeList.size(); n++) {
	    minimum[n]    = Double.POSITIVE_INFINITY;
	    maximum[n]    = Double.NEGATIVE_INFINITY;
	    average[n]    = 0.0d;
	    squaredSum[n] = 0.0d;
	}

	// generate the attribute values:
	DataRowReader reader = exampleTable.getDataReader();
	// for all examples:
	while (reader.hasNext()) {
	    DataRow dataRow = reader.next();

	    for (int j = 0; j < generators.length; j++) {
		generators[j].generate(dataRow);
	    }

	    int k = 0;
	    Iterator j = newAttributeList.iterator();
	    while (j.hasNext()) {
		Attribute generatedAttribute = (Attribute)j.next();
		double value   = dataRow.get(generatedAttribute);
		minimum[k]     =  Math.min(minimum[k], value);
		maximum[k]     =  Math.max(maximum[k], value);
		average[k]    += value;
		squaredSum[k] += value * value;
		k++;
	    }
	}

	// set the calculated minimum, maximum and average of each attribute
	int k = 0;
	Iterator i = newAttributeList.iterator();
	while (i.hasNext()) {
	    Attribute attribute = (Attribute)i.next();
	    average[k] /= exampleTable.getSize();
	    squaredSum[k] /= exampleTable.getSize();
	    attribute.setMinimum(minimum[k]);
	    attribute.setMaximum(maximum[k]);
	    attribute.setAverage(average[k]);
	    attribute.setVariance(squaredSum[k] - (average[k] * average[k]));
	    k++;
	}

	LogService.logMessage("Finished feature generation.", LogService.OPERATOR);
	LogService.logMessage("Generated set has " + exampleTable.getAttributeCount() +
			      " features, " + exampleTable.getSize() + " examples.", LogService.OPERATOR);

	return newAttributeList;
    }

    /** Returns a list of new Attributes that are generated by the given generators. */
    private static List newAttributes(FeatureGenerator[] generators, 
				      ExampleTable exampleTable) {

	List newAttributeList = new LinkedList();

	// add the attributes to the attribute set
	for (int i = 0; i < generators.length; i++) {
	    Attribute outputAttribute[] = generators[i].getOutputAttributes(exampleTable);
	    generators[i].resultAttributes = new Attribute[outputAttribute.length];

	    for (int j = 0; j < outputAttribute.length; j++) {
		Attribute newAttribute = exampleTable.getAttribute(outputAttribute[j]);
		if (newAttribute == null) {
		    newAttributeList.add(outputAttribute[j]);
		    generators[i].resultAttributes[j] = outputAttribute[j];
		} else {
		    newAttributeList.add(newAttribute);
 		    generators[i].resultAttributes[j] = newAttribute;
		    LogService.logMessage("Attribute '"+outputAttribute[j].getConstructionDescription()+
					  "' already generated", LogService.STATUS);
		}
	    }

	    // check the arguments
	    if (!generators[i].argumentsSet()) {
		throw new RuntimeException("Catastrophic error: arguments not set for "+generators[i]+"!");
	    }
	    if (!generators[i].argumentsOk(exampleTable)) {
		LogService.logMessage("Wrong argument types for " + generators[i] + ".", LogService.WARNING);
	    }
	}
	return newAttributeList;
    }

    public static int getSelectionMode() {
	return selectionMode;
    }

    public static void setSelectionMode(int mode) {
	selectionMode = mode;
    }

    public String toString() { return "FeatureGenerator (" + getClass().getName() + ")"; }

    /** A FeatureGenerator equals another FeatureGenerator if its class is equal and its arguments are equal
     *  and its function names are equal. */
    public boolean equals(Object o) {
	if (!this.getClass().equals(o.getClass())) return false;
	FeatureGenerator fg = (FeatureGenerator)o;
	if (!this.getFunction().equals(fg.getFunction())) return false;
	if (this.arguments.length != fg.arguments.length) return false;
	for (int i = 0; i < arguments.length; i++) {
	    if (!this.arguments[i].equals(fg.arguments[i])) return false;
	}
	return true;
    }
}

⌨️ 快捷键说明

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