use of org.tribuo.dataset.MinimumCardinalityDataset in project tribuo by oracle.
the class DataOptions method load.
/**
* Loads the training and testing data from {@link #trainingPath} and {@link #testingPath}
* according to the other parameters specified in this class.
* @param outputFactory The output factory to use to process the inputs.
* @param <T> The dataset output type.
* @return A pair containing the training and testing datasets. The training dataset is element 'A' and the
* testing dataset is element 'B'.
* @throws IOException If the paths could not be loaded.
*/
public <T extends Output<T>> Pair<Dataset<T>, Dataset<T>> load(OutputFactory<T> outputFactory) throws IOException {
logger.info(String.format("Loading data from %s", trainingPath));
Dataset<T> train;
Dataset<T> test;
char separator;
switch(inputFormat) {
case SERIALIZED:
//
// Load Tribuo serialised datasets.
logger.info("Deserialising dataset from " + trainingPath);
try (ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(trainingPath.toFile())));
ObjectInputStream oits = new ObjectInputStream(new BufferedInputStream(new FileInputStream(testingPath.toFile())))) {
@SuppressWarnings("unchecked") Dataset<T> tmp = (Dataset<T>) ois.readObject();
train = tmp;
if (minCount > 0) {
logger.info("Found " + train.getFeatureIDMap().size() + " features");
logger.info("Removing features that occur fewer than " + minCount + " times.");
train = new MinimumCardinalityDataset<>(train, minCount);
}
logger.info(String.format("Loaded %d training examples for %s", train.size(), train.getOutputs().toString()));
logger.info("Found " + train.getFeatureIDMap().size() + " features, and " + train.getOutputInfo().size() + " response dimensions");
@SuppressWarnings("unchecked") Dataset<T> deserTest = (Dataset<T>) oits.readObject();
test = new ImmutableDataset<>(deserTest, deserTest.getSourceProvenance(), deserTest.getOutputFactory(), train.getFeatureIDMap(), train.getOutputIDInfo(), true);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Unknown class in serialised files", e);
}
break;
case LIBSVM:
//
// Load the libsvm text-based data format.
LibSVMDataSource<T> trainSVMSource = new LibSVMDataSource<>(trainingPath, outputFactory);
train = new MutableDataset<>(trainSVMSource);
boolean zeroIndexed = trainSVMSource.isZeroIndexed();
int maxFeatureID = trainSVMSource.getMaxFeatureID();
if (minCount > 0) {
logger.info("Removing features that occur fewer than " + minCount + " times.");
train = new MinimumCardinalityDataset<>(train, minCount);
}
logger.info(String.format("Loaded %d training examples for %s", train.size(), train.getOutputs().toString()));
logger.info("Found " + train.getFeatureIDMap().size() + " features, and " + train.getOutputInfo().size() + " response dimensions");
test = new ImmutableDataset<>(new LibSVMDataSource<>(testingPath, outputFactory, zeroIndexed, maxFeatureID), train.getFeatureIDMap(), train.getOutputIDInfo(), false);
break;
case TEXT:
//
// Using a simple Java break iterator to generate ngram features.
TextFeatureExtractor<T> extractor;
if (hashDim > 0) {
extractor = new TextFeatureExtractorImpl<>(new TokenPipeline(new BreakIteratorTokenizer(Locale.US), ngram, termCounting, hashDim));
} else {
extractor = new TextFeatureExtractorImpl<>(new TokenPipeline(new BreakIteratorTokenizer(Locale.US), ngram, termCounting));
}
TextDataSource<T> trainSource = new SimpleTextDataSource<>(trainingPath, outputFactory, extractor);
train = new MutableDataset<>(trainSource);
if (minCount > 0) {
logger.info("Removing features that occur fewer than " + minCount + " times.");
train = new MinimumCardinalityDataset<>(train, minCount);
}
logger.info(String.format("Loaded %d training examples for %s", train.size(), train.getOutputs().toString()));
logger.info("Found " + train.getFeatureIDMap().size() + " features, and " + train.getOutputInfo().size() + " response dimensions");
TextDataSource<T> testSource = new SimpleTextDataSource<>(testingPath, outputFactory, extractor);
test = new ImmutableDataset<>(testSource, train.getFeatureIDMap(), train.getOutputIDInfo(), false);
break;
case CSV:
// Load the data using the simple CSV loader
if (csvResponseName == null) {
throw new IllegalArgumentException("Please supply a response column name");
}
separator = delimiter.value;
CSVLoader<T> loader = new CSVLoader<>(separator, outputFactory);
train = new MutableDataset<>(loader.loadDataSource(trainingPath, csvResponseName));
logger.info(String.format("Loaded %d training examples for %s", train.size(), train.getOutputs().toString()));
logger.info("Found " + train.getFeatureIDMap().size() + " features, and " + train.getOutputInfo().size() + " response dimensions");
test = new MutableDataset<>(loader.loadDataSource(testingPath, csvResponseName));
break;
case COLUMNAR:
if (rowProcessor == null) {
throw new IllegalArgumentException("Please supply a RowProcessor");
}
OutputFactory<?> rowOutputFactory = rowProcessor.getResponseProcessor().getOutputFactory();
if (!rowOutputFactory.equals(outputFactory)) {
throw new IllegalArgumentException("The RowProcessor doesn't use the same kind of OutputFactory as the one supplied. RowProcessor has " + rowOutputFactory.getClass().getSimpleName() + ", supplied " + outputFactory.getClass().getName());
}
// checked by the if statement above
@SuppressWarnings("unchecked") RowProcessor<T> typedRowProcessor = (RowProcessor<T>) rowProcessor;
separator = delimiter.value;
train = new MutableDataset<>(new CSVDataSource<>(trainingPath, typedRowProcessor, true, separator, csvQuoteChar));
logger.info(String.format("Loaded %d training examples for %s", train.size(), train.getOutputs().toString()));
logger.info("Found " + train.getFeatureIDMap().size() + " features, and " + train.getOutputInfo().size() + " response dimensions");
test = new MutableDataset<>(new CSVDataSource<>(testingPath, typedRowProcessor, true, separator, csvQuoteChar));
break;
default:
throw new IllegalArgumentException("Unsupported input format " + inputFormat);
}
logger.info(String.format("Loaded %d testing examples", test.size()));
if (scaleFeatures) {
logger.info("Fitting feature scaling");
TransformationMap map = new TransformationMap(Collections.singletonList(new LinearScalingTransformation()));
TransformerMap transformers = train.createTransformers(map, scaleIncZeros);
logger.info("Applying scaling to training dataset");
train = transformers.transformDataset(train);
logger.info("Applying scaling to testing dataset");
test = transformers.transformDataset(test);
}
return new Pair<>(train, test);
}
use of org.tribuo.dataset.MinimumCardinalityDataset in project tribuo by oracle.
the class DatasetTest method testMinimumCardinality.
@Test
public void testMinimumCardinality() {
MutableDataset<MockOutput> dataset = new MutableDataset<>(new MockDataSourceProvenance(), new MockOutputFactory());
Example<MockOutput> ex1 = new ListExample<>(new MockOutput("green"));
ex1.add(new Feature("f1", 1.0));
ex1.add(new Feature("f2", 0.0));
ex1.add(new Feature("f3", 1.0));
dataset.add(ex1);
Example<MockOutput> ex2 = new ListExample<>(new MockOutput("green"));
ex2.add(new Feature("f1", 1.0));
ex2.add(new Feature("f2", 0.0));
ex2.add(new Feature("f3", 1.0));
dataset.add(ex2);
Example<MockOutput> ex3 = new ListExample<>(new MockOutput("blue"));
ex3.add(new Feature("f1", 1.0));
ex3.add(new Feature("f2", 0.0));
ex3.add(new Feature("f4", 1.0));
dataset.add(ex3);
Example<MockOutput> ex4 = new ListExample<>(new MockOutput("green"));
ex4.add(new Feature("f1", 1.0));
ex4.add(new Feature("f4", 0.0));
ex4.add(new Feature("f5", 1.0));
dataset.add(ex4);
FeatureMap infoMap = dataset.getFeatureIDMap();
assertEquals(4, infoMap.get("f1").getCount());
assertEquals(0, infoMap.get("f2").getCount());
assertEquals(2, infoMap.get("f3").getCount());
// One as non-sparse zeros are ignored
assertEquals(1, infoMap.get("f4").getCount());
assertEquals(1, infoMap.get("f5").getCount());
Dataset<MockOutput> prunedDataset = new MinimumCardinalityDataset<>(dataset, 2);
infoMap = prunedDataset.getFeatureIDMap();
assertEquals(4, infoMap.get("f1").getCount());
assertNull(infoMap.get("f2"));
assertEquals(2, infoMap.get("f3").getCount());
assertNull(infoMap.get("f4"));
assertNull(infoMap.get("f5"));
ex2 = prunedDataset.getExample(1);
Feature f1 = ex2.lookup("f1");
assertEquals(1.0, f1.getValue(), 1e-5);
assertNull(ex2.lookup("f5"));
prunedDataset = new MinimumCardinalityDataset<>(dataset, 3);
assertEquals(4, prunedDataset.size());
infoMap = prunedDataset.getFeatureIDMap();
assertEquals(4, infoMap.get("f1").getCount());
assertNull(infoMap.get("f2"));
assertNull(infoMap.get("f3"));
assertNull(infoMap.get("f4"));
assertNull(infoMap.get("f5"));
// no examples make it through the pruning
prunedDataset = new MinimumCardinalityDataset<>(dataset, 5);
assertEquals(0, prunedDataset.size());
infoMap = prunedDataset.getFeatureIDMap();
assertNull(infoMap.get("f1"));
assertNull(infoMap.get("f2"));
assertNull(infoMap.get("f3"));
assertNull(infoMap.get("f4"));
assertNull(infoMap.get("f5"));
}
Aggregations