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

The following examples show how to use weka.core.Instance#dataset() . 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: SimpleMI.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Computes the distribution for a given exemplar
 *
 * @param newBag the exemplar for which distribution is computed
 * @return the distribution
 * @throws Exception if the distribution can't be computed successfully
 */
public double[] distributionForInstance(Instance newBag)
  throws Exception {

  double [] distribution = new double[2];
  Instances test = new Instances (newBag.dataset(), 0);	
  test.add(newBag);	

  test = transform(test);
  test.deleteAttributeAt(0);
  Instance newInst=test.firstInstance();

  distribution = m_Classifier.distributionForInstance(newInst);

  return distribution;	   
}
 
Example 2
Source File: AbstractEnsemble.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @return the distributions of each individual module, i.e [0] = first module's dist, [1] = second...
 */
public double[][] distributionForInstanceByConstituents(Instance instance) throws Exception{
    Instance ins = instance;
    if(this.transform!=null){
        Instances rawContainer = new Instances(instance.dataset(),0);
        rawContainer.add(instance);
        Instances converted = transform.process(rawContainer);
        ins = converted.instance(0);
    }

    double[][] distsByClassifier = new double[this.modules.length][];

    for(int i=0;i<modules.length;i++){
        distsByClassifier[i] = modules[i].getClassifier().distributionForInstance(ins);
    }

    return distsByClassifier;
}
 
Example 3
Source File: AbstractEnsemble.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
     * @return the predictions of each individual module, i.e [0] = first module's vote, [1] = second...
     */
    public double[] classifyInstanceByConstituents(Instance instance) throws Exception{
        Instance ins = instance;
        if(this.transform!=null){
            Instances rawContainer = new Instances(instance.dataset(),0);
            rawContainer.add(instance);
//            transform.setInputFormat(rawContainer);
//            Instances converted = Filter.useFilter(rawContainer,transform);


            Instances converted = transform.process(rawContainer);
            ins = converted.instance(0);
        }

        double[] predsByClassifier = new double[modules.length];

        for(int i=0;i<modules.length;i++)
            predsByClassifier[i] = modules[i].getClassifier().classifyInstance(ins);

        return predsByClassifier;
    }
 
Example 4
Source File: RnnSequenceClassifier.java    From wekaDeeplearning4j with GNU General Public License v3.0 5 votes vote down vote up
@Override
public double[] distributionForInstance(Instance instance) throws Exception {

  Instances data = new Instances(instance.dataset());
  data.add(instance);
  return distributionsForInstances(data)[0];
}
 
Example 5
Source File: DD_DTW.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
public double[] getNonScaledDistances(Instance first, Instance second){
        double dist = 0;
        double derDist = 0;

//        DTW dtw = new DTW();
        DTW_DistanceBasic dtw = new DTW_DistanceBasic();
        int classPenalty = 0;
        if(first.classIndex()>0){
            classPenalty=1;
        }

        GoreckiDerivativeFilter filter = new GoreckiDerivativeFilter();
        Instances temp = new Instances(first.dataset(),0);
        temp.add(first);
        temp.add(second);
        try{
            temp = filter.process(temp);
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }        

        dist = dtw.distance(first, second);
        derDist = dtw.distance(temp.get(0), temp.get(1), Double.MAX_VALUE);

        return new double[]{Math.sqrt(dist),Math.sqrt(derDist)};
    }
 
Example 6
Source File: RnnSequenceClassifier.java    From wekaDeeplearning4j with GNU General Public License v3.0 5 votes vote down vote up
@Override
public double[] distributionForInstance(Instance instance) throws Exception {

  Instances data = new Instances(instance.dataset());
  data.add(instance);
  return distributionsForInstances(data)[0];
}
 
Example 7
Source File: FeatureGeneratorTree.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Instance apply(final Instance data) throws PreprocessingException {
	try {
		Instances instances = new Instances(data.dataset());
		instances.clear();
		instances.add(data);
		return this.apply(instances).firstInstance();
	} catch (Exception e) {
		throw new PreprocessingException(e);
	}
}
 
