use of com.alibaba.alink.operator.stream.sink.CollectSinkStreamOp in project Alink by alibaba.
the class TensorToVectorStreamOpTest method testTensorToVectorStreamOp.
@Test
public void testTensorToVectorStreamOp() throws Exception {
List<Row> data = Collections.singletonList(Row.of("DOUBLE#6#0.0 0.1 1.0 1.1 2.0 2.1"));
CollectSinkStreamOp collectSinkStreamOp = new MemSourceStreamOp(data, "tensor string").link(new TensorToVectorStreamOp().setSelectedCol("tensor")).link(new CollectSinkStreamOp());
StreamOperator.execute();
List<Row> ret = collectSinkStreamOp.getAndRemoveValues();
Assert.assertEquals(1, ret.size());
Assert.assertTrue(ret.get(0).getField(0) instanceof DenseVector);
Assert.assertArrayEquals(new double[] { 0.0, 0.1, 1.0, 1.1, 2.0, 2.1 }, ((DenseVector) ret.get(0).getField(0)).getData(), 1e-6);
}
use of com.alibaba.alink.operator.stream.sink.CollectSinkStreamOp in project Alink by alibaba.
the class TypeConvertStreamOpTest method test.
@Test
public void test() throws Exception {
Row[] testArray = new Row[] { Row.of("1", 1, 1.1, 1.0, true), Row.of("2", -2, 0.9, 2.0, false), Row.of("3", 100, -0.01, 3.0, true), Row.of("4", -99, null, 4.0, false), Row.of("5", 1, 1.1, 5.0, true), Row.of("6", -2, 0.9, 6.0, false) };
String[] colNames = new String[] { "group", "col2", "col3", "col4", "col5" };
MemSourceStreamOp inOp = new MemSourceStreamOp(Arrays.asList(testArray), colNames);
TypeConvertStreamOp typeConvertStreamOp = new TypeConvertStreamOp().setSelectedCols(new String[] { colNames[0], colNames[1], colNames[2], colNames[3], colNames[4] }).setTargetType("bigint");
TypeConvertStreamOp typeConvertStreamOp1 = new TypeConvertStreamOp().setTargetType("string");
CollectSinkStreamOp collectSinkStreamOp = new CollectSinkStreamOp();
CollectSinkStreamOp collectSinkStreamOp1 = new CollectSinkStreamOp();
inOp.link(typeConvertStreamOp).link(collectSinkStreamOp);
inOp.link(typeConvertStreamOp1).link(collectSinkStreamOp1);
StreamOperator.execute();
MTable ret = new MTable(collectSinkStreamOp.getAndRemoveValues(), typeConvertStreamOp.getSchema());
MTable ret1 = new MTable(collectSinkStreamOp1.getAndRemoveValues(), typeConvertStreamOp1.getSchema());
Assert.assertEquals(6, ret.getNumRow());
Assert.assertEquals(6, ret1.getNumRow());
ret.orderBy(0);
ret1.orderBy(0);
Assert.assertEquals(Row.of(1L, 1L, 1L, 1L, 1L), ret.getTable().get(0));
Assert.assertEquals(Row.of("1", "1", "1.1", "1.0", "true"), ret1.getTable().get(0));
}
use of com.alibaba.alink.operator.stream.sink.CollectSinkStreamOp in project Alink by alibaba.
the class VectorBiFunctionStreamOpTest method testBiVectorFunctionDense.
@Test
public void testBiVectorFunctionDense() throws Exception {
List<Row> df = new ArrayList<>();
df.add(Row.of("1 2 3", "2 3 4"));
StreamOperator<?> data = new MemSourceStreamOp(df, "vec1 string, vec2 string");
VectorBiFunctionStreamOp vecBiFunc = new VectorBiFunctionStreamOp().setSelectedCols("vec1", "vec2").setBiFuncName("minus").setReservedCols(new String[] {}).setOutputCol("vec_minus");
StreamOperator<?> res = vecBiFunc.linkFrom(data);
CollectSinkStreamOp sop = res.link(new CollectSinkStreamOp());
StreamOperator.execute();
List<Row> list = sop.getAndRemoveValues();
for (Row row : list) {
Assert.assertEquals(row.getField(0), VectorUtil.parseDense("-1.0 -1.0 -1.0"));
}
}
use of com.alibaba.alink.operator.stream.sink.CollectSinkStreamOp in project Alink by alibaba.
the class SvmTest method pipelineTest.
@Test
public void pipelineTest() throws Exception {
String[] xVars = new String[] { "f0", "f1", "f2", "f3" };
String yVar = "labels";
String vectorName = "vec";
String svectorName = "svec";
LinearSvm svm = new LinearSvm().setLabelCol(yVar).setFeatureCols(xVars).setOptimMethod("gd").setPredictionCol("svmpred");
LinearSvm vectorSvm = new LinearSvm().setLabelCol(yVar).setVectorCol(vectorName).setPredictionCol("vsvmpred").enableLazyPrintModelInfo().enableLazyPrintTrainInfo();
LinearSvm sparseVectorSvm = new LinearSvm().setLabelCol(yVar).setVectorCol(svectorName).setOptimMethod("sgd").setMaxIter(10).setPredictionCol("svsvmpred").setPredictionDetailCol("detail");
Pipeline plSvm = new Pipeline().add(svm).add(vectorSvm).add(sparseVectorSvm);
BatchOperator<?> trainData = (BatchOperator<?>) getData(true);
PipelineModel model = plSvm.fit(trainData);
BatchOperator<?> result = model.transform(trainData).select(new String[] { "labels", "svmpred", "vsvmpred", "svsvmpred" });
List<Row> d = result.collect();
for (Row row : d) {
for (int i = 1; i < 3; ++i) {
Assert.assertEquals(row.getField(0), row.getField(i));
}
}
// below is stream test code.
CollectSinkStreamOp sop = model.transform((StreamOperator<?>) getData(false)).select(new String[] { "labels", "svmpred", "vsvmpred", "svsvmpred" }).link(new CollectSinkStreamOp());
StreamOperator.execute();
List<Row> rows = sop.getAndRemoveValues();
for (Row row : rows) {
for (int i = 1; i < 3; ++i) {
Assert.assertEquals(row.getField(0), row.getField(i));
}
}
}
use of com.alibaba.alink.operator.stream.sink.CollectSinkStreamOp in project Alink by alibaba.
the class BisectingKMeansTest method test.
@Test
public void test() throws Exception {
BisectingKMeans bisectingKMeans = new BisectingKMeans().setVectorCol("vector").setPredictionCol("pred").setK(2).setMaxIter(10);
PipelineModel model = new Pipeline().add(bisectingKMeans).fit(inputBatchOp);
BatchOperator<?> batchPredOp = model.transform(inputBatchOp).select(new String[] { "id", "pred" });
verifyPredResult(batchPredOp.collect());
CollectSinkStreamOp streamPredOp = model.transform(inputStreamOp).select(new String[] { "id", "pred" }).link(new CollectSinkStreamOp());
StreamOperator.execute();
verifyPredResult(streamPredOp.getAndRemoveValues());
}
Aggregations