public class SparseVector extends Object implements SGDVector
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.
Modifier and Type | Field and Description |
---|---|
protected int[] |
indices |
protected double[] |
values |
Constructor and Description |
---|
SparseVector(int size,
int[] indices,
double value) |
Modifier and Type | Method and Description |
---|---|
void |
add(int index,
double value)
Adds
value to the element at index . |
SGDVector |
add(SGDVector other)
Adds
other to this vector, producing a new SGDVector . |
SparseVector |
copy()
Returns a deep copy of this vector.
|
static <T extends Output<T>> |
createSparseVector(Example<T> example,
ImmutableFeatureMap featureInfo,
boolean addBias)
Builds a
SparseVector from an Example . |
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.
|
DenseVector |
densify()
Returns a dense vector copying this sparse vector.
|
int[] |
difference(SparseVector other)
Generates an array of the indices that are active in this vector
but are not present in
other . |
double |
distance(SGDVector other,
DoubleUnaryOperator transformFunc,
DoubleUnaryOperator normalizeFunc) |
double |
dot(SGDVector other)
Calculates the dot product between this vector and
other . |
boolean |
equals(Object other)
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 |
foreachInPlace(DoubleUnaryOperator f)
Applies a
DoubleUnaryOperator elementwise to this Tensor . |
double |
get(int index)
Gets an element from this vector.
|
int[] |
getShape()
Returns an int array specifying the shape of this
Tensor . |
void |
hadamardProductInPlace(Tensor other,
DoubleUnaryOperator f)
Updates this
Tensor with the Hadamard product
(i.e., a term by term multiply) of this and other . |
int |
hashCode() |
int |
indexOfMax()
Returns the index of the maximum value.
|
void |
intersectAndAddInPlace(Tensor other,
DoubleUnaryOperator f)
Updates this
Tensor by adding all the values from the intersection with other . |
int[] |
intersection(SparseVector other)
Generates an array of the indices that are active in both this
vector and
other |
VectorIterator |
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 |
numActiveElements()
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.
|
Matrix |
outer(SGDVector other)
This generates the outer product when dotted with another
SparseVector . |
double |
reduce(double initial,
DoubleUnaryOperator transform,
DoubleBinaryOperator reduction)
Reduces the vector, applying the transformation to every value (including the implicit zeros)
and reducing the output by applying the supplied reduction operator (where the right argument is
the current reduction value, and the left argument is the transformed value).
|
Tensor |
reshape(int[] newShape)
Reshapes the Tensor to the supplied shape.
|
SparseVector |
scale(double coefficient)
Generates a new vector with each element scaled by
coefficient . |
void |
set(int index,
double value)
Sets the
index to the value . |
int |
size()
Returns the dimensionality of this vector.
|
SGDVector |
subtract(SGDVector other)
Subtracts
other from this vector, producing a new SGDVector . |
double |
sum()
Calculates the sum of this vector.
|
double[] |
toArray()
Returns an array containing all the values in the vector (including any implicit zeros).
|
double[] |
toDenseArray()
Deprecated.
|
String |
toString() |
static <T extends Output<T>> |
transpose(Dataset<T> dataset)
Converts a dataset of row-major examples into an array of column-major
sparse vectors.
|
static <T extends Output<T>> |
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.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
cosineDistance, cosineSimilarity, l2Distance, variance
hadamardProductInPlace, intersectAndAddInPlace, scalarAddInPlace, scaleInPlace, shapeCheck, shapeSum
forEach, spliterator
public static <T extends Output<T>> SparseVector createSparseVector(Example<T> example, ImmutableFeatureMap featureInfo, boolean addBias)
SparseVector
from an Example
.
Used in training and inference.
Throws IllegalArgumentException
if the Example contains NaN-valued features.
T
- The type parameter of the example
.example
- The example to convert.featureInfo
- The feature information, used to calculate the dimension of this SparseVector.addBias
- Add a bias feature.public static SparseVector createSparseVector(int dimension, int[] indices, double[] values)
Throws IllegalArgumentException
if the arrays are not the same length, or if size is less than
the max index.
dimension
- The dimension of this vector.indices
- The indices of the non-zero elements.values
- The values of the non-zero elements.public static SparseVector createSparseVector(int dimension, Map<Integer,Double> indexMap)
Throws IllegalArgumentException
if dimension is less than the max index.
dimension
- The dimension of this vector.indexMap
- The map from indices to values.public SparseVector copy()
SGDVector
public int[] getShape()
Tensor
Tensor
.public Tensor reshape(int[] newShape)
Tensor
IllegalArgumentException
if the shape isn't compatible.public int size()
SGDVector
public int numActiveElements()
SGDVector
numActiveElements
in interface SGDVector
public boolean equals(Object other)
public SGDVector add(SGDVector other)
other
to this vector, producing a new SGDVector
.
If other
is a SparseVector
then the returned vector is also
a SparseVector
otherwise it's a DenseVector
.public SGDVector subtract(SGDVector other)
other
from this vector, producing a new SGDVector
.
If other
is a SparseVector
then the returned vector is also
a SparseVector
otherwise it's a DenseVector
.public void intersectAndAddInPlace(Tensor other, DoubleUnaryOperator f)
Tensor
Tensor
by adding all the values from the intersection with other
.
The function f
is applied to all values from other
before the
addition.
Each value is updated as value += f(otherValue).
intersectAndAddInPlace
in interface Tensor
other
- The other Tensor
.f
- A function to apply.public void hadamardProductInPlace(Tensor other, DoubleUnaryOperator f)
Tensor
Tensor
with the Hadamard product
(i.e., a term by term multiply) of this and other
.
The function f
is applied to all values from other
before the addition.
Each value is updated as value *= f(otherValue).
hadamardProductInPlace
in interface Tensor
other
- The other Tensor
.f
- A function to apply.public void foreachInPlace(DoubleUnaryOperator f)
Tensor
DoubleUnaryOperator
elementwise to this Tensor
.foreachInPlace
in interface Tensor
f
- The function to apply.public SparseVector scale(double coefficient)
SGDVector
coefficient
.public void add(int index, double value)
SGDVector
value
to the element at index
.public double dot(SGDVector other)
SGDVector
other
.public Matrix outer(SGDVector other)
SparseVector
.
It throws an IllegalArgumentException
if used with a DenseVector
.
outer
in interface SGDVector
other
- A vector.DenseSparseMatrix
representing the outer product.public double sum()
SGDVector
public double twoNorm()
SGDVector
public double oneNorm()
SGDVector
public double get(int index)
SGDVector
public void set(int index, double value)
SGDVector
index
to the value
.public int indexOfMax()
SGDVector
indexOfMax
in interface SGDVector
public double maxValue()
SGDVector
public double minValue()
SGDVector
public int[] difference(SparseVector other)
other
.other
- The vector to compare.public int[] intersection(SparseVector other)
other
other
- The vector to intersect.public void normalize(VectorNormalizer normalizer)
SGDVector
public double reduce(double initial, DoubleUnaryOperator transform, DoubleBinaryOperator reduction)
SGDVector
public double euclideanDistance(SGDVector other)
SGDVector
euclideanDistance
in interface SGDVector
other
- The other vector.public double l1Distance(SGDVector other)
SGDVector
l1Distance
in interface SGDVector
other
- The other vector.public double distance(SGDVector other, DoubleUnaryOperator transformFunc, DoubleUnaryOperator normalizeFunc)
public DenseVector densify()
@Deprecated public double[] toDenseArray()
public double[] toArray()
SGDVector
public double variance(double mean)
SGDVector
public VectorIterator iterator()
iterator
in interface Iterable<VectorTuple>
public static SparseVector[] transpose(SparseVector[] input)
input
- Input sparse vectors.public static <T extends Output<T>> SparseVector[] transpose(Dataset<T> dataset)
T
- The type of the dataset.dataset
- Input dataset.public static <T extends Output<T>> SparseVector[] transpose(Dataset<T> dataset, ImmutableFeatureMap fMap)
T
- The type of the dataset.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.Copyright © 2015–2021 Oracle and/or its affiliates. All rights reserved.