Example 8
Source File: RankingByPairwiseComparison.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
public List<String> predict(final Instance xTest) throws PredictionException {
	try {
		Instances datasetCopy = new Instances(xTest.dataset(), 0);
		datasetCopy.add(xTest);
		datasetCopy = this.applyFiltersToDataset(datasetCopy);

		Map<String, Double> vote = new HashMap<>();
		this.labelSet.stream().forEach(x -> vote.put(x, 0.0));

		for (PairWiseClassifier pwc : this.pwClassifiers) {
			double[] dist = pwc.c.distributionForInstance(datasetCopy.get(0));

			switch (this.config.getVotingStrategy()) {
			case RPCConfig.V_VOTING_STRATEGY_CLASSIFY:
				if (dist[0] > dist[1]) {
					Maps.increaseCounterInDoubleMap(vote, pwc.a);
				} else {
					Maps.increaseCounterInDoubleMap(vote, pwc.b);
				}
				break;
			default:
			case RPCConfig.V_VOTING_STRATEGY_PROBABILITY:
				Maps.increaseCounterInDoubleMap(vote, pwc.a, dist[0]);
				Maps.increaseCounterInDoubleMap(vote, pwc.b, dist[1]);
				break;
			}
		}

		List<String> ranking = new LinkedList<>(vote.keySet());
		ranking.sort((arg0, arg1) -> vote.get(arg1).compareTo(vote.get(arg0)));
		return ranking;
	} catch (Exception e) {
		throw new PredictionException("Could not create a prediction.", e);
	}
}
 
Example 9
Source File: WekaUtil.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
public static Instance useFilterOnSingleInstance(final Instance instance, final Filter filter) throws Exception {
	Instances data = new Instances(instance.dataset());
	data.clear();
	data.add(instance);
	Instances filteredInstances = Filter.useFilter(data, filter);
	return filteredInstances.firstInstance();
}
 
Example 10
Source File: Filter.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
  * Adds an output instance to the queue. The derived class should use this
  * method for each output instance it makes available. 
  *
  * @param instance the instance to be added to the queue.
  */
 protected void push(Instance instance) {

   if (instance != null) {
     if (instance.dataset() != null)
copyValues(instance, false);
     instance.setDataset(m_OutputFormat);
     m_OutputQueue.push(instance);
   }
 }
 
Example 11
Source File: MISVM.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Computes the distribution for a given exemplar
 *
 * @param exmp the exemplar for which distribution is computed
 * @return the distribution
 * @throws Exception if the distribution can't be computed successfully
 */
public double[] distributionForInstance(Instance exmp)
  throws Exception {

  double sum=0;
  double classValue;
  double[] distribution = new double[2];

  Instances testData = new Instances(exmp.dataset(), 0);
  testData.add(exmp);

  // convert the training dataset into single-instance dataset
  testData = Filter.useFilter(testData, m_ConvertToProp);	
  testData.deleteAttributeAt(0); //remove the bagIndex attribute	

  if (m_Filter != null)	
    testData = Filter.useFilter(testData, m_Filter); 

  for(int j = 0; j < testData.numInstances(); j++){
    Instance inst = testData.instance(j);
    double output = m_SVM.output(-1, inst); 
    if (output <= 0)
      classValue = 0.0;
    else
      classValue = 1.0;
    sum += classValue;
  }
  if (sum == 0)
    distribution[0] = 1.0;
  else 
    distribution[0] = 0.0;
  distribution [1] = 1.0 - distribution[0];

  return distribution;
}
 
Example 12
Source File: MIOptimalBall.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Computes the distribution for a given multiple instance
 *
 * @param newBag the instance for which distribution is computed
 * @return the distribution
 * @throws Exception if the distribution can't be computed successfully
 */
public double[] distributionForInstance(Instance newBag)
  throws Exception {  

  double [] distribution = new double[2];	
  double distance; 
  distribution[0]=0;	 
  distribution[1]=0;

  Instances insts = new Instances(newBag.dataset(),0);
  insts.add(newBag);  

  // Filter instances 
  insts= Filter.useFilter( insts, m_ConvertToSI); 	
  if (m_Filter!=null) 
    insts = Filter.useFilter(insts, m_Filter);     

  //calculate the distance from each single instance to the ball center
  int numInsts = insts.numInstances(); 		
  insts.deleteAttributeAt(0); //remove the bagIndex attribute, no use for the distance calculation

  for (int i=0; i<numInsts; i++){
    distance =0;	   
    for (int j=0; j<insts.numAttributes()-1; j++)
      distance += (insts.instance(i).value(j) - m_Center[j])*(insts.instance(i).value(j)-m_Center[j]);  

    if (distance <=m_Radius*m_Radius){  // check whether this single instance is inside the ball
      distribution[1]=1.0;  //predicted as a positive bag   	  
      break;
    }	
  }

  distribution[0]= 1-distribution[1]; 

  return distribution; 
}
 
