Java Code Examples for weka.core.Instance#deleteAttributeAt()

The following examples show how to use weka.core.Instance#deleteAttributeAt() . 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: AttributeFilterBridge.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
public Instance filterInstance(Instance ins){
	int nosDeleted=0;
	int nosKept=0;
	int dataPos=0;
	Instance newIns=new DenseInstance(ins);
		//Advance to the next to keep
	while(dataPos<newIns.numAttributes()-1 && nosKept<attsToKeep.length){
		while(dataPos!=attsToKeep[nosKept]-nosDeleted && dataPos<newIns.numAttributes()-1){
			newIns.deleteAttributeAt(dataPos);
			nosDeleted++;
		}
		nosKept++;
		dataPos++;
	}
	while(dataPos<newIns.numAttributes()-1)
		newIns.deleteAttributeAt(dataPos);
	return newIns;
	
}
 
Example 2
Source File: RT.java    From meka with GNU General Public License v3.0 6 votes vote down vote up
/**
 * ConvertInstance - Convert an Instance to multi-class format by deleting all but one of the label attributes.
 * @param	x	incoming Instance
 * @return	the converted Instance
 */
public Instance convertInstance(Instance x) {

	int L = x.classIndex();

	//Copy the original instance
	Instance x_ = (Instance) x.copy(); 
	x_.setDataset(null);

	//Delete all class attributes
	for (int i = 0; i < L; i++)
		x_.deleteAttributeAt(0);

	//Add one of those class attributes at the begginning
	x_.insertAttributeAt(0);

	//Hopefully setting the dataset will configure that attribute properly
	x_.setDataset(m_InstancesTemplate);

	return x_;
}
 
Example 3
Source File: CCUpdateable.java    From meka with GNU General Public License v3.0 6 votes vote down vote up
protected void update(Instance x) throws Exception {

			Instance x_ = (Instance)x.copy();
			x_.setDataset(null);

			// delete all except one (leaving a binary problem)
			// delete all the attributes (and track where our index ends up)
			int c_index = this.value;
			for(int i = excld.length-1; i >= 0; i--) {
				x_.deleteAttributeAt(excld[i]);
				if (excld[i] < this.index)
					c_index--; 
			}
			x_.setDataset(this._template);

			((UpdateableClassifier)this.classifier).updateClassifier(x_);

			if (next != null)
				next.update(x);
		}
 
Example 4
Source File: CCUpdateable.java    From meka with GNU General Public License v3.0 6 votes vote down vote up
protected void classify(Instance test) throws Exception {
	// copy
	Instance copy = (Instance)test.copy();
	copy.setDataset(null);

	// delete attributes we don't need
	for(int i = excld.length-1; i >= 0; i--) {
		copy.deleteAttributeAt(this.excld[i]);
	}

	//set template
	copy.setDataset(this._template);

	//set class
	test.setValue(this.index,(int)(this.classifier.classifyInstance(copy))); 

	//carry on
	if (next!=null) next.classify(test);
}
 
Example 5
Source File: CCUtils.java    From meka with GNU General Public License v3.0 6 votes vote down vote up
/**
 * LinkTransform - prepare 'x' for testing at a node 'j' of the chain, by excluding 'exl'.
 * @param	x		instance
 * @param	excl	indices of labels which are NOT parents of j
 * @param	_D		the dataset template to use
 * @return	the transformed instance
 */
public static Instance linkTransformation(Instance x, int excl[], Instances _D) {
	// copy
	Instance copy = (Instance)x.copy();
	copy.setDataset(null);

	// delete attributes we don't need
	for(int i = excl.length-1; i >= 0; i--) {
		copy.deleteAttributeAt(excl[i]);
	}

	//set template
	copy.setDataset(_D);

	return copy;
}
 
