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

The following examples show how to use weka.core.Instance#numAttributes() . 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: Dl4JMlpFilterTest.java    From wekaDeeplearning4j with GNU General Public License v3.0 6 votes vote down vote up
protected void checkLayer(Dl4jMlpClassifier clf, Instances instances, String[] transformationLayerNames,
    String clfPath, boolean useZooModel) throws Exception {
  Instances activationsExpected = clf.getActivationsAtLayers(transformationLayerNames, instances);
  Dl4jMlpFilter filter = new Dl4jMlpFilter();
  // Load the MNIST III if we're being called on the MNIST dataset (dataset is in meta format (String, class))
  if (ImageInstanceIterator.isMetaArff(instances))
    filter.setInstanceIterator(DatasetLoader.loadMiniMnistImageIterator());
  filter.setSerializedModelFile(new File(clfPath));
  filter.setTransformationLayerNames(transformationLayerNames);
  filter.setInputFormat(instances);
  filter.setPoolingType(PoolingType.NONE);

  Instances activationsActual = Filter.useFilter(instances, filter);

  for (int i = 0; i < activationsActual.size(); i++) {
    Instance expected = activationsExpected.get(i);
    Instance actual = activationsActual.get(i);
    for (int j = 0; j < expected.numAttributes(); j++) {
      assertEquals(expected.value(j), actual.value(j), 1e-6);
    }
  }
}
 
Example 2
Source File: PlainText.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
  * Builds a string listing the attribute values in a specified range of indices,
  * separated by commas and enclosed in brackets.
  *
  * @param instance 	the instance to print the values from
  * @return 		a string listing values of the attributes in the range
  */
 protected String attributeValuesString(Instance instance) {
   StringBuffer text = new StringBuffer();
   if (m_Attributes != null) {
     boolean firstOutput = true;
     m_Attributes.setUpper(instance.numAttributes() - 1);
     for (int i=0; i<instance.numAttributes(); i++)
if (m_Attributes.isInRange(i) && i != instance.classIndex()) {
  if (firstOutput) text.append("(");
  else text.append(",");
  text.append(instance.toString(i));
  firstOutput = false;
}
     if (!firstOutput) text.append(")");
   }
   return text.toString();
 }
 
Example 3
Source File: LbMsm.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Lower bound distance for MSM with early abandon
 *
 * @param q           first time series
 * @param c           second time series
 * @param cc          c param
 * @param qMax        max of first time series
 * @param qMin        min of second time series
 * @param cutOffValue cutoff value for early abandon
 * @return lower bound distance
 */
public static double distance(final Instance q, final Instance c, final double cc, final double qMax, final double qMin, final double cutOffValue) {
    final int len = q.numAttributes() - 1;

    double d = Math.abs(q.value(0) - c.value(0));

    for (int i = 1; i < len; i++) {
        final double curr = c.value(i);
        final double prev = c.value(i - 1);
        if (prev >= curr && curr > qMax) {
            d += Math.min(Math.abs(curr - qMax), cc);
            if (d >= cutOffValue)
                return Double.MAX_VALUE;
        } else if (prev <= curr && curr < qMin) {
            d += Math.min(Math.abs(curr - qMin), cc);
            if (d >= cutOffValue)
                return Double.MAX_VALUE;
        }
    }

    return d;
}
 
Example 4
Source File: WekaUtil.java    From AILibs with GNU Affero General Public License v3.0 6 votes vote down vote up
public static Instance getRefactoredInstance(final Instance instance, final List<String> classes) {

		/* modify instance */
		Instances dataset = WekaUtil.getEmptySetOfInstancesWithRefactoredClass(instance.dataset(), classes);
		int numAttributes = instance.numAttributes();
		int classIndex = instance.classIndex();
		Instance iNew = new DenseInstance(numAttributes);
		for (int i = 0; i < numAttributes; i++) {
			Attribute a = instance.attribute(i);
			if (i != classIndex) {
				iNew.setValue(a, instance.value(a));
			} else {
				iNew.setValue(a, 0.0); // the value does not matter since this should only be used for TESTING
			}
		}
		dataset.add(iNew);
		iNew.setDataset(dataset);
		return iNew;
	}
 
Example 5
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 6
Source File: LbLcss.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Build the upper and lower envelope for Lb Keogh with modification for LCSS
 *
 * @param candidate candidate sequence
 * @param epsilon   epsilon value
 * @param delta     delta value
 * @param U         upper envelope
 * @param L         lower envelope
 */
