Java Code Examples for weka.core.DenseInstance#setValue()

The following examples show how to use weka.core.DenseInstance#setValue() . 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: BayesianOptimisedSearch.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
public double GetIG(double[] params){
    
    int length = (int)params[1];
    //if we have invalid shapelet lengths of positions we want to fail the NelderMead.
    if (length < 3 || length > seriesLength)
        return 1E99;
    
    if(params[0] < 0 || params[0] >= seriesLength - length)
        return 1E99;
    
    try 
    {
        DenseInstance new_inst = new DenseInstance(3);
        new_inst.setValue(0, params[0]);
        new_inst.setValue(1, params[1]);
        new_inst.setValue(2, 0); //set it as 0, because we don't know it yet.

        return 1.0 - current_gp.classifyInstance(new_inst);
        
    } catch (Exception ex) {
        System.out.println("bad");
    }
    
    return 1E99;
}
 
Example 2
Source File: Efficient1NN.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
public static Instances concatenate(Instances[] train) {
    // make a super arff for finding params that need stdev etc
    Instances temp = new Instances(train[0], 0);
    for (int i = 1; i < train.length; i++) {
        for (int j = 0; j < train[i].numAttributes() - 1; j++) {
            temp.insertAttributeAt(train[i].attribute(j), temp.numAttributes() - 1);
        }
    }

    int dataset, attFromData;

    for (int insId = 0; insId < train[0].numInstances(); insId++) {
        DenseInstance dense = new DenseInstance(temp.numAttributes());
        for (int attId = 0; attId < temp.numAttributes() - 1; attId++) {

            dataset = attId / (train[0].numAttributes() - 1);
            attFromData = attId % (train[0].numAttributes() - 1);
            dense.setValue(attId, train[dataset].instance(insId).value(attFromData));

        }
        dense.setValue(temp.numAttributes() - 1, train[0].instance(insId).classValue());
        temp.add(dense);
    }
    return temp;
}
 
Example 3
Source File: DataSetUtilsTest.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
public void cifar10InstancesAttributesTest() {
    ArrayList<Attribute> atts = new ArrayList<>();
    for (int i = 0; i < 32 * 32 * 3 + 1; i++) {
        atts.add(new Attribute("blub" + i));
    }
    Instances instances = new Instances("test", atts, 1);
    DenseInstance inst = new DenseInstance(atts.size());
    for (int i = 0; i < inst.numAttributes(); i++) {
        inst.setValue(i, 1d);
    }
    inst.setDataset(instances);
    instances.add(inst);

    INDArray result = DataSetUtils.cifar10InstanceToMatrix(inst);
    Assert.assertArrayEquals(new long[]{32, 32, 3}, result.shape());
}
 
Example 4
Source File: BayesianOptimisedSearch.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
public Instance ConvertPairToInstance(ShapeletSearch.CandidateSearchData pair) {
    DenseInstance new_inst = new DenseInstance(3);
    new_inst.setValue(0, pair.getStartPosition());
    new_inst.setValue(1, pair.getLength());
    new_inst.setValue(2, 0); //set it as 0, because we don't know it yet.

    return new_inst;
}
 
Example 5
Source File: TSBF.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
Instances formatIntervalInstances(Instances data){

        //3 stats for whole subseries, start and end point, 3 stats per interval
        int numFeatures=(3+2+3*numIntervals); 
        //Set up instances size and format. 
        FastVector atts=new FastVector();
        String name;
        for(int j=0;j<numFeatures;j++){
                name = "F"+j;
                atts.addElement(new Attribute(name));
        }
        //Get the class values as a fast vector			
        Attribute target =data.attribute(data.classIndex());
       FastVector vals=new FastVector(target.numValues());
        for(int j=0;j<target.numValues();j++)
                vals.addElement(target.value(j));
        atts.addElement(new Attribute(data.attribute(data.classIndex()).name(),vals));
//create blank instances with the correct class value                
        Instances result = new Instances("SubsequenceIntervals",atts,data.numInstances());
        result.setClassIndex(result.numAttributes()-1);
        for(int i=0;i<data.numInstances();i++){
            double cval=data.instance(i).classValue();
            for(int j=0;j<numSubSeries;j++){
                DenseInstance in=new DenseInstance(result.numAttributes());
                in.setValue(result.numAttributes()-1,cval);
                result.add(in);
            }
        }
        return result;
    }
 
