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

The following examples show how to use weka.core.Instance#toDoubleArray() . 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: PowerSpectrum.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) {
    String problemPath = "E:/TSCProblems/";
    String resultsPath="E:/Temp/";
    String datasetName="ItalyPowerDemand";
    Instances train =DatasetLoading.loadDataNullable("E:/TSCProblems/"+datasetName+"/"+datasetName+"_TRAIN");
    PowerSpectrum ps= new PowerSpectrum();
    try {
        Instances trans=ps.process(train);
        OutFile out = new OutFile(resultsPath+datasetName+"PS_JAVA.csv");
        out.writeLine(datasetName);
        for(Instance ins: trans){
            double[] d=ins.toDoubleArray();
            for(int j=0;j<d.length;j++){
                if(j!=trans.classIndex())
                    out.writeString(d[j]+",");
            }
            out.writeString("\n");
        }
    } catch (Exception ex) {
        System.out.println("ERROR IN DEMO");
        Logger.getLogger(ACF.class.getName()).log(Level.SEVERE, null, ex);
    }
}
 
Example 2
Source File: ShapeletDistance.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
public void setCandidate(Instance inst, int start, int len, int dim) {
    //extract shapelet and nomrliase.
    cand = new ShapeletCandidate();
    startPos = start;
    length = len;
    dimension =  dim;

    //only call to double array when we've changed series.
    if(candidateInst==null || candidateInst != inst){
        candidateArray = inst.toDoubleArray();
        candidateInst = inst;
    }
    
    double[] temp = new double[length];
    //copy the data from the whole series into a candidate.
    System.arraycopy(candidateArray, start, temp, 0, length);
    cand.setShapeletContent(temp);
    
    // znorm candidate here so it's only done once, rather than in each distance calculation
    cand.setShapeletContent(seriesRescaler.rescaleSeries(cand.getShapeletContent(), false));
}
 
Example 3
Source File: DataSetUtils.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
public static INDArray instanceToMatrix(final Instance instance, final long[] refShape) {
	INDArray result;

	// If ref shape is not null, the shape has been preserved
	if (refShape != null) {
		result = Nd4j.zeros(refShape);
		for (int i = 0; i < instance.numAttributes() - 1; i++) {
			result.putScalar(i, instance.value(i));
		}
	} else {
		double[] data = instance.toDoubleArray();
		// Get data without last element
		result = Nd4j.create(Arrays.copyOf(data, data.length - 1));
	}

	return result;
}
 
Example 4
Source File: RISE.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
@Override
    public double[] distributionForInstance(Instance ins) throws Exception {
        double[] votes=new double[ins.numClasses()];
////Build instance
        double[] series=ins.toDoubleArray();
        for(int i=0;i<baseClassifiers.length;i++){
            int numFeatures=endPoints[i]-startPoints[i]+1;
        //extract the interval
            for(int j=0;j<numFeatures;j++){
                testHolders[i].instance(0).setValue(j, ins.value(j+startPoints[i]));
            }
//Do the transform
            Instances temp=filterData(testHolders[i]);
            int c=(int)baseClassifiers[i].classifyInstance(temp.instance(0));
            votes[c]++;

        }
        for(int i=0;i<votes.length;i++)
            votes[i]/=baseClassifiers.length;
        return votes;
    }
 
Example 5
Source File: NN_CID.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
@Override
        public double distance(Instance first, Instance second, double cutoff){
            
            double d=0;
//Find the acf terms
            double d1=0,d2=0;
            double[] data1=first.toDoubleArray();
            double[] data2=second.toDoubleArray();
            
            d=dtw.distance(first, second);
            for(int i=0;i<first.numAttributes()-2;i++)
                d1+=(data1[i]-data1[i+1])*(data1[i]-data1[i+1]);
            for(int i=0;i<first.numAttributes()-2;i++)
                d2+=(data2[i]-data2[i+1])*(data2[i]-data2[i+1]);
            d1=Math.sqrt(d1)+0.001; //This is from theircode
            d2=Math.sqrt(d2)+0.001; //This is from theircode
            if(d1<d2){
                double temp=d1;
                d1=d2;
                d2=temp;
            }
            d=d*(d1/d2);
            return d;
        }
 