public static void fillUL(final Instance candidate, final double epsilon, final int delta, final double[] U, final double[] L) {
    final int length = candidate.numAttributes() - 1;
    double min, max;

    for (int i = 0; i < length; i++) {
        min = Double.POSITIVE_INFINITY;
        max = Double.NEGATIVE_INFINITY;
        final int startR = (i - delta < 0) ? 0 : i - delta;
        final int stopR = (i + delta + 1 > length) ? length : i + delta + 1;
        for (int j = startR; j < stopR; j++) {
            final double value = candidate.value(j);
            min = Math.min(min, value);
            max = Math.max(max, value);
        }
        L[i] = min - epsilon;
        U[i] = max + epsilon;
    }
}
 
Example 7
Source File: ThresholdClassifier.java    From orbit-image-analysis with GNU General Public License v3.0 6 votes vote down vote up
@Override
public double classifyInstance(Instance instance) throws Exception {
    if (instance == null) throw new IllegalArgumentException("instance cannot be null");
    //System.out.println(threshs.length+" threshs: "+ Arrays.toString(threshs));
    //System.out.println(instance.numAttributes()+" attributes: "+ Arrays.toString(instance.toDoubleArray()));

    if (mins == null || ((instance.numAttributes() - 1) != mins.length))
        throw new IllegalStateException("thresholds length is not equal to feature length (mins=" + ((mins == null) ? "null" : mins.length) + " attributes-1=" + (instance.numAttributes() - 1) + ")");

    int yes = 0, no = 0;
    for (int a = 0; a < instance.numAttributes() - 1; a++) {  // -1 because last attribute is (missing) class value
        if (Double.isNaN(mins[a]) || Double.isNaN(maxs[a])) continue; // NaN means this dimension should be ignored
        if (instance.value(a) >= mins[a] && instance.value(a) <= maxs[a]) yes++;
        else no++;
    }
    if (yes > no) return 1;
    else return 0; // majority vote
}
 
Example 8
Source File: MatlabSaver.java    From tsml with GNU General Public License v3.0 6 votes vote down vote up
/**
  * turns the instance into a Matlab row.
  * 
  * @param inst	the instance to transform
  * @return		the generated Matlab row
  */
 protected String instanceToMatlab(Instance inst) {
   StringBuffer	result;
   int			i;
   
   result = new StringBuffer();

   // attributes
   for (i = 0; i < inst.numAttributes(); i++) {
     if (i > 0)
result.append((m_UseTabs ? "\t" : " "));
     result.append(m_Format.format(inst.value(i)));
   }
   
   return result.toString();
 }
 
Example 9
Source File: BoTSWEnsemble.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Assumes class index, if present, is last
 * @return data of passed instance in a double array with the class value removed if present
 */
protected static double[] toArrayNoClass(Instance inst) {
    int length = inst.numAttributes();
    if (inst.classIndex() >= 0)
        --length;

    double[] data = new double[length];

    for (int i=0, j=0; i < inst.numAttributes(); ++i)
        if (inst.classIndex() != i)
            data[j++] = inst.value(i);

    return data;
}
 
Example 10
Source File: LbYi.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
public static double distance(final Instance candidate, final SequenceStatsCache queryCache, final int indexQuery) {
    double lb = 0;

    for (int i = 0; i < candidate.numAttributes()-1; i++) {
        if (candidate.value(i) < queryCache.getMin(indexQuery)) {
            lb += queryCache.getMin(indexQuery);
        } else if (queryCache.getMax(indexQuery) < candidate.value(i)) {
            lb += queryCache.getMax(indexQuery);
        }
    }

    return lb;
}
 
Example 11
Source File: MSMDistance.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 12
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 13
Source File: Cobweb.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Adds an instance to the clusterer.
 *
 * @param newInstance the instance to be added
 * @throws Exception 	if something goes wrong
 */
public void updateClusterer(Instance newInstance) throws Exception {
  m_numberOfClustersDetermined = false;
  
  if (m_cobwebTree == null) {
    m_cobwebTree = new CNode(newInstance.numAttributes(), newInstance);
  } else {
    m_cobwebTree.addInstance(newInstance);
  }
}
 
Example 14
Source File: ClusteringUtilities.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
public static void zNormaliseWithClass(Instance inst){
    double meanSum = 0;
    int length = inst.numAttributes()-1;

    for (int i = 0; i < inst.numAttributes(); i++){
        if (inst.classIndex() != i) {
            meanSum += inst.value(i);
        }
    }

    double mean = meanSum / length;

    double squareSum = 0;

    for (int i = 0; i < inst.numAttributes(); i++){
        if (inst.classIndex() != i) {
            double temp = inst.value(i) - mean;
            squareSum += temp * temp;
        }
    }

    double stdev = Math.sqrt(squareSum/(length-1));

    if (stdev == 0){
        stdev = 1;
    }

    for (int i = 0; i < inst.numAttributes(); i++){
        if (inst.classIndex() != i) {
            inst.setValue(i, (inst.value(i) - mean) / stdev);
        }
    }
}
 
