Java Code Examples for weka.core.Instances#delete()

The following examples show how to use weka.core.Instances#delete() . 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: Decorate.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
    * Removes a specified number of instances from the given set of instances.
    *
    * @param data given instances
    * @param numRemove number of instances to delete from the given instances
    */
   protected void removeInstances(Instances data, int numRemove){
int num = data.numInstances();
for(int i=num - 1; i>num - 1 - numRemove;i--){
    data.delete(i);
}
   }
 
Example 2
Source File: LeastMedSq.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Builds a new LinearRegression without the 'bad' data
  * found by buildWeight
  *
  * @throws Exception if building fails
  */
 private void buildRLSRegression()throws Exception{

   buildWeight();
   m_RLSData = new Instances(m_Data);
   int x = 0;
   int y = 0;
   int n = m_RLSData.numInstances();
   while(y < n){
     if (m_weight[x] == 0){
m_RLSData.delete(y);
n = m_RLSData.numInstances();
y--;
     }
     x++;
     y++;
   }
   if ( m_RLSData.numInstances() == 0){
     System.err.println("rls regression unbuilt");
     m_ls = m_currentRegression;
   }else{
     m_ls = new LinearRegression();
     m_ls.setOptions(new String[]{"-S", "1"});
     m_ls.buildClassifier(m_RLSData);
     m_currentRegression = m_ls;
   }

 }
 
Example 3
Source File: RDG1.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Resets the class values of all instances using voting.
 * For each instance the class value that satisfies the most rules
 * is choosen as new class value.
 *
 * @param dataset the dataset to work on
 * @return the changed instances
 * @throws Exception if something goes wrong
 */
private Instances voteDataset(Instances dataset) throws Exception {
  for (int i = 0; i < dataset.numInstances(); i++) {
    Instance inst = dataset.firstInstance();
    inst = votedReclassifyExample(inst); 
    dataset.add(inst);
    dataset.delete(0);
  }  

  return dataset;
}
 
Example 4
Source File: ErrorBased.java    From gsn with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected Instances process(Instances instances) throws Exception {
	
	Instances output = new Instances(instances);
	
	double[] dif = m_errors.clone();
	Arrays.sort(dif);
	double quantil = dif[(int)(dif.length*(1-1.0/m_ratio))];
	int after = (int)(dif.length*(1-1.0/m_ratio));
	int middle = (int)(dif.length*(1-1.0/m_ratio));
	int before = (int)(dif.length*(1-1.0/m_ratio));
	while (after < dif.length && dif[after] == quantil){after++;}
	while (before >= 0 && dif[before] == quantil){before--;}
	Random r = new Random();
	if(instances.numInstances() <= m_ratio){return output;}

	for(int i=output.numInstances()-1;i>=0;i--){
		if(output.numInstances() <= m_ratio){break;}
		if(m_errors.length > i && m_errors[i] < quantil){output.delete(i);}
		if(m_errors.length > i && m_errors[i]==quantil && r.nextInt(after-before)>middle-before){
			output.delete(i);
		}
		
		

	}		
	
	return output;
}
 
Example 5
Source File: FilterFromSelection.java    From gsn with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected Instances process(Instances instances) throws Exception {
	if (m_select == null || m_select.length != instances.numInstances()){
		return instances;
	}
	else{
		Instances output = new Instances(instances);
		for(int i=m_select.length-1;i>=0;i--){
			if(!m_select[i]){output.delete(i);}
		}
		return output;
	}
}
 
Example 6
Source File: MLCBMaD.java    From meka with GNU General Public License v3.0 4 votes vote down vote up
@Override
   public Instance transformInstance(Instance x) throws Exception{
Instances tmpInst = new Instances(x.dataset());

tmpInst.delete();
tmpInst.add(x);

Instances features = this.extractPart(tmpInst, false);

Instances pseudoLabels = new Instances(this.compressedMatrix);
Instance tmpin = pseudoLabels.instance(0);
pseudoLabels.delete();

pseudoLabels.add(tmpin);

for ( int i = 0; i< pseudoLabels.classIndex(); i++) {
    pseudoLabels.instance(0).setMissing(i);
}

Instances newDataSet = Instances.mergeInstances(pseudoLabels, features);
newDataSet.setClassIndex(this.size);


return newDataSet.instance(0);
   }
 
Example 7
Source File: Maniac.java    From meka with GNU General Public License v3.0 4 votes vote down vote up
@Override
   public Instance transformInstance(Instance x) throws Exception{

Instances tmpInst = new Instances(x.dataset());

tmpInst.delete();
tmpInst.add(x);

Instances features = this.extractPart(tmpInst, false);

Instances pseudoLabels = new Instances(this.compressedTemplateInst);
Instance tmpin = pseudoLabels.instance(0);
pseudoLabels.delete();

pseudoLabels.add(tmpin);

for ( int i = 0; i< pseudoLabels.classIndex(); i++) {
    pseudoLabels.instance(0).setMissing(i);
}

Instances newDataSet = Instances.mergeInstances(pseudoLabels, features);
newDataSet.setClassIndex(pseudoLabels.numAttributes());

return newDataSet.instance(0);
   }
 
Example 8
Source File: PLST.java    From meka with GNU General Public License v3.0 4 votes vote down vote up
/**
    * Transforms the instance in the prediction process before given to the internal multi-label
    * or multi-target classifier. The instance is passed having the original set of labels, these
    * must be replaced with the transformed labels (attributes) so that the internla classifier
    * can predict them.
    *
    * @param x The instance to transform. Consists of features and labels.
    * @return The transformed instance. Consists of features and transformed labels.
    */
   @Override
   public Instance transformInstance(Instance x) throws Exception{
Instances tmpInst = new Instances(x.dataset());

tmpInst.delete();
tmpInst.add(x);

Instances features = this.extractPart(tmpInst, false);

Instances labels = new Instances(this.m_PatternInstances);

labels.add(new DenseInstance(labels.numAttributes()));

Instances result = Instances.mergeInstances(labels, features);

result.setClassIndex(labels.numAttributes());

return result.instance(0);
   }
 
Example 9
Source File: SubSample.java    From gsn with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected Instances process(Instances instances) throws Exception {

	instances.sort(m_index);
	
	Instances output = new Instances(instances);
	
	if(instances.numInstances() <= m_ratio){return output;}
	
	for(int i=output.numInstances()-1;i>=0;i--){
		if((i+1) % m_ratio != 0){output.delete(i);}
	}
	//output.compactify();
	
	
	return output;
}
 
Example 10
Source File: RandomSample.java    From gsn with GNU General Public License v3.0 3 votes vote down vote up
@Override
protected Instances process(Instances instances) throws Exception {
	
	Instances output = new Instances(instances);

	if(instances.numInstances() <= 2*m_ratio){return output;}
	
	Random r = new Random();
	
	for(int i=output.numInstances()-1;i>=0;i--){
		if(output.numInstances()>2 && r.nextInt(m_ratio) != 0){output.delete(i);}
	}		
	
	return output;
}