Example 6
Source File: TSBF.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
Instances formatProbabilityInstances(double[][] probs,Instances data){
        int numClasses=data.numClasses();
        int numFeatures=(numClasses-1)*numSubSeries;
        //Set up instances size and format. 
        FastVector atts=new FastVector();
        String name;
        for(int j=0;j<numFeatures;j++){
                name = "ProbFeature"+j;
                atts.addElement(new Attribute(name));
        }
        //Get the class values as a fast vector			
        Attribute target =data.attribute(data.classIndex());
       FastVector vals=new FastVector(target.numValues());
        for(int j=0;j<target.numValues();j++)
                vals.addElement(target.value(j));
        atts.addElement(new Attribute(data.attribute(data.classIndex()).name(),vals));
//create blank instances with the correct class value                
        Instances result = new Instances("SubsequenceIntervals",atts,data.numInstances());
        result.setClassIndex(result.numAttributes()-1);
        for(int i=0;i<data.numInstances();i++){
            double cval=data.instance(i).classValue();
            DenseInstance in=new DenseInstance(result.numAttributes());
            in.setValue(result.numAttributes()-1,cval);
            int pos=0;
            for(int j=0;j<numSubSeries;j++){
                for(int k=0;k<numClasses-1;k++)
                    in.setValue(pos++, probs[j+numSubSeries*i][k]);
            }           
            result.add(in);
        }
        return result;
    }
 
Example 7
Source File: TSBF.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
Instances formatFrequencyBinInstances(int[][][] counts,double[][] classProbs,Instances data){
        int numClasses=data.numClasses();
        int numFeatures=numBins*(numClasses-1)+numClasses;
        //Set up instances size and format. 
        FastVector atts=new FastVector();
        String name;
        for(int j=0;j<numFeatures;j++){
                name = "FreqBinFeature"+j;
                atts.addElement(new Attribute(name));
        }
                
        //Get the class values as a fast vector			
        Attribute target =data.attribute(data.classIndex());
       FastVector vals=new FastVector(target.numValues());
        for(int j=0;j<target.numValues();j++)
                vals.addElement(target.value(j));
        atts.addElement(new Attribute(data.attribute(data.classIndex()).name(),vals));
//create blank instances with the correct class value                
        Instances result = new Instances("HistogramCounts",atts,data.numInstances());
        result.setClassIndex(result.numAttributes()-1);
        for(int i=0;i<data.numInstances();i++){
            double cval=data.instance(i).classValue();
            DenseInstance in=new DenseInstance(result.numAttributes());
            in.setValue(result.numAttributes()-1,cval);
            int pos=0;
//Set values here            
            for(int j=0;j<numClasses-1;j++){
                for(int k=0;k<numBins;k++)
                    in.setValue(pos++,counts[i][j][k]);
            }           
//            
           for(int j=0;j<numClasses;j++)
               in.setValue(pos++,classProbs[i][j]);
             
            result.add(in);
        }
        return result;
    }
 
Example 8
Source File: PerformanceKnowledgeBase.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
public Instance getInstanceForIndividualCI(final String benchmarkName, final ComponentInstance ci, final double score) {
	Instances instancesInd = this.performanceInstancesIndividualComponents.get(benchmarkName).get(ci.getComponent().getName());
	DenseInstance instanceInd = new DenseInstance(instancesInd.numAttributes());
	for (int i = 0; i < instancesInd.numAttributes() - 1; i++) {
		Attribute attr = instancesInd.attribute(i);
		String attrFQN = attr.name();
		String attrName = attrFQN.substring(attrFQN.indexOf("::") + 2);
		Parameter param = ci.getComponent().getParameterWithName(attrName);
		String value;
		if (ci.getParametersThatHaveBeenSetExplicitly().contains(param)) {
			value = ci.getParameterValues().get(param.getName());
		} else {
			value = param.getDefaultValue().toString();
		}
		if (value != null) {
			if (param.isCategorical()) {
				boolean attrContainsValue = false;
				Enumeration<Object> possibleValues = attr.enumerateValues();
				while (possibleValues.hasMoreElements() && !attrContainsValue) {
					Object o = possibleValues.nextElement();
					if (o.equals(value)) {
						attrContainsValue = true;
					}
				}
				if (attrContainsValue) {
					instanceInd.setValue(attr, value);
				}
			} else if (param.isNumeric()) {
				double finalValue = Double.parseDouble(value);
				instanceInd.setValue(attr, finalValue);
			}
		}
	}
	Attribute scoreAttrInd = instancesInd.classAttribute();
	instanceInd.setValue(scoreAttrInd, score);
	return instanceInd;
}
 