Example 13
Source File: MIBoost.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Computes the distribution for a given exemplar
 *
 * @param exmp the exemplar for which distribution is computed
 * @return the classification
 * @throws Exception if the distribution can't be computed successfully
 */
public double[] distributionForInstance(Instance exmp) 
  throws Exception { 

  double[] rt = new double[m_NumClasses];

  Instances insts = new Instances(exmp.dataset(), 0);
  insts.add(exmp);

  // convert the training dataset into single-instance dataset
  insts = Filter.useFilter( insts, m_ConvertToSI);
  insts.deleteAttributeAt(0); //remove the bagIndex attribute	

  double n = insts.numInstances();

  if(m_DiscretizeBin > 0)
    insts = Filter.useFilter(insts, m_Filter);

  for(int y=0; y<n; y++){
    Instance ins = insts.instance(y);	
    for(int x=0; x<m_NumIterations; x++){ 
      rt[(int)m_Models[x].classifyInstance(ins)] += m_Beta[x]/n;
    }
  }

  for(int i=0; i<rt.length; i++)
    rt[i] = Math.exp(rt[i]);

  Utils.normalize(rt);
  return rt;
}
 
Example 14
Source File: DTD_C.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
public double[] getNonScaledDistances(Instance first, Instance second){

            DTW_DistanceBasic dtw = new DTW_DistanceBasic();
            int classPenalty = 0;
            if(first.classIndex()>0){
                classPenalty=1;
            }
            Instances temp = new Instances(first.dataset(),0);
            temp.add(first);
            temp.add(second);
            try{
                SimpleBatchFilter bf=null;
                switch(this.transformType){
                    case COS:
                        bf=new Cosine();
                        break;
                    case SIN:
                        bf=new Sine();
                        break;
                    case HIL: default:
                         bf=new Hilbert();
                        break;
                }
                bf.setInputFormat(temp);
                temp = Filter.useFilter(temp,bf);    
            }catch(Exception e){
                e.printStackTrace();
                return null;
            }        

            double dist = dtw.distance(first, second);
            double transDist = dtw.distance(temp.get(0), temp.get(1), Double.MAX_VALUE);

            return new double[]{Math.sqrt(dist),Math.sqrt(transDist)};
        }
 
Example 15
Source File: AbstractEnsemble.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
@Override
    public double[] distributionForInstance(Instance instance) throws Exception{
        Instance ins = instance;
        if(this.transform!=null){
            Instances rawContainer = new Instances(instance.dataset(),0);
            rawContainer.add(instance);
//            transform.setInputFormat(rawContainer);
//            Instances converted = Filter.useFilter(rawContainer,transform);
            Instances converted = transform.process(rawContainer);            
            ins = converted.instance(0);
            
        }

        if (testResults == null || (testInstCounter == 0 && prevTestInstance == null)) {//definitely the first call, not e.g the first inst being classified for the second time
            printlnDebug("\n**TEST**");

            testResults = new ClassifierResults(numClasses);
            testResults.setTimeUnit(TimeUnit.NANOSECONDS);
            testResults.setBuildTime(buildTime);
        }

        if (readIndividualsResults && testInstCounter >= numTestInsts) //if no test files loaded, numTestInsts == -1
            throw new Exception("Received more test instances than expected, when loading test results files, found " + numTestInsts + " test cases");

        double[] dist;
        long startTime = System.nanoTime();
        long predTime;
        if (readIndividualsResults) { //have results loaded from file
            dist = votingScheme.distributionForTestInstance(modules, testInstCounter);
            predTime = System.nanoTime() - startTime; //time for ensemble to form vote
            for (EnsembleModule module : modules) //            +time for each member's predictions
                predTime += module.testResults.getPredictionTime(testInstCounter);
        }
        else {//need to classify them normally
            dist = votingScheme.distributionForInstance(modules, ins);
            predTime = System.nanoTime() - startTime;
        }
        
        testResults.turnOffZeroTimingsErrors();
        testResults.addPrediction(dist, indexOfMax(dist), predTime, "");
        testResults.turnOnZeroTimingsErrors();
        
        if (prevTestInstance != instance)
            ++testInstCounter;
        prevTestInstance = instance;

        return dist;
    }
 
