use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.
the class ToVectorMapperTest method testVec.
@Test
public void testVec() throws Exception {
final Mapper mapper = new ToVectorMapper(new TableSchema(new String[] { "vec" }, new TypeInformation<?>[] { VectorTypes.VECTOR }), new Params().set(ToVectorParams.SELECTED_COL, "vec"));
String vecStr = "0.0 0.1 1.0 1.1 2.0 2.1";
DenseVector vector = VectorUtil.parseDense(vecStr);
DenseVector result = (DenseVector) mapper.map(Row.of(vector)).getField(0);
Assert.assertEquals(vector, result);
String sVecStr = "0:0.0 1:0.1 5:1.0 10:1.1 12:2.0 15:2.1";
SparseVector sVector = VectorUtil.parseSparse(sVecStr);
SparseVector sResult = (SparseVector) mapper.map(Row.of(sVector)).getField(0);
Assert.assertEquals(sVector, sResult);
}
use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.
the class ToVectorMapperTest method test.
@Test
public void test() throws Exception {
final Mapper mapper = new ToVectorMapper(new TableSchema(new String[] { "vec" }, new TypeInformation<?>[] { Types.STRING }), new Params().set(ToVectorParams.SELECTED_COL, "vec"));
String vecStr = "0.0 0.1 1.0 1.1 2.0 2.1";
DenseVector vector = VectorUtil.parseDense(vecStr);
DenseVector result = (DenseVector) mapper.map(Row.of(vecStr)).getField(0);
Assert.assertEquals(vector, result);
String sVecStr = "0:0.0 1:0.1 5:1.0 10:1.1 12:2.0 15:2.1";
SparseVector sVector = VectorUtil.parseSparse(sVecStr);
SparseVector sResult = (SparseVector) mapper.map(Row.of(sVecStr)).getField(0);
Assert.assertEquals(sVector, sResult);
}
use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.
the class ToVectorMapperTest method testOp.
@Test
public void testOp() throws Exception {
final String vecStr = "1 0 3 4";
final DenseVector expect = VectorUtil.parseDense("1 0 3 4");
final SparseVector expect1 = VectorUtil.parseSparse("$4$0:1 2:3 3:4");
Row[] rows = new Row[] { Row.of(vecStr) };
MemSourceBatchOp memSourceBatchOp = new MemSourceBatchOp(rows, new String[] { "vec" });
memSourceBatchOp.link(new ToVectorBatchOp().setSelectedCol("vec")).lazyCollect(rows1 -> Assert.assertEquals(expect, rows1.get(0).getField(0)));
memSourceBatchOp.link(new ToVectorBatchOp().setVectorType(VectorType.SPARSE).setSelectedCol("vec")).lazyCollect(rows1 -> Assert.assertEquals(expect1, rows1.get(0).getField(0)));
BatchOperator.execute();
}
use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.
the class VectorSliceMapperTest method testSparse.
@Test
public void testSparse() throws Exception {
TableSchema schema = new TableSchema(new String[] { "vec" }, new TypeInformation<?>[] { Types.STRING });
Params params = new Params().set(VectorSliceParams.SELECTED_COL, "vec").set(VectorSliceParams.OUTPUT_COL, "res").set(VectorSliceParams.RESERVED_COLS, new String[] {}).set(VectorSliceParams.INDICES, new int[] { 0, 2, 4 });
VectorSliceMapper mapper = new VectorSliceMapper(schema, params);
assertEquals(mapper.map(Row.of(new SparseVector(5, new int[] { 0, 2, 4 }, new double[] { 3.0, 4.0, 3.0 }))).getField(0), new SparseVector(3, new int[] { 0, 1, 2 }, new double[] { 3.0, 4.0, 3.0 }));
assertEquals(mapper.getOutputSchema(), new TableSchema(new String[] { "res" }, new TypeInformation<?>[] { VectorTypes.VECTOR }));
}
use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.
the class FastDistanceTest method testSparseVectorRowInput.
@Test
public void testSparseVectorRowInput() {
Vector vec = new SparseVector(10, new int[] { 1, 2 }, new double[] { 1.0, 1.0 });
EuclideanDistance distance = new EuclideanDistance();
FastDistanceVectorData vectorData = distance.prepareVectorData(Row.of(vec, 0, "a"), 0, 1, 2);
assertVectorInput(vectorData, vec, Row.of(0, "a"));
}
Aggregations