Package com.linkedin.feathr.common
Class FeatureValue
java.lang.Object
com.linkedin.feathr.common.FeatureValue
- All Implemented Interfaces:
com.esotericsoftware.kryo.KryoSerializable,Serializable
public class FeatureValue
extends Object
implements Serializable, com.esotericsoftware.kryo.KryoSerializable
Describes the basic representation of a feature value in Feathr.
This is the OLD FeatureValue API. Feathr is migrating to a new FeatureValue API defined at
com.linkedin.feathr.common.value.FeatureValue. This implementation of the old API is a wrapper around the new API,
with lots of glue to maintain compatibility.
SERIALIZATION: For use with Spark RDD API, this class is serializable both by Java Object Serialization and by Kryo.
Note that because the serialized representation is based on NTV (name-term-value), serialization will NOT work for
tensor features having rank greater than 1.
MUTABILITY: For legacy compatibility, instances of this class are mutable under the following conditions:
1. When constructed with zero-argument constructor
2. When constructed with Map<String,Float> constructor
3. When constructed specifically as a term-vector using any createStringTermVector factory methods
4. When deserialized with Kryo or Java-Object Serialization
When mutable, instances may be mutated by:
1. Using modifier methods the Map objects returned by .getValue or .getAsTermVector
2. Using modifier methods on ._value public member field.
3. Using the .put method.
All mutability support in FeatureValue is DEPRECATED.
NOTE: This class will be deprecated soon in favor of
FeatureValue- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionDeprecated.FeatureValue(TypedTensor tensor) Constructor to create FeatureValue with a tensor objectFeatureValue(FeatureValue featureValueInternal) FeatureValue(Object value) Deprecated.Use the static factory methods such ascreateNumeric(Number)to construct features instead N.B.FeatureValue(Object value, FeatureTypes type) Deprecated.Use the static factory methods such ascreateNumeric(Number)to construct features insteadFeatureValue(Map<String, Float> value) Deprecated.Use the static factory methods such ascreateStringTermVector(Map)to construct features instead -
Method Summary
Modifier and TypeMethodDescriptionstatic FeatureValueCreates a boolean feature.static FeatureValuecreateCategorical(Character term) Creates a categorical feature from a character termstatic FeatureValueCreates a categorical feature from a string termstatic FeatureValuecreateCategorical(Number term) Deprecated.static FeatureValuecreateCharacterCategoricalSet(Collection<Character> characters) Creates a categorical set feature from character termsstatic <T extends Number>
FeatureValuecreateDenseVector(List<T> vec) Creates a dense vector feature from aListof numbersstatic <T extends Number>
FeatureValuecreateDenseVector(T[] vec) Creates a dense vector feature from an array of numbersstatic FeatureValuecreateNumeric(Number num) Creates a numeric feature.static <T extends Number>
FeatureValuecreateNumericCategoricalSet(Collection<T> terms) Creates a categorical set feature from a collection of Numbers that are whole numbersstatic <K extends Number,V extends Number>
FeatureValuecreateNumericTermVector(Collection<Map<K, V>> termValues) Creates a term vector feature (e.g.static <K extends Number,V extends Number>
FeatureValuecreateNumericTermVector(Map<K, V> inputMap) Creates a term vector feature (e.g.static <T extends CharSequence>
FeatureValuecreateStringCategoricalSet(Collection<T> terms) Creates a categorical set feature from string termsstatic <K extends CharSequence,V extends Number>
FeatureValuecreateStringTermVector(Collection<Map<K, V>> termValues) Creates a term vector feature (AKA sparse vector) from multiple term vectors with string termsstatic <K extends CharSequence,V extends Number>
FeatureValuecreateStringTermVector(Map<K, V> inputMap) Creates a term vector feature (aka sparse vector) using strings for the termsstatic FeatureValuecreateTensor(TensorData tensorData) Create a FeatureValue from a TensorData (adding a TensorType inferred from the primitives of the TensorData).static FeatureValuecreateTensor(Object value, TensorType type) Creates a (tensor) FeatureValue based on the provided TensorType.booleanGet feature value as booleanReturns the single categorical value associated with the feature Note: this method will returnStringeven if the feature was created usingcreateCategorical(Number)because categorical are meant to contain only discrete values which are represented by Stringget feature value as numberDeprecated.usegetAsCategorical()insteadGets the feature in an NTV term-vector representation.intDeprecated.getValue()Deprecated.Prefer to usegetAsTermVector()which has a more descriptive nameinthashCode()booleanisEmpty()Check if the FeatureValue is empty or not.voidDeprecated.mutability in FeatureValue is deprecated; see class-level Javadocvoidread(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Input input) intsize()Deprecated.Prefer to usegetNumTerms()which has a more descriptive nametoString()voidwrite(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Output output)
-
Field Details
-
DEFAULT_VALUE
public static final float DEFAULT_VALUE- See Also:
-
EMPTY_TERM
- See Also:
-
-
Constructor Details
-
FeatureValue
-
FeatureValue
Deprecated.Use the static factory methods such ascreateNumeric(Number)to construct features instead N.B. Although this takesObject, there are runtime type checks to ensure the passed type can be converted to a feature value. -
FeatureValue
Deprecated.Use the static factory methods such ascreateNumeric(Number)to construct features instead -
FeatureValue
Deprecated.Use the static factory methods such ascreateNumeric(Number)to construct features instead -
FeatureValue
Constructor to create FeatureValue with a tensor object- Parameters:
tensor-TypedTensor- Throws:
NullPointerException- if tensor parameter is null
-
FeatureValue
Deprecated.Use the static factory methods such ascreateStringTermVector(Map)to construct features instead
-
-
Method Details
-
put
Deprecated.mutability in FeatureValue is deprecated; see class-level JavadocModify a term-vector FeatureValue by adding a term-value mapping. Mutability is only allowed under certain conditions, described in the class-level Javadoc.- Throws:
RuntimeException- if not mutable
-
isEmpty
public boolean isEmpty()Check if the FeatureValue is empty or not. When it's empty, it return true; otherwise false. Emptiness of FeatureValue is defined in the following way: 1. For tensor based features, empty means an tensor. 2. For legacy term vector based features, empty means an empty vector. Note: Since Boolean NTV feature false is equivalent to empty map, so it's false is considered as empty as well. -
size
Deprecated.Prefer to usegetNumTerms()which has a more descriptive name- Throws:
RuntimeException- if this instance stores a tensor of rank greater than 1
-
getValue
Deprecated.Prefer to usegetAsTermVector()which has a more descriptive name -
getNumTerms
Deprecated.Get the number of terms in feature when interpreted as a term vector.- For a numeric feature, this will always be 1
- For a categorical feature, this will always be 1
- For a categorical set feature, this will be the number of terms in the set
- For any vector feature, it will be the number of explicitly present elements in the vector
- Returns:
- the number of terms in this feature
- Throws:
RuntimeException- if this instance stores a tensor of rank greater than 1
-
getFeatureType
-
getAsTypedTensor
- Returns:
- a Quince TypedTensor representation of this feature value
-
getAsTensorData
- Returns:
- a Quince TensorData representation of this feature value
-
getAsTermVector
Gets the feature in an NTV term-vector representation.- Returns:
MapfromStringterm names toFloatvalues- Throws:
RuntimeException- if the feature is a tensor of rank greater than 1, then this feature cannot be represented as an NTV term vector
-
getAsNumeric
get feature value as number- Returns:
- number value
- Throws:
RuntimeException- if this is not a valid numeric feature
-
getAsString
Deprecated.usegetAsCategorical()insteadGets a CATEGORICAL feature value represented as a string.- Returns:
- string value
-
getAsCategorical
Returns the single categorical value associated with the feature Note: this method will returnStringeven if the feature was created usingcreateCategorical(Number)because categorical are meant to contain only discrete values which are represented by String- Returns:
- the categorical term as a String
- Throws:
RuntimeException- if this is not a valid categorical feature
-
getAsBoolean
Get feature value as boolean- Returns:
- boolean value
- Throws:
RuntimeException- if this is not a valid boolean feature
-
createNumeric
Creates a numeric feature.- Parameters:
num-Number- Returns:
FeatureValue
-
createCategorical
Deprecated.Creates a categorical feature using a whole number term which is within some precision of a whole number. The whole number term will be stored as aString. Only whole number terms are accepted because categorical are meant to contain discrete values. The common use case for using number as the term would be to represent some entity ID (e.g. y, x)- Parameters:
term-Numberthe numeric term which will be stored as aStringterm- Returns:
- a new instance of
FeatureValue - Throws:
RuntimeException- if num is not within some precision of a whole number
-
createCategorical
Creates a categorical feature from a string term- Parameters:
term-CharSequence- Returns:
- a new instance of
FeatureValue
-
createCategorical
Creates a categorical feature from a character term- Parameters:
term-Character- Returns:
- a new instance of
FeatureValue
-
createDenseVector
Creates a dense vector feature from aListof numbers- Type Parameters:
T- the type ofNumber- Parameters:
vec-List- Returns:
- a new instance of
FeatureValue
-
createDenseVector
Creates a dense vector feature from an array of numbers- Type Parameters:
T- the type ofNumber- Parameters:
vec-FeatureValuearray of items- Returns:
- a new instance of
FeatureValue
-
createStringTermVector
public static <K extends CharSequence,V extends Number> FeatureValue createStringTermVector(Map<K, V> inputMap) Creates a term vector feature (aka sparse vector) using strings for the terms- Type Parameters:
K- the string term typeV- the numeric value type- Parameters:
inputMap-Mapfrom a string term to numeric values- Returns:
- a new instance of
FeatureValue
-
createNumericTermVector
public static <K extends Number,V extends Number> FeatureValue createNumericTermVector(Map<K, V> inputMap) Creates a term vector feature (e.g. sparse vector) using a numeric to numeric map- Type Parameters:
K- the numeric term typeV- the numeric value type- Parameters:
inputMap-Mapfrom a number term (within some precision of a whole numbers) to numeric value- Returns:
- a new instance of
FeatureValue - Throws:
RuntimeException- if input numeric key is a not within some precision of a whole numbers (e.g. 1.5f)RuntimeException- if multiple input numeric keys map to the same whole number (e.g. 1.00f and 1.0000d)
-
createStringTermVector
public static <K extends CharSequence,V extends Number> FeatureValue createStringTermVector(Collection<Map<K, V>> termValues) Creates a term vector feature (AKA sparse vector) from multiple term vectors with string terms- Type Parameters:
K- the string term typeV- the numeric value type- Parameters:
termValues-CollectionofMapfrom string term to numeric value- Returns:
- a new instance of
FeatureValue - Throws:
RuntimeException- if input numeric key is a not within some precision of a whole numbersRuntimeException- if multiple input numeric keys map to the same whole number (e.g. 1.00f and 1.0000d)
-
createNumericTermVector
public static <K extends Number,V extends Number> FeatureValue createNumericTermVector(Collection<Map<K, V>> termValues) Creates a term vector feature (e.g. sparse vector) by merging multiple numeric to numeric map- Type Parameters:
K- the numeric term typeV- the numeric value type- Parameters:
termValues-Mapfrom a number term to numeric value- Returns:
- a new instance of
FeatureValue - Throws:
RuntimeException- if input numeric keys are not within some precision of a whole numbers (e.g. 1.5f)RuntimeException- if multiple input numeric keys map to the same whole number (e.g. 1.00f and 1.0000d)
-
createNumericCategoricalSet
Creates a categorical set feature from a collection of Numbers that are whole numbers- Type Parameters:
T- the numeric type- Parameters:
terms-CollectionofNumberterms- Returns:
- a new instance of
FeatureValue - Throws:
RuntimeException- if input numeric elements are not within some precision of a whole numbers (e.g. 1.5f)
-
createStringCategoricalSet
Creates a categorical set feature from string terms- Type Parameters:
T- the string term type- Parameters:
terms-CollectionofCharSequenceterms- Returns:
- a new instance of
FeatureValue
-
createCharacterCategoricalSet
Creates a categorical set feature from character terms- Parameters:
characters-CollectionofCharacter- Returns:
- a new instance of
FeatureValue
-
createBoolean
Creates a boolean feature.- Parameters:
b-Boolean- Returns:
- a new instance of
FeatureValue
-
createTensor
Creates a (tensor) FeatureValue based on the provided TensorType.- Parameters:
value- The data used to creates the the tensor.type- The corresponding type needed to create the tensor.- Throws:
IllegalArgumentException- if the data type is not supported or if create a 2-d or higher dimension tensor.
-
createTensor
Create a FeatureValue from a TensorData (adding a TensorType inferred from the primitives of the TensorData). NOTE that the TensorType will not have any shape, so may be NOT reused for constructing new dense tensors from arrays and lists. -
toString
-
equals
-
hashCode
public int hashCode() -
write
public void write(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Output output) - Specified by:
writein interfacecom.esotericsoftware.kryo.KryoSerializable
-
read
public void read(com.esotericsoftware.kryo.Kryo kryo, com.esotericsoftware.kryo.io.Input input) - Specified by:
readin interfacecom.esotericsoftware.kryo.KryoSerializable
-
createNumeric(Number)to construct features instead