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

📄 statistics.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;

import edu.udo.cs.yale.gui.SwingTools;
import edu.udo.cs.yale.tools.ParameterService;
import edu.udo.cs.yale.tools.Tools;

import javax.imageio.ImageIO;
import java.awt.Image;
import javax.swing.Icon;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.event.ActionEvent;
import java.awt.Component;
import java.awt.Color;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.Comparator;
import java.util.Collection;
import java.util.SortedSet;
import java.util.TreeSet;
import java.io.*;

/** A data list that contains Object arrays that record experiment results. Instances
 *  of this class are automatically created by {@link Experiment#getStatistics()} and are
 *  used mainly by the {@link edu.udo.cs.yale.operator.ExperimentLogOperator}.
 *  
 *  @version $Id: Statistics.java,v 2.15 2004/08/27 11:57:30 ingomierswa Exp $
 */
public class Statistics {

    private List data = new LinkedList();
    private String name;
    private List listenerList = new LinkedList();
    private String[] columns;

    public Statistics(String name) {
	this.name = name;
    }

    public void init(String[] columns) {
	synchronized (data) {
	    data.clear();
	}
	this.columns = columns;
	fireEvent();
    }
    
    public String getColumnName(int i) {
	return columns[i];
    }

    public int getColumnIndex(String name) {
	for (int i = 0; i < columns.length; i++) {
	    if (columns[i].equals(name)) return i;
	}
	return -1;
    }

    public int getNumberOfColumns() {
	return columns.length;
    }

    public String[] getColumnNames() {
	return columns;
    }

    public String getName() {
	return name;
    }

    public void add(Object[] row) {
	add(null, row);
    }

    public synchronized void add(String id, Object[] row) {
	synchronized (data) {
	    data.add(new StatisticsRow(id, row));
	    fireEvent();
	}
    }

    public synchronized Iterator iterator() {
	Iterator i = null;
	synchronized (data) {
	    i = data.iterator();
	}
	return i;
    }

    public int getNumberOfRows() {
	int result = 0;
	synchronized (data) {
	    result = data.size();
	}
	return result;
    }

    public void addStatisticsListener(StatisticsListener statisticsListener) {
	listenerList.add(statisticsListener);
    }

    public void removeStatisticsListener(StatisticsListener statisticsListener) {
	listenerList.remove(statisticsListener);
    }

    private void fireEvent() {
	Iterator i = listenerList.iterator();
	while (i.hasNext()) {
	    ((StatisticsListener)i.next()).statisticsUpdated(this);
	}
    }

    public void write(PrintWriter out) throws IOException {
	out.println("# Generated by "+ getName() + "["+getClass().getName()+"]");
	for (int j = 0; j < getNumberOfColumns(); j++) {
	    out.print((j != 0 ? "\t" : "# ") + getColumnName(j));
	}
	out.println();
	
	Iterator i = iterator();
	while (i.hasNext()) {
	    StatisticsRow statRow = (StatisticsRow)i.next();
	    Object[] row = statRow.getRow();
	    for (int j = 0; j < row.length; j++) {
		out.print((j != 0 ? "\t" : "") + row[j]);
	    }
	    out.println();
	}
	out.flush();
    }


    private static final Comparator ROW_COMPARATOR = new Comparator() {
	    public int compare(Object o1, Object o2) {
		double[] row1 = (double[])o1;
		double[] row2 = (double[])o2;
		for (int i = 0; i < row1.length; i++) {
		    if (row1[i] < row2[i]) return -1;
		    else if (row1[i] > row2[i]) return +1;
		}
		return 0;
	    };
	};

    private void write2DGNUPlotData(PrintStream out, int x, int y) {
	Collection plot = new TreeSet(ROW_COMPARATOR);
	Iterator i = data.iterator();
	while (i.hasNext()) {
	    StatisticsRow statRow = (StatisticsRow)i.next();
	    Object[] row = statRow.getRow();
	    if ((row[x] instanceof Double) && (row[y] instanceof Double)) {
		plot.add(new double[] {((Double)row[x]).doubleValue(),
				       ((Double)row[y]).doubleValue()});
	    }
	}

	Iterator j = plot.iterator();
	while (j.hasNext()) {
	    double[] row = (double[])j.next();
	    out.println(row[0]+"\t"+row[1]);
	}
    }

