Search in sources :

Example 21 with SparseVector

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

the class PolynomialExpansionMapperTest method testSparse.

@Test
public void testSparse() throws Exception {
    TableSchema schema = new TableSchema(new String[] { "vec" }, new TypeInformation<?>[] { Types.STRING });
    Params params = new Params().set(VectorPolynomialExpandParams.SELECTED_COL, "vec").set(VectorPolynomialExpandParams.OUTPUT_COL, "res").set(VectorPolynomialExpandParams.DEGREE, 2);
    PolynomialExpansionMapper mapper = new PolynomialExpansionMapper(schema, params);
    assertEquals(new SparseVector(9, new int[] { 0, 1, 5, 6, 8 }, new double[] { 2.0, 4.0, 3.0, 6.0, 9.0 }), mapper.map(Row.of(new SparseVector(3, new int[] { 0, 2 }, new double[] { 2.0, 3.0 }))).getField(1));
    assertEquals(mapper.getOutputSchema(), new TableSchema(new String[] { "vec", "res" }, new TypeInformation<?>[] { Types.STRING, VectorTypes.VECTOR }));
}
Also used : TableSchema(org.apache.flink.table.api.TableSchema) VectorPolynomialExpandParams(com.alibaba.alink.params.dataproc.vector.VectorPolynomialExpandParams) Params(org.apache.flink.ml.api.misc.param.Params) SparseVector(com.alibaba.alink.common.linalg.SparseVector) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) Test(org.junit.Test)

Example 22 with SparseVector

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

the class VectorAssemblerMapperTest method testStringValue.

@Test
public void testStringValue() throws Exception {
    TableSchema schema = new TableSchema(new String[] { "c0", "c1", "c2" }, new TypeInformation<?>[] { Types.STRING, Types.DOUBLE, Types.STRING });
    TableSchema outSchema = new TableSchema(new String[] { "c0", "out" }, new TypeInformation<?>[] { Types.STRING, VectorTypes.VECTOR });
    Params params = new Params().set(VectorAssemblerParams.SELECTED_COLS, new String[] { "c0", "c1", "c2" }).set(VectorAssemblerParams.OUTPUT_COL, "out").set(VectorAssemblerParams.HANDLE_INVALID, VectorAssemblerParams.HandleInvalidMethod.SKIP).set(VectorAssemblerParams.RESERVED_COLS, new String[] { "c0" });
    VectorAssemblerMapper mapper = new VectorAssemblerMapper(schema, params);
    assertEquals(mapper.map(Row.of(new DenseVector(new double[] { 3.0, 4.0 }), "1", new SparseVector(11, new int[] { 0, 10 }, new double[] { 1.0, 4.0 }))).getField(1), new SparseVector(14, new int[] { 0, 1, 2, 3, 13 }, new double[] { 3.0, 4.0, 1.0, 1.0, 4.0 }));
    assertEquals(mapper.getOutputSchema(), outSchema);
}
Also used : TableSchema(org.apache.flink.table.api.TableSchema) VectorAssemblerParams(com.alibaba.alink.params.dataproc.vector.VectorAssemblerParams) Params(org.apache.flink.ml.api.misc.param.Params) SparseVector(com.alibaba.alink.common.linalg.SparseVector) DenseVector(com.alibaba.alink.common.linalg.DenseVector) Test(org.junit.Test)

Example 23 with SparseVector

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

the class VectorAssemblerMapperTest method testKeep.

