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

📄 splittedexampleset.java

📁 著名的开源仿真软件yale
💻 JAVA
字号:
/* *  YALE - Yet Another Learning Environment *  Copyright (C) 2002, 2003 *      Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,  *          Katharina Morik, Oliver Ritthoff *      Artificial Intelligence Unit *      Computer Science Department *      University of Dortmund *      44221 Dortmund,  Germany *  email: yale@ls8.cs.uni-dortmund.de *  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. */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, 	     new Partition(new double[] {splitRatio,1-splitRatio}, exampleSet.getSize(), true));    }    /** Creates an example set that is splitted into <i>numberOfSubsets</i> parts. */    public SplittedExampleSet(ExampleSet exampleSet, int numberOfSubsets) {	this(exampleSet,	     new Partition(numberOfSubsets, exampleSet.getSize(), true));    }    /** 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,					    exampleSet.getSize(),					    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,					    exampleSet.getSize(),					    2);	return new SplittedExampleSet(exampleSet, partition);     }}

⌨️ 快捷键说明

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