Search in sources :

Example 1 with KnnClassifier

use of com.alibaba.alink.pipeline.classification.KnnClassifier in project Alink by alibaba.

the class Chap13 method c_6.

static void c_6() throws Exception {
    BatchOperator.setParallelism(4);
    AkSourceBatchOp train_data = new AkSourceBatchOp().setFilePath(DATA_DIR + SPARSE_TRAIN_FILE);
    AkSourceBatchOp test_data = new AkSourceBatchOp().setFilePath(DATA_DIR + SPARSE_TEST_FILE);
    new KnnClassifier().setK(3).setVectorCol(VECTOR_COL_NAME).setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).fit(train_data).transform(test_data).link(new EvalMultiClassBatchOp().setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).lazyPrintMetrics("KnnClassifier - 3 - EUCLIDEAN"));
    BatchOperator.execute();
    new KnnClassifier().setDistanceType(DistanceType.COSINE).setK(3).setVectorCol(VECTOR_COL_NAME).setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).fit(train_data).transform(test_data).link(new EvalMultiClassBatchOp().setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).lazyPrintMetrics("KnnClassifier - 3 - COSINE"));
    BatchOperator.execute();
    new KnnClassifier().setK(7).setVectorCol(VECTOR_COL_NAME).setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).fit(train_data).transform(test_data).link(new EvalMultiClassBatchOp().setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).lazyPrintMetrics("KnnClassifier - 7 - EUCLIDEAN"));
    BatchOperator.execute();
}
Also used : EvalMultiClassBatchOp(com.alibaba.alink.operator.batch.evaluation.EvalMultiClassBatchOp) AkSourceBatchOp(com.alibaba.alink.operator.batch.source.AkSourceBatchOp) KnnClassifier(com.alibaba.alink.pipeline.classification.KnnClassifier)

Example 2 with KnnClassifier

use of com.alibaba.alink.pipeline.classification.KnnClassifier in project Alink by alibaba.

the class Chap19 method c_4.

static void c_4() throws Exception {
    AkSourceBatchOp dense_train_data = new AkSourceBatchOp().setFilePath(DATA_DIR + DENSE_TRAIN_FILE);
    AkSourceBatchOp dense_test_data = new AkSourceBatchOp().setFilePath(DATA_DIR + DENSE_TEST_FILE);
    AkSourceBatchOp sparse_train_data = new AkSourceBatchOp().setFilePath(DATA_DIR + SPARSE_TRAIN_FILE);
    AkSourceBatchOp sparse_test_data = new AkSourceBatchOp().setFilePath(DATA_DIR + SPARSE_TEST_FILE);
    Stopwatch sw = new Stopwatch();
    sw.reset();
    sw.start();
    new KnnClassifier().setK(3).setVectorCol(VECTOR_COL_NAME).setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).fit(dense_train_data).transform(dense_test_data).link(new EvalMultiClassBatchOp().setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).lazyPrintMetrics("KnnClassifier Dense"));
    BatchOperator.execute();
    sw.stop();
    System.out.println(sw.getElapsedTimeSpan());
    sw.reset();
    sw.start();
    new KnnClassifier().setK(3).setVectorCol(VECTOR_COL_NAME).setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).fit(sparse_train_data).transform(sparse_test_data).link(new EvalMultiClassBatchOp().setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).lazyPrintMetrics("KnnClassifier Sparse"));
    BatchOperator.execute();
    sw.stop();
    System.out.println(sw.getElapsedTimeSpan());
    sw.reset();
    sw.start();
    new Pipeline().add(new PCA().setK(39).setCalculationType(CalculationType.COV).setVectorCol(VECTOR_COL_NAME).setPredictionCol(VECTOR_COL_NAME)).add(new KnnClassifier().setK(3).setVectorCol(VECTOR_COL_NAME).setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME)).fit(dense_train_data).transform(dense_test_data).link(new EvalMultiClassBatchOp().setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).lazyPrintMetrics("Knn with PCA Dense"));
    BatchOperator.execute();
    sw.stop();
    System.out.println(sw.getElapsedTimeSpan());
    sw.reset();
    sw.start();
    new Pipeline().add(new PCA().setK(39).setCalculationType(CalculationType.COV).setVectorCol(VECTOR_COL_NAME).setPredictionCol(VECTOR_COL_NAME)).add(new KnnClassifier().setK(3).setVectorCol(VECTOR_COL_NAME).setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME)).fit(sparse_train_data).transform(sparse_test_data).link(new EvalMultiClassBatchOp().setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).lazyPrintMetrics("Knn with PCA Sparse"));
    BatchOperator.execute();
    sw.stop();
    System.out.println(sw.getElapsedTimeSpan());
    sw.reset();
    sw.start();
    new Pipeline().add(new PCAModel().setVectorCol(VECTOR_COL_NAME).setPredictionCol(VECTOR_COL_NAME).setModelData(new AkSourceBatchOp().setFilePath(DATA_DIR + PCA_MODEL_FILE))).add(new KnnClassifier().setK(3).setVectorCol(VECTOR_COL_NAME).setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME)).fit(dense_train_data).transform(dense_test_data).link(new EvalMultiClassBatchOp().setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).lazyPrintMetrics("Knn PCAModel Dense"));
    BatchOperator.execute();
    sw.stop();
    System.out.println(sw.getElapsedTimeSpan());
    sw.reset();
    sw.start();
    new Pipeline().add(new PCAModel().setVectorCol(VECTOR_COL_NAME).setPredictionCol(VECTOR_COL_NAME).setModelData(new AkSourceBatchOp().setFilePath(DATA_DIR + PCA_MODEL_FILE))).add(new KnnClassifier().setK(3).setVectorCol(VECTOR_COL_NAME).setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME)).fit(sparse_train_data).transform(sparse_test_data).link(new EvalMultiClassBatchOp().setLabelCol(LABEL_COL_NAME).setPredictionCol(PREDICTION_COL_NAME).lazyPrintMetrics("Knn PCAModel Sparse"));
    BatchOperator.execute();
    sw.stop();
    System.out.println(sw.getElapsedTimeSpan());
}
Also used : PCAModel(com.alibaba.alink.pipeline.feature.PCAModel) EvalMultiClassBatchOp(com.alibaba.alink.operator.batch.evaluation.EvalMultiClassBatchOp) AkSourceBatchOp(com.alibaba.alink.operator.batch.source.AkSourceBatchOp) Stopwatch(com.alibaba.alink.common.utils.Stopwatch) KnnClassifier(com.alibaba.alink.pipeline.classification.KnnClassifier) Pipeline(com.alibaba.alink.pipeline.Pipeline) PCA(com.alibaba.alink.pipeline.feature.PCA)

Aggregations

EvalMultiClassBatchOp (com.alibaba.alink.operator.batch.evaluation.EvalMultiClassBatchOp)2 AkSourceBatchOp (com.alibaba.alink.operator.batch.source.AkSourceBatchOp)2 KnnClassifier (com.alibaba.alink.pipeline.classification.KnnClassifier)2 Stopwatch (com.alibaba.alink.common.utils.Stopwatch)1 Pipeline (com.alibaba.alink.pipeline.Pipeline)1 PCA (com.alibaba.alink.pipeline.feature.PCA)1 PCAModel (com.alibaba.alink.pipeline.feature.PCAModel)1