Package trust.weka4jason

Source Code of trust.weka4jason.WekaClusterClassifier

package trust.weka4jason;

import java.io.File;

import weka.classifiers.Classifier;
import weka.classifiers.trees.J48;
import weka.core.DenseInstance;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ArffLoader;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.AddCluster;

public class WekaClusterClassifier extends WekaClassifier{

 
  public WekaClusterClassifier(){
    super.classifier = new J48();
  }
 
  /**
   * Return a decision tree classifier, with J48, built on the set of
   * instances clustered by the EM weka algorithm.
   * @param data: dataset
   * @throws Exception
   */
  public Classifier getClassifier() throws Exception{
   
    //filter string attributes
    dataset.nominalFilter();
    Instances data = dataset.getDataset();
   
    //safe check: no class attribute
    data.setClassIndex(-1);
   
    /*
     * make clustering with EM and add label instances
     */
    AddCluster filter = new AddCluster();
    String[] options = new String[2];
    options[0] = "-W";                                    // "cluster options"
    options[1] = "weka.clusterers.EM -I 100 -N -1 -M 1.0E-6 -S 100";
    filter.setOptions(options);
    filter.setInputFormat(data);
    data = Filter.useFilter(data, filter);
    System.out.println("Filtered data:"+data);
   
    /*
     * Remove score attribute
     */
    data.deleteAttributeAt(data.numAttributes() - 2);
    System.out.println("Filtered data:"+data);
   
    //set class attribute
    data.setClassIndex(data.numAttributes()-1);
   
    /*
     * build J48 classifier
     */
    classifier.buildClassifier(data);
   
    System.out.println(classifier);
   
     setDataset(data);
   
    return classifier;
   
   
  }
 
 
  public static void main(String[] args){
    try{
      final long startTime = System.currentTimeMillis();
      final long endTime;
      final float duration;

     
       // load data
       ArffLoader loader = new ArffLoader();
       loader.setFile(new File("./src/trust/weka/BelWeka.arff"));
       Instances data = loader.getDataSet();
      
       WekaClusterClassifier classifier = new WekaClusterClassifier();
       classifier.setDataset(data);
       classifier.getClassifier();
      
     
      
       endTime = System.currentTimeMillis();;
       duration = (endTime - startTime)/1000F;
      
       System.out.println("\nElapsed time: "+(duration)+"sec");

      //test on instance
       Instance instance = new DenseInstance(4);
       instance.setValue(data.attribute("Abilities"), "pediatrician");
       instance.setValue(data.attribute("Dispositions"), "cautious");
       instance.setValue(data.attribute("Cross"), "female");
       instance.setDataset(data);
      
       classifier.testInstance(instance);
      
       System.out.println(instance);
      
      
    }catch(Exception e){
      e.printStackTrace();
    }
  }

  @Override
  public double agentTrustEvuation(Object... list) throws Exception {
    // TODO Auto-generated method stub
    return 0;
  }
}
TOP

Related Classes of trust.weka4jason.WekaClusterClassifier

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.