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 }));
}
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);
}
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);
}
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 }));
}
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);
}
Aggregations