use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.
the class CosineDistance method baseCalc.
static void baseCalc(FastDistanceVectorData left, FastDistanceSparseData right, double[] res, double fillValue, Functional.SerializableFunction<Double, Double> function) {
Arrays.fill(res, fillValue);
int[][] rightIndices = right.getIndices();
double[][] rightValues = right.getValues();
if (left.vector instanceof DenseVector) {
double[] vector = ((DenseVector) left.vector).getData();
for (int i = 0; i < vector.length; i++) {
if (null != rightIndices[i]) {
for (int j = 0; j < rightIndices[i].length; j++) {
res[rightIndices[i][j]] -= rightValues[i][j] * vector[i];
}
}
}
} else {
SparseVector vector = (SparseVector) left.getVector();
int[] indices = vector.getIndices();
double[] values = vector.getValues();
for (int i = 0; i < indices.length; i++) {
if (null != rightIndices[indices[i]]) {
for (int j = 0; j < rightIndices[indices[i]].length; j++) {
res[rightIndices[indices[i]][j]] += function.apply(rightValues[indices[i]][j] * values[i]);
}
}
}
}
}
use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.
the class VectorAssemblerMapper method appendVector.
private static int appendVector(Vector vec, Map<Integer, Double> map, int pos) {
if (vec instanceof SparseVector) {
SparseVector sv = (SparseVector) vec;
if (sv.size() <= 0) {
throw new RuntimeException("The append sparse vector must have size.");
}
int[] idx = sv.getIndices();
double[] values = sv.getValues();
for (int j = 0; j < idx.length; ++j) {
map.put(pos + idx[j], values[j]);
}
pos += sv.size();
} else if (vec instanceof DenseVector) {
DenseVector dv = (DenseVector) vec;
for (int j = 0; j < dv.size(); ++j) {
map.put(pos++, dv.get(j));
}
}
return pos;
}
use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.
the class VectorElementwiseProductMapper method mapColumn.
@Override
protected Object mapColumn(Object input) {
if (null == input) {
return null;
}
Vector vector = VectorUtil.getVector(input);
if (vector instanceof DenseVector) {
double[] vec = ((DenseVector) vector).getData();
for (int i = 0; i < vec.length; ++i) {
vec[i] = vec[i] * scalingVector.get(i);
}
} else {
SparseVector vec = (SparseVector) vector;
double[] vecValues = vec.getValues();
int[] vecIndices = vec.getIndices();
for (int i = 0; i < vecValues.length; ++i) {
vecValues[i] *= scalingVector.get(vecIndices[i]);
}
}
return vector;
}
use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.
the class ItemCfRecommKernel method rate.
static Double rate(Object userId, Object itemId, ItemCfRecommData model) {
SparseVector userItem = model.userItemRates.get(userId);
Integer itemIndex = model.itemMap.get(itemId);
if (null == userItem || null == itemIndex) {
return null;
}
SparseVector itemSimilarity = model.itemSimilarities.get(itemIndex);
double dot = 0;
double similarity = 0;
int p0 = 0;
int p1 = 0;
double[] userItemsValues = userItem.getValues();
int[] userItemsIndices = userItem.getIndices();
double[] itemSimilarityValues = itemSimilarity.getValues();
int[] itemSimilarityIndices = itemSimilarity.getIndices();
while (p0 < userItemsValues.length && p1 < itemSimilarityValues.length) {
if (userItemsIndices[p0] == itemSimilarityIndices[p1]) {
dot += userItemsValues[p0] * itemSimilarityValues[p1];
similarity += itemSimilarityValues[p1];
p0++;
p1++;
} else if (userItemsIndices[p0] < itemSimilarityIndices[p1]) {
p0++;
} else {
p1++;
}
}
return model.rateCol == null ? (dot / userItem.numberOfValues()) : (similarity == 0 ? 0.0 : dot / similarity);
}
use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.
the class ItemCfRecommKernel method findSimilarItems.
static MTable findSimilarItems(Object itemId, ItemCfRecommData model, int topN, String objectname, TypeInformation<?> objType) {
PriorityQueue<RecommItemTopKResult> queue = new PriorityQueue<>(Comparator.comparing(o -> o.similarity));
Integer itemIndex = model.itemMap.get(itemId);
if (null == itemIndex) {
return null;
}
SparseVector itemSimilarity = model.itemSimilarities.get(itemIndex);
int[] key = itemSimilarity.getIndices();
double[] value = itemSimilarity.getValues();
double head = 0;
for (int i = 0; i < key.length; i++) {
head = updateQueue(queue, topN, value[i], model.items[key[i]], head);
}
return serializeQueue(queue, "similarities", objectname, objType);
}
Aggregations