Java Code Examples for weka.classifiers.AbstractClassifier#makeCopies()

The following examples show how to use weka.classifiers.AbstractClassifier#makeCopies() . 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: HASEL.java    From meka with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void buildClassifier(Instances D) throws Exception {

	int L = D.classIndex();
	int N = D.numInstances();

	// Get partition from dataset hierarchy
	kMap = SuperLabelUtils.getPartitionFromDatasetHierarchy(D); 
	m_M = kMap.length;
	m_Classifiers = AbstractClassifier.makeCopies(m_Classifier,m_M);
	m_InstancesTemplates = new Instances[m_M];

	for(int i = 0; i < m_M; i++) {

		if (getDebug()) 
			System.out.println("Building model "+(i+1)+"/"+m_M+": "+Arrays.toString(kMap[i]));
		Instances D_i = SuperLabelUtils.makePartitionDataset(D,kMap[i]);
		m_Classifiers[i].buildClassifier(D_i);
		m_InstancesTemplates[i] = new Instances(D_i,0);
	}

}
 
Example 2
Source File: RAkEL.java    From meka with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void buildClassifier(Instances D) throws Exception {
	testCapabilities(D);

	int L = D.classIndex();
	Random random = new Random(m_S);


	if (getDebug())
		System.out.println("Building "+m_M+" models of "+m_K+" random subsets:");

	m_InstancesTemplates = new Instances[m_M];
	kMap = new int[m_M][m_K];
	m_Classifiers = AbstractClassifier.makeCopies(m_Classifier,m_M);
	for(int i = 0; i < m_M; i++) {
		kMap[i] = SuperLabelUtils.get_k_subset(L,m_K,random);
		if (getDebug()) 
			System.out.println("\tmodel "+(i+1)+"/"+m_M+": "+Arrays.toString(kMap[i])+", P="+m_P+", N="+m_N);
		Instances D_i = SuperLabelUtils.makePartitionDataset(D,kMap[i],m_P,m_N);
		m_Classifiers[i].buildClassifier(D_i);
		m_InstancesTemplates[i] = new Instances(D_i,0);
	}
}
 
Example 3
Source File: BR.java    From meka with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void buildClassifier(Instances D) throws Exception {
	testCapabilities(D);
  	
	int L = D.classIndex();

	if(getDebug()) System.out.print("Creating "+L+" models ("+m_Classifier.getClass().getName()+"): ");
	m_MultiClassifiers = AbstractClassifier.makeCopies(m_Classifier,L);
	m_InstancesTemplates = new Instances[L];

	for(int j = 0; j < L; j++) {

		//Select only class attribute 'j'
		Instances D_j = F.keepLabels(new Instances(D),L,new int[]{j});
		D_j.setClassIndex(0);

		//Build the classifier for that class
		m_MultiClassifiers[j].buildClassifier(D_j);
		if(getDebug()) System.out.print(" " + (D_j.classAttribute().name()));

		m_InstancesTemplates[j] = new Instances(D_j, 0);
	}
}
 
Example 4
Source File: CR.java    From meka with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void buildClassifier(Instances D) throws Exception {
  	testCapabilities(D);
  	
	int L = D.classIndex();

	if(getDebug()) System.out.print("Creating "+L+" models ("+m_Classifier.getClass().getName()+"): ");
	m_MultiClassifiers = AbstractClassifier.makeCopies(m_Classifier,L);
	m_Templates = new Instances[L];

	for(int j = 0; j < L; j++) {

		//Select only class attribute 'j'
		m_Templates[j] = MLUtils.keepAttributesAt(new Instances(D),new int[]{j},L);
		m_Templates[j].setClassIndex(0);

		//Build the classifier for that class
		m_MultiClassifiers[j].buildClassifier(m_Templates[j]);
		if(getDebug()) System.out.print(" " + (m_Templates[j].classAttribute().name()));

		m_Templates[j] = new Instances(m_Templates[j], 0);
	}
}
 