Example 6
Source File: DataSetUtils.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
public static FastBitmap cifar10InstanceToBitmap(final Instance instance) {
	final BufferedImage image = new BufferedImage(32, 32, BufferedImage.TYPE_INT_RGB);
	final double[] imageValues = instance.toDoubleArray();
	for (int i = 0; i < 32; i++) {
		for (int j = 0; j < 32; j++) {
			final int offset = (i + 1);
			final int a = 255;
			final int r = (int) imageValues[offset * j];
			final int g = (int) imageValues[1024 + offset * j];
			final int b = (int) imageValues[2048 + offset * j];
			int p = 0;
			p = p | (a << 24);
			p = p | (r << 16);
			p = p | (g << 8);
			p = p | b;
			image.setRGB(i, j, p);
		}
	}
	return new FastBitmap(image);
}
 
Example 7
Source File: NN_CID.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
@Override
        public double distance(Instance first, Instance second, double cutoff){
            
            double d=0;
//Find the acf terms
            double d1=0,d2=0;
            double[] data1=first.toDoubleArray();
            double[] data2=second.toDoubleArray();
            for(int i=0;i<first.numAttributes()-1;i++)
                d+=(data1[i]-data2[i])*(data1[i]-data2[i]);
            d=Math.sqrt(d);
            for(int i=0;i<first.numAttributes()-2;i++)
                d1+=(data1[i]-data1[i+1])*(data1[i]-data1[i+1]);
            for(int i=0;i<first.numAttributes()-2;i++)
                d2+=(data2[i]-data2[i+1])*(data2[i]-data2[i+1]);
            d1=Math.sqrt(d1+0.001); //This is from theircode
            d2=Math.sqrt(d2+0.001); //This is from theircode
            if(d1<d2){
                double temp=d1;
                d1=d2;
                d2=temp;
            }
            d=Math.sqrt(d);
            d=d*(d1/d2);
            return d;
        }
 
Example 8
Source File: DataSetUtils.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
public static INDArray cifar10InstanceToMatrix(final Instance instance) {
	final INDArray result = Nd4j.create(32, 32, 3);
	final double[] imageValues = instance.toDoubleArray();
	if (imageValues.length != (32 * 32 * 3 + 1)) {
		throw new IllegalArgumentException("Cifar 10 instances must have the dimensionality of 32 x 32 x 3!");
	}

	for (int i = 0; i < 32; i++) {
		for (int j = 0; j < 32; j++) {
			final int offset = i + 1;
			result.putScalar(new int[] { i, j, 0 }, imageValues[offset * j]);
			result.putScalar(new int[] { i, j, 1 }, imageValues[1024 + offset * j]);
			result.putScalar(new int[] { i, j, 2 }, imageValues[2048 + offset * j]);
		}
	}

	return result;
}
 
Example 9
Source File: ACF.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
    PRE: An instance of ACF data. Performs a test of significance on the 
    * ACF terms until it finds the first insignificant one.
    * Will not work if the class variable is not 
    the last. 
    * @param inst
 * @return 
 */    
    private int findSingleCutOff(Instance inst){
/** Finds the threshold of the first non significant ACF term for all the series.
*/            
        double[] r=inst.toDoubleArray();
        int count=0;
        if(useGlobalSigThreshold){
            for(int i=0;i<inst.numAttributes();i++){
                if(i!=inst.classIndex()){
                    sigThreshold[count]=globalSigThreshold;
                    count++;
                }
            }
        }
        else{   ///DO NOT USE, I'm not sure of the logic of this, need to look up the paper
            sigThreshold[0]=r[0]*r[0];
            count=1;
            for(int i=1;i<inst.numAttributes();i++){
                if(i!=inst.classIndex()){
                sigThreshold[count]=sigThreshold[count-1]+r[i]*r[i]; 
                count++;
                }
            }
            for(int i=0;i<sigThreshold.length;i++){
                sigThreshold[i]=(1+sigThreshold[i])/seriesLength;
                sigThreshold[i]=2/Math.sqrt(sigThreshold[i]);
            }
        }
        for(int i=0;i<sigThreshold.length;i++)
            if(Math.abs(r[i])<sigThreshold[i])
                return i;
        return sigThreshold.length-1;
    }
 
