Ejemplo n.º 1
0
  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));
    }
    Sequence seq = new Sequence(sequence);

    double minD = Double.MAX_VALUE;
    String classValue = null;
    for (ClassedSequence s : prototypes) {
      double tmpD = seq.distance(s.sequence);
      if (tmpD < minD) {
        minD = tmpD;
        classValue = s.classValue;
      }
    }
    // System.out.println(prototypes.size());
    return sample.classAttribute().indexOfValue(classValue);
  }
  /**
   * test on one sample
   *
   * @param sample
   * @return p(y|sample) forall y
   * @throws Exception
   */
  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));
    }
    Sequence seq = new Sequence(sequence);

    // for each class
    String classValue = null;
    double maxProb = 0.0;
    double[] pr = new double[classedData.keySet().size()];
    for (String clas : classedData.keySet()) {
      int c = trainingData.classAttribute().indexOfValue(clas);
      double prob = 0.0;
      for (int k = 0; k < centroidsPerClass[c].length; k++) {
        // compute P(Q|k_c)
        if (sigmasPerClass[c][k] == Double.NaN || sigmasPerClass[c][k] == 0) {
          System.err.println("sigma=NAN||sigma=0");
          continue;
        }
        double dist = seq.distanceEuc(centroidsPerClass[c][k]);
        double p = computeProbaForQueryAndCluster(sigmasPerClass[c][k], dist);
        prob += p / centroidsPerClass[c].length;
        //				prob += p*prior[c][k];
        if (p > maxProb) {
          maxProb = p;
          classValue = clas;
        }
      }
      //			if (prob > maxProb) {
      //				maxProb = prob;
      //				classValue = clas;
      //			}
    }
    //		System.out.println(Arrays.toString(pr));
    //		System.out.println(classValue);
    return sample.classAttribute().indexOfValue(classValue);
  }