Example 5
Source File: NBTreeNoSplit.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
  * Utility method for fast 5-fold cross validation of a naive bayes
  * model
  *
  * @param fullModel a <code>NaiveBayesUpdateable</code> value
  * @param trainingSet an <code>Instances</code> value
  * @param r a <code>Random</code> value
  * @return a <code>double</code> value
  * @exception Exception if an error occurs
  */
 public static double crossValidate(NaiveBayesUpdateable fullModel,
		       Instances trainingSet,
		       Random r) throws Exception {
   // make some copies for fast evaluation of 5-fold xval
   Classifier [] copies = AbstractClassifier.makeCopies(fullModel, 5);
   Evaluation eval = new Evaluation(trainingSet);
   // make some splits
   for (int j = 0; j < 5; j++) {
     Instances test = trainingSet.testCV(5, j);
     // unlearn these test instances
     for (int k = 0; k < test.numInstances(); k++) {
test.instance(k).setWeight(-test.instance(k).weight());
((NaiveBayesUpdateable)copies[j]).updateClassifier(test.instance(k));
// reset the weight back to its original value
test.instance(k).setWeight(-test.instance(k).weight());
     }
     eval.evaluateModel(copies[j], test);
   }
   return eval.incorrect();
 }
 
Example 6
Source File: IndependentDimensionEnsemble.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
protected void initialiseModules() throws Exception{
    classifiers = AbstractClassifier.makeCopies(original_model, numChannels);
    classifierNames = new String[numChannels];
    
    //one module for each channel.
    this.modules = new EnsembleModule[numChannels];
    for (int m = 0; m < numChannels; m++){
        classifierNames[m] = classifiers[m].getClass().getSimpleName() +"_"+m;
        modules[m] = new EnsembleModule(classifierNames[m], classifiers[m], "");
        if(priorWeights != null)
            modules[m].priorWeight = priorWeights[m];
    }
    
    weightingScheme.defineWeightings(modules, numClasses);
    votingScheme.trainVotingScheme(modules, numClasses);
}
 
Example 7
Source File: TransformEnsembles.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
public void buildClassifier(Instances data) throws Exception {
//Sometimes I just want to re-weight it, which must be done with findWeights(). 
//		rebuild stays true by default unless explicitly set by rebuildClassifier(boolean f)
// this is just a bit of a hack to speed up experiments,
		if(rebuild){	
			System.out.println("Build whole ...");
			init(data); //Assume its already standardised
			train.add(data);
			Instances t1=ps.process(data);
			Instances t2=acf.process(data);
			if(normaliseAtts){
				nPs=new NormalizeAttribute(t1);
				t1=nPs.process(t1);
				nAcf=new NormalizeAttribute(t2);
				t2=nAcf.process(t2);
			}
			pca.buildEvaluator(data);
			Instances t3=pca.transformedData(data);
			train.add(t1); //
			train.add(t2);
			train.add(t3);
			nosTransforms=train.size();
			findWeights();
			all= AbstractClassifier.makeCopies(base,train.size());
                        all[0]=AbstractClassifier.makeCopy(baseTime);
			for(int i=0;i<all.length;i++){
				all[i].buildClassifier(train.get(i));
			}
		}
	}
 
Example 8
Source File: RAkELd.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void buildClassifier(Instances D) throws Exception {

	int L = D.classIndex();
	int N = D.numInstances();
	Random random = new Random(m_S);

	// Note: a slightly roundabout way of doing it:
	int num = (int)Math.ceil(L / m_K);
	kMap = SuperLabelUtils.generatePartition(A.make_sequence(L),num,random,true);
	m_M = kMap.length;
	m_Classifiers = AbstractClassifier.makeCopies(m_Classifier,m_M);
	m_InstancesTemplates = new Instances[m_M];

	if (getDebug())
		System.out.println("Building "+m_M+" models of "+m_K+" partitions:");

	for(int i = 0; i < m_M; i++) {

		if (getDebug()) 
			System.out.println("\tpartitioning model "+(i+1)+"/"+m_M+": "+Arrays.toString(kMap[i])+", P="+m_P+", N="+m_N);
		Instances D_i = SuperLabelUtils.makePartitionDataset(D,kMap[i],m_P,m_N);
		if (getDebug()) 
			System.out.println("\tbuilding model "+(i+1)+"/"+m_M+": "+Arrays.toString(kMap[i]));

		m_Classifiers[i].buildClassifier(D_i);
		m_InstancesTemplates[i] = new Instances(D_i,0);

	}

}
 
Example 9
Source File: AdvancedCollective.java    From collective-classification-weka-package with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Set the base learner. Creates copies of the given classifier for the two
 * classifiers used internally. <br/>
 * Note: also unsets the flag whether the classifier has been built so far.
 *
 * @param newClassifier the classifier to use.
 * @see #m_ClassifierBuilt
 */
