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

📄 splittedexampleset.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.example;

import edu.udo.cs.yale.tools.Ontology;

/** An example set that can be split into subsets. 
 * 
 *  @version $Id: SplittedExampleSet.java,v 2.9 2004/08/28 17:04:48 ingomierswa Exp $ 
 */
public class SplittedExampleSet extends ExampleSetAdapter {

    /** The partition. */
    private Partition partition;

    /** Clone constructor. */
    public SplittedExampleSet(SplittedExampleSet exampleSet) {
	super((ExampleSetAdapter)exampleSet);
	this.partition = (Partition)exampleSet.partition.clone();
    }

    /** Constructs a SplittedExampleSet with the given partition. */
    private SplittedExampleSet(ExampleSet exampleSet, Partition partition) { 
	super(exampleSet);
	this.partition = partition;
    }

    /** Creates an example set that is splitted into two subsets. */
    public SplittedExampleSet(ExampleSet exampleSet, double splitRatio) {
	this(exampleSet, splitRatio, true);
    }

    /** Creates an example set that is splitted into two subsets. */
    public SplittedExampleSet(ExampleSet exampleSet, double splitRatio, boolean shuffle) {
	this(exampleSet, new Partition(new double[] {splitRatio,1-splitRatio}, exampleSet.getSize(), shuffle));
    }

    /** Creates an example set that is splitted into <i>numberOfSubsets</i> parts. */
    public SplittedExampleSet(ExampleSet exampleSet, int numberOfSubsets) {
	this(exampleSet, numberOfSubsets, true);
    }

    /** Creates an example set that is splitted into <i>numberOfSubsets</i> parts. */
    public SplittedExampleSet(ExampleSet exampleSet, int numberOfSubsets, boolean shuffle) {
	this(exampleSet, new Partition(numberOfSubsets, exampleSet.getSize(), shuffle));
    }

    /** Selects exactly one subset. */
    public void selectSingleSubset(int index) {
	partition.clearSelection();
	partition.selectSubset(index);
	clearUserData();
    }

    /** Selects all but one subset. */
    public void selectAllSubsetsBut(int index) {
	partition.clearSelection();
	for (int i = 0; i < partition.getNumberOfSubsets(); i++) {
	    if (i != index)
		partition.selectSubset(i);
	}
	clearUserData();
    }

    /** Returns the number of subsets. */
    public int getNumberOfSubsets() {
	return partition.getNumberOfSubsets();
    }

    /** Returns an example reader that splits all examples that are not selected. */
    public ExampleReader getExampleReader() {
	return new SplittedExampleSetReader(super.getExampleReader(),
					    (Partition)partition.clone());
    }

    public int getSize() {
	return partition.getSelectionSize();
    }

    // -------------------- Factory methods --------------------

    /** Works only for nominal and integer attributes. If <i>k</i> is
     *  the number of classes, this method splits the example set into
     *  <i>k</i> subsets according to the value of the given
     *  attribute. 
     *  @yale.todo Could be implemented with a Condition
     */
    public static SplittedExampleSet splitByAttribute(ExampleSet exampleSet, Attribute attribute) {
	int offset = 0;

	if (Ontology.ATTRIBUTE_VALUE_TYPE.isA(attribute.getValueType(), Ontology.NOMINAL)) {
	    offset = Attribute.FIRST_CLASS_INDEX;
	} else if (Ontology.ATTRIBUTE_VALUE_TYPE.isA(attribute.getValueType(), Ontology.INTEGER)) {
	    offset = 0;
	} else {
	    throw new RuntimeException("ExampleSet can only be splitted by a nominal or integer attribute!");
	}

	int[] elements = new int[exampleSet.getSize()];
	ExampleReader reader = exampleSet.getExampleReader();
	int i = 0;
	int maxNumber = 0;
	while (reader.hasNext()) {
	    Example example = reader.next();
	    int value = (int)example.getValue(attribute) - offset;
	    maxNumber = Math.max(maxNumber, value);
	    elements[i++] = value;
	}

	Partition partition = new Partition(elements, maxNumber+1);

	return new SplittedExampleSet(exampleSet, partition);
    }

    /** Works only for numerical attributes. Splits the example set into two subsets according
     *  to the value of the given attribute.
     *  @yale.todo Could be implemented with a Condition
     */
    public static SplittedExampleSet splitByAttribute(ExampleSet exampleSet, Attribute attribute, double threshold) {

	if (!Ontology.ATTRIBUTE_VALUE_TYPE.isA(attribute.getValueType(), Ontology.NUMERICAL)) {
	    throw new RuntimeException("ExampleSet can only be splitted by a numerical attribute at threshold!");
	}

	int[] elements = new int[exampleSet.getSize()];
	ExampleReader reader = exampleSet.getExampleReader();
	int i = 0; 

	while (reader.hasNext()) {
	    Example example = reader.next();
	    if (example.getValue(attribute) <= threshold) 
		elements[i++] = 0;
	    else elements[i++] = 1;
	}

	Partition partition = new Partition(elements, 2);

	return new SplittedExampleSet(exampleSet, partition); 
    }

}

⌨️ 快捷键说明

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