Search in sources :

Example 16 with SparseVector

use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.

the class FeatureLabelUtil method getVectorFeature.

public static Vector getVectorFeature(Object input, boolean hasInterceptItem, Integer vectorSize) {
    Vector aVector;
    Vector vec = VectorUtil.getVector(input);
    if (vec instanceof SparseVector) {
        SparseVector tmp = (SparseVector) vec;
        if (null != vectorSize && tmp.size() > 0) {
            tmp.setSize(vectorSize);
        }
        aVector = hasInterceptItem ? tmp.prefix(1.0) : tmp;
    } else {
        DenseVector tmp = (DenseVector) vec;
        aVector = hasInterceptItem ? tmp.prefix(1.0) : tmp;
    }
    return aVector;
}
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 17 with SparseVector

use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.

the class FeatureLabelUtil method getFeatureVector.

/**
 * Retrieve the feature vector from the input row data.
 */
public static Vector getFeatureVector(Row row, boolean hasInterceptItem, int featureN, int[] featureIdx, int vectorColIndex, Integer vectorSize) {
    Vector aVector;
    if (vectorColIndex != -1) {
        Vector vec = VectorUtil.getVector(row.getField(vectorColIndex));
        if (vec instanceof SparseVector) {
            SparseVector tmp = (SparseVector) vec;
            if (null != vectorSize && tmp.size() > 0) {
                tmp.setSize(vectorSize);
            }
            aVector = hasInterceptItem ? tmp.prefix(1.0) : tmp;
        } else {
            DenseVector tmp = (DenseVector) vec;
            aVector = hasInterceptItem ? tmp.prefix(1.0) : tmp;
        }
    } else {
        if (hasInterceptItem) {
            aVector = new DenseVector(featureN + 1);
            aVector.set(0, 1.0);
            for (int i = 0; i < featureN; i++) {
                if (row.getField(featureIdx[i]) instanceof Number) {
                    aVector.set(i + 1, ((Number) row.getField(featureIdx[i])).doubleValue());
                }
            }
        } else {
            aVector = new DenseVector(featureN);
            for (int i = 0; i < featureN; i++) {
                if (row.getField(featureIdx[i]) instanceof Number) {
                    aVector.set(i, ((Number) row.getField(featureIdx[i])).doubleValue());
                }
            }
        }
    }
    return aVector;
}
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 18 with SparseVector

use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.

the class PcaModelMapper method map.

@Override
protected void map(SlicedSelectedSample selection, SlicedResult result) throws Exception {
    // transform data
    double[] data = new double[this.model.nx];
    if (isVector) {
        Vector parsed = VectorUtil.getVector(selection.get(featureIdxs[0]));
        if (parsed instanceof SparseVector) {
            if (parsed.size() < 0) {
                ((SparseVector) parsed).setSize(model.nx);
            }
        }
        for (int i = 0; i < parsed.size(); i++) {
            data[i] = parsed.get(i);
        }
    } else {
        for (int i = 0; i < this.featureIdxs.length; ++i) {
            data[i] = (Double) selection.get(this.featureIdxs[i]);
        }
    }
    double[] predictData;
    if (model.idxNonEqual.length != data.length) {
        Integer[] indices = model.idxNonEqual;
        double[] dataNe = new double[indices.length];
        for (int i = 0; i < indices.length; i++) {
            if (Math.abs(this.sourceStd[i]) > 1e-12) {
                int idx = indices[i];
                dataNe[i] = (data[idx] - this.sourceMean[i]) / this.sourceStd[i];
            }
        }
        predictData = model.calcPrinValue(dataNe);
    } else {
        for (int i = 0; i < data.length; i++) {
            if (Math.abs(this.sourceStd[i]) > 1e-12) {
                data[i] = (data[i] - this.sourceMean[i]) / this.sourceStd[i];
            }
        }
        predictData = model.calcPrinValue(data);
    }
    result.set(0, new DenseVector(predictData));
}
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 19 with SparseVector

use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.

the class CrossFeatureModelMapper method map.

@Override
protected void map(SlicedSelectedSample selection, SlicedResult result) throws Exception {
    for (int i = 0; i < selectedColIndices.length; i++) {
        Object data = selection.get(selectedColIndices[i]);
        if (data == null) {
            if (nullIndex[i] != -1) {
                dataIndices[i] = nullIndex[i];
            } else {
                SparseVector sv = new SparseVector(svLength);
                result.set(0, sv);
                return;
            }
        } else if (tokenAndIndex[i].containsKey(data.toString())) {
            dataIndices[i] = tokenAndIndex[i].get(data.toString());
        } else {
            SparseVector sv = new SparseVector(svLength);
            result.set(0, sv);
            return;
        }
    }
    int resIndex = 0;
    for (int i = 0; i < carry.length; i++) {
        resIndex += carry[i] * dataIndices[i];
    }
    SparseVector sv = new SparseVector(svLength, new int[] { resIndex }, new double[] { 1.0 });
    result.set(0, sv);
}
Also used : SparseVector(com.alibaba.alink.common.linalg.SparseVector)

Example 20 with SparseVector

use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.

the class FeatureHasherMapper method map.

@Override
protected void map(SlicedSelectedSample selection, SlicedResult result) throws Exception {
    TreeMap<Integer, Double> feature = new TreeMap<>();
    for (int key : numericColIndexes) {
        if (null != selection.get(key)) {
            double value = ((Number) selection.get(key)).doubleValue();
            String colName = selectedCols[key];
            updateMap(colName, value, feature, numFeature);
        }
    }
    for (int key : categoricalColIndexes) {
        if (null != selection.get(key)) {
            String colName = selectedCols[key];
            updateMap(colName + "=" + selection.get(key).toString(), 1.0, feature, numFeature);
        }
    }
    result.set(0, new SparseVector(numFeature, feature));
}
Also used : TreeMap(java.util.TreeMap) 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