Search in sources :

Example 1 with Strategy

use of com.alibaba.alink.params.dataproc.vector.VectorImputerTrainParams.Strategy in project Alink by alibaba.

the class VectorImputerModelDataConverter method serializeModel.

/**
 * Serialize the model data to "Tuple3<Params, List<String>, List<Row>>".
 *
 * @param modelData The model data to serialize.
 * @return The serialization result.
 */
public Tuple3<Params, Iterable<String>, Iterable<Row>> serializeModel(Tuple3<Strategy, BaseVectorSummary, Double> modelData) {
    Strategy strategy = modelData.f0;
    BaseVectorSummary summary = modelData.f1;
    double fillValue = modelData.f2;
    double[] values = null;
    Params meta = new Params().set(SELECTED_COL, vectorColName).set(STRATEGY, strategy);
    switch(strategy) {
        case MIN:
            if (summary.min() instanceof DenseVector) {
                values = ((DenseVector) summary.min()).getData();
            } else {
                values = ((SparseVector) summary.min()).toDenseVector().getData();
            }
            break;
        case MAX:
            if (summary.max() instanceof DenseVector) {
                values = ((DenseVector) summary.max()).getData();
            } else {
                values = ((SparseVector) summary.max()).toDenseVector().getData();
            }
            break;
        case MEAN:
            if (summary.mean() instanceof DenseVector) {
                values = ((DenseVector) summary.mean()).getData();
            } else {
                values = ((SparseVector) summary.mean()).getValues();
            }
            break;
        default:
            meta.set(FILL_VALUE, fillValue);
    }
    List<String> data = new ArrayList<>();
    data.add(JsonConverter.toJson(values));
    return Tuple3.of(meta, data, new ArrayList<>());
}
Also used : BaseVectorSummary(com.alibaba.alink.operator.common.statistics.basicstatistic.BaseVectorSummary) ArrayList(java.util.ArrayList) Strategy(com.alibaba.alink.params.dataproc.vector.VectorImputerTrainParams.Strategy) Params(org.apache.flink.ml.api.misc.param.Params) SparseVector(com.alibaba.alink.common.linalg.SparseVector) DenseVector(com.alibaba.alink.common.linalg.DenseVector)

Example 2 with Strategy

use of com.alibaba.alink.params.dataproc.vector.VectorImputerTrainParams.Strategy in project Alink by alibaba.

the class VectorImputerModelDataConverter method deserializeModel.

/**
 * Deserialize the model data.
 *
 * @param meta         The model meta data.
 * @param data         The model concrete data.
 * @param additionData The additional data.
 * @return The model data used by mapper.
 */
@Override
public Tuple3<Strategy, double[], Double> deserializeModel(Params meta, Iterable<String> data, Iterable<Row> additionData) {
    Strategy strategy = meta.get(STRATEGY);
    double[] values = null;
    if (data.iterator().hasNext()) {
        values = JsonConverter.fromJson(data.iterator().next(), double[].class);
    }
    Double fillValue = meta.get(FILL_VALUE);
    return Tuple3.of(strategy, values, fillValue);
}
Also used : Strategy(com.alibaba.alink.params.dataproc.vector.VectorImputerTrainParams.Strategy)

Aggregations

Strategy (com.alibaba.alink.params.dataproc.vector.VectorImputerTrainParams.Strategy)2 DenseVector (com.alibaba.alink.common.linalg.DenseVector)1 SparseVector (com.alibaba.alink.common.linalg.SparseVector)1 BaseVectorSummary (com.alibaba.alink.operator.common.statistics.basicstatistic.BaseVectorSummary)1 ArrayList (java.util.ArrayList)1 Params (org.apache.flink.ml.api.misc.param.Params)1