Class SparseVector
- All Implemented Interfaces:
Serializable
,Iterable<VectorTuple>
,SGDVector
,Tensor
Uses binary search to look up a specific index, so it's usually faster to use the iterator to iterate the values.
This vector has immutable indices. It cannot get new indices after construction,
and will throw IllegalArgumentException
if such an operation is tried.
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(int index, double value) Addsvalue
to the element atindex
.Addsother
to this vector, producing a newSGDVector
.copy()
Returns a deep copy of this vector.static SparseVector
createSparseVector
(int dimension, int[] indices, double[] values) Defensively copies the input, and checks that the indices are sorted.static SparseVector
createSparseVector
(int dimension, Map<Integer, Double> indexMap) Builds a SparseVector from a map.static <T extends Output<T>>
SparseVectorcreateSparseVector
(Example<T> example, ImmutableFeatureMap featureInfo, boolean addBias) Builds aSparseVector
from anExample
.int[]
difference
(SparseVector other) Generates an array of the indices that are active in this vector but are not present inother
.double
distance
(SGDVector other, DoubleUnaryOperator transformFunc, DoubleUnaryOperator normalizeFunc) double
Calculates the dot product between this vector andother
.boolean
Equals is defined mathematically, that is two SGDVectors are equal iff they have the same indices and the same values at those indices.double
euclideanDistance
(SGDVector other) The l2 or euclidean distance between this vector and the other vector.void
Applies aDoubleUnaryOperator
elementwise to thisTensor
.double
get
(int index) Gets an element from this vector.int[]
getShape()
Returns an int array specifying the shape of thisTensor
.void
hadamardProductInPlace
(Tensor other, DoubleUnaryOperator f) Updates thisTensor
with the Hadamard product (i.e., a term by term multiply) of this andother
.int
hashCode()
int
Returns the index of the maximum value.void
intersectAndAddInPlace
(Tensor other, DoubleUnaryOperator f) Updates thisTensor
by adding all the values from the intersection withother
.int[]
intersection
(SparseVector other) Generates an array of the indices that are active in both this vector andother
iterator()
double
l1Distance
(SGDVector other) The l1 or Manhattan distance between this vector and the other vector.double
maxValue()
Returns the maximum value.double
minValue()
Returns the minimum value.void
normalize
(VectorNormalizer normalizer) Normalizes the vector using the supplied vector normalizer.int
Returns the number of non-zero elements (on construction, an element could be set to zero and it would still remain active).double
oneNorm()
Calculates the Manhattan norm for this vector.This generates the outer product when dotted with anotherSparseVector
.reshape
(int[] newShape) Reshapes the Tensor to the supplied shape.scale
(double coefficient) Generates a new vector with each element scaled bycoefficient
.void
set
(int index, double value) Sets theindex
to thevalue
.int
size()
Returns the dimensionality of this vector.Subtractsother
from this vector, producing a newSGDVector
.double
sum()
Calculates the sum of this vector.double[]
toString()
static <T extends Output<T>>
SparseVector[]Converts a dataset of row-major examples into an array of column-major sparse vectors.static <T extends Output<T>>
SparseVector[]transpose
(Dataset<T> dataset, ImmutableFeatureMap fMap) Converts a dataset of row-major examples into an array of column-major sparse vectors.static SparseVector[]
transpose
(SparseVector[] input) Transposes an array of sparse vectors from row-major to column-major or vice versa.double
twoNorm()
Calculates the euclidean norm for this vector.double
variance
(double mean) Calculates the variance of this vector based on the supplied mean.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
Methods inherited from interface org.tribuo.math.la.SGDVector
cosineDistance, cosineSimilarity, l2Distance, variance
Methods inherited from interface org.tribuo.math.la.Tensor
hadamardProductInPlace, intersectAndAddInPlace, scalarAddInPlace, scaleInPlace
-
Field Details
-
indices
-
values
-
-
Constructor Details
-
SparseVector
-
-
Method Details
-
createSparseVector
public static <T extends Output<T>> SparseVector createSparseVector(Example<T> example, ImmutableFeatureMap featureInfo, boolean addBias) Builds aSparseVector
from anExample
.Used in training and inference.
Throws
IllegalArgumentException
if the Example contains NaN-valued features.- Type Parameters:
T
- The type parameter of theexample
.- Parameters:
example
- The example to convert.featureInfo
- The feature information, used to calculate the dimension of this SparseVector.addBias
- Add a bias feature.- Returns:
- A SparseVector representing the example's features.
-
createSparseVector
Defensively copies the input, and checks that the indices are sorted. If not, it sorts them.Throws
IllegalArgumentException
if the arrays are not the same length, or if size is less than the max index.- Parameters:
dimension
- The dimension of this vector.indices
- The indices of the non-zero elements.values
- The values of the non-zero elements.- Returns:
- A SparseVector encapsulating the indices and values.
-
createSparseVector
Builds a SparseVector from a map.Throws
IllegalArgumentException
if dimension is less than the max index.- Parameters:
dimension
- The dimension of this vector.indexMap
- The map from indices to values.- Returns:
- A SparseVector.
-
copy
Description copied from interface:SGDVector
Returns a deep copy of this vector. -
getShape
-
reshape
Description copied from interface:Tensor
Reshapes the Tensor to the supplied shape. ThrowsIllegalArgumentException
if the shape isn't compatible. -
size
-
numActiveElements
Description copied from interface:SGDVector
Returns the number of non-zero elements (on construction, an element could be set to zero and it would still remain active).- Specified by:
numActiveElements
in interfaceSGDVector
- Returns:
- The number of non-zero elements.
-
equals
Equals is defined mathematically, that is two SGDVectors are equal iff they have the same indices and the same values at those indices. -
hashCode
-
add
Addsother
to this vector, producing a newSGDVector
. Ifother
is aSparseVector
then the returned vector is also aSparseVector
otherwise it's aDenseVector
. -
subtract
Subtractsother
from this vector, producing a newSGDVector
. Ifother
is aSparseVector
then the returned vector is also aSparseVector
otherwise it's aDenseVector
. -
intersectAndAddInPlace
Description copied from interface:Tensor
Updates thisTensor
by adding all the values from the intersection withother
.The function
f
is applied to all values fromother
before the addition.Each value is updated as value += f(otherValue).
- Specified by:
intersectAndAddInPlace
in interfaceTensor
- Parameters:
other
- The otherTensor
.f
- A function to apply.
-
hadamardProductInPlace
Description copied from interface:Tensor
Updates thisTensor
with the Hadamard product (i.e., a term by term multiply) of this andother
.The function
f
is applied to all values fromother
before the addition.Each value is updated as value *= f(otherValue).
- Specified by:
hadamardProductInPlace
in interfaceTensor
- Parameters:
other
- The otherTensor
.f
- A function to apply.
-
foreachInPlace
Description copied from interface:Tensor
Applies aDoubleUnaryOperator
elementwise to thisTensor
.- Specified by:
foreachInPlace
in interfaceTensor
- Parameters:
f
- The function to apply.
-
scale
-
add
-
dot
-
outer
This generates the outer product when dotted with anotherSparseVector
.It throws an
IllegalArgumentException
if used with aDenseVector
.- Specified by:
outer
in interfaceSGDVector
- Parameters:
other
- A vector.- Returns:
- A
DenseSparseMatrix
representing the outer product.
-
sum
-
twoNorm
-
oneNorm
-
get
-
set
-
indexOfMax
Description copied from interface:SGDVector
Returns the index of the maximum value. Requires probing the array.- Specified by:
indexOfMax
in interfaceSGDVector
- Returns:
- The index of the maximum value.
-
maxValue
-
minValue
-
difference
Generates an array of the indices that are active in this vector but are not present inother
.- Parameters:
other
- The vector to compare.- Returns:
- An array of indices that are active only in this vector.
-
intersection
Generates an array of the indices that are active in both this vector andother
- Parameters:
other
- The vector to intersect.- Returns:
- An array of indices that are active in both vectors.
-
normalize
Description copied from interface:SGDVector
Normalizes the vector using the supplied vector normalizer. -
euclideanDistance
Description copied from interface:SGDVector
The l2 or euclidean distance between this vector and the other vector.- Specified by:
euclideanDistance
in interfaceSGDVector
- Parameters:
other
- The other vector.- Returns:
- The euclidean distance between them.
-
l1Distance
Description copied from interface:SGDVector
The l1 or Manhattan distance between this vector and the other vector.- Specified by:
l1Distance
in interfaceSGDVector
- Parameters:
other
- The other vector.- Returns:
- The l1 distance.
-
distance
public double distance(SGDVector other, DoubleUnaryOperator transformFunc, DoubleUnaryOperator normalizeFunc) -
toString
-
toDenseArray
-
variance
-
iterator
- Specified by:
iterator
in interfaceIterable<VectorTuple>
-
transpose
Transposes an array of sparse vectors from row-major to column-major or vice versa.- Parameters:
input
- Input sparse vectors.- Returns:
- A column-major array of SparseVectors.
-
transpose
Converts a dataset of row-major examples into an array of column-major sparse vectors.- Type Parameters:
T
- The type of the dataset.- Parameters:
dataset
- Input dataset.- Returns:
- A column-major array of SparseVectors.
-
transpose
public static <T extends Output<T>> SparseVector[] transpose(Dataset<T> dataset, ImmutableFeatureMap fMap) Converts a dataset of row-major examples into an array of column-major sparse vectors.- Type Parameters:
T
- The type of the dataset.- Parameters:
dataset
- Input dataset.fMap
- The feature map to use. If it's different to the feature map used by the dataset then behaviour is undefined.- Returns:
- A column-major array of SparseVectors.
-