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

📄 abstractexampleset.java

📁 一个很好的LIBSVM的JAVA源码。对于要研究和改进SVM算法的学者。可以参考。来自数据挖掘工具YALE工具包。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		k++;
	    }
	    stats.init(columnNames);
	    ExampleReader reader = getExampleReader();
	    while (reader.hasNext()) {
		Example example = reader.next();
		Double[] data = new Double[getNumberOfAttributes() + getSpecialAttributeNames().size()];
		for (int i = 0; i < getNumberOfAttributes(); i++)
		    data[i] = new Double(example.getValue(getAttribute(i)));
		s = getSpecialAttributeNames().iterator();
		k = 0;
		while (s.hasNext()) {
		    Attribute specialAtt = getAttribute((String)s.next());
		    //System.out.println()
		    data[getNumberOfAttributes() + k] = 
			new Double(example.getValue(specialAtt));
		    k++;
		}
		stats.add(getIdAttribute() == null ? null : example.getValueAsString(getIdAttribute()), data);
	    }
	    final PlotterPanel plotterPanel = new PlotterPanel(stats);
	    
	    // toggle radio button for views
	    final JRadioButton tableButton = new JRadioButton("table view", true);
	    tableButton.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e) {
			if (tableButton.isSelected()) {
			    mainPanel.remove(plotterPanel);
			    mainPanel.add(label, BorderLayout.CENTER);
			    mainPanel.repaint();
			}
		    }
		});
	    final JRadioButton plotButton = new JRadioButton("plot view", false);
	    plotButton.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent e) {
			if (plotButton.isSelected()) {
			    mainPanel.remove(label);
			    mainPanel.add(plotterPanel, BorderLayout.CENTER);
			    mainPanel.repaint();
			}
		    }
		});
	    ButtonGroup group = new ButtonGroup();
	    group.add(tableButton);
	    group.add(plotButton);
	    JPanel togglePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
	    togglePanel.add(tableButton);
	    togglePanel.add(plotButton);
	    mainPanel.add(togglePanel, BorderLayout.NORTH);
	} else {
	    // too much examples for plot
	    JLabel noPlotLabel = new JLabel("<html>Too many examples for plot view: " + getSize() + ".<br>Increase example limit in the settings dialog (example limit: " + getExampleLimit()+ ").</html>");
	    mainPanel.add(noPlotLabel, BorderLayout.NORTH);
	}

	return mainPanel;
    }


    // -------------------- misc --------------------

    public int getBlockEndIndex(int startindex) {
	Attribute startAtt = getAttribute(startindex);
	if (!Ontology.ATTRIBUTE_BLOCK_TYPE.isA(startAtt.getBlockType(), Ontology.VALUE_SERIES_START)) {
	    throw new RuntimeException("Attribute  "+startAtt + " is not start of a value series!");
	}
	for (int i = startindex+1; i < getNumberOfAttributes(); i++) {
	    Attribute attribute = getAttribute(i);
	    if (attribute.getBlockNr() != startAtt.getBlockNr()) 
		throw new RuntimeException("Value series has mixed block numbers: "+startAtt.getBlockNr()+", "+attribute.getBlockNr());
	    if (Ontology.ATTRIBUTE_BLOCK_TYPE.isA(attribute.getBlockType(), Ontology.VALUE_SERIES_END)) {
		return i;
	    }
	}
	throw new RuntimeException("Value series not terminated!");
    }

    public void save(File file) throws IOException {
	File attFile = new File(file.getAbsolutePath()+".xml");
	writeToFile(file, attFile);
    }


    public void writeToFile(File dataFile, File attFile) throws IOException {
	PrintWriter out = new PrintWriter(new FileWriter(dataFile));
	ExampleReader reader = getExampleReader();
	while (reader.hasNext()) {
	    out.println(reader.next().toString());
	}
	out.close();
	if (attFile != null) { writeAttributeFile(dataFile, attFile, false); }
    }

    public void writeToFileSparse(int format, File dataFile, File attFile) throws IOException {
	PrintWriter out = new PrintWriter(new FileWriter(dataFile));
	ExampleReader reader = getExampleReader();
	while (reader.hasNext()) {
	    out.println(reader.next().toSparseString(format));
	}
	out.close();
	if (attFile != null) { writeAttributeFile(dataFile, attFile, true); }
    }

    private void writeAttributeFile(File dataFile, File attFile, boolean sparse) throws IOException {
	PrintWriter aout = new PrintWriter(new FileWriter(attFile));
	aout.println("<attributeset default_source=\"" + dataFile.getAbsolutePath() + "\">");
	
	int sourcecol = 1;
	
	Attribute id = getIdAttribute();
	if (id != null) {
	    writeAttributeData(ID_NAME, id, sourcecol, aout, sparse);
	    if (!sparse) sourcecol++;
	}
	
	for (int i = 0; i < getNumberOfAttributes(); i++) {
	    writeAttributeData("attribute", getAttribute(i), sourcecol, aout, sparse);
	    sourcecol++;
	}
	
	// write label
	Attribute label = getLabel();
	if (label != null) {
	    writeAttributeData(LABEL_NAME, label, sourcecol, aout, sparse);
	    if (!sparse) sourcecol++;
	}
	// write weight
	Attribute weight = getWeight();
	if (weight != null) {
	    writeAttributeData(WEIGHT_NAME, weight, sourcecol, aout, sparse);
	    if (!sparse) sourcecol++;
	}
	// write cluster
	Attribute clusterAttribute = getCluster();
	if (clusterAttribute != null) {
	    writeAttributeData(CLUSTER_NAME, clusterAttribute, sourcecol, aout, sparse);
	    if (!sparse) sourcecol++;
	}
	aout.println("</attributeset>");
	aout.close();
    }

    /** Writes the data of this attribute in the given stream. */
    private static void writeAttributeData(String tag,
					   Attribute attribute, 
					   int sourcecol, 
					   PrintWriter aout,
					   boolean sparse) {
	aout.println("  <"+tag);
	aout.println("    name         = \"" + attribute.getName() + "\"");
	if (!sparse || tag.equals("attribute")) {
	    aout.println("    sourcecol    = \"" + sourcecol + "\"");
	}
	aout.println("    valuetype    = \"" + Ontology.ATTRIBUTE_VALUE_TYPE.mapIndex(attribute.getValueType()) + "\"");
	if (attribute.isNominal()) {
	    aout.print("    classes      = \"");
	    Iterator i = attribute.getValues().iterator();
	    int n = 0;
	    while (i.hasNext()) {
		if (n != 0) aout.print(" ");
		n++;
		aout.print((String)i.next());
	    }
	    aout.println("\"");
	}
	aout.println("    blocktype    = \"" + Ontology.ATTRIBUTE_BLOCK_TYPE.mapIndex(attribute.getBlockType()) + "\"");
	aout.println("    blocknumber  = \"" + attribute.getBlockNr() + "\"");
	aout.println("    unit         = \"" + attribute.unitToString() + "\"");
	aout.println("  />");
    } 

    /** Returns true, if  all attributes including labels and other special attributes are equal. */
    public boolean equals(Object o) {
	if (!(o instanceof ExampleSet)) {
	    return false;
	}
	ExampleSet es = (ExampleSet)o;
	if (es.getNumberOfAttributes() != this.getNumberOfAttributes()) return false;
	for (int i = 0; i < this.getNumberOfAttributes(); i++) {
	    if (!equals(this.getAttribute(i), es.getAttribute(i))) return false;
	}
	if (!equals(this.getLabel(), es.getLabel())) return false;
	if (!equals(this.getPredictedLabel(), es.getPredictedLabel())) return false;
	if (!equals(this.getWeight(), es.getWeight())) return false;
	if (!equals(this.getCluster(), es.getCluster())) return false;
	return true;
    }

    /** Returns true iff both attributes are null or <code>a1.equals(a2)</code> returns true. */
    private static boolean equals(Attribute a1, Attribute a2) {
	if ((a1 == null) && (a2 == null)) return true;
	if ((a1 == null) || (a2 == null)) return false;
	return a1.equals(a2);
    }

    /** Clones the example set by invoking a single argument clone constructor. */
    public Object clone() {
	try {
	    Class clazz = getClass();
	    java.lang.reflect.Constructor cloneConstructor = clazz.getConstructor(new Class[] { clazz });
	    return cloneConstructor.newInstance(new Object[] { this } );
	} catch (NoSuchMethodException e) {
	    throw new RuntimeException("'"+getClass().getName()+"' does not implement clone constructor!");
	} catch (java.lang.reflect.InvocationTargetException e) {
	    throw new RuntimeException("Cannot clone "+getClass().getName()+": "+e+
				       ". Target: "+e.getCause()+". Cause: "+e.getTargetException()+".");
	} catch (Throwable throwable) {
	    throw new RuntimeException("Cannot clone "+getClass().getName()+": "+throwable);
	} 
    }

    /** Recalculates the attribute statistics for all attributes. 
     *  They are average value, variance, minimum, and maximum. For nominal attributes the occurences for
     *  all values are counted. Uses <code>recalculateAttributeStatistics(Attribute attribute)</code> for both
     *  regular and special attributes and performs therefore one data scan for each attribute. If this is not desired, 
     *  for example when working directly on a database, a subclass should be written with better calculations. */
    public void recalculateAllAttributeStatistics() {
	for (int i = 0; i < getNumberOfAttributes(); i++) {
	    recalculateAttributeStatistics(getAttribute(i));
	}
	
	Iterator i = getSpecialAttributeNames().iterator();
	while (i.hasNext()) {
	    recalculateAttributeStatistics(getAttribute(((String)i.next())));
	}
    }

    /** Recalculate the attribute statistics of the given attribute. */
    public void recalculateAttributeStatistics(Attribute attribute) {
	if (attribute.isNominal()) {
	    Map valueToCounterMap = new HashMap();
	    ExampleReader reader = getExampleReader();
	    while (reader.hasNext()) {
		Example example = reader.next();
		String value = attribute.mapIndex((int)example.getValue(attribute));
		Integer counter = (Integer)valueToCounterMap.get(value);
		if (counter == null) {
		    valueToCounterMap.put(value, new Integer(1));
		} else {
		    valueToCounterMap.remove(value);
		    int newValue = counter.intValue() + 1;
		    valueToCounterMap.put(value, new Integer(newValue));
		}
	    }
	    attribute.setSymbolToCounterMap(valueToCounterMap);
	} else {
	    double minimum = Double.POSITIVE_INFINITY;
	    double maximum = Double.NEGATIVE_INFINITY;
	    double average    = 0.0d;
	    double squaredSum = 0.0d; 
	    ExampleReader reader = getExampleReader();
	    while (reader.hasNext()) {
		Example example = reader.next();
		double value = example.getValue(attribute);
		if (!Double.isNaN(value)) {
		    if (minimum > value) minimum = value;
		    if (maximum < value) maximum = value;
		    average += value;
		    squaredSum += value * value;
		}
	    }
	    
	    average /= getSize();
	    squaredSum /= getSize();
	    
	    attribute.setMinimum(minimum);
	    attribute.setMaximum(maximum);
	    attribute.setAverage(average);
	    attribute.setVariance(squaredSum - (average*average));
	}
    }
}

⌨️ 快捷键说明

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