Example 15
Source File: LbKeoghPrunedDTW.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
@Override
public double classifyInstance(Instance sample) throws Exception {
	// transform instance to sequence
	MonoDoubleItemSet[] sequence = new MonoDoubleItemSet[sample.numAttributes() - 1];
	int shift = (sample.classIndex() == 0) ? 1 : 0;
	for (int t = 0; t < sequence.length; t++) {
		sequence[t] = new MonoDoubleItemSet(sample.value(t + shift));
	}
	SymbolicSequence seq = new SymbolicSequence(sequence);

	double minD = Double.MAX_VALUE;
	String classValue = null;
	seq.LB_KeoghFillUL(bestWarpingWindow, U, L);
	
	for (int i = 0; i < train.length; i++) {
		SymbolicSequence s = train[i];
		if (SymbolicSequence.LB_KeoghPreFilled(s, U, L) < minD) {
			double tmpD = seq.DTW(s,bestWarpingWindow, warpingMatrix);
			if (tmpD < minD) {
				minD = tmpD;
				classValue = classMap[i];
			}
		}
	}
	// System.out.println(prototypes.size());
	return sample.classAttribute().indexOfValue(classValue);
}
 
Example 16
Source File: AutoTestAdjust.java    From bestconf with Apache License 2.0 4 votes vote down vote up
public Instances runExp(Instances samplePoints, String perfAttName){
	Instances retVal = null;
	if(samplePoints.attribute(perfAttName) == null){
		Attribute performance = new Attribute(perfAttName);
		samplePoints.insertAttributeAt(performance, samplePoints.numAttributes());
	}
	int pos = samplePoints.numInstances();
	int count = 0;
	for (int i = 0; i < pos; i++) {
		Instance ins = samplePoints.get(i);
		HashMap hm = new HashMap();
		int tot = 0;
		for (int j = 0; j < ins.numAttributes(); j++) {
			hm.put(ins.attribute(j).name(), ins.value(ins.attribute(j)));
		}

		boolean testRet;
		if (Double.isNaN(ins.value(ins.attribute(ins.numAttributes() - 1)))) {
			testRet = this.startTest(hm, i, isInterrupt);
			double y = 0;
			if (!testRet) {// the setting does not work, we skip it
				y = -1;
				count++;
				if (count >= targetTestErrorNum) {
					System.out.println("There must be somthing wrong with the system. Please check and restart.....");
					System.exit(1);
				}
			} else {
				y = getPerformanceByType(performanceType);
				count = 0;
			}

			ins.setValue(samplePoints.numAttributes() - 1, y);
			writePerfstoFile(ins);
		} else {
			continue;
		}
	}
	retVal = samplePoints;
	retVal.setClassIndex(retVal.numAttributes()-1);
	
	return retVal;
}
 
Example 17
Source File: LbEnhanced.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
public static double distance(final Instance a, final Instance b,
                              final double[] U, final double[] L,
                              final int w, final int v) {
    final int n = a.numAttributes() - 1;
    final int m = b.numAttributes() - 1;
    final int l = n - 1;
    final int nBands = Math.min(l / 2, v);
    final int lastIndex = l - nBands;
    final double d00 = a.value(0) - b.value(0);
    final double dnm = a.value(n - 1) - b.value(m - 1);

    int i, j, rightEnd, rightStart;
    double minL, minR, tmp, aVal;

    double res = d00 * d00 + dnm * dnm;

    for (i = 1; i < nBands; i++) {
        rightEnd = l - i;
        minL = a.value(i) - b.value(i);
        minL *= minL;
        minR = a.value(rightEnd) - b.value(rightEnd);
        minR *= minR;
        for (j = Math.max(0, i - w); j < i; j++) {
            rightStart = l - j;
            tmp = a.value(i) - b.value(j);
            minL = Math.min(minL, tmp * tmp);
            tmp = a.value(j) - b.value(i);
            minL = Math.min(minL, tmp * tmp);

            tmp = a.value(rightEnd) - b.value(rightStart);
            minR = Math.min(minR, tmp * tmp);
            tmp = a.value(rightStart) - b.value(rightEnd);
            minR = Math.min(minR, tmp * tmp);
        }
        res += minL + minR;
    }

    for (i = nBands; i <= lastIndex; i++) {
        aVal = a.value(i);
        if (aVal > U[i]) {
            tmp = aVal - U[i];
            res += tmp * tmp;
        } else if (aVal < L[i]) {
            tmp = L[i] - aVal;
            res += tmp * tmp;
        }
    }

    return res;
}
 
