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

📄 abstractmysvmmodel.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.operator.learner.kernel;

import edu.udo.cs.yale.operator.learner.IOModel;
import edu.udo.cs.yale.Statistics;
import edu.udo.cs.yale.example.ExampleSet;
import edu.udo.cs.yale.example.ExampleReader;
import edu.udo.cs.yale.example.Example;
import edu.udo.cs.yale.example.Attribute;
import edu.udo.cs.yale.operator.OperatorException;
import edu.udo.cs.yale.gui.PlotterPanel;
import edu.udo.cs.yale.gui.Plotter;

import edu.udo.cs.mySVM.SVM.*;
import edu.udo.cs.mySVM.Kernel.*;

import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Collections;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/** The abstract superclass for the SVM models by Stefan Rueping. 
 *
 *  @version $Id: AbstractMySVMModel.java,v 1.2 2004/08/27 11:57:39 ingomierswa Exp $
 */
public abstract class AbstractMySVMModel extends IOModel {

    private class AlphaValue implements Comparable {
	private double alpha;
	private String id;
	public AlphaValue(String id, double alpha) {
	    this.id = id;
	    this.alpha = alpha;
	}
	public String getId() { return id; }
	public double getAlpha() { return alpha; }
	public int compareTo(Object o) {
	    return (-1) * Double.compare(this.alpha, ((AlphaValue)o).alpha);
	}
    }

    private edu.udo.cs.mySVM.Examples.ExampleSet model;
    private Kernel kernel;
    private int kernelType;

    public AbstractMySVMModel(Attribute labelAttribute) {
	super(labelAttribute);
    }

    public AbstractMySVMModel(Attribute labelAttribute, 
			      edu.udo.cs.mySVM.Examples.ExampleSet model, 
			      Kernel kernel, 
			      int kernelType) {
	super(labelAttribute);
	this.model  = model;
	this.kernel = kernel;
	this.kernelType = kernelType;
    }

    /** Creates a new SVM for prediction. */
    public abstract SVMInterface createSVM();

    /** Sets the correct prediction to the example from the result value of the SVM. */
    public abstract void setPrediction(Example example, double prediction);

    /** Reads the support vectors and the kernel from the input stream. */
    public void readData(ObjectInputStream in) throws IOException {
	// read SVs
	this.model = new edu.udo.cs.mySVM.Examples.ExampleSet(in);

	// read kernel
	int kernelType = in.readInt();
	int cacheSize  = in.readInt();
	this.kernel = AbstractMySVMLearner.createKernel(kernelType);
	this.kernel.readKernelParameters(in);
	this.kernel.init(model, cacheSize);
    }
    
    /** Writes the support vectors and the kernel into the input stream. */
    public void writeData(ObjectOutputStream out) throws IOException {
	// write SVs
	model.writeSupportVectors(out);

	// write kernel
	out.writeInt(kernelType);
	out.writeInt(kernel.getCacheSize());
	kernel.writeKernelParameters(out);
    }
    
    public void apply(ExampleSet exampleSet) throws OperatorException {
	edu.udo.cs.mySVM.Examples.ExampleSet toPredict = 
	    new edu.udo.cs.mySVM.Examples.ExampleSet(exampleSet, model.getMeanVariances());

	SVMInterface svm = createSVM();
	svm.init(kernel, model);
	svm.predict(toPredict);

	// set predictions from toPredict
	ExampleReader reader = exampleSet.getExampleReader();
	int k = 0;
	while (reader.hasNext()) {
	    //System.out.println("Prediction: " + toPredict.get_y(k));
	    setPrediction(reader.next(), toPredict.get_y(k++));
	}
    }

    public String toString() {
	return model.toString(0, false);
    }

    public String toResultString() {
	if (model.count_examples() < 500)
	    return model.toString(true);
	else 
	    return model.toString(500, true) + "\n...";
    }

    /** Returns a html label with a table view or a plotter for statistic view. */
    public Component getVisualisationComponent() {
	final JPanel mainPanel = new JPanel();
	mainPanel.setLayout(new BorderLayout());

	// html table view
  	final JLabel label = new JLabel("<html>" + toHTML(toResultString()) + "</html>");
  	label.setBorder(javax.swing.BorderFactory.createEmptyBorder(11,11,11,11));
  	label.setFont(label.getFont().deriveFont(java.awt.Font.PLAIN));

	mainPanel.add(label, BorderLayout.CENTER);

	Statistics stats = new Statistics("JMySVM model");
	String[] columnNames = new String[] { "counter", "alpha", "abs(alpha)" }; 
	stats.init(columnNames);

	java.util.List alphaValues = new LinkedList();	
	java.util.List absAlphaValues = new LinkedList();	
	for (int i = 0; i < model.count_examples(); i++) {
	    double alpha = model.get_alpha(i);
	    if (alpha != 0.0d) {
		alphaValues.add(new AlphaValue(model.getId(i), alpha));
		absAlphaValues.add(new AlphaValue(model.getId(i), Math.abs(alpha)));
	    }
	}
	Collections.sort(alphaValues);
	Collections.sort(absAlphaValues);
	Iterator i = alphaValues.iterator();
	Iterator j = absAlphaValues.iterator();
	int counter = 0;
	while (i.hasNext() && j.hasNext()) {
	    AlphaValue currentAlpha    = (AlphaValue)i.next();
	    AlphaValue currentAbsAlpha = (AlphaValue)j.next();
	    stats.add(currentAlpha.getId(), new Double[] { new Double(counter++), 
							   new Double(currentAlpha.getAlpha()), 
							   new Double(currentAbsAlpha.getAlpha()) });
	}
	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);
	
	return mainPanel;
    }
}

⌨️ 快捷键说明

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