public void setClassifier(Classifier newClassifier) {
  super.setClassifier(newClassifier);

  try {
    m_Classifier2 = AbstractClassifier.makeCopies(newClassifier, 1)[0];
  }
  catch (Exception e) {
    e.printStackTrace();
    m_Classifier  = new weka.classifiers.trees.J48();
    m_Classifier2 = new weka.classifiers.trees.J48();
  }
}
 
Example 10
Source File: Grading.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Generates the meta data
  * 
  * @param newData the data to work on
  * @param random the random number generator used in the generation
  * @throws Exception if generation fails
  */
 protected void generateMetaLevel(Instances newData, Random random) 
   throws Exception {

   m_MetaFormat = metaFormat(newData);
   Instances [] metaData = new Instances[m_Classifiers.length];
   for (int i = 0; i < m_Classifiers.length; i++) {
     metaData[i] = metaFormat(newData);
   }
   for (int j = 0; j < m_NumFolds; j++) {

     Instances train = newData.trainCV(m_NumFolds, j, random);
     Instances test = newData.testCV(m_NumFolds, j);

     // Build base classifiers
     for (int i = 0; i < m_Classifiers.length; i++) {
getClassifier(i).buildClassifier(train);
       for (int k = 0; k < test.numInstances(); k++) {
  metaData[i].add(metaInstance(test.instance(k),i));
       }
     }
   }
       
   // calculate InstPerClass
   m_InstPerClass = new double[newData.numClasses()];
   for (int i=0; i < newData.numClasses(); i++) m_InstPerClass[i]=0.0;
   for (int i=0; i < newData.numInstances(); i++) {
     m_InstPerClass[(int)newData.instance(i).classValue()]++;
   }
   
   m_MetaClassifiers = AbstractClassifier.makeCopies(m_MetaClassifier,
				      m_Classifiers.length);

   for (int i = 0; i < m_Classifiers.length; i++) {
     m_MetaClassifiers[i].buildClassifier(metaData[i]);
   }
 }
 
Example 11
Source File: RandomCommittee.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
   * Builds the committee of randomizable classifiers.
   *
   * @param data the training data to be used for generating the
   * bagged classifier.
   * @exception Exception if the classifier could not be built successfully
   */
  public void buildClassifier(Instances data) throws Exception {

    // can classifier handle the data?
    getCapabilities().testWithFail(data);

    // remove instances with missing class
    m_data = new Instances(data);
    m_data.deleteWithMissingClass();
    super.buildClassifier(m_data);
    
    if (!(m_Classifier instanceof Randomizable)) {
      throw new IllegalArgumentException("Base learner must implement Randomizable!");
    }

    m_Classifiers = AbstractClassifier.makeCopies(m_Classifier, m_NumIterations);

    Random random = m_data.getRandomNumberGenerator(m_Seed);

    // Resample data based on weights if base learner can't handle weights
    if (!(m_Classifier instanceof WeightedInstancesHandler)) {
      m_data = m_data.resampleWithWeights(random);
    }

    for (int j = 0; j < m_Classifiers.length; j++) {

      // Set the random number seed for the current classifier.
      ((Randomizable) m_Classifiers[j]).setSeed(random.nextInt());
      
      // Build the classifier.
//      m_Classifiers[j].buildClassifier(m_data);
    }
    
    buildClassifiers();
    
    // save memory
    m_data = null;
  }
 
Example 12
Source File: MultiSamplingEvaluator.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
protected void cloneClassifiers(Classifier[] classifiers) throws Exception {
    // clone them all here in one go for efficiency of serialisation
    foldClassifiers = new Classifier[classifiers.length][];

    for (int c = 0; c < classifiers.length; ++c)
        foldClassifiers[c] = AbstractClassifier.makeCopies(classifiers[c], numFolds);
}
 
