use of com.alibaba.alink.pipeline.ModelBase in project Alink by alibaba.
the class GridSearchCVTest method testSplit.
@Test
public void testSplit() throws Exception {
List<Row> rows = Arrays.asList(Row.of(1.0, "A", 0, 0, 0), Row.of(2.0, "B", 1, 1, 0), Row.of(3.0, "C", 2, 2, 1), Row.of(4.0, "D", 3, 3, 1), Row.of(1.0, "A", 0, 0, 0), Row.of(2.0, "B", 1, 1, 0), Row.of(3.0, "C", 2, 2, 1), Row.of(4.0, "D", 3, 3, 1), Row.of(1.0, "A", 0, 0, 0), Row.of(2.0, "B", 1, 1, 0), Row.of(3.0, "C", 2, 2, 1));
String[] colNames = new String[] { "f0", "f1", "f2", "f3", "label" };
MemSourceBatchOp data = new MemSourceBatchOp(rows, colNames);
String[] featureColNames = new String[] { colNames[0], colNames[1], colNames[2], colNames[3] };
String[] categoricalColNames = new String[] { colNames[1] };
String labelColName = colNames[4];
RandomForestClassifier rf = new RandomForestClassifier().setFeatureCols(featureColNames).setCategoricalCols(categoricalColNames).setLabelCol(labelColName).setPredictionCol("pred_result").setPredictionDetailCol("pred_detail").setSubsamplingRatio(1.0);
Pipeline pipeline = new Pipeline(rf);
ParamGrid paramGrid = new ParamGrid().addGrid(rf, "SUBSAMPLING_RATIO", new Double[] { 1.0 }).addGrid(rf, "NUM_TREES", new Integer[] { 3 });
BinaryClassificationTuningEvaluator tuning_evaluator = new BinaryClassificationTuningEvaluator().setLabelCol(labelColName).setPredictionDetailCol("pred_detail").setTuningBinaryClassMetric("Accuracy");
GridSearchTVSplit cv = new GridSearchTVSplit().setEstimator(pipeline).setParamGrid(paramGrid).setTuningEvaluator(tuning_evaluator).setTrainRatio(0.8);
ModelBase cvModel = cv.fit(data);
cvModel.transform(data).print();
}
use of com.alibaba.alink.pipeline.ModelBase in project Alink by alibaba.
the class OneVsRest method fit.
@Override
public OneVsRestModel fit(BatchOperator<?> input) {
String labelColName = classifier.getParams().get(HasLabelCol.LABEL_COL);
BatchOperator<?> allLabels = getAllLabels(input, labelColName);
int numClasses = getNumClass();
int labelColIdx = TableUtil.findColIndexWithAssertAndHint(input.getColNames(), labelColName);
TypeInformation<?> labelColType = input.getColTypes()[labelColIdx];
ModelBase<?>[] models = new ModelBase<?>[numClasses];
for (int iCls = 0; iCls < numClasses; iCls++) {
this.classifier.set(HasPositiveLabelValueString.POS_LABEL_VAL_STR, "1");
BatchOperator<?> trainData = generateTrainData(input, allLabels, iCls, labelColIdx);
models[iCls] = this.classifier.fit(trainData);
}
Table modelData = unionAllModels(models);
Params meta = new Params().set(ModelParamName.NUM_CLASSES, numClasses).set(ModelParamName.BIN_CLS_CLASS_NAME, this.classifier.getClass().getCanonicalName()).set(ModelParamName.BIN_CLS_PARAMS, this.classifier.getParams().toJson()).set(ModelParamName.LABEL_TYPE_NAME, FlinkTypeConverter.getTypeString(labelColType)).set(ModelParamName.MODEL_COL_NAMES, models[0].getModelData().getSchema().getFieldNames()).set(ModelParamName.MODEL_COL_TYPES, toJdbcColTypes(models[0].getModelData().getSchema().getFieldTypes()));
Table modelMeta = createModelMeta(meta, allLabels);
OneVsRestModel oneVsRestModel = new OneVsRestModel(classifier.getParams().clone().merge(this.getParams()));
oneVsRestModel.setModelData(BatchOperator.fromTable(TableUtil.concatTables(new Table[] { modelMeta, modelData, allLabels.getOutputTable() }, getMLEnvironmentId())));
return oneVsRestModel;
}
Aggregations