weka.classifiers.lazy.IBk Java Examples

The following examples show how to use weka.classifiers.lazy.IBk. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DatasetLists.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
public static void testArffs(String[] problems, String path){
    String header;
    Instances train,test;
    
    for(String str:problems){
        System.out.println("Loading ARFF for "+str);
       train=DatasetLoading.loadDataNullable(path+str+"\\"+str+"_TRAIN.arff");
       test=DatasetLoading.loadDataNullable(path+str+"\\"+str+"_TEST.arff");
       Classifier c= new IBk();
       double acc = ClassifierTools.singleTrainTestSplitAccuracy(c, train, test);
        System.out.println(" 1NN acc on "+str +" = "+acc);
       
       
    }

    
}
 
Example #2
Source File: CollectiveIBk.java    From collective-classification-weka-package with GNU General Public License v3.0 6 votes vote down vote up
/**
 * performs initialization of members
 */
@Override
protected void initializeMembers() {
  super.initializeMembers();

  m_KNNdetermined    = -1;
  m_NeighborsTestset = null;
  m_TrainsetNew      = null;
  m_TestsetNew       = null;
  m_UseNaiveSearch   = false;
  m_LabeledTestset   = null;
  m_Missing          = new ReplaceMissingValues();
  
  m_Classifier = new IBk();
  m_Classifier.setKNN(10);
  m_Classifier.setCrossValidate(true);
  m_Classifier.setWindowSize(0);
  m_Classifier.setMeanSquared(false);
  
  m_KNN = m_Classifier.getKNN();
  
  m_AdditionalMeasures.add("measureDeterminedKNN");
}
 
Example #3
Source File: kNN.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
public static void test1NNvsIB1(boolean norm){
        System.out.println("FIRST BASIC SANITY TEST FOR THIS WRAPPER");
        System.out.print("Compare 1-NN with IB1, normalisation turned");
        String str=norm?" on":" off";
        System.out.println(str);
        System.out.println("Compare on the UCI data sets");
        System.out.print("If normalisation is off, then there may be differences");
        kNN knn = new kNN(1);
        IBk ib1=new IBk(1);
        knn.normalise(norm);
        int diff=0;
        DecimalFormat df = new DecimalFormat("####.###");
        for(String s:DatasetLists.uciFileNames){
            Instances train=DatasetLoading.loadDataNullable("Z:/ArchiveData/Uci_arff/"+s+"/"+s+"-train");
            Instances test=DatasetLoading.loadDataNullable("Z:/ArchiveData/Uci_arff/"+s+"/"+s+"-test");
            try{
                knn.buildClassifier(train);
//                ib1.buildClassifier(train);
                ib1.buildClassifier(train);
                double a1=ClassifierTools.accuracy(test, knn);
                double a2=ClassifierTools.accuracy(test, ib1);
                if(a1!=a2){
                    diff++;
                    System.out.println(s+": 1-NN ="+df.format(a1)+" ib1="+df.format(a2));
                }
            }catch(Exception e){
                System.out.println(" Exception builing a classifier");
                System.exit(0);
            }
        }
         System.out.println("Total problems ="+DatasetLists.uciFileNames.length+" different on "+diff);
    }
 
Example #4
Source File: kNN.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
public static void testkNNvsIBk(boolean norm, boolean crossValidate){
        System.out.println("FIRST BASIC SANITY TEST FOR THIS WRAPPER");
        System.out.print("Compare 1-NN with IB1, normalisation turned");
        String str=norm?" on":" off";
        System.out.println(str);
        System.out.print("Cross validation turned");
        str=crossValidate?" on":" off";
        System.out.println(str);
        System.out.println("Compare on the UCI data sets");
        System.out.print("If normalisation is off, then there may be differences");
        kNN knn = new kNN(100);
        IBk ibk=new IBk(100);
        knn.normalise(norm);
        knn.setCrossValidate(crossValidate);
        ibk.setCrossValidate(crossValidate);
        int diff=0;
        DecimalFormat df = new DecimalFormat("####.###");
        for(String s:DatasetLists.uciFileNames){
            Instances train=DatasetLoading.loadDataNullable("Z:/ArchiveData/Uci_arff/"+s+"\\"+s+"-train");
            Instances test=DatasetLoading.loadDataNullable("Z:/ArchiveData/Uci_arff/"+s+"\\"+s+"-test");
            try{
                knn.buildClassifier(train);
//                ib1.buildClassifier(train);
                ibk.buildClassifier(train);
                double a1=ClassifierTools.accuracy(test, knn);
                double a2=ClassifierTools.accuracy(test, ibk);
                if(a1!=a2){
                    diff++;
                    System.out.println(s+": 1-NN ="+df.format(a1)+" ibk="+df.format(a2));
                }
            }catch(Exception e){
                System.out.println(" Exception builing a classifier");
                System.exit(0);
            }
        }
         System.out.println("Total problems ="+DatasetLists.uciFileNames.length+" different on "+diff);
    }
 
