/* * RapidMiner * * Copyright (C) 2001-2007 by Rapid-I and the contributors * * Complete list of developers available at our web site: * * http://rapid-i.com * * 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 com.rapidminer.operator.preprocessing.sampling; import java.util.Arrays; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import com.rapidminer.example.Attribute; import com.rapidminer.example.AttributeRole; import com.rapidminer.example.Example; import com.rapidminer.example.ExampleSet; import com.rapidminer.example.set.SimpleExampleSet; import com.rapidminer.example.set.SplittedExampleSet; import com.rapidminer.example.table.DataRow; import com.rapidminer.example.table.ExampleTable; import com.rapidminer.example.table.ListDataRowReader; import com.rapidminer.example.table.MemoryExampleTable; import com.rapidminer.operator.IOObject; import com.rapidminer.operator.Operator; import com.rapidminer.operator.OperatorDescription; import com.rapidminer.operator.OperatorException; import com.rapidminer.parameter.ParameterType; import com.rapidminer.parameter.ParameterTypeDouble; import com.rapidminer.parameter.ParameterTypeInt; /** * Stratified sampling operator. This operator performs a random sampling of a * given fraction. In contrast to the simple sampling operator, this operator * performs a stratified sampling for data sets with nominal label attributes, * i.e. the class distributions remains (almost) the same after sampling. Hence, * this operator cannot be applied on data sets without a label or with a * numerical label. In these cases a simple sampling without stratification * is performed. * * @author Ingo Mierswa * @version $Id: StratifiedSampling.java,v 1.2 2006/04/05 08:57:27 ingomierswa * Exp $ */ public class StratifiedSampling extends Operator { /** The parameter name for "The fraction of examples which should be sampled" */ public static final String PARAMETER_SAMPLE_RATIO = "sample_ratio"; /** The parameter name for "Use the given random seed instead of global random numbers (-1: use global)." */ public static final String PARAMETER_LOCAL_RANDOM_SEED = "local_random_seed"; public StratifiedSampling(OperatorDescription description) { super(description); } public IOObject[] apply() throws OperatorException { ExampleSet exampleSet = getInput(ExampleSet.class); // perform stratified sampling SplittedExampleSet splittedExampleSet = new SplittedExampleSet(exampleSet, getParameterAsDouble(PARAMETER_SAMPLE_RATIO), SplittedExampleSet.STRATIFIED_SAMPLING, getParameterAsInt(PARAMETER_LOCAL_RANDOM_SEED)); splittedExampleSet.selectSingleSubset(0); // fill new table List<DataRow> dataList = new LinkedList<DataRow>(); Iterator<Example> reader = splittedExampleSet.iterator(); while (reader.hasNext()) { Example example = reader.next(); dataList.add(example.getDataRow()); checkForStop(); } List<Attribute> attributes = Arrays.asList(splittedExampleSet.getExampleTable().getAttributes()); ExampleTable exampleTable = new MemoryExampleTable(attributes, new ListDataRowReader(dataList.iterator())); // regular attributes List<Attribute> regularAttributes = new LinkedList<Attribute>(); for (Attribute attribute : exampleSet.getAttributes()) { regularAttributes.add(attribute); } // special attributes ExampleSet result = new SimpleExampleSet(exampleTable, regularAttributes); Iterator<AttributeRole> special = exampleSet.getAttributes().specialAttributes(); while (special.hasNext()) { AttributeRole role = special.next(); result.getAttributes().setSpecialAttribute(role.getAttribute(), role.getSpecialName()); } return new IOObject[] { result }; } public Class[] getInputClasses() { return new Class[] { ExampleSet.class }; } public Class[] getOutputClasses() { return new Class[] { ExampleSet.class }; } public List<ParameterType> getParameterTypes() { List<ParameterType> types = super.getParameterTypes(); ParameterType type = new ParameterTypeDouble(PARAMETER_SAMPLE_RATIO, "The fraction of examples which should be sampled", 0.0d, 1.0d, 0.1d); type.setExpert(false); types.add(type); types.add(new ParameterTypeInt(PARAMETER_LOCAL_RANDOM_SEED, "Use the given random seed instead of global random numbers (-1: use global).", -1, Integer.MAX_VALUE, -1)); return types; } }