Search in sources :

Example 36 with TableSourceBatchOp

use of com.alibaba.alink.operator.batch.source.TableSourceBatchOp in project Alink by alibaba.

the class MinMaxTest method test.

@Test
public void test() throws Exception {
    BatchOperator batchData = new TableSourceBatchOp(GenerateData.getBatchTable());
    StreamOperator streamData = new TableSourceStreamOp(GenerateData.getStreamTable());
    MinMaxScalerTrainBatchOp op = new MinMaxScalerTrainBatchOp().setSelectedCols("f0", "f1").linkFrom(batchData);
    new MinMaxScalerPredictBatchOp().linkFrom(op, batchData).lazyCollect();
    new MinMaxScalerPredictStreamOp(op).linkFrom(streamData).print();
    MinMaxScalerModel model = new MinMaxScaler().setSelectedCols("f0", "f1").setOutputCols("f0_1", "f1_1").fit(batchData);
    List<Row> rows = model.transform(batchData).collect();
    rows.sort(new Comparator<Row>() {

        @Override
        public int compare(Row o1, Row o2) {
            if (o1.getField(0) == null) {
                return -1;
            }
            if (o2.getField(0) == null) {
                return 1;
            }
            if ((double) o1.getField(0) > (double) o2.getField(0)) {
                return 1;
            }
            if ((double) o1.getField(0) < (double) o2.getField(0)) {
                return -1;
            }
            return 0;
        }
    });
    assertEquals(rows.get(0), Row.of(null, null, null, null));
    assertEquals(rows.get(1), Row.of(-1., -3., 0., 0.));
    assertEquals(rows.get(2), Row.of(1., 2., 0.4, 1.));
    assertEquals(rows.get(3), Row.of(4., 2., 1., 1.));
    model.transform(streamData).print();
    StreamOperator.execute();
}
Also used : TableSourceBatchOp(com.alibaba.alink.operator.batch.source.TableSourceBatchOp) BatchOperator(com.alibaba.alink.operator.batch.BatchOperator) MinMaxScalerModel(com.alibaba.alink.pipeline.dataproc.MinMaxScalerModel) MinMaxScalerPredictStreamOp(com.alibaba.alink.operator.stream.dataproc.MinMaxScalerPredictStreamOp) MinMaxScaler(com.alibaba.alink.pipeline.dataproc.MinMaxScaler) TableSourceStreamOp(com.alibaba.alink.operator.stream.source.TableSourceStreamOp) Row(org.apache.flink.types.Row) StreamOperator(com.alibaba.alink.operator.stream.StreamOperator) Test(org.junit.Test)

Example 37 with TableSourceBatchOp

use of com.alibaba.alink.operator.batch.source.TableSourceBatchOp in project Alink by alibaba.

the class TensorTypesTest method test.

@Test
public void test() throws Exception {
    Row[] tensors = new Row[] { Row.of(new FloatTensor(new float[] { 0.0f, 1.0f })) };
    MemSourceBatchOp memSourceBatchOp = new MemSourceBatchOp(Arrays.asList(tensors), new TableSchema(new String[] { "col0" }, new TypeInformation<?>[] { TensorTypes.TENSOR }));
    DataSet<Tensor<?>> mapedTensor = memSourceBatchOp.getDataSet().map(new MapFunction<Row, Tensor<?>>() {

        @Override
        public Tensor<?> map(Row value) throws Exception {
            return (Tensor<?>) value.getField(0);
        }
    });
    new TableSourceBatchOp(DataSetConversionUtil.toTable(MLEnvironmentFactory.DEFAULT_ML_ENVIRONMENT_ID, mapedTensor.map(new MapFunction<Tensor<?>, Row>() {

        @Override
        public Row map(Tensor<?> value) throws Exception {
            return Row.of(value);
        }
    }), new String[] { "col0" }, new TypeInformation<?>[] { TensorTypes.TENSOR })).print();
}
Also used : TableSchema(org.apache.flink.table.api.TableSchema) MapFunction(org.apache.flink.api.common.functions.MapFunction) TableSourceBatchOp(com.alibaba.alink.operator.batch.source.TableSourceBatchOp) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) MemSourceBatchOp(com.alibaba.alink.operator.batch.source.MemSourceBatchOp) Row(org.apache.flink.types.Row) Test(org.junit.Test)

