Search in sources :

Example 1 with LibSvmSourceBatchOp

use of com.alibaba.alink.operator.batch.source.LibSvmSourceBatchOp in project Alink by alibaba.

the class Chap03 method c_2_2.

static void c_2_2() throws Exception {
    new TsvSourceBatchOp().setFilePath(LOCAL_DIR + "u.data").setSchemaStr("user_id long, item_id long, rating float, ts long").firstN(5).print();
    new TextSourceBatchOp().setFilePath(LOCAL_DIR + "iris.scale").firstN(5).print();
    new LibSvmSourceBatchOp().setFilePath(LOCAL_DIR + "iris.scale").firstN(5).lazyPrint(5, "< read by LibSvmSourceBatchOp >").link(new VectorNormalizeBatchOp().setSelectedCol("features")).print();
}
Also used : LibSvmSourceBatchOp(com.alibaba.alink.operator.batch.source.LibSvmSourceBatchOp) VectorNormalizeBatchOp(com.alibaba.alink.operator.batch.dataproc.vector.VectorNormalizeBatchOp) TsvSourceBatchOp(com.alibaba.alink.operator.batch.source.TsvSourceBatchOp) TextSourceBatchOp(com.alibaba.alink.operator.batch.source.TextSourceBatchOp)

Example 2 with LibSvmSourceBatchOp

use of com.alibaba.alink.operator.batch.source.LibSvmSourceBatchOp in project Alink by alibaba.

the class Chap23 method c_1.

