Java Code Examples for org.apache.flink.ml.api.misc.param.Params#fromJson()

The following examples show how to use org.apache.flink.ml.api.misc.param.Params#fromJson() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ParamsTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetAliasParam() {
	ParamInfo <String> predResultColName = ParamInfoFactory
		.createParamInfo("predResultColName", String.class)
		.setDescription("Column name of predicted result.")
		.setRequired()
		.setAlias(new String[] {"predColName", "outputColName"})
		.build();

	Params params = Params.fromJson("{\"predResultColName\":\"\\\"f0\\\"\"}");

	Assert.assertEquals("f0", params.get(predResultColName));

	params = Params.fromJson("{\"predResultColName\":\"\\\"f0\\\"\", \"predColName\":\"\\\"f0\\\"\"}");

	try {
		params.get(predResultColName);
		Assert.fail("failure");
	} catch (IllegalArgumentException ex) {
		Assert.assertTrue(ex.getMessage().startsWith("Duplicate parameters of predResultColName and predColName"));
	}
}
 
Example 2
Source File: ParseRowModel.java    From Alink with Apache License 2.0 6 votes vote down vote up
@Override
public void mapPartition(Iterable<Row> iterable,
                         Collector<Tuple2<DenseVector, double[]>> collector) throws Exception {
    DenseVector coefVector = null;
    double[] lossCurve = null;
    int taskId = getRuntimeContext().getIndexOfThisSubtask();
    if (taskId == 0) {
        for (Row row : iterable) {
            Params params = Params.fromJson((String)row.getField(0));
            coefVector = params.get(ModelParamName.COEF);
            lossCurve = params.get(ModelParamName.LOSS_CURVE);
        }

        if (coefVector != null) {
            collector.collect(Tuple2.of(coefVector, lossCurve));
        }
    }
}
 
Example 3
Source File: ParamsTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetAliasParam() {
	ParamInfo <String> predResultColName = ParamInfoFactory
		.createParamInfo("predResultColName", String.class)
		.setDescription("Column name of predicted result.")
		.setRequired()
		.setAlias(new String[] {"predColName", "outputColName"})
		.build();

	Params params = Params.fromJson("{\"predResultColName\":\"\\\"f0\\\"\"}");

	Assert.assertEquals("f0", params.get(predResultColName));

	params = Params.fromJson("{\"predResultColName\":\"\\\"f0\\\"\", \"predColName\":\"\\\"f0\\\"\"}");

	try {
		params.get(predResultColName);
		Assert.fail("failure");
	} catch (IllegalArgumentException ex) {
		Assert.assertTrue(ex.getMessage().startsWith("Duplicate parameters of predResultColName and predColName"));
	}
}
 
Example 4
Source File: MultiStringIndexerModelDataConverter.java    From Alink with Apache License 2.0 5 votes vote down vote up
@Override
public MultiStringIndexerModelData load(List<Row> rows) {
    MultiStringIndexerModelData modelData = new MultiStringIndexerModelData();
    modelData.tokenAndIndex = new ArrayList<>();
    modelData.tokenNumber = new HashMap<>();
    for (Row row : rows) {
        long colIndex = (Long) row.getField(0);
        if (colIndex < 0L) {
            modelData.meta = Params.fromJson((String) row.getField(1));
        } else {
            int columnIndex = ((Long) row.getField(0)).intValue();
            Long tokenIndex = Long.valueOf(String.valueOf(row.getField(2)));
            modelData.tokenAndIndex.add(Tuple3.of(columnIndex, (String) row.getField(1), tokenIndex));
            modelData.tokenNumber.merge(columnIndex, 1L, Long::sum);
        }
    }

    // To ensure that every columns has token number.
    int numFields = 0;
    if (modelData.meta != null) {
        numFields = modelData.meta.get(HasSelectedCols.SELECTED_COLS).length;
    }
    for (int i = 0; i < numFields; i++) {
        modelData.tokenNumber.merge(i, 0L, Long::sum);
    }
    return modelData;
}
 
Example 5
Source File: OneVsRestModelMapper.java    From Alink with Apache License 2.0 5 votes vote down vote up
public Params extractMeta(List<Row> modelRows) {
    Params meta = null;
    for (Row row : modelRows) {
        if (row.getField(1) != null) {
            meta = Params.fromJson((String) row.getField(1));
            break;
        }
    }
    return meta;
}
 
Example 6
Source File: LinearModelData.java    From Alink with Apache License 2.0 5 votes vote down vote up
public void loadOldFromatModel(List <Row> rows) {
	if (rows.get(0).getArity() == 4) { // old model format
		int m = rows.size();
		String metaStr = "";
		String[] strs = new String[m];
		for (Row row : rows) {
			int idx = ((Long) row.getField(0)).intValue();
			if (idx == 0L) {
				metaStr = (String) row.getField(1);
			} else {
				strs[idx - 1] = (String) row.getField(1);
			}
		}
		StringBuilder sbd = new StringBuilder();
		for (int i = 0; i < m; i++) {
			if (null == strs[i]) {
				break;
			}
			sbd.append(strs[i]);
		}
		Params meta = Params.fromJson(metaStr);
		List <String> data = Arrays.asList(sbd.toString());
		meta.set(ModelParamName.IS_OLD_FORMAT, true);
		deserializeModel(meta, data, recoverLabelsFromOldFormatModel(meta));
		return;
	} else {
		throw new RuntimeException("Not old format model");
	}
}
 
Example 7
Source File: ParamsTest.java    From Alink with Apache License 2.0 5 votes vote down vote up
@Test
public void testFromJson() {
    String json = "{\"enumType\":\"\\\"aAA\\\"\",\"appendType\":\"\\\"DENSE\\\"\"}";
    Params params = Params.fromJson(json);
    Assert.assertEquals(CalcType.aAA, params.get(HasEnumType.ENUM_TYPE));

    TestBatchOp op = new TestBatchOp(params);
    Assert.assertEquals(CalcType.aAA, op.getEnumType());
    Assert.assertEquals(CalcType.aAA, op.get(HasEnumType.ENUM_TYPE));
}
 
Example 8
Source File: ParamsTest.java    From Alink with Apache License 2.0 5 votes vote down vote up
@Test
public void testColorFromJson() {
    String json = "{\"enumTypeColor\":\"\\\"green\\\"\",\"appendType\":\"\\\"DENSE\\\"\"}";
    Params params = Params.fromJson(json);

    TestBatchOpColor testBatchOp = new TestBatchOpColor(params);
    Assert.assertEquals(Color.GREEN, testBatchOp.getEnumTypeColor());

    System.out.println(testBatchOp.getParams().toJson());
}
 
Example 9
Source File: BaseMetrics.java    From Alink with Apache License 2.0 2 votes vote down vote up
/**
 * Extract the params from a serialized row.
 *
 * @param row The Row generated by the serialize() function.
 */
public BaseMetrics(Row row) {
    params = Params.fromJson(row.getField(0).toString());
}