Java调用多标签学习开源库Mulan

Java调用多标签学习开源库Mulan

环境准备

  • 下载Mulan并导入Jar包。Mulan下载地址:http://mulan.sourceforge.net/download.html
  • 下载多标签数据集。下载地址:http://mulan.sourceforge.net/datasets-mlc.html

源码

代码为mulan提供的ML-KNN算法的实验代码,本源码原作者为:Eleftherios Spyromitros-Xioufis ([email protected]),稍做了改动。

import mulan.data.MultiLabelInstances;
import mulan.evaluation.Evaluator;
import mulan.evaluation.MultipleEvaluation;
import mulan.evaluation.measure.*;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MLkNN {

    public static void main(String[] args) {
        try {
            String path = "../dataset/bookmarks/";
            System.out.println("Loading the data set");
            MultiLabelInstances dataSet = new MultiLabelInstances(path + "bookmarks.arff", path + "bookmarks.xml");

            Evaluator eval = new Evaluator();
            MultipleEvaluation results;
            List measures = new ArrayList<>(5);
            measures.add(new HammingLoss());
            measures.add(new OneError());
            measures.add(new Coverage());
            measures.add(new RankingLoss());
            measures.add(new AveragePrecision());

            int numOfNeighbors;
            for (int i = 8; i <= 12; i++) {
                System.out.println("MLkNN Experiment for " + i + " neighbors:");
                numOfNeighbors = i;
                double smooth = 1.0;
                mulan.classifier.lazy.MLkNN mlknn = new mulan.classifier.lazy.MLkNN(numOfNeighbors, smooth);
                // mlknn.setDebug(true);
                results = eval.crossValidate(mlknn, dataSet, measures, 10);
                System.out.println(results);
            }
        } catch (Exception ex) {
            Logger.getLogger(MLkNN.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

你可能感兴趣的:(机器学习)