Example #5
Source File: EvaluationUtils.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
public static double performEnsemble(Instances instances) throws Exception {
	List<Instances> subsample = WekaUtil.getStratifiedSplit(instances, 42, .05f);
	instances = subsample.get(0);

	/* Relief */
	ReliefFAttributeEval relief = new ReliefFAttributeEval();
	relief.buildEvaluator(instances);
	double attEvalSum = 0;
	for (int i = 0; i < instances.numAttributes() - 1; i++) {
		attEvalSum += relief.evaluateAttribute(i);
	}
	attEvalSum /= instances.numAttributes();

	/* Variance */
	double varianceMean = 0;
	int totalNumericCount = 0;
	for (int i = 0; i < instances.numAttributes() - 1; i++) {
		if (instances.attribute(i).isNumeric()) {
			instances.attributeStats(i).numericStats.calculateDerived();
			varianceMean += Math.pow(instances.attributeStats(i).numericStats.stdDev, 2);
			totalNumericCount++;
		}
	}
	varianceMean /= (totalNumericCount != 0 ? totalNumericCount : 1);

	/* KNN */
	List<Instances> split = WekaUtil.getStratifiedSplit(instances, 42, .7f);
	IBk knn = new IBk(10);
	knn.buildClassifier(split.get(0));
	Evaluation eval = new Evaluation(split.get(0));
	eval.evaluateModel(knn, split.get(1));
	double knnResult = eval.pctCorrect() / 100d;

	return 1 - (0.33 * attEvalSum + 0.33 * knnResult + 0.33 * varianceMean);
}
 
Example #6
Source File: EnsembleEvaluatorTest.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void knnEvaluatorTest() throws Exception {
    logger.info("Starting knn evaluation test...");

    /* load dataset and create a train-test-split */
    OpenmlConnector connector = new OpenmlConnector();
    DataSetDescription ds = connector.dataGet(DataSetUtils.SEGMENT_ID);
    File file = ds.getDataset(DataSetUtils.API_KEY);
    Instances data = new Instances(new BufferedReader(new FileReader(file)));
    data.setClassIndex(data.numAttributes() - 1);
    List<Instances> split = WekaUtil.getStratifiedSplit(data, 42, .05f);

    Instances insts = split.get(0);
    List<Instances> split2 = WekaUtil.getStratifiedSplit(insts, 42, .7f);

    long timeStart = System.currentTimeMillis();

    IBk knn = new IBk(10);
    knn.buildClassifier(split2.get(0));

    long timeStartEval = System.currentTimeMillis();

    Evaluation eval = new Evaluation(split2.get(0));
    eval.evaluateModel(knn, split2.get(1));
    logger.debug("Pct correct: " + eval.pctCorrect());
    Assert.assertTrue(eval.pctCorrect() > 0);

    long timeTaken = System.currentTimeMillis() - timeStart;
    long timeTakenEval = System.currentTimeMillis() - timeStartEval;

    logger.debug("KNN took " + (timeTaken / 1000) + " s.");
    logger.debug("KNN eval took " + (timeTakenEval / 1000) + " s.");
}
 
