Class Example<T extends Output<T>>
java.lang.Object
org.tribuo.Example<T>
- Type Parameters:
T- The type of output that this example contains.
- All Implemented Interfaces:
Serializable,Iterable<Feature>
- Direct Known Subclasses:
ArrayExample,BinaryFeaturesExample,ListExample
public abstract class Example<T extends Output<T>>
extends Object
implements Iterable<Feature>, Serializable
An example used for training and evaluation. Examples have a true output
associated with them or an instance from
OutputFactory.getUnknownOutput()
and a list of features that can be used for prediction.
An example is a sorted set of features, sorted by the String comparator on the feature name.
Examples have metadata associated with them, stored as a map from a String key, to an Object value. This metadata is append only for any given example, and the metadata values should be immutable (as they will be referenced rather than copied when an example is copied).
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final intThe default initial size of the metadata map.static final floatThe default weight.The example metadata.static final StringBy convention the example name is stored using this metadata key.protected final TThe output associated with this example.protected floatThe weight associated with this example. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCopies the output, weight and metadata into this example.protectedConstruct an empty example using the supplied output andDEFAULT_WEIGHTas the weight.protectedConstruct an empty example using the supplied output and weight.protectedConstruct an empty example using the supplied output, weight and metadata.protectedConstruct an empty example using the supplied output, metadata andDEFAULT_WEIGHTas the weight. -
Method Summary
Modifier and TypeMethodDescriptionabstract voidAdds a feature.abstract voidaddAll(Collection<? extends Feature> features) Adds a collection of features.abstract voidcanonicalize(FeatureMap featureMap) Reassigns feature name Strings in the Example to point to those in theFeatureMap.booleancontainsMetadata(String key) Test if the metadata contains the supplied key.copy()Returns a deep copy of this Example.protected abstract voidConverts all implicit zeros into explicit zeros based on the supplied feature names.protected voiddensify(FeatureMap fMap) Converts all implicit zeros into explicit zeros based on the supplied feature map.Returns a copy of this example's metadata.getMetadataValue(String key) Gets the associated metadata value for this key, if it exists.Gets the example'sOutput.floatGets the example's weight.Returns the Feature in this Example which has the supplied name, if it's present.abstract voidreduceByName(Merger merger) Merges features with the same name using the suppliedMerger.abstract voidremoveFeatures(List<Feature> featureList) Removes all features in this list from the Example.abstract voidOverwrites the feature with the matching name.voidsetMetadataValue(String key, Object value) Puts the specified key, value pair into the metadata.voidsetWeight(float weight) Sets the example's weight.abstract intsize()Return how many features are in this example.protected abstract voidsort()Sorts the example by the string comparator.voidtransform(TransformerMap transformerMap) Transforms this example by applying the transformations from the suppliedTransformerMap.abstract booleanChecks the example to see if all the feature names are unique, the feature values are not NaN, and there is at least one feature.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, iterator, spliterator
-
Field Details
-
DEFAULT_METADATA_SIZE
The default initial size of the metadata map.- See Also:
-
DEFAULT_WEIGHT
-
NAME
-
output
-
weight
The weight associated with this example. -
metadata
-
-
Constructor Details
-
Example
-
Example
-
Example
-
Example
Construct an empty example using the supplied output andDEFAULT_WEIGHTas the weight.- Parameters:
output- The output.
-
Example
-
-
Method Details
-
getOutput
-
getWeight
-
setWeight
-
getMetadataValue
Gets the associated metadata value for this key, if it exists. Otherwise returnOptional.empty().- Parameters:
key- The key to check.- Returns:
- The value if present.
-
setMetadataValue
Puts the specified key, value pair into the metadata.Example metadata is append only, and so this method throws
IllegalArgumentExceptionif the key is already present.- Parameters:
key- The key.value- The value.
-
containsMetadata
Test if the metadata contains the supplied key.- Parameters:
key- The key to test.- Returns:
- True if the metadata contains a value for the supplied key.
-
getMetadata
Returns a copy of this example's metadata.- Returns:
- The metadata.
-
sort
Sorts the example by the string comparator. -
add
-
addAll
Adds a collection of features. This maintains the sorted invariant but is more efficient than adding one at a time due to allocation.- Parameters:
features- The features to add.
-
size
Return how many features are in this example.- Returns:
- The number of features.
-
removeFeatures
Removes all features in this list from the Example.- Parameters:
featureList- Features to remove from this Example.
-
reduceByName
Merges features with the same name using the suppliedMerger.- Parameters:
merger- A function to merge two doubles.
-
validateExample
Checks the example to see if all the feature names are unique, the feature values are not NaN, and there is at least one feature.- Returns:
- true if the example is valid.
-
transform
Transforms this example by applying the transformations from the suppliedTransformerMap.Can be overridden for performance reasons.
- Parameters:
transformerMap- The transformations to apply.
-
densify
Converts all implicit zeros into explicit zeros based on the supplied feature map.- Parameters:
fMap- The feature map to use for densification.
-
densify
-
copy
-
lookup
-
set
Overwrites the feature with the matching name.Throws
IllegalArgumentExceptionif there isn't a feature with that name in this example.- Parameters:
feature- The new feature value.
-
canonicalize
Reassigns feature name Strings in the Example to point to those in theFeatureMap. This significantly reduces memory allocation. It is called when an Example is added to aMutableDataset, and should not be called outside of that context as it may interact unexpectedly withHashedFeatureMap.- Parameters:
featureMap- The feature map containing canonical feature names.
-