Example 6
Source File: PCA.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Instance transform(Instance inst) {
    Instance newInst= null;
    try {
        newInst = pca.convertInstance(inst);
        while(newInst.numAttributes()-1>numAttributesToKeep)
            newInst.deleteAttributeAt(newInst.numAttributes()-2);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return newInst;
}
 
Example 7
Source File: CCp.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
protected void classify(Instance test) throws Exception {
	// copy
	Instance copy = (Instance)test.copy();
	copy.setDataset(null);

	// delete attributes we don't need
	for(int i = excld.length-1; i >= 0; i--) {
		copy.deleteAttributeAt(this.excld[i]);
	}

	//set template
	copy.setDataset(this._template);

	// round
	for(int k = 0; k < this.j; k++) {
		copy.setValue(j,Math.round(copy.value(k)));
	}

	//set class
	double dist[] = this.classifier.distributionForInstance(copy);
	int max_index = Utils.maxIndex(dist);
	confidences[this.index] = dist[max_index];
	test.setValue(this.index,max_index);

	//carry on
	if (next!=null) next.classify(test);
}
 
Example 8
Source File: PSUtils.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Convert a multi-label instance into a multi-class instance, according to a template.
 */
public static Instance convertInstance(Instance x, int L, Instances template) {
	Instance x_ = (Instance) x.copy(); 
	x_.setDataset(null);
	for (int i = 0; i < L; i++)
		x_.deleteAttributeAt(0);
	x_.insertAttributeAt(0);
	x_.setDataset(template);
	return x_;
}
 
Example 9
Source File: F.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
/**
 * meka2mulan - Move L label attributes from the beginning to end of attribute space of an Instance. 
 * Necessary because MULAN assumes label attributes are at the end, not the beginning.
 * (the extra time for this process is not counted in the running-time analysis of published work).
 */
public static final Instance meka2mulan(Instance x, int L) {
	x.setDataset(null);
	for(int j = 0; j < L; j++) {
		x.insertAttributeAt(x.numAttributes());
		x.deleteAttributeAt(0);
	}
	return x;
}
 
Example 10
Source File: MLUtils.java    From meka with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Delete attributes from an instance 'x' indexed by 'indicesToRemove[]'.
 * @param	x					instance
 * @param	indicesToRemove		array of attribute indices
 * @return	the modified dataset
 */
public static final Instance deleteAttributesAt(Instance x, int indicesToRemove[]) {//, boolean keep) {
	Arrays.sort(indicesToRemove);
	for(int j = indicesToRemove.length-1; j >= 0; j--) {
		x.deleteAttributeAt(indicesToRemove[j]);
	}
	return x;
}
 
Example 11
Source File: RT.java    From meka with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void buildClassifier(Instances D) throws Exception {
  	testCapabilities(D);
  	
	int L = D.classIndex();

	//Create header
	Instances D_ = new Instances(D,0,0);

	//Delete the old class attributes
	for (int j = 0; j < L; j++)
		D_.deleteAttributeAt(0); 

	//Make the new class attribute
	FastVector classes = new FastVector(L);
	for (int j = 0; j < L; j++)
		classes.addElement("C"+j);

	//Add the new class attribute
	D_.insertAttributeAt(new Attribute("ClassY",classes),0);
	D_.setClassIndex(0);

	//Loop through D again
	for (int i = 0; i < D.numInstances(); i++) {
		for (int j = 0; j < L; j++) {
			if((int)D.instance(i).value(j) > 0) {
				// make a copy here ...
				Instance x_ = (Instance)D.instance(i).copy();
				x_.setDataset(null);
				// make it multi-class, and set the appropriate class value ...
				for (int k = 1; k < L; k++)
					x_.deleteAttributeAt(1); 
				x_.setDataset(D_);
				x_.setClassValue(j); // (*) this just ponts to the right index
				D_.add(x_);
			}
		}
	}

	//Save the template
	m_InstancesTemplate = new Instances(D_,0);

	//Build
	if(getDebug())  System.out.println("Building classifier "+m_Classifier.getClass().getName()+" on "+D_.numInstances()+" instances (originally "+D.numInstances()+")");
	m_Classifier.buildClassifier(D_);

}