Java Code Examples for weka.core.Instances#clear()

The following examples show how to use weka.core.Instances#clear() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WekaUtil.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
public static Map<String, Instances> getInstancesPerClass(final Instances data) {
	Instances emptyInstances = new Instances(data);
	emptyInstances.clear();
	Map<String, Instances> classWiseSeparation = new HashMap<>();
	for (Instance i : data) {
		String assignedClass = data.classAttribute().value((int) i.classValue());
		if (!classWiseSeparation.containsKey(assignedClass)) {
			Instances inst = new Instances(emptyInstances);
			classWiseSeparation.put(assignedClass, inst);
		}
		classWiseSeparation.get(assignedClass).add(i);
	}
	return classWiseSeparation;
}
 
Example 2
Source File: WekaUtil.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
public static Collection<Integer>[] getArbitrarySplit(final IWekaInstances data, final Random rand, final double... portions) {

		/* check that portions sum up to s.th. smaller than 1 */
		double sum = 0;
		for (double p : portions) {
			sum += p;
		}
		if (sum > 1) {
			throw new IllegalArgumentException(MSG_SUM1);
		}

		LinkedList<Integer> indices = new LinkedList<>(ContiguousSet.create(Range.closed(0, data.size() - 1), DiscreteDomain.integers()).asList());
		Collections.shuffle(indices, rand);

		@SuppressWarnings("unchecked")
		Collection<Integer>[] folds = new ArrayList[portions.length + 1];
		Instances emptyInstances = new Instances(data.getList());
		emptyInstances.clear();

		/* distribute instances over the folds */
		for (int i = 0; i <= portions.length; i++) {
			double portion = i < portions.length ? portions[i] : 1 - sum;
			int numberOfItems = (int) Math.floor(data.size() * portion);
			Collection<Integer> fold = new ArrayList<>(numberOfItems);
			for (int j = 0; j < numberOfItems; j++) {
				fold.add(indices.poll());
			}
			folds[i] = fold;
		}

		/* distribute remaining ones over the folds */
		while (!indices.isEmpty()) {
			folds[rand.nextInt(folds.length)].add(indices.poll());
		}

		if (debug && Arrays.asList(folds).stream().mapToInt(Collection::size).sum() != data.size()) {
			throw new IllegalStateException(MSG_DEVIATING_NUMBER_OF_INSTANCES);
		}
		return folds;
	}
 
Example 3
Source File: WekaUtil.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
public static Instance useFilterOnSingleInstance(final Instance instance, final Filter filter) throws Exception {
	Instances data = new Instances(instance.dataset());
	data.clear();
	data.add(instance);
	Instances filteredInstances = Filter.useFilter(data, filter);
	return filteredInstances.firstInstance();
}
 
Example 4
Source File: FeatureGeneratorTree.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Instance apply(final Instance data) throws PreprocessingException {
	try {
		Instances instances = new Instances(data.dataset());
		instances.clear();
		instances.add(data);
		return this.apply(instances).firstInstance();
	} catch (Exception e) {
		throw new PreprocessingException(e);
	}
}
 
Example 5
Source File: Standardization.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Instances apply(final Instances data) throws PreprocessingException {
	Instances newInstances = new Instances(data);
	newInstances.clear();
	for (Instance i : data) {
		newInstances.add(this.apply(i));
	}
	return newInstances;
}
 
Example 6
Source File: Normalization.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Instances apply(final Instances data) throws PreprocessingException {
	Instances newInstances = new Instances(data);
	newInstances.clear();
	for (Instance i : data) {
		newInstances.add(this.apply(i));
	}
	return newInstances;
}