Example 16
Source File: InputMappedClassifier.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
public Instance constructMappedInstance(Instance incoming) throws Exception {
  
  boolean regenerateMapping = false;
  
  if (m_inputHeader == null) {
    m_inputHeader = incoming.dataset();
    regenerateMapping = true;
    m_initialTestStructureKnown = false;
  } else if (!m_inputHeader.equalHeaders(incoming.dataset())) {
    /*System.out.println("[InputMappedClassifier] incoming data does not match " +
              "last known input format - regenerating mapping...");
    System.out.println("Incoming\n" + new Instances(incoming.dataset(), 0));
    System.out.println("Stored input header\n" + new Instances(m_inputHeader, 0));
    System.out.println("Model header\n" + new Instances(m_modelHeader, 0)); */
    m_inputHeader = incoming.dataset();
    
    regenerateMapping = true;
    m_initialTestStructureKnown = false;
  } else if (m_attributeMap == null) {
    regenerateMapping = true;
    m_initialTestStructureKnown = false;
  }
  
  if (regenerateMapping) {
    regenerateMapping();
    m_vals = null;
    
    if (!m_suppressMappingReport) {
      StringBuffer result = createMappingReport();
      System.out.println(result.toString());
    }
  }    
  
  m_vals = new double[m_modelHeader.numAttributes()];
  
  for (int i = 0; i < m_modelHeader.numAttributes(); i++) {
    if (m_attributeStatus[i] == OK) {
      Attribute modelAtt = m_modelHeader.attribute(i);
      Attribute incomingAtt = m_inputHeader.attribute(m_attributeMap[i]);
      
      if (Utils.isMissingValue(incoming.value(m_attributeMap[i]))) {
        m_vals[i] = Utils.missingValue();
        continue;
      }
      
      if (modelAtt.isNumeric()) {
        m_vals[i] = incoming.value(m_attributeMap[i]);
      } else if (modelAtt.isNominal()) {
        int mapVal = m_nominalValueMap[i][(int)incoming.value(m_attributeMap[i])];
        
        if (mapVal == NO_MATCH) {
          m_vals[i] = Utils.missingValue();
        } else {
          m_vals[i] = mapVal;
        }
      }
    } else {
      m_vals[i] = Utils.missingValue();
    }
  }
  
  Instance newInst = new DenseInstance(incoming.weight(), m_vals);
  newInst.setDataset(m_modelHeader);

  return newInst;
}
 
Example 17
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 18
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 19
Source File: Dl4jMlpClassifier.java    From wekaDeeplearning4j with GNU General Public License v3.0 3 votes vote down vote up
/**
 * The method to use when making a prediction for a test instance. Use distributionsForInstances()
 * instead for speed if possible.
 *
 * @param inst the instance to get a prediction for
 * @return the class probability estimates (if the class is nominal) or the numeric prediction (if
 * it is numeric)
 * @throws Exception if something goes wrong at prediction time
 */
@Override
public double[] distributionForInstance(Instance inst) throws Exception {

  Instances data = new Instances(inst.dataset());
  data.add(inst);
  return distributionsForInstances(data)[0];
}
 
Example 20
Source File: Dl4jMlpClassifier.java    From wekaDeeplearning4j with GNU General Public License v3.0 3 votes vote down vote up
/**
 * The method to use when making a prediction for a test instance. Use distributionsForInstances()
 * instead for speed if possible.
 *
 * @param inst the instance to get a prediction for
 * @return the class probability estimates (if the class is nominal) or the numeric prediction (if
 * it is numeric)
 * @throws Exception if something goes wrong at prediction time
 */
@Override
public double[] distributionForInstance(Instance inst) throws Exception {

  Instances data = new Instances(inst.dataset());
  data.add(inst);
  return distributionsForInstances(data)[0];
}