Example 10
Source File: BestConf.java    From bestconf with Apache License 2.0 5 votes vote down vote up
public static void testCOMT2() throws Exception{
	BestConf bestconf = new BestConf();
	Instances trainingSet = DataIOFile.loadDataFromArffFile("data/trainingBestConf0.arff");
	trainingSet.setClassIndex(trainingSet.numAttributes()-1);
	
	Instances samplePoints = LHSInitializer.getMultiDimContinuous(bestconf.getAttributes(), InitialSampleSetSize, false);
	samplePoints.insertAttributeAt(trainingSet.classAttribute(), samplePoints.numAttributes());
	samplePoints.setClassIndex(samplePoints.numAttributes()-1);
	
	COMT2 comt = new COMT2(samplePoints, COMT2Iteration);
	
	comt.buildClassifier(trainingSet);
	
	Evaluation eval = new Evaluation(trainingSet);
	eval.evaluateModel(comt, trainingSet);
	System.err.println(eval.toSummaryString());
	
	Instance best = comt.getInstanceWithPossibleMaxY(samplePoints.firstInstance());
	Instances bestInstances = new Instances(trainingSet,2);
	bestInstances.add(best);
	DataIOFile.saveDataToXrffFile("data/trainingBestConf_COMT2.arff", bestInstances);
	
	//now we output the training set with the class value updated as the predicted value
	Instances output = new Instances(trainingSet, trainingSet.numInstances());
	Enumeration<Instance> enu = trainingSet.enumerateInstances();
	while(enu.hasMoreElements()){
		Instance ins = enu.nextElement();
		double[] values = ins.toDoubleArray();
		values[values.length-1] = comt.classifyInstance(ins);
		output.add(ins.copy(values));
	}
	DataIOFile.saveDataToXrffFile("data/trainingBestConf0_predict.xrff", output);
}
 
Example 11
Source File: DataSetUtils.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
public static INDArray mnistInstanceToMatrix(final Instance instance) {
	final INDArray result = Nd4j.create(28, 28);
	final double[] imageValues = instance.toDoubleArray();
	if (imageValues.length != (28 * 28 + 1)) {
		throw new IllegalArgumentException("MNIST instances must have the dimensionality of 28 x 28 x 1!");
	}

	for (int i = 0; i < 28; i++) {
		for (int j = 0; j < 28; j++) {
			final int offset = i + 1;
			result.putScalar(i, j, imageValues[offset * j]);
		}
	}
	return result;
}
 
Example 12
Source File: TSF.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
* @param ins to classifier
* @return array of doubles: probability of each class 
* @throws Exception 
*/   
   @Override
   public double[] distributionForInstance(Instance ins) throws Exception {
       double[] d=new double[ins.numClasses()];
       //Build transformed instance
       double[] series=ins.toDoubleArray();
       for(int i=0;i<trees.size();i++){
           for(int j=0;j<numIntervals;j++){
               //extract all intervals
               FeatureSet f= new FeatureSet();
               f.setFeatures(series, intervals.get(i)[j][0], intervals.get(i)[j][1]);
               testHolder.instance(0).setValue(j*3, f.mean);
               testHolder.instance(0).setValue(j*3+1, f.stDev);
               testHolder.instance(0).setValue(j*3+2, f.slope);
           }
           if(voteEnsemble){
               int c=(int)trees.get(i).classifyInstance(testHolder.instance(0));
               d[c]++;
           }else{
               double[] temp=trees.get(i).distributionForInstance(testHolder.instance(0));
               for(int j=0;j<temp.length;j++)
                   d[j]+=temp[j];
           }
       }
       double sum=0;
       for(double x:d)
           sum+=x;
       if(sum>0)
           for(int i=0;i<d.length;i++)
               d[i]=d[i]/sum;
       return d;
   }
 
Example 13
Source File: ERPDistance.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * distance method that converts instances to arrays of doubles
 *
 * @param first       instance 1
 * @param second      instance 2
 * @param cutOffValue used for early abandon
 * @return distance between instances
 */
@Override
public double distance(Instance first, Instance second, double cutOffValue) {
    //remove class index from first instance if there is one
    int firtClassIndex = first.classIndex();
    double[] arr1;
    if (firtClassIndex > 0) {
        arr1 = new double[first.numAttributes() - 1];
        for (int i = 0, j = 0; i < first.numAttributes(); i++) {
            if (i != firtClassIndex) {
                arr1[j] = first.value(i);
                j++;
            }
        }
    } else {
        arr1 = first.toDoubleArray();
    }

    //remove class index from second instance if there is one
    int secondClassIndex = second.classIndex();
    double[] arr2;
    if (secondClassIndex > 0) {
        arr2 = new double[second.numAttributes() - 1];
        for (int i = 0, j = 0; i < second.numAttributes(); i++) {
            if (i != secondClassIndex) {
                arr2[j] = second.value(i);
                j++;
            }
        }
    } else {
        arr2 = second.toDoubleArray();
    }

    return distance(arr1, arr2, cutOffValue);
}
 