Example 13
Source File: RacedIncrementalLogitBoost.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/** 
    * performs a boosting iteration, returning a new model for the committee
    * 
    * @param data the data to boost on
    * @return the new model
    * @throws Exception if anything goes wrong
    */
   protected Classifier[] boost(Instances data) throws Exception {
     
     Classifier[] newModel = AbstractClassifier.makeCopies(m_Classifier, m_NumClasses);
     
     // Create a copy of the data with the class transformed into numeric
     Instances boostData = new Instances(data);
     boostData.deleteWithMissingClass();
     int numInstances = boostData.numInstances();
     
     // Temporarily unset the class index
     int classIndex = data.classIndex();
     boostData.setClassIndex(-1);
     boostData.deleteAttributeAt(classIndex);
     boostData.insertAttributeAt(new Attribute("'pseudo class'"), classIndex);
     boostData.setClassIndex(classIndex);
     double [][] trainFs = new double [numInstances][m_NumClasses];
     double [][] trainYs = new double [numInstances][m_NumClasses];
     for (int j = 0; j < m_NumClasses; j++) {
for (int i = 0, k = 0; i < numInstances; i++, k++) {
  while (data.instance(k).classIsMissing()) k++;
  trainYs[i][j] = (data.instance(k).classValue() == j) ? 1 : 0;
}
     }
     
     // Evaluate / increment trainFs from the classifiers
     for (int x = 0; x < m_models.size(); x++) {
for (int i = 0; i < numInstances; i++) {
  double [] pred = new double [m_NumClasses];
  double predSum = 0;
  Classifier[] model = (Classifier[]) m_models.elementAt(x);
  for (int j = 0; j < m_NumClasses; j++) {
    pred[j] = model[j].classifyInstance(boostData.instance(i));
    predSum += pred[j];
  }
  predSum /= m_NumClasses;
  for (int j = 0; j < m_NumClasses; j++) {
    trainFs[i][j] += (pred[j] - predSum) * (m_NumClasses-1) 
      / m_NumClasses;
  }
}
     }

     for (int j = 0; j < m_NumClasses; j++) {

// Set instance pseudoclass and weights
for (int i = 0; i < numInstances; i++) {
  double p = RtoP(trainFs[i], j);
  Instance current = boostData.instance(i);
  double z, actual = trainYs[i][j];
  if (actual == 1) {
    z = 1.0 / p;
    if (z > Z_MAX) { // threshold
      z = Z_MAX;
    }
  } else if (actual == 0) {
    z = -1.0 / (1.0 - p);
    if (z < -Z_MAX) { // threshold
      z = -Z_MAX;
    }
  } else {
    z = (actual - p) / (p * (1 - p));
  }

  double w = (actual - p) / z;
  current.setValue(classIndex, z);
  current.setWeight(numInstances * w);
}

Instances trainData = boostData;
if (m_UseResampling) {
  double[] weights = new double[boostData.numInstances()];
  for (int kk = 0; kk < weights.length; kk++) {
    weights[kk] = boostData.instance(kk).weight();
  }
  trainData = boostData.resampleWithWeights(m_RandomInstance, 
					    weights);
}

// Build the classifier
newModel[j].buildClassifier(trainData);
     }      
     
     return newModel;
   }
 
Example 14
Source File: RAkELd.java    From meka with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void buildClassifier(Instances D) throws Exception {
	/*
	NOTE: This is a slow way of doing things at the moment, making use of multitarget.SCC functionality,
	even though multilabel.RAkELd is not a meta multi-label classifier.
	 */

	int L = D.classIndex();
	int N = D.numInstances();
	Random r = new Random(m_S);

	// Note: a slightly round-about way of doing it:
	int num = (int)Math.ceil(L / m_K);
	kMap = SuperLabelUtils.generatePartition(A.make_sequence(L),num,r,true);
	m_M = kMap.length;
	vMap = new int[m_M][][];
	m_Classifiers = AbstractClassifier.makeCopies(m_Classifier,m_M);
	m_InstancesTemplates = new Instances[m_M];

	if (getDebug())
		System.out.println("Building "+m_M+" models of "+m_K+" partitions:");

	D = SuperLabelUtils.SLTransformation(D, kMap, m_P, m_N);

	for(int i = 0; i < m_M; i++) {

		/*
		if (getDebug()) 
			System.out.println("\tpartitioning model "+(i+1)+"/"+m_M+": "+Arrays.toString(kMap[i])+", P="+m_P+", N="+m_N);

		Instances D_i = SuperLabelUtils.makePartitionDataset(D,kMap[i],m_P,m_N);
		*/

		Instances D_i = F.keepLabels(D,D.classIndex(),new int[]{i});
		D_i.setClassIndex(0);

		//vMap[i] = SuperLabelUtils.extractValues(D_i);

		if (getDebug()) 
			System.out.println("\tbuilding model "+(i+1)+"/"+m_M+": "+Arrays.toString(kMap[i]));

		m_Classifiers[i].buildClassifier(D_i);
		m_InstancesTemplates[i] = new Instances(D_i,0);

	}

}
 