Example 9
Source File: KShape.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
public void calculateDistance(Instance first, Instance second, boolean calcShift){
    int oldLength = first.numAttributes()-1;
    int oldLengthY = second.numAttributes()-1;

    int length = paddedLength(oldLength);

    //FFT and IFFT
    fft = new FFT();
            
    Complex[] firstC = fft(first, oldLength, length);
    Complex[] secondC = fft(second, oldLengthY, length);
    
    for (int i = 0; i < length; i++){
        secondC[i].conjugate();
        firstC[i].multiply(secondC[i]);
    }
    
    fft.inverseFFT(firstC, length);

    //Calculate NCCc values
    double firstNorm = sumSquare(first);
    double secondNorm = sumSquare(second);
    double norm = Math.sqrt(firstNorm * secondNorm);
    
    double[] ncc = new double[oldLength*2-1];
    int idx = 0;
    
    for (int i = length-oldLength+1; i < length; i++){
        ncc[idx++] = firstC[i].getReal()/norm;
    }
    
    for (int i = 0; i < oldLength; i++){
        ncc[idx++] = firstC[i].getReal()/norm;
    }
    
    double maxValue = 0;
    int shift = -1;

    //Largest NCCc value and index
    for (int i = 0; i < ncc.length; i++){
        if (ncc[i] > maxValue){
            maxValue = ncc[i];
            shift = i;
        }
    }
    
    dist = 1 - maxValue;

    //Create y', shifting the second instance in a direction and padding with 0s
    if (calcShift){
        if (oldLength > oldLengthY){
            shift -= oldLength-1;
        }
        else {
            shift -= oldLengthY-1;
        }

        yShift = new DenseInstance(1, new double[second.numAttributes()]);

        if (shift >= 0){
            for (int i = 0; i < oldLengthY-shift; i++){
                yShift.setValue(i + shift, second.value(i));
            }
        }
        else {
            for (int i = 0; i < oldLengthY+shift; i++){
                yShift.setValue(i, second.value(i-shift));
            }
        }
    }
}
 
Example 10
Source File: LPS.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
public double classifyInstance(Instance ins) throws Exception{
        
        int[][] testNodeCounts=new int[nosTrees][];
//Extract sequences, shove them into instances. 
//        concatenate these segments rowwise, let resulting matrix be M
            

        for(int i=0;i<nosTrees;i++){    
            ArrayList<Attribute> atts=new ArrayList<>();
            String name;
            for(int j=0;j<2*nosSegments;j++){
                    name = "SegFeature"+j;
                    atts.add(new Attribute(name));
            }
            sequences = new Instances("SubsequenceIntervals",atts,segLengths[i]);            
            for(int k=0;k<segLengths[i];k++){
                DenseInstance in=new DenseInstance(sequences.numAttributes());
                for(int m=0;m<nosSegments;m++)
                    in.setValue(m, ins.value(segStarts[i][m]+k));
                for(int m=0;m<nosSegments;m++)
                    in.setValue(nosSegments+m, ins.value(segDiffStarts[i][m]+k)-ins.value(segDiffStarts[i][m]+k+1));
                sequences.add(in);
//                System.out.println(" TEST INS ="+in+" CLASS ="+ins.classValue());
            }            
            sequences.setClassIndex(classAtt[i]);
            testNodeCounts[i]=new int[trees[i].nosLeafNodes];
            for(int k=0;k<sequences.numInstances();k++){
                trees[i].distributionForInstance(sequences.instance(k));
                int leafID=RandomRegressionTree.lastNode;
//                    System.out.println("Seq Number ="+(j*segLengths[i]+k));
                testNodeCounts[i][leafID]++;
            }
        }
//        System.out.println(" TEST NODE COUNTS =");
//        for(int i=0;i<testNodeCounts.length;i++){
//            for(int j=0;j<testNodeCounts[i].length;j++)
//                System.out.print(" "+testNodeCounts[i][j]);
//            System.out.println("");
//        }
//        System.out.println(" TRAIN NODE COUNTS =");
//        for(int k=0;k<leafNodeCounts.length;k++){
//            for(int i=0;i<leafNodeCounts[k].length;i++){
//                for(int j=0;j<leafNodeCounts[k][i].length;j++)
//                    System.out.print(" "+leafNodeCounts[k][i][j]);
//                System.out.println("");
//            }
//        }
            
//1-NN on the counts
        double minDist=Double.MAX_VALUE;
        int closest=0;
        for(int i=0;i<leafNodeCounts.length;i++){
            double d=distance(testNodeCounts,leafNodeCounts[i]);
            if(d<minDist){
                minDist=d;
                closest=i;
            }
        }
        return trainClassVals[closest];
    }
 