static void c_1() throws Exception {
    BatchOperator<?> train_set = new LibSvmSourceBatchOp().setFilePath(ORIGIN_DATA_DIR + "train" + File.separator + "labeledBow.feat").setStartIndex(0);
    train_set.lazyPrint(1, "train_set");
    train_set.groupBy("label", "label, COUNT(label) AS cnt").orderBy("label", 100).lazyPrint(-1, "labels of train_set");
    BatchOperator<?> test_set = new LibSvmSourceBatchOp().setFilePath(ORIGIN_DATA_DIR + "test" + File.separator + "labeledBow.feat").setStartIndex(0);
    train_set = train_set.select("CASE WHEN label>5 THEN 'pos' ELSE 'neg' END AS label, " + "features AS " + VECTOR_COL_NAME);
    test_set = test_set.select("CASE WHEN label>5 THEN 'pos' ELSE 'neg' END AS label, " + "features AS " + VECTOR_COL_NAME);
    train_set.lazyPrint(1, "train_set");
    new NaiveBayesTextClassifier().setModelType("Multinomial").setVectorCol(VECTOR_COL_NAME).setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).setPredictionDetailCol(PRED_DETAIL_COL_NAME).enableLazyPrintModelInfo().fit(train_set).transform(test_set).link(new EvalBinaryClassBatchOp().setPositiveLabelValueString("pos").setLabelCol(LABEL_COL_NAME).setPredictionDetailCol(PRED_DETAIL_COL_NAME).lazyPrintMetrics("NaiveBayesTextClassifier + Multinomial"));
    BatchOperator.execute();
    new Pipeline().add(new Binarizer().setSelectedCol(VECTOR_COL_NAME).enableLazyPrintTransformData(1, "After Binarizer")).add(new NaiveBayesTextClassifier().setModelType("Bernoulli").setVectorCol(VECTOR_COL_NAME).setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).setPredictionDetailCol(PRED_DETAIL_COL_NAME).enableLazyPrintModelInfo()).fit(train_set).transform(test_set).link(new EvalBinaryClassBatchOp().setPositiveLabelValueString("pos").setLabelCol(LABEL_COL_NAME).setPredictionDetailCol(PRED_DETAIL_COL_NAME).lazyPrintMetrics("Binarizer + NaiveBayesTextClassifier + Bernoulli"));
    BatchOperator.execute();
    new LogisticRegression().setVectorCol(VECTOR_COL_NAME).setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).setPredictionDetailCol(PRED_DETAIL_COL_NAME).enableLazyPrintTrainInfo("< LR train info >").enableLazyPrintModelInfo("< LR model info >").fit(train_set).transform(test_set).link(new EvalBinaryClassBatchOp().setPositiveLabelValueString("pos").setLabelCol(LABEL_COL_NAME).setPredictionDetailCol(PRED_DETAIL_COL_NAME).lazyPrintMetrics("LogisticRegression"));
    BatchOperator.execute();
    AlinkGlobalConfiguration.setPrintProcessInfo(true);
    LogisticRegression lr = new LogisticRegression().setVectorCol(VECTOR_COL_NAME).setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).setPredictionDetailCol(PRED_DETAIL_COL_NAME);
    GridSearchCV gridSearch = new GridSearchCV().setEstimator(new Pipeline().add(lr)).setParamGrid(new ParamGrid().addGrid(lr, LogisticRegression.MAX_ITER, new Integer[] { 10, 20, 30, 40, 50, 60, 80, 100 })).setTuningEvaluator(new BinaryClassificationTuningEvaluator().setLabelCol(LABEL_COL_NAME).setPositiveLabelValueString("pos").setPredictionDetailCol(PRED_DETAIL_COL_NAME).setTuningBinaryClassMetric(TuningBinaryClassMetric.AUC)).setNumFolds(6).enableLazyPrintTrainInfo();
    GridSearchCVModel bestModel = gridSearch.fit(train_set);
    bestModel.transform(test_set).link(new EvalBinaryClassBatchOp().setPositiveLabelValueString("pos").setLabelCol(LABEL_COL_NAME).setPredictionDetailCol(PRED_DETAIL_COL_NAME).lazyPrintMetrics("LogisticRegression"));
    BatchOperator.execute();
}
Also used : ParamGrid(com.alibaba.alink.pipeline.tuning.ParamGrid) LibSvmSourceBatchOp(com.alibaba.alink.operator.batch.source.LibSvmSourceBatchOp) GridSearchCV(com.alibaba.alink.pipeline.tuning.GridSearchCV) NaiveBayesTextClassifier(com.alibaba.alink.pipeline.classification.NaiveBayesTextClassifier) LogisticRegression(com.alibaba.alink.pipeline.classification.LogisticRegression) GridSearchCVModel(com.alibaba.alink.pipeline.tuning.GridSearchCVModel) Binarizer(com.alibaba.alink.pipeline.feature.Binarizer) BinaryClassificationTuningEvaluator(com.alibaba.alink.pipeline.tuning.BinaryClassificationTuningEvaluator) EvalBinaryClassBatchOp(com.alibaba.alink.operator.batch.evaluation.EvalBinaryClassBatchOp) Pipeline(com.alibaba.alink.pipeline.Pipeline)

Aggregations

LibSvmSourceBatchOp (com.alibaba.alink.operator.batch.source.LibSvmSourceBatchOp)2 VectorNormalizeBatchOp (com.alibaba.alink.operator.batch.dataproc.vector.VectorNormalizeBatchOp)1 EvalBinaryClassBatchOp (com.alibaba.alink.operator.batch.evaluation.EvalBinaryClassBatchOp)1 TextSourceBatchOp (com.alibaba.alink.operator.batch.source.TextSourceBatchOp)1 TsvSourceBatchOp (com.alibaba.alink.operator.batch.source.TsvSourceBatchOp)1 Pipeline (com.alibaba.alink.pipeline.Pipeline)1 LogisticRegression (com.alibaba.alink.pipeline.classification.LogisticRegression)1 NaiveBayesTextClassifier (com.alibaba.alink.pipeline.classification.NaiveBayesTextClassifier)1 Binarizer (com.alibaba.alink.pipeline.feature.Binarizer)1 BinaryClassificationTuningEvaluator (com.alibaba.alink.pipeline.tuning.BinaryClassificationTuningEvaluator)1 GridSearchCV (com.alibaba.alink.pipeline.tuning.GridSearchCV)1 GridSearchCVModel (com.alibaba.alink.pipeline.tuning.GridSearchCVModel)1 ParamGrid (com.alibaba.alink.pipeline.tuning.ParamGrid)1