use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.
the class FeatureHasherMapperTest method test3.
@Test
public void test3() throws Exception {
TableSchema schema = new TableSchema(new String[] { "double", "bool", "number", "str" }, new TypeInformation<?>[] { Types.DOUBLE(), Types.BOOLEAN(), Types.STRING(), Types.STRING() });
Params params = new Params().set(FeatureHasherParams.SELECTED_COLS, new String[] { "double", "bool", "number", "str" }).set(FeatureHasherParams.OUTPUT_COL, "output").set(FeatureHasherParams.NUM_FEATURES, 10).set(FeatureHasherParams.CATEGORICAL_COLS, new String[] { "double" });
FeatureHasherMapper mapper = new FeatureHasherMapper(schema, params);
mapper.open();
assertEquals(mapper.map(Row.of(1.1, true, "2", "A")).getField(4), new SparseVector(10, new int[] { 0, 5, 9 }, new double[] { 1.0, 2.0, 1.0 }));
assertEquals(mapper.map(Row.of(2.1, true, "1", "B")).getField(4), new SparseVector(10, new int[] { 1, 5, 6 }, new double[] { 2.0, 1.0, 1.0 }));
}
use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.
the class FeatureHasherMapperTest method test1.
@Test
public void test1() throws Exception {
TableSchema schema = new TableSchema(new String[] { "double", "bool", "number", "str" }, new TypeInformation<?>[] { Types.DOUBLE(), Types.BOOLEAN(), Types.STRING(), Types.STRING() });
Params params = new Params().set(FeatureHasherParams.SELECTED_COLS, new String[] { "double", "bool", "number", "str" }).set(FeatureHasherParams.OUTPUT_COL, "output").set(FeatureHasherParams.RESERVED_COLS, new String[] {});
FeatureHasherMapper mapper = new FeatureHasherMapper(schema, params);
mapper.open();
assertEquals(mapper.map(Row.of(1.1, true, "2", "A")).getField(0), new SparseVector(262144, new int[] { 62393, 85133, 120275, 214318 }, new double[] { 1.0, 1.0, 1.0, 1.1 }));
assertEquals(mapper.map(Row.of(2.1, true, "1", "A")).getField(0), new SparseVector(262144, new int[] { 76287, 85133, 120275, 214318 }, new double[] { 1.0, 1.0, 1.0, 2.1 }));
assertEquals(mapper.getOutputSchema(), new TableSchema(new String[] { "output" }, new TypeInformation<?>[] { VectorTypes.VECTOR }));
}
use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.
the class FeatureHasherMapperTest method test2.
@Test
public void test2() throws Exception {
TableSchema schema = new TableSchema(new String[] { "double", "bool", "number", "str" }, new TypeInformation<?>[] { Types.DOUBLE(), Types.BOOLEAN(), Types.STRING(), Types.STRING() });
Params params = new Params().set(FeatureHasherParams.SELECTED_COLS, new String[] { "double", "bool", "number", "str" }).set(FeatureHasherParams.OUTPUT_COL, "output").set(FeatureHasherParams.NUM_FEATURES, 10);
FeatureHasherMapper mapper = new FeatureHasherMapper(schema, params);
mapper.open();
assertEquals(mapper.map(Row.of(1.1, true, "2", "A")).getField(4), new SparseVector(10, new int[] { 5, 8, 9 }, new double[] { 2.0, 1.1, 1.0 }));
assertEquals(mapper.map(Row.of(2.1, true, "1", "B")).getField(4), new SparseVector(10, new int[] { 1, 5, 6, 8 }, new double[] { 1.0, 1.0, 1.0, 2.1 }));
assertEquals(mapper.getOutputSchema(), new TableSchema(new String[] { "double", "bool", "number", "str", "output" }, new TypeInformation<?>[] { Types.DOUBLE(), Types.BOOLEAN(), Types.STRING(), Types.STRING(), VectorTypes.VECTOR }));
}
use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.
the class OneHotModelMapperTest method testHandleInvalidVector.
@Test
public void testHandleInvalidVector() throws Exception {
Params params = new Params().set(OneHotPredictParams.ENCODE, HasEncodeWithoutWoe.Encode.VECTOR).set(OneHotPredictParams.HANDLE_INVALID, HasHandleInvalid.HandleInvalid.SKIP).set(OneHotPredictParams.SELECTED_COLS, new String[] { "cnt", "word", "docid" }).set(OneHotPredictParams.DROP_LAST, false);
OneHotModelMapper mapper = new OneHotModelMapper(modelSchema, dataSchema, params);
mapper.loadModel(model);
assertEquals(mapper.map(nullElseRow), Row.of(null, new SparseVector(8, new int[] { 7 }, new double[] { 1.0 }), new SparseVector(6, new int[] { 2 }, new double[] { 1.0 })));
mapper.loadModel(newModel);
assertEquals(mapper.map(nullElseRow), Row.of(null, null, new SparseVector(5, new int[] { 2 }, new double[] { 1.0 })));
}
use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.
the class OneHotModelMapperTest method testVector.
@Test
public void testVector() throws Exception {
Params params = new Params().set(OneHotPredictParams.ENCODE, HasEncodeWithoutWoe.Encode.VECTOR).set(OneHotPredictParams.SELECTED_COLS, new String[] { "cnt", "word", "docid" }).set(OneHotPredictParams.DROP_LAST, false);
OneHotModelMapper mapper = new OneHotModelMapper(modelSchema, dataSchema, params);
mapper.loadModel(model);
assertEquals(mapper.map(defaultRow), Row.of(new SparseVector(5, new int[] { 2 }, new double[] { 1.0 }), new SparseVector(9, new int[] { 5 }, new double[] { 1.0 }), new SparseVector(7, new int[] { 2 }, new double[] { 1.0 })));
assertEquals(mapper.map(nullElseRow), Row.of(new SparseVector(5, new int[] { 3 }, new double[] { 1.0 }), new SparseVector(9, new int[] { 8 }, new double[] { 1.0 }), new SparseVector(7, new int[] { 2 }, new double[] { 1.0 })));
mapper.loadModel(newModel);
assertEquals(mapper.map(defaultRow), Row.of(new SparseVector(4, new int[] { 2 }, new double[] { 1.0 }), new SparseVector(8, new int[] { 5 }, new double[] { 1.0 }), new SparseVector(6, new int[] { 2 }, new double[] { 1.0 })));
assertEquals(mapper.map(nullElseRow), Row.of(new SparseVector(4, new int[] { 3 }, new double[] { 1.0 }), new SparseVector(8, new int[] { 7 }, new double[] { 1.0 }), new SparseVector(6, new int[] { 2 }, new double[] { 1.0 })));
}
Aggregations