Search in sources :

Example 96 with SparseVector

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]);
                }
            }
        }
    }
}
Also used : SparseVector(com.alibaba.alink.common.linalg.SparseVector) DenseVector(com.alibaba.alink.common.linalg.DenseVector)

Example 97 with SparseVector

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;
}
Also used : SparseVector(com.alibaba.alink.common.linalg.SparseVector) DenseVector(com.alibaba.alink.common.linalg.DenseVector)

Example 98 with SparseVector

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;
}
Also used : SparseVector(com.alibaba.alink.common.linalg.SparseVector) Vector(com.alibaba.alink.common.linalg.Vector) DenseVector(com.alibaba.alink.common.linalg.DenseVector) SparseVector(com.alibaba.alink.common.linalg.SparseVector) DenseVector(com.alibaba.alink.common.linalg.DenseVector)

Example 99 with SparseVector

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);
}
Also used : SparseVector(com.alibaba.alink.common.linalg.SparseVector)

Example 100 with SparseVector

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);
}
Also used : Arrays(java.util.Arrays) Tuple2(org.apache.flink.api.java.tuple.Tuple2) PriorityQueue(java.util.PriorityQueue) Set(java.util.Set) TableSchema(org.apache.flink.table.api.TableSchema) HashMap(java.util.HashMap) BaseItemsPerUserRecommParams(com.alibaba.alink.params.recommendation.BaseItemsPerUserRecommParams) BaseSimilarItemsRecommParams(com.alibaba.alink.params.recommendation.BaseSimilarItemsRecommParams) Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) List(java.util.List) MTable(com.alibaba.alink.common.MTable) Map(java.util.Map) Row(org.apache.flink.types.Row) Queue(java.util.Queue) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) SparseVector(com.alibaba.alink.common.linalg.SparseVector) Comparator(java.util.Comparator) Params(org.apache.flink.ml.api.misc.param.Params) FlinkTypeConverter(com.alibaba.alink.operator.common.io.types.FlinkTypeConverter) Collections(java.util.Collections) PriorityQueue(java.util.PriorityQueue) SparseVector(com.alibaba.alink.common.linalg.SparseVector)

Aggregations

SparseVector (com.alibaba.alink.common.linalg.SparseVector)125 Test (org.junit.Test)63 DenseVector (com.alibaba.alink.common.linalg.DenseVector)60 Params (org.apache.flink.ml.api.misc.param.Params)45 Row (org.apache.flink.types.Row)45 Vector (com.alibaba.alink.common.linalg.Vector)40 TableSchema (org.apache.flink.table.api.TableSchema)27 ArrayList (java.util.ArrayList)21 TypeInformation (org.apache.flink.api.common.typeinfo.TypeInformation)15 HashMap (java.util.HashMap)12 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)12 List (java.util.List)11 DenseMatrix (com.alibaba.alink.common.linalg.DenseMatrix)10 MTable (com.alibaba.alink.common.MTable)7 BaseVectorSummary (com.alibaba.alink.operator.common.statistics.basicstatistic.BaseVectorSummary)6 CollectSinkStreamOp (com.alibaba.alink.operator.stream.sink.CollectSinkStreamOp)6 Map (java.util.Map)6 MemSourceBatchOp (com.alibaba.alink.operator.batch.source.MemSourceBatchOp)5 VectorAssemblerParams (com.alibaba.alink.params.dataproc.vector.VectorAssemblerParams)5 OneHotPredictParams (com.alibaba.alink.params.feature.OneHotPredictParams)5