Example 18
Source File: XMeans.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
/**
  * Calculates the log-likelihood of the data for the given model, taken
  * at the maximum likelihood point.
  *
  * @param numInst number of instances that belong to the center
  * @param center the center
  * @param distortion distortion 
  * @param numCent number of centers 
  * @return the likelihood estimate
  */
 protected double logLikelihoodEstimate(int numInst, 
			       Instance center, 
			       double distortion, 
			       int numCent) {
   // R(n) num of instances of the center -> numInst
   // K num of centers -> not used
   //
   //todo take the diff comments away
   double loglike = 0;
   /* if is new */
   if (numInst > 1) {
     /* diff variance is new */
     //
     // distortion = Sum over instances x of the center(x-center)
     // different to paper; sum should be squared
     //
     // (Sum of distances to center) / R(n) - 1.0
     // different to paper; should be R(n)-K
     double variance =  distortion / (numInst - 1.0); 
 
     //
     //  -R(n)/2 * log(pi*2)
     //
     double p1 = - (numInst / 2.0) * Math.log(Math.PI * 2.0);
     /* diff
 thats how we had it
 double p2 = -((ni * center.numAttributes()) / 2) * distortion;
     */
     //
     // -(R(n)*M)/2 * log(variance) 
     //
     double p2 = - (numInst * center.numAttributes()) / 2 * Math.log(variance);
     
     /* diff
 thats how we had it, the difference is a bug in x-means
 double p3 = - (numInst - numCent) / 2;
     */
     //
     // -(R(n)-1)/2
     //
     double p3 = - (numInst - 1.0) / 2.0;
     
     //
     // R(n)*log(R(n))
     //
     double p4 = numInst * Math.log(numInst);
     
     /* diff x-means doesn't have this part 
 double p5 = - numInst * Math.log(numInstTotal);
     */
     
     /*
loglike = -(ni / 2) * Math.log(Math.PI * 2) 
- (ni * center.numAttributes()) / 2.0) * logdistortion
- (ni - k) / 2.0 
+ ni * Math.log(ni) 
- ni * Math.log(r);
     */
     loglike = p1 + p2 + p3 + p4; // diff + p5;
     //the log(r) is something that can be reused.
     //as is the log(2 PI), these could provide extra speed up later on.
     //since distortion is so expensive to compute, I only do that once.
   }
   return loglike;
 }
 
Example 19
Source File: MSMDistance.java    From tsml with GNU General Public License v3.0 4 votes vote down vote up
@Override
public double distance(final Instance first,
    final Instance second,
    final double limit,
    final PerformanceStats stats) {

    checkData(first, second);

    int aLength = first.numAttributes() - 1;
    int bLength = second.numAttributes() - 1;

    double[][] cost = new double[aLength][bLength];

    // Initialization
    cost[0][0] = Math.abs(first.value(0) - second.value(0));
    for(int i = 1; i < aLength; i++) {
        cost[i][0] = cost[i - 1][0] + findCost(first.value(i), first.value(i - 1), second.value(0));
    }
    for(int i = 1; i < bLength; i++) {
        cost[0][i] = cost[0][i - 1] + findCost(second.value(i), first.value(0), second.value(i - 1));
    }

    // Main Loop
    double min;
    for(int i = 1; i < aLength; i++) {
        min = limit;
        for(int j = 1; j < bLength; j++) {
            double d1, d2, d3;
            d1 = cost[i - 1][j - 1] + Math.abs(first.value(i) - second.value(j));
            d2 = cost[i - 1][j] + findCost(first.value(i), first.value(i - 1), second.value(j));
            d3 = cost[i][j - 1] + findCost(second.value(j), first.value(i), second.value(j - 1));
            cost[i][j] = Math.min(d1, Math.min(d2, d3));

            if(cost[i][j] >= limit) {
                cost[i][j] = Double.POSITIVE_INFINITY;
            }

            if(cost[i][j] < min) {
                min = cost[i][j];
            }
        }
        if(min >= limit) {
            return Double.POSITIVE_INFINITY;
        }
    }
    // Output
    return cost[aLength - 1][bLength - 1];
}
 
Example 20
Source File: LbErp.java    From tsml with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Proposed lower bound function for ERP
 * |sum(Q)-sum(C)|
 * Modified slightly to have g value
 * http://www.vldb.org/conf/2004/RS21P2.PDF
 * http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.90.6387&rep=rep1&type=pdf
 *
 * @param a first time series
 * @param b second time series
 * @param g g parameter
 * @return LB ERP distance distance
 */
public static double distance(final Instance a, final Instance b, final double g) {
    final int m = a.numAttributes() - 1;
    final int n = b.numAttributes() - 1;

    if (m == n) {
        sum2(a, b, g);
        return Math.abs(s[0] - s[1]);
    } else {
        return Math.abs(sum(a, g) - sum(b, g));
    }
}