Example 14
Source File: WeightedDTW.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
     * distance method that converts instances to arrays of doubles
     * 
     * @param first instance 1
     * @param second instance 2
     * @param cutOffValue used for early abandon
     * @return distance between instances
     */
    @Override
    public double distance(Instance first, Instance second, double cutOffValue){
//        if(this.distanceCount % 10000000 == 0){
//            System.out.println("New Instance: "+this.distanceCount);
//        }
//        this.distanceCount++;
        //remove class index from first instance if there is one
        int firtClassIndex = first.classIndex();
        double[] arr1;
        if(firtClassIndex > 0){
            arr1 = new double[first.numAttributes()-1];
            for(int i = 0,j = 0; i < first.numAttributes(); i++){
                if(i != firtClassIndex){
                    arr1[j]= first.value(i);
                    j++;
                }
            }
        }else{
            arr1 = first.toDoubleArray();
        }
        
        //remove class index from second instance if there is one
        int secondClassIndex = second.classIndex();
        double[] arr2;
        if(secondClassIndex > 0){
            arr2 = new double[second.numAttributes()-1];
            for(int i = 0,j = 0; i < second.numAttributes(); i++){
                if(i != secondClassIndex){
                    arr2[j]= second.value(i);
                    j++;
                }
            }
        }else{
            arr2 = second.toDoubleArray();
        }
        
        return distance(arr1,arr2,cutOffValue);
    }
 
Example 15
Source File: WekaNeurophSample.java    From NeurophFramework with Apache License 2.0 5 votes vote down vote up
/**
 * Prints Weka data set
 *
 * @param wekaDataset Instances Weka data set
 */
private static void printDataSet(Instances wekaDataset) {
    System.out.println("Weka dataset");
    Enumeration en = wekaDataset.enumerateInstances();
    while (en.hasMoreElements()) {
        Instance instance = (Instance) en.nextElement();
        double[] values = instance.toDoubleArray();
        System.out.println(Arrays.toString(values));
        System.out.println(instance.stringValue(instance.classIndex()));
    }
}
 
Example 16
Source File: PowerCepstrum.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
@Override
    public Instances process(Instances instances) throws Exception {
//Find power spectrum                
        Instances output=super.process(instances);
//Take logs
        logDataSet(output);
//Take Inverse FFT of logged Spectrum.
        for(int i=0;i<output.numInstances();i++){
//Get out values, store in a complex array   
            Instance next=output.instance(i);
            double[] ar=next.toDoubleArray();
//Have to pad
            int n = (int)MathsPower2.roundPow2(ar.length-1);
            if(n<ar.length-1)
                n*=2;
            FFT.Complex[] complex=new FFT.Complex[n];
            for(int j=0;j<ar.length-1;j++)
                complex[j]=new FFT.Complex(ar[j],0);
            for(int j=ar.length-1;j<n;j++)
                complex[j]=new FFT.Complex(0,0);


            //Take inverse FFT
            inverseFFT(complex,complex.length);
//Square the terms for the PowerCepstrum 
            for(int j=0;j<ar.length-1;j++)
                next.setValue(j,complex[j].real*complex[j].real+complex[j].imag*complex[j].imag);

        }

        return output;

    }
 
Example 17
Source File: TWEDistance.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * distance method that converts instances to arrays of doubles
 *
 * @param first instance 1
 * @param second instance 2
 * @param cutOffValue used for early abandon
 * @return distance between instances
 */
@Override
public double distance(Instance first, Instance second, 
        double cutOffValue)
{
    //remove class index from first instance if there is one
    int firtClassIndex = first.classIndex();
    double[] arr1;
    if(firtClassIndex > 0){
        arr1 = new double[first.numAttributes()-1];
        for(int i = 0,j = 0; i < first.numAttributes(); i++){
            if(i != firtClassIndex){
                arr1[j]= first.value(i);
                j++;
            }
        }
    }else{
        arr1 = first.toDoubleArray();
    }

    //remove class index from second instance if there is one
    int secondClassIndex = second.classIndex();
    double[] arr2;
    if(secondClassIndex > 0){
        arr2 = new double[second.numAttributes()-1];
        for(int i = 0,j = 0; i < second.numAttributes(); i++){
            if(i != secondClassIndex){
                arr2[j]= second.value(i);
                j++;
            }
        }
    }else{
        arr2 = second.toDoubleArray();
    }

    return distance(arr1,arr2,cutOffValue);
}
 