Example #7
Source File: EnsembleProvider.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Initializes the CAWPE ensemble model consisting of five classifiers (SMO,
 * KNN, J48, Logistic and MLP) using a majority voting strategy. The ensemble
 * uses Weka classifiers. It refers to "Heterogeneous ensemble of standard
 * classification algorithms" (HESCA) as described in Lines, Jason & Taylor,
 * Sarah & Bagnall, Anthony. (2018). Time Series Classification with HIVE-COTE:
 * The Hierarchical Vote Collective of Transformation-Based Ensembles. ACM
 * Transactions on Knowledge Discovery from Data. 12. 1-35. 10.1145/3182382.
 *
 * @param seed
 *            Seed used within the classifiers and the majority confidence
 *            voting scheme
 * @param numFolds
 *            Number of folds used within the determination of the classifier
 *            weights for the {@link MajorityConfidenceVote}
 * @return Returns an initialized (but untrained) ensemble model.
 * @throws Exception
 *             Thrown when the initialization has failed
 */
public static Classifier provideCAWPEEnsembleModel(final int seed, final int numFolds) throws Exception {
	Classifier[] classifiers = new Classifier[5];

	Vote voter = new MajorityConfidenceVote(numFolds, seed);

	SMO smo = new SMO();
	smo.turnChecksOff();
	smo.setBuildCalibrationModels(true);
	PolyKernel kl = new PolyKernel();
	kl.setExponent(1);
	smo.setKernel(kl);
	smo.setRandomSeed(seed);
	classifiers[0] = smo;

	IBk k = new IBk(100);
	k.setCrossValidate(true);
	EuclideanDistance ed = new EuclideanDistance();
	ed.setDontNormalize(true);
	k.getNearestNeighbourSearchAlgorithm().setDistanceFunction(ed);
	classifiers[1] = k;

	J48 c45 = new J48();
	c45.setSeed(seed);
	classifiers[2] = c45;

	classifiers[3] = new Logistic();

	classifiers[4] = new MultilayerPerceptron();

	voter.setClassifiers(classifiers);
	return voter;
}
 
Example #8
Source File: EvaluationTests.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
public void testIncrementalEvaluation() {
	// Batch
	Result r1 = null, r2 = null;
	// Load Data
	Instances D = loadInstances("Music.arff");
	// Train ECCUpdateable
	BaggingMLUpdateable h = new BaggingMLUpdateable();
	CCUpdateable cc = new CCUpdateable();
	cc.setClassifier(new IBk());
	h.setClassifier(cc);
	try {
		r1 = IncrementalEvaluation.evaluateModel(h,D);
		r2 = IncrementalEvaluation.evaluateModel(h,D);
	} catch(Exception e) {
		System.err.println("FAILED TO GET r1, r2");
		e.printStackTrace();
	}
	// Good @TODO
	//assertTrue("Inc. Eval OK? ?", r1.info.get("Accuracy").equals("0.486 +/- 0.045"));
	// The same?
	if (r1==null)
		System.out.println("r1 is null");
	if (r2==null)
		System.out.println("r2 is null");

	assertTrue("Inc. Eval the same?", ((String)r1.getMeasurement("Accuracy")).equals(((String)r2.getMeasurement("Accuracy"))));
	// test/train

	// compare with non-ss
}
 