Example 11
Source File: PAA.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) {
//        System.out.println("PAAtest\n\n");
//        
//        try {
//            Instances test = ClassifierTools.loadData("C:\\tempbakeoff\\TSC Problems\\Car\\Car_TEST.arff");
//            PAA paa = new PAA();
//            paa.setNumIntervals(2);
//            Instances result = paa.process(test);
//            
//            System.out.println(test);
//            System.out.println("\n\n\nResults:\n\n");
//            System.out.println(result);
//        }
//        catch (Exception e) {
//            System.out.println(e);
//        }

        
        // Jason's Test
        try{
            double[] wavey = {0.841470985,0.948984619,0.997494987,0.983985947,0.909297427,0.778073197,0.598472144,0.381660992,0.141120008,-0.108195135,-0.350783228,-0.571561319,-0.756802495,-0.894989358,-0.977530118,-0.999292789,-0.958924275,-0.858934493,-0.705540326,-0.508279077,-0.279415498};
            
            PAA paa = new PAA();
            paa.setNumIntervals(10);
            
            // convert into Instances format            
            ArrayList<Attribute> atts = new ArrayList<>();
            DenseInstance ins = new DenseInstance(wavey.length+1);
            for(int i = 0; i < wavey.length; i++){
                ins.setValue(i, wavey[i]);
                atts.add(new Attribute("att"+i));
            }
            atts.add(new Attribute("classVal"));
            ins.setValue(wavey.length, 1);

            Instances instances = new Instances("blah", atts, 1);
            instances.setClassIndex(instances.numAttributes()-1);
            instances.add(ins);
                        
            Instances out = paa.process(instances);

            for(int i = 0; i < out.numAttributes()-1;i++){
                System.out.print(out.instance(0).value(i)+",");
            }
            System.out.println();
        }catch(Exception e){
            e.printStackTrace();
        }

    }
 
Example 12
Source File: MultilayerPerceptron.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
  * Call this function to predict the class of an instance once a 
  * classification model has been built with the buildClassifier call.
  * @param i The instance to classify.
  * @return A double array filled with the probabilities of each class type.
  * @throws Exception if can't classify instance.
  */
 public double[] distributionForInstance(Instance i) throws Exception {
  
   // default model?
   if (m_useDefaultModel) {
     return m_ZeroR.distributionForInstance(i);
   }

   m_currentInstance = new DenseInstance(i);
   
   if (m_useNomToBin) {
     m_nominalToBinaryFilter.input(m_currentInstance);
     m_currentInstance = m_nominalToBinaryFilter.output();
   }
   
   if (m_normalizeAttributes) {
     for (int noa = 0; noa < m_instances.numAttributes(); noa++) {
if (noa != m_instances.classIndex()) {
  if (m_attributeRanges[noa] != 0) {
    m_currentInstance.setValue(noa, (m_currentInstance.value(noa) - 
				     m_attributeBases[noa]) / 
			       m_attributeRanges[noa]);
  }
  else {
    m_currentInstance.setValue(noa, m_currentInstance.value(noa) -
			       m_attributeBases[noa]);
  }
}
     }
   }
   resetNetwork();
   
   //since all the output values are needed.
   //They are calculated manually here and the values collected.
   double[] theArray = new double[m_numClasses];
   for (int noa = 0; noa < m_numClasses; noa++) {
     theArray[noa] = m_outputs[noa].outputValue(true);
   }
   if (m_instances.classAttribute().isNumeric()) {
     return theArray;
   }
   
   //now normalize the array
   double count = 0;
   for (int noa = 0; noa < m_numClasses; noa++) {
     count += theArray[noa];
   }
   if (count <= 0) {
     return m_ZeroR.distributionForInstance(i);
   }
   for (int noa = 0; noa < m_numClasses; noa++) {
     theArray[noa] /= count;
   }
   return theArray;
 }
 
Example 13
Source File: LearnPatternSimilarityLearningAlgorithm.java    From AILibs with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Function generating subseries feature instances based on the given
 * <code>segments</code> and <code>segmentsDifference</code> matrices. The
 * <code>len</code> parameter indicates which subsequence instance is generated
 * within this call. The values are extracted and used for calculation (for
 * difference) from <code>instValues</code>.
 *
 * @param instValues
 *            Instance values used for feature generation
 * @param segments
 *            Segment start indices used for feature generation
 * @param segmentsDifference
 *            Segment difference start indices used for feature generation
 * @param len
 *            Current length (is added to the segment and segment difference
 *            locations)
 * @return Returns a Weka instance storing the generated features
 */
public static Instance generateSubseriesFeatureInstance(final double[] instValues, final int[] segments, final int[] segmentsDifference, final int len) {
	if (segments.length != segmentsDifference.length) {
		throw new IllegalArgumentException("The number of segments and the number of segments differences must be the same!");
	}
	if (instValues.length < len) {
		throw new IllegalArgumentException("If the segments' length is set to '" + len + "', the number of time series variables must be greater or equals!");
	}

	DenseInstance instance = new DenseInstance(2 * segments.length);
	for (int seq = 0; seq < segments.length; seq++) {
		instance.setValue(seq * 2, instValues[segments[seq] + len]);

		double difference = instValues[segmentsDifference[seq] + len + 1] - instValues[segmentsDifference[seq] + len];
		instance.setValue(seq * 2 + 1, difference);
	}
	return instance;
}