WEKA - Error on assigning Instances to kmeans.buil

2019-09-03 07:06发布

I'm pretty new in Weka framework. So far i find it pretty simple and easy to use and understand but i'm facing some problems i cannot understand. I'm trying to cluster a dataset from an csv file. I got a class that clusters the dataset using kmeans and as far as i read in the Weka's wiki (http://weka.wikispaces.com/) the class works fine. The only problem was that it was reading an arff file and i needed to read from a csv file. So far so good. The problem is when i assign the instances from the dataset to kmeans.buildClusterer(data) method. Then i get the following exception:

Exception in thread "main" java.lang.NoClassDefFoundError: org/pentaho/packageManagement/PackageManager 
        at weka.core.WekaPackageManager.<clinit>(WekaPackageManager.java:86) 
        at weka.core.Utils.readProperties(Utils.java:142) 
        at weka.core.Capabilities.<init>(Capabilities.java:261) 
        at weka.clusterers.AbstractClusterer.getCapabilities(AbstractClusterer.java:179) 
        at weka.clusterers.SimpleKMeans.getCapabilities(SimpleKMeans.java:289) 
        at weka.clusterers.SimpleKMeans.buildClusterer(SimpleKMeans.java:441) 
        at weka_examples.ClusteringDemo.<init>(ClusteringDemo.java:48) 
        at weka_examples.ClusteringDemo.main(ClusteringDemo.java:70) 
Caused by: java.lang.ClassNotFoundException: org.pentaho.packageManagement.PackageManager 
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366) 
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355) 
        at java.security.AccessController.doPrivileged(Native Method) 
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354) 
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425) 
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) 
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358) 
        ... 8 more 
Java Result: 1 

The code is the following:

import weka.core.Instances; 
import weka.clusterers.DensityBasedClusterer; 
import weka.clusterers.ClusterEvaluation; 
import java.io.File; 
import weka.clusterers.SimpleKMeans; 
import weka.core.converters.CSVLoader; 

/** 
 * An example class that shows the use of Weka clusterers from Java. 
 * 
 * @author FracPete 
 */ 
public class ClusteringDemo { 

    /** 
     * Run clusterers 
     * 
     * @param filename the name of the ARFF file to run on 
     */ 
    public ClusteringDemo(String filename) throws Exception { 
        ClusterEvaluation eval; 
        Instances data; 
        String[] options; 
        DensityBasedClusterer cl; 

        String Origem = filename; 

   // data = new Instances(new BufferedReader(new FileReader(filename))); 
        // load CSV 
        CSVLoader loader = new CSVLoader(); 
        loader.setSource(new File(Origem)); 
        data = loader.getDataSet(); 

        SimpleKMeans kmeans = new SimpleKMeans(); 

        kmeans.setSeed(10); 

// This is the important parameter to set 
        kmeans.setPreserveInstancesOrder(true); 
        kmeans.setNumClusters(5); 
        kmeans.buildClusterer(data); 

// This array returns the cluster number (starting with 0) for each instance 
// The array has as many elements as the number of instances 
        int[] assignments = kmeans.getAssignments(); 

        int i = 0; 
        for (int clusterNum : assignments) { 
            System.out.printf("Instance %d -> Cluster %d", i, clusterNum); 
            i++; 
        } 
    } 

    /** 
     * usage: ClusteringDemo arff-file 
     */ 
    public static void main(String[] args) throws Exception { 

        /*if (args.length != 1) { 
         System.out.println("usage: " + ClusteringDemo.class.getName() + " <arff-file>"); 
         System.exit(1); 
         }*/ 
        new ClusteringDemo("Teste/Query1.csv"); 
    } 
} 

I included the binary file: weka-dev-3.7.10 Am i missing something important?

Kind regards

1条回答
ら.Afraid
2楼-- · 2019-09-03 07:57

I finally managed to answer this question. It turns out that, from version 3.7.2, weka core is much more compact, meaning that some packages i need were not present in the weka-dev. The solution is simple: add to the classpath the weka.jar and not the weka-dev package. Alternatively the missing packages can also be installed http://weka.wikispaces.com/How+do+I+use+the+package+manager%3F

Best regards

查看更多
登录 后发表回答