Example 38 with TableSourceBatchOp

use of com.alibaba.alink.operator.batch.source.TableSourceBatchOp in project Alink by alibaba.

the class BisectingKMeansTest method before.

@Before
public void before() {
    Row[] rows = new Row[] { Row.of(0, "0  0  0"), Row.of(1, "0.1  0.1  0.1"), Row.of(2, "0.2  0.2  0.2"), Row.of(3, "9  9  9"), Row.of(4, "9.1  9.1  9.1"), Row.of(5, "9.2  9.2  9.2") };
    inputBatchOp = new TableSourceBatchOp(MLEnvironmentFactory.getDefault().createBatchTable(rows, new String[] { "id", "vector" }));
    inputStreamOp = new TableSourceStreamOp(MLEnvironmentFactory.getDefault().createStreamTable(rows, new String[] { "id", "vector" }));
    expectedPrediction = new long[] { 0, 0, 0, 1, 1, 1 };
}
Also used : Row(org.apache.flink.types.Row) TableSourceStreamOp(com.alibaba.alink.operator.stream.source.TableSourceStreamOp) TableSourceBatchOp(com.alibaba.alink.operator.batch.source.TableSourceBatchOp) Before(org.junit.Before)

Example 39 with TableSourceBatchOp

use of com.alibaba.alink.operator.batch.source.TableSourceBatchOp in project Alink by alibaba.

the class KMeansTest method before.

@Before
public void before() {
    Row[] rows = new Row[] { Row.of(0, "0  0  0"), Row.of(1, "0.1  0.1  0.1"), Row.of(2, "0.2  0.2  0.2"), Row.of(3, "9  9  9"), Row.of(4, "9.1  9.1  9.1"), Row.of(5, "9.2  9.2  9.2") };
    inputBatchOp = new TableSourceBatchOp(MLEnvironmentFactory.getDefault().createBatchTable(rows, new String[] { "id", "vector" }));
    inputStreamOp = new TableSourceStreamOp(MLEnvironmentFactory.getDefault().createStreamTable(rows, new String[] { "id", "vector" }));
    expectedPrediction = new double[] { 0.173, 0, 0.173, 0.173, 0, 0.173 };
}
Also used : Row(org.apache.flink.types.Row) TableSourceStreamOp(com.alibaba.alink.operator.stream.source.TableSourceStreamOp) TableSourceBatchOp(com.alibaba.alink.operator.batch.source.TableSourceBatchOp) Before(org.junit.Before)

Aggregations

TableSourceBatchOp (com.alibaba.alink.operator.batch.source.TableSourceBatchOp)39 Row (org.apache.flink.types.Row)29 BatchOperator (com.alibaba.alink.operator.batch.BatchOperator)22 Test (org.junit.Test)18 TypeInformation (org.apache.flink.api.common.typeinfo.TypeInformation)12 TableSourceStreamOp (com.alibaba.alink.operator.stream.source.TableSourceStreamOp)10 Params (org.apache.flink.ml.api.misc.param.Params)10 List (java.util.List)8 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)8 StreamOperator (com.alibaba.alink.operator.stream.StreamOperator)6 ArrayList (java.util.ArrayList)6 TableSchema (org.apache.flink.table.api.TableSchema)6 MemSourceBatchOp (com.alibaba.alink.operator.batch.source.MemSourceBatchOp)5 Comparator (java.util.Comparator)4 HashMap (java.util.HashMap)4 MapFunction (org.apache.flink.api.common.functions.MapFunction)4 DataSet (org.apache.flink.api.java.DataSet)4 Mapper (com.alibaba.alink.common.mapper.Mapper)3 ModelMapper (com.alibaba.alink.common.mapper.ModelMapper)3 PipelineModelMapper (com.alibaba.alink.common.mapper.PipelineModelMapper)3