Search in sources :

Example 1 with DecisionTreeModelInfo

use of com.alibaba.alink.operator.common.tree.TreeModelInfo.DecisionTreeModelInfo in project Alink by alibaba.

the class Chap09 method c_5.

static void c_5() throws Exception {
    BatchOperator train_data = new AkSourceBatchOp().setFilePath(DATA_DIR + TRAIN_FILE);
    BatchOperator test_data = new AkSourceBatchOp().setFilePath(DATA_DIR + TEST_FILE);
    for (TreeType treeType : new TreeType[] { TreeType.GINI, TreeType.INFOGAIN, TreeType.INFOGAINRATIO }) {
        BatchOperator<?> model = train_data.link(new DecisionTreeTrainBatchOp().setTreeType(treeType).setFeatureCols(FEATURE_COL_NAMES).setCategoricalCols(FEATURE_COL_NAMES).setLabelCol(LABEL_COL_NAME).lazyPrintModelInfo("< " + treeType.toString() + " >").lazyCollectModelInfo(new Consumer<DecisionTreeModelInfo>() {

            @Override
            public void accept(DecisionTreeModelInfo decisionTreeModelInfo) {
                try {
                    decisionTreeModelInfo.saveTreeAsImage(DATA_DIR + "tree_" + treeType.toString() + ".jpg", true);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }));
        DecisionTreePredictBatchOp predictor = new DecisionTreePredictBatchOp().setPredictionCol(PREDICTION_COL_NAME).setPredictionDetailCol(PRED_DETAIL_COL_NAME);
        predictor.linkFrom(model, test_data);
        predictor.link(new EvalBinaryClassBatchOp().setPositiveLabelValueString("p").setLabelCol(LABEL_COL_NAME).setPredictionDetailCol(PRED_DETAIL_COL_NAME).lazyPrintMetrics("< " + treeType.toString() + " >"));
    }
    BatchOperator.execute();
}
Also used : TreeType(com.alibaba.alink.params.shared.tree.HasIndividualTreeType.TreeType) AkSourceBatchOp(com.alibaba.alink.operator.batch.source.AkSourceBatchOp) Consumer(java.util.function.Consumer) DecisionTreeModelInfo(com.alibaba.alink.operator.common.tree.TreeModelInfo.DecisionTreeModelInfo) IOException(java.io.IOException) DecisionTreePredictBatchOp(com.alibaba.alink.operator.batch.classification.DecisionTreePredictBatchOp) BatchOperator(com.alibaba.alink.operator.batch.BatchOperator) DecisionTreeTrainBatchOp(com.alibaba.alink.operator.batch.classification.DecisionTreeTrainBatchOp) EvalBinaryClassBatchOp(com.alibaba.alink.operator.batch.evaluation.EvalBinaryClassBatchOp)

Example 2 with DecisionTreeModelInfo

use of com.alibaba.alink.operator.common.tree.TreeModelInfo.DecisionTreeModelInfo in project Alink by alibaba.

the class Chap09 method c_2_5.

static void c_2_5() throws Exception {
    MemSourceBatchOp source = new MemSourceBatchOp(new Row[] { Row.of("sunny", 85.0, 85.0, false, "no"), Row.of("sunny", 80.0, 90.0, true, "no"), Row.of("overcast", 83.0, 78.0, false, "yes"), Row.of("rainy", 70.0, 96.0, false, "yes"), Row.of("rainy", 68.0, 80.0, false, "yes"), Row.of("rainy", 65.0, 70.0, true, "no"), Row.of("overcast", 64.0, 65.0, true, "yes"), Row.of("sunny", 72.0, 95.0, false, "no"), Row.of("sunny", 69.0, 70.0, false, "yes"), Row.of("rainy", 75.0, 80.0, false, "yes"), Row.of("sunny", 75.0, 70.0, true, "yes"), Row.of("overcast", 72.0, 90.0, true, "yes"), Row.of("overcast", 81.0, 75.0, false, "yes"), Row.of("rainy", 71.0, 80.0, true, "no") }, new String[] { "Outlook", "Temperature", "Humidity", "Windy", "Play" });
    source.lazyPrint(-1);
    source.link(new C45TrainBatchOp().setFeatureCols("Outlook", "Temperature", "Humidity", "Windy").setCategoricalCols("Outlook", "Windy").setLabelCol("Play").lazyPrintModelInfo().lazyCollectModelInfo(new Consumer<DecisionTreeModelInfo>() {

        @Override
        public void accept(DecisionTreeModelInfo decisionTreeModelInfo) {
            try {
                decisionTreeModelInfo.saveTreeAsImage(DATA_DIR + "weather_tree_model.png", true);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }));
    BatchOperator.execute();
}
Also used : MemSourceBatchOp(com.alibaba.alink.operator.batch.source.MemSourceBatchOp) C45TrainBatchOp(com.alibaba.alink.operator.batch.classification.C45TrainBatchOp) Consumer(java.util.function.Consumer) DecisionTreeModelInfo(com.alibaba.alink.operator.common.tree.TreeModelInfo.DecisionTreeModelInfo) IOException(java.io.IOException)

Example 3 with DecisionTreeModelInfo

use of com.alibaba.alink.operator.common.tree.TreeModelInfo.DecisionTreeModelInfo in project Alink by alibaba.

the class Chap02 method c_6.

static void c_6() throws Exception {
    MemSourceBatchOp source = new MemSourceBatchOp(new Row[] { Row.of("sunny", 85.0, 85.0, false, "no"), Row.of("sunny", 80.0, 90.0, true, "no"), Row.of("overcast", 83.0, 78.0, false, "yes"), Row.of("rainy", 70.0, 96.0, false, "yes"), Row.of("rainy", 68.0, 80.0, false, "yes"), Row.of("rainy", 65.0, 70.0, true, "no"), Row.of("overcast", 64.0, 65.0, true, "yes"), Row.of("sunny", 72.0, 95.0, false, "no"), Row.of("sunny", 69.0, 70.0, false, "yes"), Row.of("rainy", 75.0, 80.0, false, "yes"), Row.of("sunny", 75.0, 70.0, true, "yes"), Row.of("overcast", 72.0, 90.0, true, "yes"), Row.of("overcast", 81.0, 75.0, false, "yes"), Row.of("rainy", 71.0, 80.0, true, "no") }, new String[] { "outlook", "Temperature", "Humidity", "Windy", "play" });
    source.link(new C45TrainBatchOp().setFeatureCols("outlook", "Temperature", "Humidity", "Windy").setCategoricalCols("outlook", "Windy").setLabelCol("play")).link(new AkSinkBatchOp().setFilePath(DATA_DIR + TREE_MODEL_FILE).setOverwriteSink(true));
    BatchOperator.execute();
    new AkSourceBatchOp().setFilePath(DATA_DIR + TREE_MODEL_FILE).link(new DecisionTreeModelInfoBatchOp().lazyPrintModelInfo().lazyCollectModelInfo(new Consumer<DecisionTreeModelInfo>() {

        @Override
        public void accept(DecisionTreeModelInfo decisionTreeModelInfo) {
            try {
                decisionTreeModelInfo.saveTreeAsImage(DATA_DIR + "tree_model.png", true);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }));
    BatchOperator.execute();
    MemSourceBatchOp train_set = new MemSourceBatchOp(new Row[] { Row.of(2009, 0.5), Row.of(2010, 9.36), Row.of(2011, 52.0), Row.of(2012, 191.0), Row.of(2013, 350.0), Row.of(2014, 571.0), Row.of(2015, 912.0), Row.of(2016, 1207.0), Row.of(2017, 1682.0) }, new String[] { "x", "gmv" });
    Pipeline pipeline = new Pipeline().add(new Select().setClause("*, x*x AS x2")).add(new LinearRegression().setFeatureCols("x", "x2").setLabelCol("gmv").setPredictionCol("pred"));
    pipeline.fit(train_set).save(DATA_DIR + PIPELINE_MODEL_FILE, true);
    BatchOperator.execute();
    PipelineModel pipelineModel = PipelineModel.load(DATA_DIR + PIPELINE_MODEL_FILE);
    TransformerBase<?>[] stages = pipelineModel.getTransformers();
    for (int i = 0; i < stages.length; i++) {
        System.out.println(String.valueOf(i) + "\t" + stages[i]);
    }
    ((LinearRegressionModel) stages[1]).getModelData().link(new LinearRegModelInfoBatchOp().lazyPrintModelInfo());
    BatchOperator.execute();
}
Also used : C45TrainBatchOp(com.alibaba.alink.operator.batch.classification.C45TrainBatchOp) LinearRegModelInfoBatchOp(com.alibaba.alink.operator.batch.regression.LinearRegModelInfoBatchOp) IOException(java.io.IOException) DecisionTreeModelInfoBatchOp(com.alibaba.alink.operator.batch.classification.DecisionTreeModelInfoBatchOp) Pipeline(com.alibaba.alink.pipeline.Pipeline) PipelineModel(com.alibaba.alink.pipeline.PipelineModel) MemSourceBatchOp(com.alibaba.alink.operator.batch.source.MemSourceBatchOp) AkSourceBatchOp(com.alibaba.alink.operator.batch.source.AkSourceBatchOp) Consumer(java.util.function.Consumer) DecisionTreeModelInfo(com.alibaba.alink.operator.common.tree.TreeModelInfo.DecisionTreeModelInfo) Select(com.alibaba.alink.pipeline.sql.Select) AkSinkBatchOp(com.alibaba.alink.operator.batch.sink.AkSinkBatchOp) LinearRegression(com.alibaba.alink.pipeline.regression.LinearRegression) TransformerBase(com.alibaba.alink.pipeline.TransformerBase)

Aggregations

DecisionTreeModelInfo (com.alibaba.alink.operator.common.tree.TreeModelInfo.DecisionTreeModelInfo)3 IOException (java.io.IOException)3 Consumer (java.util.function.Consumer)3 C45TrainBatchOp (com.alibaba.alink.operator.batch.classification.C45TrainBatchOp)2 AkSourceBatchOp (com.alibaba.alink.operator.batch.source.AkSourceBatchOp)2 MemSourceBatchOp (com.alibaba.alink.operator.batch.source.MemSourceBatchOp)2 BatchOperator (com.alibaba.alink.operator.batch.BatchOperator)1 DecisionTreeModelInfoBatchOp (com.alibaba.alink.operator.batch.classification.DecisionTreeModelInfoBatchOp)1 DecisionTreePredictBatchOp (com.alibaba.alink.operator.batch.classification.DecisionTreePredictBatchOp)1 DecisionTreeTrainBatchOp (com.alibaba.alink.operator.batch.classification.DecisionTreeTrainBatchOp)1 EvalBinaryClassBatchOp (com.alibaba.alink.operator.batch.evaluation.EvalBinaryClassBatchOp)1 LinearRegModelInfoBatchOp (com.alibaba.alink.operator.batch.regression.LinearRegModelInfoBatchOp)1 AkSinkBatchOp (com.alibaba.alink.operator.batch.sink.AkSinkBatchOp)1 TreeType (com.alibaba.alink.params.shared.tree.HasIndividualTreeType.TreeType)1 Pipeline (com.alibaba.alink.pipeline.Pipeline)1 PipelineModel (com.alibaba.alink.pipeline.PipelineModel)1 TransformerBase (com.alibaba.alink.pipeline.TransformerBase)1 LinearRegression (com.alibaba.alink.pipeline.regression.LinearRegression)1 Select (com.alibaba.alink.pipeline.sql.Select)1