Example 15
Source File: StackingC.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
  * Method that builds meta level.
  * 
  * @param newData the data to work with
  * @param random the random number generator to use for cross-validation
  * @throws Exception if generation fails
  */
 protected void generateMetaLevel(Instances newData, Random random) 
   throws Exception {

   Instances metaData = metaFormat(newData);
   m_MetaFormat = new Instances(metaData, 0);
   for (int j = 0; j < m_NumFolds; j++) {
     Instances train = newData.trainCV(m_NumFolds, j, random);

     // Build base classifiers
     for (int i = 0; i < m_Classifiers.length; i++) {
getClassifier(i).buildClassifier(train);
     }

     // Classify test instances and add to meta data
     Instances test = newData.testCV(m_NumFolds, j);
     for (int i = 0; i < test.numInstances(); i++) {
metaData.add(metaInstance(test.instance(i)));
     }
   }
   
   m_MetaClassifiers = AbstractClassifier.makeCopies(m_MetaClassifier,
				      m_BaseFormat.numClasses());
   
   int [] arrIdc = new int[m_Classifiers.length + 1];
   arrIdc[m_Classifiers.length] = metaData.numAttributes() - 1;
   Instances newInsts;
   for (int i = 0; i < m_MetaClassifiers.length; i++) {
     for (int j = 0; j < m_Classifiers.length; j++) {
arrIdc[j] = m_BaseFormat.numClasses() * j + i;
     }
     m_makeIndicatorFilter = new weka.filters.unsupervised.attribute.MakeIndicator();
     m_makeIndicatorFilter.setAttributeIndex("" + (metaData.classIndex() + 1));
     m_makeIndicatorFilter.setNumeric(true);
     m_makeIndicatorFilter.setValueIndex(i);
     m_makeIndicatorFilter.setInputFormat(metaData);
     newInsts = Filter.useFilter(metaData,m_makeIndicatorFilter);
     
     m_attrFilter = new weka.filters.unsupervised.attribute.Remove();
     m_attrFilter.setInvertSelection(true);
     m_attrFilter.setAttributeIndicesArray(arrIdc);
     m_attrFilter.setInputFormat(m_makeIndicatorFilter.getOutputFormat());
     newInsts = Filter.useFilter(newInsts,m_attrFilter);
     
     newInsts.setClassIndex(newInsts.numAttributes()-1);
     
     m_MetaClassifiers[i].buildClassifier(newInsts);
   }
 }
 
Example 16
Source File: BRq.java    From meka with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void buildClassifier(Instances data) throws Exception {
	testCapabilities(data);

	int c = data.classIndex();

	if(getDebug()) System.out.print("-: Creating "+c+" models ("+m_Classifier.getClass().getName()+"): ");
	m_MultiClassifiers = AbstractClassifier.makeCopies(m_Classifier,c);

	Instances sub_data = null;

	for(int i = 0; i < c; i++) {

		int indices[][] = new int[c][c - 1];
		for(int j = 0, k = 0; j < c; j++) {
			if(j != i) {
				indices[i][k++] = j;
			}
		}

		//Select only class attribute 'i'
		Remove FilterRemove = new Remove();
		FilterRemove.setAttributeIndicesArray(indices[i]);
		FilterRemove.setInputFormat(data);
		FilterRemove.setInvertSelection(true);
		sub_data = Filter.useFilter(data, FilterRemove);
		sub_data.setClassIndex(0);
		/* BEGIN downsample for this link */
		sub_data.randomize(m_Random);
		int numToRemove = sub_data.numInstances() - (int)Math.round(sub_data.numInstances() * m_DownSampleRatio);
		for(int m = 0, removed = 0; m < sub_data.numInstances(); m++) {
			if (sub_data.instance(m).classValue() <= 0.0) {
				sub_data.instance(m).setClassMissing();
				if (++removed >= numToRemove)
					break;
			}
		}
		sub_data.deleteWithMissingClass();
		/* END downsample for this link */


		//Build the classifier for that class
		m_MultiClassifiers[i].buildClassifier(sub_data);
		if(getDebug()) System.out.print(" " + (i+1));

	}

	if(getDebug()) System.out.println(" :-");

	m_InstancesTemplate = new Instances(sub_data, 0);

}
 
Example 17
Source File: MultiSamplingEvaluator.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
protected void cloneClassifier(Classifier classifier) throws Exception {
    // clone them all here in one go for efficiency of serialisation
    foldClassifiers = new Classifier[1][];

    foldClassifiers[0] = AbstractClassifier.makeCopies(classifier, numFolds);
}