Example 18
Source File: LatentSemanticAnalysis.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Transform an instance in original (unnormalized) format
 * @param instance an instance in the original (unnormalized) format
 * @return a transformed instance
 * @throws Exception if instance can't be transformed
 */
public Instance convertInstance(Instance instance) throws Exception {
  if (m_s == null) {
    throw new Exception("convertInstance: Latent Semantic Analysis not " +
                         "performed yet.");
  }
  
  // array to hold new attribute values
  double [] newValues = new double[m_outputNumAttributes];
  
  // apply filters so new instance is in same format as training instances
  Instance tempInstance = (Instance)instance.copy();
  if (!instance.dataset().equalHeaders(m_trainHeader)) {
    throw new Exception("Can't convert instance: headers don't match: " +
    "LatentSemanticAnalysis");
  }
  // replace missing values
  m_replaceMissingFilter.input(tempInstance);
  m_replaceMissingFilter.batchFinished();
  tempInstance = m_replaceMissingFilter.output();
  // normalize
  if (m_normalize) {
    m_normalizeFilter.input(tempInstance);
    m_normalizeFilter.batchFinished();
    tempInstance = m_normalizeFilter.output();
  }
  // convert nominal attributes to binary
  m_nominalToBinaryFilter.input(tempInstance);
  m_nominalToBinaryFilter.batchFinished();
  tempInstance = m_nominalToBinaryFilter.output();
  // remove class/other attributes
  if (m_attributeFilter != null) {
    m_attributeFilter.input(tempInstance);
    m_attributeFilter.batchFinished();
    tempInstance = m_attributeFilter.output();
  }
  
  // record new attribute values
  if (m_hasClass) { // copy class value
    newValues[m_outputNumAttributes - 1] = instance.classValue();
  }
  double [][] oldInstanceValues = new double[1][m_numAttributes];
  oldInstanceValues[0] = tempInstance.toDoubleArray();
  Matrix instanceVector = new Matrix(oldInstanceValues); // old attribute values
  instanceVector = instanceVector.times(m_transformationMatrix); // new attribute values
  for (int i = 0; i < m_actualRank; i++) {
    newValues[i] = instanceVector.get(0, i);
  }
  
  // return newly transformed instance
  if (instance instanceof SparseInstance) {
    return new SparseInstance(instance.weight(), newValues);
  } else {
    return new DenseInstance(instance.weight(), newValues);
  }
}
 
Example 19
Source File: GeneticSearch.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
private List<Shapelet> evolvePopulation(Instance timeSeries, List<Shapelet> shapesIn, ShapeletSearch.ProcessCandidate checkCandidate){
    List<Shapelet> newPopulation = new ArrayList<>();
    List<Pair<Integer, Integer>> populationToBe = new ArrayList<>();

    // Keep our best individual
    if (elitism) {
        newPopulation.add(getBestShapelet(shapesIn));
    }

    // Crossover population
    int elitismOffset = elitism ? 1 : 0;

    // Loop over the population size and create new individuals with
    // crossover
    for (int i = elitismOffset; i < shapesIn.size(); i++) {
        Shapelet indiv1 = tournamentSelection(shapesIn);
        Shapelet indiv2 = tournamentSelection(shapesIn);
        Pair<Integer, Integer> crossed = crossOver(indiv1, indiv2);
        populationToBe.add(crossed);
    }

    double[] series = timeSeries.toDoubleArray();
    
    // Mutate population
    for (Pair<Integer, Integer> populationToBe1 : populationToBe) {
        mutate(populationToBe1);
        
        //check it's valid. PURGE THE MUTANT! Replace with random valid replacement.
        if(!validMutation(populationToBe1)){
            Pair<Integer, Integer> pair = createRandomShapelet(series);
            populationToBe1 = pair;
        }
        
        Shapelet sh = checkCandidate.process(timeSeries, populationToBe1.var2, populationToBe1.var1);
        evaluated++;
        if(sh != null)
        newPopulation.add(sh);
    }
    
    return newPopulation;
}
 
Example 20
Source File: UnsupervisedShapelets.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
UShapelet(int startPoint, int length, Instance inst){
    this.startPoint = startPoint;
    this.length = length;
    this.series = inst.toDoubleArray();
}