Example #9
Source File: MultivariateShapeletTransformClassifier.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Classifiers used in the HIVE COTE paper
 */    
    public void configureDefaultEnsemble(){
//HIVE_SHAPELET_SVMQ    HIVE_SHAPELET_RandF    HIVE_SHAPELET_RotF    
//HIVE_SHAPELET_NN    HIVE_SHAPELET_NB    HIVE_SHAPELET_C45    HIVE_SHAPELET_SVML   
        ensemble=new CAWPE();
        ensemble.setWeightingScheme(new TrainAcc(4));
        ensemble.setVotingScheme(new MajorityConfidence());
        Classifier[] classifiers = new Classifier[7];
        String[] classifierNames = new String[7];
        
        SMO smo = new SMO();
        smo.turnChecksOff();
        smo.setBuildLogisticModels(true);
        PolyKernel kl = new PolyKernel();
        kl.setExponent(2);
        smo.setKernel(kl);
        if (seedClassifier)
            smo.setRandomSeed((int)seed);
        classifiers[0] = smo;
        classifierNames[0] = "SVMQ";

        RandomForest r=new RandomForest();
        r.setNumTrees(500);
        if(seedClassifier)
           r.setSeed((int)seed);            
        classifiers[1] = r;
        classifierNames[1] = "RandF";
            
            
        RotationForest rf=new RotationForest();
        rf.setNumIterations(100);
        if(seedClassifier)
           rf.setSeed((int)seed);
        classifiers[2] = rf;
        classifierNames[2] = "RotF";
        IBk nn=new IBk();
        classifiers[3] = nn;
        classifierNames[3] = "NN";
        NaiveBayes nb=new NaiveBayes();
        classifiers[4] = nb;
        classifierNames[4] = "NB";
        J48 c45=new J48();
        classifiers[5] = c45;
        classifierNames[5] = "C45";
        SMO svml = new SMO();
        svml.turnChecksOff();
        svml.setBuildLogisticModels(true);
        PolyKernel k2 = new PolyKernel();
        k2.setExponent(1);
        smo.setKernel(k2);
        classifiers[6] = svml;
        classifierNames[6] = "SVML";
        ensemble.setClassifiers(classifiers, classifierNames, null);
    }
 
Example #10
Source File: TestIBk.java    From Java-Data-Analysis with MIT License 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    DataSource source = new DataSource("data/AnonFruit.arff");
    Instances instances = source.getDataSet();
    instances.setClassIndex(3);  // target attribute: (Sweet)
    
    IBk ibk = new IBk();
    ibk.buildClassifier(instances);

    for (Instance instance : instances) {
        double prediction = ibk.classifyInstance(instance);
        System.out.printf("%4.0f%4.0f%n", 
                instance.classValue(), prediction);
    }
}
 
Example #11
Source File: EnsembleProvider.java    From AILibs with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Initializes the HIVE COTE ensemble consisting of 7 classifiers using a
 * majority voting strategy as described in J. Lines, S. Taylor and A. Bagnall,
 * "HIVE-COTE: The Hierarchical Vote Collective of Transformation-Based
 * Ensembles for Time Series Classification," 2016 IEEE 16th International
 * Conference on Data Mining (ICDM), Barcelona, 2016, pp. 1041-1046. doi:
 * 10.1109/ICDM.2016.0133.
 *
 * @param seed
 *            Seed used within the classifiers and the majority confidence
 *            voting scheme
 * @param numFolds
 *            Number of folds used within the determination of the classifier
 *            weights for the {@link MajorityConfidenceVote}
 * @return Returns the initialized (but untrained) HIVE COTE ensemble model.
 */
public static Classifier provideHIVECOTEEnsembleModel(final long seed) {
	Classifier[] classifier = new Classifier[7];

	Vote voter = new MajorityConfidenceVote(5, seed);

	// SMO poly2
	SMO smop = new SMO();
	smop.turnChecksOff();
	smop.setBuildCalibrationModels(true);
	PolyKernel kernel = new PolyKernel();
	kernel.setExponent(2);
	smop.setKernel(kernel);
	smop.setRandomSeed((int)seed);
	classifier[0] = smop;

	// Random Forest
	RandomForest rf = new RandomForest();
	rf.setSeed((int)seed);
	rf.setNumIterations(500);
	classifier[1] = rf;

	// Rotation forest
	RotationForest rotF = new RotationForest();
	rotF.setSeed((int)seed);
	rotF.setNumIterations(100);
	classifier[2] = rotF;

	// NN
	IBk nn = new IBk();
	classifier[3] = nn;

	// Naive Bayes
	NaiveBayes nb = new NaiveBayes();
	classifier[4] = nb;

	// C45
	J48 c45 = new J48();
	c45.setSeed((int)seed);
	classifier[5] = c45;

	// SMO linear
	SMO smol = new SMO();
	smol.turnChecksOff();
	smol.setBuildCalibrationModels(true);
	PolyKernel linearKernel = new PolyKernel();
	linearKernel.setExponent(1);
	smol.setKernel(linearKernel);
	classifier[6] = smol;

	voter.setClassifiers(classifier);
	return voter;
}