Ejemplo n.º 1
0
 public static int[][] dataSetToArray(Dataset dataset) {
   int[][] result = new int[dataset.size()][];
   for (int i = 0; i < dataset.size(); i++) {
     Instance instance = dataset.get(i);
     result[i] = instanceToArray(instance);
   }
   return result;
 }
Ejemplo n.º 2
0
 /**
  * converts two dimensional array of vectors to the dataset
  *
  * @param points
  * @return
  */
 public static Dataset arrayToDataSet(int[][] points) {
   Dataset dataset = new DefaultDataset();
   for (int i = 0; i < points[0].length; i++) {
     double[] values = new double[points.length];
     for (int j = 0; j < points.length; j++) {
       values[j] = points[j][i];
     }
     dataset.add(new DenseInstance(values));
   }
   return dataset;
 }
 /**
  * Tests a classifier on a data set
  *
  * @param cls the classifier to test
  * @param data the data set to test on
  * @return the performance for each class
  */
 public static Map<Object, PerformanceMeasure> testDataset(Classifier cls, Dataset data) {
   Map<Object, PerformanceMeasure> out = new HashMap<Object, PerformanceMeasure>();
   for (Object o : data.classes()) {
     out.put(o, new PerformanceMeasure());
   }
   for (Instance instance : data) {
     Object prediction = cls.classify(instance);
     if (instance.classValue().equals(prediction)) { // prediction
       // ==class
       for (Object o : out.keySet()) {
         if (o.equals(instance.classValue())) {
           out.get(o).tp++;
         } else {
           out.get(o).tn++;
         }
       }
     } else { // prediction != class
       for (Object o : out.keySet()) {
         /* prediction is positive class */
         if (prediction.equals(o)) {
           out.get(o).fp++;
         }
         /* instance is positive class */
         else if (o.equals(instance.classValue())) {
           out.get(o).fn++;
         }
         /* none is positive class */
         else {
           out.get(o).tn++;
         }
       }
     }
   }
   return out;
 }
Ejemplo n.º 4
0
  public setsMeanStdDev produceDataSets(Vector<String> all, int tooHigh, int tooLow) {

    double mean = 0;
    double standardDev = 0;
    double standardDevTimesN = 0;
    double sum = 0;
    double curCount = 0;

    DexComReading garbage = new DexComReading(0, "empty", 0);
    Vector<DexComReading> dexReadings = new Vector<DexComReading>();
    for (String curString : all) {
      try {
        DexComReading newRead = produceReading(curString);
        dexReadings.add(newRead);
      } catch (NumberFormatException e) {
        Log.d("KNNMethods", "Bad Date Format");
      }
    }
    Vector<Boolean> dangerListHigh = new Vector<Boolean>();
    Vector<Boolean> dangerListLow = new Vector<Boolean>();
    for (int i = 0; i < 12; ++i) {
      dangerListHigh.add(false);
      dangerListLow.add(false);
    }
    for (int i = 12; i < dexReadings.size() - 6; ++i) {

      sum = sum + dexReadings.get(i + 6).getSgv();
      ++curCount;
    }
    mean = sum / curCount;
    curCount = 0;

    for (int i = 12; i < dexReadings.size() - 6; ++i) {
      standardDevTimesN = standardDevTimesN + Math.pow((dexReadings.get(i).getSgv() - mean), 2);
      ++curCount;
      if (dexReadings.get(i + 6).getSgv() > tooHigh) {
        dangerListHigh.add(true);
        dangerListLow.add(false);
      } else if (dexReadings.get(i + 6).getSgv() > tooLow) {
        dangerListHigh.add(false);
        dangerListLow.add(false);
      } else if (dexReadings.get(i + 6).getSgv() < tooLow) {
        dangerListLow.add(true);
        dangerListHigh.add(false);
      }
    }

    standardDev = standardDevTimesN / curCount;

    double[][] sets13 = new double[dangerListHigh.size()][13];
    for (int i = 12; i < dexReadings.size() - 6; ++i) {
      double[] set13 = new double[13];
      for (int j = 0; j < 12; ++j) {
        set13[11 - j] = (dexReadings.get(i - j).getDoubleSgv() - mean) / standardDev;
      }
      set13[12] = dexReadings.get(i).getTime();
      sets13[i - 12] = set13;
    }

    Dataset dataHigh = new DefaultDataset();
    Dataset dataLow = new DefaultDataset();
    Log.d(TAG, "Value=" + tooHigh);
    for (int i = 0; i < sets13.length; ++i) {
      Instance instanceWClassValueHigh = new DenseInstance(sets13[i], dangerListHigh.get(i));
      Instance instanceWClassValueLow = new DenseInstance(sets13[i], dangerListLow.get(i));
      dataHigh.add(instanceWClassValueHigh);
      dataLow.add(instanceWClassValueLow);
    }
    Vector<Dataset> dataSets = new Vector<Dataset>();
    dataSets.add(dataHigh);
    dataSets.add(dataLow);
    setsMeanStdDev toReturn = new setsMeanStdDev(dataSets, mean, standardDev);
    return toReturn;
  }