@Test
public void testKeep() throws Exception {
    TableSchema schema = new TableSchema(new String[] { "c0", "c1", "c2" }, new TypeInformation<?>[] { Types.STRING, Types.DOUBLE, Types.STRING });
    TableSchema outSchema = new TableSchema(new String[] { "c0", "out" }, new TypeInformation<?>[] { Types.STRING, VectorTypes.VECTOR });
    Params params = new Params().set(VectorAssemblerParams.SELECTED_COLS, new String[] { "c0", "c1", "c2" }).set(VectorAssemblerParams.OUTPUT_COL, "out").set(VectorAssemblerParams.HANDLE_INVALID, VectorAssemblerParams.HandleInvalidMethod.SKIP).set(VectorAssemblerParams.RESERVED_COLS, new String[] { "c0" });
    VectorAssemblerMapper mapper = new VectorAssemblerMapper(schema, params);
    assertEquals(mapper.map(Row.of(new DenseVector(new double[] { 3.0, 4.0 }), 1, new SparseVector(11, new int[] { 0, 10 }, new double[] { 1.0, 4.0 }))).getField(1), new SparseVector(14, new int[] { 0, 1, 2, 3, 13 }, new double[] { 3.0, 4.0, 1, 1.0, 4.0 }));
    assertEquals(mapper.getOutputSchema(), outSchema);
}
Also used : TableSchema(org.apache.flink.table.api.TableSchema) VectorAssemblerParams(com.alibaba.alink.params.dataproc.vector.VectorAssemblerParams) Params(org.apache.flink.ml.api.misc.param.Params) SparseVector(com.alibaba.alink.common.linalg.SparseVector) DenseVector(com.alibaba.alink.common.linalg.DenseVector) Test(org.junit.Test)

Example 24 with SparseVector

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

the class VectorElementwiseProductMapperTest method test4.

@Test
public void test4() throws Exception {
    TableSchema schema = new TableSchema(new String[] { "vec" }, new TypeInformation<?>[] { Types.STRING });
    Params params = new Params().set(VectorElementwiseProductParams.SELECTED_COL, "vec").set(VectorElementwiseProductParams.OUTPUT_COL, "res").set(VectorElementwiseProductParams.SCALING_VECTOR, "$10$1:3.0 2:10.0 9:4.5");
    VectorElementwiseProductMapper mapper = new VectorElementwiseProductMapper(schema, params);
    assertEquals(mapper.map(Row.of(new SparseVector(10, new int[] { 1, 5, 9 }, new double[] { 2.0, 4.0, 3.0 }))).getField(1), new SparseVector(10, new int[] { 1, 5, 9 }, new double[] { 6.0, 0.0, 13.5 }));
    assertEquals(mapper.getOutputSchema(), new TableSchema(new String[] { "vec", "res" }, new TypeInformation<?>[] { Types.STRING, VectorTypes.VECTOR }));
}
Also used : TableSchema(org.apache.flink.table.api.TableSchema) VectorElementwiseProductParams(com.alibaba.alink.params.dataproc.vector.VectorElementwiseProductParams) Params(org.apache.flink.ml.api.misc.param.Params) SparseVector(com.alibaba.alink.common.linalg.SparseVector) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) Test(org.junit.Test)

Example 25 with SparseVector

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

the class VectorInteractionMapperTest method testSparse.

@Test
public void testSparse() throws Exception {
    TableSchema schema = new TableSchema(new String[] { "c0", "c1" }, new TypeInformation<?>[] { Types.STRING, Types.STRING });
    TableSchema outSchema = new TableSchema(new String[] { "c0", "out" }, new TypeInformation<?>[] { Types.STRING, VectorTypes.VECTOR });
    Params params = new Params().set(VectorInteractionParams.SELECTED_COLS, new String[] { "c0", "c1" }).set(VectorInteractionParams.OUTPUT_COL, "out").set(VectorInteractionParams.RESERVED_COLS, new String[] { "c0" });
    VectorInteractionMapper mapper = new VectorInteractionMapper(schema, params);
    assertEquals(mapper.map(Row.of(new SparseVector(10, new int[] { 0, 9 }, new double[] { 1.0, 4.0 }), new SparseVector(10, new int[] { 0, 9 }, new double[] { 1.0, 4.0 }))).getField(1), new SparseVector(100, new int[] { 0, 9, 90, 99 }, new double[] { 1.0, 4.0, 4.0, 16.0 }));
    assertEquals(mapper.getOutputSchema(), outSchema);
}
Also used : TableSchema(org.apache.flink.table.api.TableSchema) VectorInteractionParams(com.alibaba.alink.params.dataproc.vector.VectorInteractionParams) Params(org.apache.flink.ml.api.misc.param.Params) SparseVector(com.alibaba.alink.common.linalg.SparseVector) Test(org.junit.Test)

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