    public void write3DGNUPlotData(PrintStream out, int x, int y, int z) {
	Collection plot = new TreeSet(ROW_COMPARATOR);
	Iterator i = data.iterator();
	while (i.hasNext()) {
	    StatisticsRow statRow = (StatisticsRow)i.next();
	    Object[] row = statRow.getRow();
	    if ((row[x] instanceof Double) && (row[y] instanceof Double) && (row[2] instanceof Double)) {
		plot.add(new double[] {((Double)row[x]).doubleValue(),
				       ((Double)row[y]).doubleValue(),
				       ((Double)row[z]).doubleValue()});

	    }
	}

	double oldX = Double.NaN;
	Iterator j = plot.iterator();
	while (j.hasNext()) {
	    double[] row = (double[])j.next();
	    if ((!Double.isNaN(oldX)) && (row[0] != oldX)) {
		out.println();
	    }
	    out.println(row[0]+"\t"+row[1]+"\t"+row[2]);
	    oldX = row[0];
	}
    }

    private void writeGNUPlotHeader(PrintStream out,
				    String xAxis,
				    String yAxis,
				    String zAxis,
				    String additionalCommands,
				    String terminal) {
	out.println("#!"+System.getProperty("yale.tools.gnuplot.command"));
	out.println("# Generated by "+getClass() + " on "+DateFormat.getDateTimeInstance().format(new Date()));
	if (xAxis != null) out.println("set xlabel \""+xAxis+"\"");
	if (yAxis != null) out.println("set ylabel \""+yAxis+"\"");
	if (zAxis != null) out.println("set zlabel \""+zAxis+"\"");
	if (additionalCommands != null) out.println(additionalCommands);
	if (terminal != null) out.println("set terminal "+terminal);
    }

    public void writeGNUPlot(PrintStream out,
			     int x, int y, int[] z,
			     String linetype,
			     String additionalCommands,
			     String terminal) {
	writeGNUPlotHeader(out,
			   columns[x], 
			   (y != -1 ? columns[y] : columns[z[0]]), 
			   columns[z[0]],
			   additionalCommands,
			   terminal);
	if (y != -1) out.print("splot ");
	else out.print("plot ");
	for (int i = 0; i < z.length; i++) {
	    if (i>0) out.print(", ");
	    out.print("'-' title \""+columns[z[i]]+"\" with " + linetype);//linespoints");
	}
	out.println();
	for (int i = 0; i < z.length; i++) {
	    if (y != -1) write3DGNUPlotData(out, x, y, z[i]);
	    else write2DGNUPlotData(out, x, z[i]);
	    out.println("e");
	}
    }


    private static Image createMessageImage(String message) {
	String[] lines = message.split("\n");

	BufferedImage image = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB);
	Graphics g = image.getGraphics();
	int lineHeight = g.getFont().getSize();
	g.setColor(Color.white);
	g.fillRect(0,0,640,480);
	g.setColor(Color.black);
	Icon icon = javax.swing.UIManager.getIcon("OptionPane.errorIcon");
	if (icon != null)
	    icon.paintIcon(null, g, 100-icon.getIconWidth()-11, 50-lineHeight);
	for (int i = 0; i < lines.length; i++)
	    g.drawString(lines[i], 100, 50+i*lineHeight);
	return image;
    }
    
    public Image createGNUPlot(int x, int y, int[] z, String linetype, String additionalCommands) {

	if (System.getProperty("yale.tools.gnuplot.command") == null) {
	    return createMessageImage("Property yale.tools.gnuplot.command is not set!");
	} else {
	    try {
		Process process = Runtime.getRuntime().exec(System.getProperty("yale.tools.gnuplot.command"));
		PrintStream out = new PrintStream(process.getOutputStream());
		writeGNUPlot(out, x, y, z, linetype, additionalCommands, "png");
		out.close();
		
		Image image = ImageIO.read(process.getInputStream());
		process.getInputStream().close();
		return image;
	    } catch (IOException e) {
		return createMessageImage("GNUPlot caused exception:\n"+e);
	    }
	}
    }
				      
}

⌨️ 快捷键说明

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