예제 #1
0
  @Override
  public List<Classifier> buildClassifiers(User user, List<Sample> validSamples) {

    Instances trainingSet =
        new TrainingSetBuilder()
            .setAttributes(user.getBssids())
            .setClassAttribute(
                "Location",
                user.getLocations().stream().map(Location::getName).collect(Collectors.toList()))
            .build("TrainingSet", validSamples.size());

    // Create instances
    validSamples.forEach(
        sample -> {
          Map<String, Integer> BSSIDLevelMap = getBSSIDLevelMap(sample);

          Instance instance = new Instance(trainingSet.numAttributes());

          for (Enumeration e = trainingSet.enumerateAttributes(); e.hasMoreElements(); ) {
            Attribute attribute = (Attribute) e.nextElement();
            String bssid = attribute.name();
            int level = (BSSIDLevelMap.containsKey(bssid)) ? BSSIDLevelMap.get(bssid) : 0;
            instance.setValue(attribute, level);
          }

          instance.setValue(trainingSet.classAttribute(), sample.getLocation());

          instance.setDataset(trainingSet);
          trainingSet.add(instance);
        });

    // Build classifiers
    List<Classifier> classifiers = buildClassifiers(trainingSet);
    return classifiers;
  }
예제 #2
0
  @Override
  public String classify(User user, Sample sample) {

    Instances trainingSet =
        new TrainingSetBuilder()
            .setAttributes(user.getBssids())
            .setClassAttribute(
                "Location",
                user.getLocations().stream().map(Location::getName).collect(Collectors.toList()))
            .build("TrainingSet", 1);

    // Create instance
    Map<String, Integer> BSSIDLevelMap = getBSSIDLevelMap(sample);

    Instance instance = new Instance(trainingSet.numAttributes());

    for (Enumeration e = trainingSet.enumerateAttributes(); e.hasMoreElements(); ) {
      Attribute attribute = (Attribute) e.nextElement();
      String bssid = attribute.name();
      int level = (BSSIDLevelMap.containsKey(bssid)) ? BSSIDLevelMap.get(bssid) : 0;
      instance.setValue(attribute, level);
    }

    if (sample.getLocation() != null)
      instance.setValue(trainingSet.classAttribute(), sample.getLocation());

    instance.setDataset(trainingSet);
    trainingSet.add(instance);

    int predictedClass = classify(fromBase64(user.getClassifiers()), instance);

    return trainingSet.classAttribute().value(predictedClass);
  }
예제 #3
0
  private static void loadattributes(
      String s,
      Instance instclassificationpickup,
      Instance instclassificationdropoff,
      Instance instregressionpickup,
      Instance instregressiondropoff) {
    String[] attributes = s.split(",");

    for (int i = 0; i < 8; i++) {
      if (attributes[i].equals("?")) {
        instclassificationpickup.setMissing(i);
        instclassificationdropoff.setMissing(i);
        instregressionpickup.setMissing(i);
        instregressiondropoff.setMissing(i);
      } else {
        if ((i == 2) || (i == 3) || (i == 5)) {
          instclassificationpickup.setValue(i, attributes[i]);
          instclassificationdropoff.setValue(i, attributes[i]);
          instregressionpickup.setValue(i, attributes[i]);
          instregressiondropoff.setValue(i, attributes[i]);
        } else {
          instclassificationpickup.setValue(i, Double.parseDouble(attributes[i]));
          instclassificationdropoff.setValue(i, Double.parseDouble(attributes[i]));
          instregressionpickup.setValue(i, Double.parseDouble(attributes[i]));
          instregressiondropoff.setValue(i, Double.parseDouble(attributes[i]));
        }
      }
    }
    instclassificationpickup.setMissing(8);
    instclassificationdropoff.setMissing(8);
    instregressionpickup.setMissing(8);
    instregressiondropoff.setMissing(8);
  }
 private void addInstance(
     final double min,
     final double max,
     final double mean,
     final double stdDev,
     final String label) {
   Instance dataInstance = new Instance(NUMBER_OF_ATTRIBUTES);
   dataInstance.setValue(minAttribute, min);
   dataInstance.setValue(maxAttribute, max);
   dataInstance.setValue(meanAttribute, mean);
   dataInstance.setValue(stdDevAttribute, stdDev);
   dataInstance.setValue(activityAttribute, label);
   instances.add(dataInstance);
 }
 // 将一个文本数据转换成weka可以识别的Instance
 private Instance makeInstance(String text, Instances data) {
   Instance instance = new Instance(2);
   Attribute messageAtt = data.attribute("Message");
   instance.setValue(messageAtt, messageAtt.addStringValue(text));
   instance.setDataset(data);
   return instance;
 }
예제 #6
0
 private Instance instanceFromStream(StreamElement data) {
   try {
     Instance i = new Instance(data.getFieldNames().length);
     for (int j = 0; j < data.getFieldNames().length; j++) {
       i.setValue(j, ((Double) data.getData()[j]));
     }
     // scaling specific to opensense data!! should be put in the parameters?
     i.setValue(0, i.value(0) / 1400.0);
     i.setValue(2, i.value(2) / 50);
     i.setValue(3, i.value(3) / 100.0);
     i.setValue(4, i.value(4) / 100.0 - 4);
     return i;
   } catch (Exception e) {
     return null;
   }
 }
예제 #7
0
  /**
   * Input an instance for filtering.
   *
   * @param instance the input instance
   * @return true if the filtered instance may now be collected with output().
   * @throws Exception if the input format was not set or the date format cannot be parsed
   */
  public boolean input(Instance instance) throws Exception {

    if (getInputFormat() == null) {
      throw new IllegalStateException("No input instance format defined");
    }
    if (m_NewBatch) {
      resetQueue();
      m_NewBatch = false;
    }
    Instance newInstance = (Instance) instance.copy();
    int index = m_AttIndex.getIndex();
    if (!newInstance.isMissing(index)) {
      double value = instance.value(index);
      try {
        // Format and parse under the new format to force any required
        // loss in precision.
        value = m_OutputAttribute.parseDate(m_OutputAttribute.formatDate(value));
      } catch (ParseException pe) {
        throw new RuntimeException("Output date format couldn't parse its own output!!");
      }
      newInstance.setValue(index, value);
    }
    push(newInstance);
    return true;
  }
  private void createLimbInstance(
      Skeleton skeleton, int limb, int[] limb_about, List<Attribute> limb_att) {
    int att = 1;
    Instance ist = new DenseInstance((SPATIAL_COORD * limb_about.length) + 1);

    limb_att = classifier.getLimbFeatureDataSet(limb);

    for (int i = 0; i < limb_about.length; i++) {
      if (limb_about[i] != TORSO) {
        /*
         * il punto di riferimento per tutti i joint (meno che per il
         * TORSO), ossia il centro degli assi che descrivono lo spazio
         * in cui si possono muovere è il TORSO stesso.
         */
        Point3D<Float> joint = skeleton.getJoint(SKELETON_JOINT(limb_about[i])).getPosition();

        ist.setValue((Attribute) limb_att.get(att), (joint.getX() - x_torso));
        ist.setValue((Attribute) limb_att.get(att + 1), (joint.getY() - y_torso));
        ist.setValue((Attribute) limb_att.get(att + 2), (joint.getZ() - z_torso));

        att += 3;
      } else {
        /*
         * il punto di riferimento per il BODY invece è il punto con
         * coordinate (0, -Ytorso, 0), e cioè, il punto sul pavimento
         * esattamente perpendicolare al TORSO stesso
         */
        Point3D<Float> left_foot = skeleton.getJoint(JointType.LEFT_FOOT).getPosition();
        Point3D<Float> right_foot = skeleton.getJoint(JointType.RIGHT_FOOT).getPosition();

        // save the two probably torso's new coordinates
        float torso_footSX = y_torso - left_foot.getY();
        float torso_footDX = y_torso - right_foot.getY();

        float y_torso_rifFoot = (torso_footSX > torso_footDX) ? torso_footSX : torso_footDX;

        ist.setValue((Attribute) limb_att.get(att), 0);
        ist.setValue((Attribute) limb_att.get(att + 1), y_torso_rifFoot);
        ist.setValue((Attribute) limb_att.get(att + 2), 0);

        att += 3;
      }
    }

    skeletonIstances.add(ist);
  }
  protected void imputeMedian(Instances instances) {
    Attribute indicator = instances.attribute(ATTNAME_INDICATOR);

    for (int i = 0; i < instances.numInstances(); ++i) {
      Instance current = instances.get(i);
      current.setValue(indicator, 0.0); // 0.0 means "false"
      for (int j = 0; j < instances.numAttributes(); ++j) {
        if (instances.attribute(j).isNumeric() == false) {
          continue;
        }
        if (Utils.isMissingValue(current.value(j))) {
          current.setValue(j, medians[j]);
          current.setValue(indicator, 1.0);
          imputations[j] += 1;
        }
      }
    }
  }
예제 #10
0
  /**
   * Input an instance for filtering. The instance is processed and made available for output
   * immediately.
   *
   * @param instance the input instance
   * @return true if the filtered instance may now be collected with output().
   * @throws IllegalStateException if no input format has been set.
   */
  public boolean input(Instance instance) {

    if (getInputFormat() == null) {
      throw new IllegalStateException("No input instance format defined");
    }
    if (m_NewBatch) {
      resetQueue();
      m_NewBatch = false;
    }
    Instance newInstance = (Instance) instance.copy();
    if ((int) newInstance.value(m_AttIndex.getIndex()) == m_SecondIndex.getIndex()) {
      newInstance.setValue(m_AttIndex.getIndex(), (double) m_FirstIndex.getIndex());
    } else if ((int) newInstance.value(m_AttIndex.getIndex()) > m_SecondIndex.getIndex()) {
      newInstance.setValue(m_AttIndex.getIndex(), newInstance.value(m_AttIndex.getIndex()) - 1);
    }
    push(newInstance);
    return true;
  }
예제 #11
0
  // create an instance in the format of CreateAppInsertIntoVm.arff
  // eval can either be a Instance - MissingValue or the evaluation value
  private Instance createInstance(double eval, VirtualMachine vm) {
    Instance instance = new Instance(64);

    LinkedList<Integer> cpuallhist = vm.getCpuAllocationHistory(10);
    LinkedList<Integer> cpuusehist = vm.getCpuUsageHistory(10);

    LinkedList<Integer> memallhist = vm.getMemoryAllocationHistory(10);
    LinkedList<Integer> memusehist = vm.getMemoryUsageHistory(10);

    LinkedList<Integer> storageallhist = vm.getStorageAllocationHistory(10);
    LinkedList<Integer> storageusehist = vm.getStorageUsageHistory(10);

    // CPU/Memory/Storage - Allocation history before the new vm was created
    for (int i = 0; i < 10; i++) {
      // cpu allocation
      instance.setValue(getKnowledgeBase().attribute(i), clusterValue(cpuallhist.get(i)));
      // cpu usage
      instance.setValue(getKnowledgeBase().attribute(i + 10), clusterValue(cpuusehist.get(i)));

      // memory allocation
      instance.setValue(getKnowledgeBase().attribute(i + 20), clusterValue(memallhist.get(i)));
      // memory usage
      instance.setValue(getKnowledgeBase().attribute(i + 30), clusterValue(memusehist.get(i)));

      // storage allocation
      instance.setValue(getKnowledgeBase().attribute(i + 40), clusterValue(storageallhist.get(i)));
      // storage usage
      instance.setValue(getKnowledgeBase().attribute(i + 50), clusterValue(storageusehist.get(i)));
    }

    // SLAs
    // CPU
    instance.setValue(getKnowledgeBase().attribute(60), clusterValue(app.getCpu()));
    // Memory
    instance.setValue(getKnowledgeBase().attribute(61), clusterValue(app.getMemory()));
    // Storage
    instance.setValue(getKnowledgeBase().attribute(62), clusterValue(app.getStorage()));

    // Evaluation
    instance.setValue(getKnowledgeBase().attribute(63), eval);

    return instance;
  }
예제 #12
0
  /**
   * method to set a new value
   *
   * @param r random function
   * @param numOfValues
   * @param instance
   * @param useMissing
   */
  private void changeValueRandomly(
      Random r, int numOfValues, int indexOfAtt, Instance instance, boolean useMissing) {
    int currValue;

    // get current value
    // if value is missing set current value to number of values
    // whiche is the highest possible value plus one
    if (instance.isMissing(indexOfAtt)) {
      currValue = numOfValues;
    } else {
      currValue = (int) instance.value(indexOfAtt);
    }

    // with only two possible values it is easier
    if ((numOfValues == 2) && (!instance.isMissing(indexOfAtt))) {
      instance.setValue(indexOfAtt, (double) ((currValue + 1) % 2));
    } else {
      // get randomly a new value not equal to the current value
      // if missing values are used as values they must be treated
      // in a special way
      while (true) {
        int newValue;
        if (useMissing) {
          newValue = (int) (r.nextDouble() * (double) (numOfValues + 1));
        } else {
          newValue = (int) (r.nextDouble() * (double) numOfValues);
        }
        // have we found a new value?
        if (newValue != currValue) {
          // the value 1 above the highest possible value (=numOfValues)
          // is used as missing value
          if (newValue == numOfValues) {
            instance.setMissing(indexOfAtt);
          } else {
            instance.setValue(indexOfAtt, (double) newValue);
          }
          break;
        }
      }
    }
  }
예제 #13
0
  /**
   * Input an instance for filtering. The instance is processed and made available for output
   * immediately.
   *
   * @param instance the input instance
   * @return true if the filtered instance may now be collected with output().
   * @throws IllegalStateException if no input structure has been defined.
   */
  @Override
  public boolean input(Instance instance) {

    if (getInputFormat() == null) {
      throw new IllegalStateException("No input instance format defined");
    }
    if (m_NewBatch) {
      resetQueue();
      m_NewBatch = false;
    }
    Instance newInstance = (Instance) instance.copy();
    if (!newInstance.isMissing(m_AttIndex.getIndex())) {
      if ((int) newInstance.value(m_AttIndex.getIndex()) == m_SecondIndex.getIndex()) {
        newInstance.setValue(m_AttIndex.getIndex(), m_FirstIndex.getIndex());
      } else if ((int) newInstance.value(m_AttIndex.getIndex()) == m_FirstIndex.getIndex()) {
        newInstance.setValue(m_AttIndex.getIndex(), m_SecondIndex.getIndex());
      }
    }
    push(newInstance, false); // No need to copy
    return true;
  }
  /**
   * classifies a tweet
   *
   * @param stringa the tweet to classify
   * @return the tweet polarity
   */
  public double classifyDouble(String stringa)
      throws FileNotFoundException, IOException, Exception {
    String string_new;
    Preprocesser pre = new Preprocesser();
    string_new = pre.preprocessDocument(stringa);
    String tmp = "";
    StringTokenizer st = new StringTokenizer(string_new, " ");
    // Instances unlabeled = new Instances(new BufferedReader(new
    // FileReader("D:/prova.arff")));
    Instances unlabeled = new Instances(_train, 1);
    Instance inst = new Instance(unlabeled.numAttributes());
    // load unlabeled data
    inst.setDataset(unlabeled);
    int j = 0;
    while (j < unlabeled.numAttributes()) {
      inst.setValue(j, "0");
      j++;
    }
    while (st.hasMoreTokens()) {
      tmp = st.nextToken();
      if (unlabeled.attribute(tmp) != null) inst.setValue(unlabeled.attribute(tmp), "1");
    }
    unlabeled.add(inst);

    // set class attribute
    unlabeled.setClassIndex(unlabeled.numAttributes() - 1);

    // create copy
    Instances labeled = new Instances(unlabeled);

    // label instances
    for (int i = 0; i < unlabeled.numInstances(); i++) {
      double clsLabel = _cl.classifyInstance(unlabeled.instance(i));
      labeled.instance(i).setClassValue(clsLabel);
    }

    // return labeled.instance(0).stringValue(unlabeled.numAttributes()-1);
    // System.out.println("weight: " + labeled.instance(0).weight());
    return labeled.instance(0).classValue();
  }
예제 #15
0
 @Override
 public Instance nextInstance() {
   InstancesHeader header = getHeader();
   Instance inst = new DenseInstance(header.numAttributes());
   inst.setDataset(header);
   int waveform = this.instanceRandom.nextInt(NUM_CLASSES);
   int choiceA = 0, choiceB = 0;
   switch (waveform) {
     case 0:
       choiceA = 0;
       choiceB = 1;
       break;
     case 1:
       choiceA = 0;
       choiceB = 2;
       break;
     case 2:
       choiceA = 1;
       choiceB = 2;
       break;
   }
   double multiplierA = this.instanceRandom.nextDouble();
   double multiplierB = 1.0 - multiplierA;
   for (int i = 0; i < NUM_BASE_ATTRIBUTES; i++) {
     inst.setValue(
         this.numberAttribute[i],
         (multiplierA * hFunctions[choiceA][i])
             + (multiplierB * hFunctions[choiceB][i])
             + this.instanceRandom.nextGaussian());
   }
   if (this.addNoiseOption.isSet()) {
     for (int i = NUM_BASE_ATTRIBUTES; i < TOTAL_ATTRIBUTES_INCLUDING_NOISE; i++) {
       inst.setValue(this.numberAttribute[i], this.instanceRandom.nextGaussian());
     }
   }
   inst.setClassValue(waveform);
   return inst;
 }
예제 #16
0
파일: WekaTest.java 프로젝트: fsteeg/tm2
 private static Instance createInstance(double[] ds, String string, Instances instances) {
   int l = ds.length;
   // Create the instance
   Instance instance = new Instance(1 + l);
   instance.setDataset(instances);
   for (int i = 1; i <= l; i++) {
     instance.setValue(i, ds[i - 1]);
   }
   if (string == null) {
     instance.setClassMissing();
   } else {
     instance.setClassValue(string);
   }
   return instance;
 }
  /**
   * 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 {
    // Make a copy of the instance so that it isn't modified
    m_currentInstance = (Instance) i.copy();

    if (m_normalizeAttributes) {
      for (int noa = 0; noa < m_currentInstance.dataset().numAttributes(); noa++) {
        if (noa != m_currentInstance.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];
    double count = 0;

    for (int noa = 0; noa < m_numClasses; noa++) {
      theArray[noa] = m_outputs[noa].outputValue(true);
      count += theArray[noa];
    }

    if (count <= 0) {
      return m_ZeroR.m_Counts;
    }

    return theArray;
  }
예제 #18
0
파일: CNode.java 프로젝트: Waikato/meka
 /**
  * Transform - turn [y1,y2,y3,x1,x2] into [y1,y2,x1,x2].
  *
  * @return transformed Instance
  */
 public Instance transform(Instance x, double ypred[]) throws Exception {
   x = (Instance) x.copy();
   int L = x.classIndex();
   int L_c = (paY.length + 1);
   x.setDataset(null);
   for (int j = 0; j < (L - L_c); j++) {
     x.deleteAttributeAt(0);
   }
   for (int pa : paY) {
     // System.out.println("x_["+map[pa]+"] <- "+ypred[pa]);
     x.setValue(map[pa], ypred[pa]);
   }
   x.setDataset(T);
   x.setClassMissing();
   return x;
 }
예제 #19
0
  @Override
  public boolean evaluate() {
    if (curInstance == null) {
      curInstance = createInstance(0, selectedVm); // create a Instance with the past values
    }

    if (app.getSuspendedTicks() > 0
        || app.getVm().getSuspendedTicks() > 0
        || app.getVm().getPm().getSuspendedTicks() > 0) {
      return false;
    } else if (waitForEvaluation > 0) {
      waitForEvaluation--;
      return false;
    } else {
      // System.out.println("APP - Running Ticks: " + app.getRunningTicks());
      LinkedList<Integer> cpuusagehist = selectedVm.getPm().getCpuUsageHistory(10);

      LinkedList<Integer> memusagehist = selectedVm.getPm().getMemoryUsageHistory(10);

      LinkedList<Integer> storageusagehist = selectedVm.getPm().getStorageUsageHistory(10);

      double evaluation =
          (255
                  - calculateUsageRatio(cpuusagehist, 85)
                  - calculateUsageRatio(memusagehist, 85)
                  - calculateUsageRatio(storageusagehist, 85))
              / 255;

      // subtract SLA Violations
      evaluation -=
          (app.getCpuSlaErrorcount() + app.getMemorySlaErrorcount() + app.getStorageSlaErrorcount())
              / 10;

      // minimum of 0
      // evaluation = Math.max(0, evaluation);
      Monitor.getInstance().logExecution(app, this, evaluation, this.globalTickExecution);
      curInstance.setValue(getKnowledgeBase().attribute(63), evaluation);
      this.setLocalEvaluation(evaluation);
      getKnowledgeBase().add(curInstance);
    }
    return true;
  }
  @Override
  public void setAttributeValue(Attribute attribute, Instance instance, Patient patient) {

    RegaService rs = ruleService.getRegaService();

    DataService ds = ruleService.getDataService();

    List<TestResult> postTreatmentCD4Counts = rs.getPostTreatmentCD4Counts(patient);

    Double leastSquaresRegressionGradient = ds.getLeastSquaresFitGradient(postTreatmentCD4Counts);

    logger.info(
        "PTCD4 #: "
            + postTreatmentCD4Counts.size()
            + ", Gradient: "
            + leastSquaresRegressionGradient);

    if (leastSquaresRegressionGradient != null)
      instance.setValue(attribute, leastSquaresRegressionGradient);
  }
예제 #21
0
 private Instance getFVSFilteredInstance(
     Instances output, Instance old_inst, List<List<Value>> list, Double[] substitution) {
   double[] oldValues = old_inst.toDoubleArray();
   Instance instance = new Instance(old_inst);
   // Change with value that is available
   for (int i = 0; i < oldValues.length - 1; i++) {
     // System.out.println(oldValues[i]);
     // System.out.println(list.get(i));
     // System.out.println("############################");
     // If list doesn't contain, then delete
     Value v = new Value(oldValues[i]);
     int idx = list.get(i).indexOf(v);
     // If not found in the index
     if (idx == -1) {
       // Change with substitution
       instance.setValue(i, substitution[i]);
       // Change into missing
       // instance.setMissing(i);
     }
   }
   return instance;
 }
  private static Instance makeInstance(Instances instances, String inputLine) {
    inputLine = inputLine.trim();
    // We need to store the lastName as well...
    String[] parts = inputLine.split("\\s+");
    String label = parts[0];
    String firstName = parts[1].toLowerCase();
    String lastName = parts[2].toLowerCase();

    Instance instance = new Instance(features.length + 1);
    instance.setDataset(instances);

    Set<String> feats = new HashSet<String>();
    /*
    feats.add("firstName0=" + firstName.charAt(0));
    feats.add("firstNameN=" + firstName.charAt(firstName.length() - 1));
           */
    for (int f = 0; f < 9; f++) {
      if (firstName.length() > f) feats.add("firstName" + f + "=" + firstName.charAt(f));
    }
    for (int l = 0; l < 9; l++) {
      if (lastName.length() > l) feats.add("lastName" + l + "=" + lastName.charAt(l));
    }
    /////////////////////////////////////////////////////////////////
    for (int featureId = 0; featureId < features.length; featureId++) {
      Attribute att = instances.attribute(features[featureId]);

      String name = att.name();
      String featureLabel;
      if (feats.contains(name)) {
        featureLabel = "1";
      } else featureLabel = "0";
      instance.setValue(att, featureLabel);
    }

    instance.setClassValue(label);

    return instance;
  }
예제 #23
0
  /**
   * Create an Instance of the same type as the Instances object you are searching in.
   *
   * @param p - a 3D point
   * @param dataset - the dataset you are searching in, which was used to build the KDTree
   * @return an Instance that the nearest neighbor can be found for
   */
  public Instance createInstance(Integer points[], final Instances dataset) {
    // Create numeric attributes "x" and "y" and "z"
    Attribute attr[] = new Attribute[10];
    for (int i = 0; i < points.length; i++) {
      attr[i] = new Attribute("p" + i);
      attr[i] = dataset.attribute(i);
    }

    // Create vector of the above attributes
    FastVector attributes = new FastVector(points.length);
    for (int i = 0; i < points.length; i++) attributes.addElement(attr[i]);

    // Create empty instance with X attribute values
    Instance inst = new Instance(points.length);

    // Set instance's values for the attributes "x", "y", and "z"
    for (int i = 0; i < points.length; i++) inst.setValue(attr[i], points[i]);

    // Set instance's dataset to be the dataset "points1"
    inst.setDataset(dataset);

    return inst;
  }
  public void buildClusterer(ArrayList<String> seqDB, double[][] sm) {
    seqList = seqDB;

    this.setSimMatrix(sm);

    Attribute seqString = new Attribute("sequence", (FastVector) null);
    FastVector attrInfo = new FastVector();
    attrInfo.addElement(seqString);
    Instances data = new Instances("data", attrInfo, 0);

    for (int i = 0; i < seqList.size(); i++) {
      Instance currentInst = new Instance(1);
      currentInst.setDataset(data);
      currentInst.setValue(0, seqList.get(i));
      data.add(currentInst);
    }

    try {
      buildClusterer(data);
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
예제 #25
0
  @Override
  public void setAttributeValue(Attribute attribute, Instance instance, Patient patient) {
    RegaService rs = ruleService.getRegaService();

    PatientAttributeValue pav =
        rs.getPatientAttributeValue(
            patient, ClinicalAttributeFactory.CLINICAL_REGA_ATTRIBUTE_RECENT_BLOOD_HB);

    if (pav == null) return; // The is no recent blood hb for this patient

    String strVal = pav.getValue();
    Double dVal;

    if (strVal.equals("N/A")) {
      return; // There is no value
    } else if (strVal.contains("/")) {
      dVal = Double.parseDouble(strVal.replace('/', '.'));
    } else {
      dVal = Double.parseDouble(strVal);
    }

    logger.info("Recent Blood HB for " + patient.getPatientId() + " is " + dVal);
    instance.setValue(attribute, dVal);
  }
예제 #26
0
  /**
   * Input an instance for filtering. The instance is processed and made available for output
   * immediately.
   *
   * @param instance the input instance.
   * @return true if the filtered instance may now be collected with output().
   * @throws IllegalStateException if no input structure has been defined.
   */
  public boolean input(Instance instance) {

    if (getInputFormat() == null) {
      throw new IllegalStateException("No input instance format defined");
    }
    if (m_NewBatch) {
      resetQueue();
      m_NewBatch = false;
    }

    if (isOutputFormatDefined()) {
      Instance newInstance = (Instance) instance.copy();

      // make sure that we get the right indexes set for the converted
      // string attributes when operating on a second batch of instances
      for (int i = 0; i < newInstance.numAttributes(); i++) {
        if (newInstance.attribute(i).isString()
            && !newInstance.isMissing(i)
            && m_AttIndices.isInRange(i)) {
          Attribute outAtt = getOutputFormat().attribute(newInstance.attribute(i).name());
          String inVal = newInstance.stringValue(i);
          int outIndex = outAtt.indexOfValue(inVal);
          if (outIndex < 0) {
            newInstance.setMissing(i);
          } else {
            newInstance.setValue(i, outIndex);
          }
        }
      }
      push(newInstance);
      return true;
    }

    bufferInput(instance);
    return false;
  }
예제 #27
0
  /**
   * Processes the given data (may change the provided dataset) and returns the modified version.
   * This method is called in batchFinished(). This implementation only calls process(Instance) for
   * each instance in the given dataset.
   *
   * @param instances the data to process
   * @return the modified data
   * @throws Exception in case the processing goes wrong
   * @see #batchFinished()
   */
  protected Instances process(Instances instances) throws Exception {
    Instances result;
    Instance instOld;
    Instance instNew;
    int i;
    int n;
    double[] values;
    int numAttNew;
    int numAttOld;

    if (!isFirstBatchDone()) computeThresholds(instances);

    result = getOutputFormat();
    numAttOld = instances.numAttributes();
    numAttNew = result.numAttributes();

    for (n = 0; n < instances.numInstances(); n++) {
      instOld = instances.instance(n);
      values = new double[numAttNew];
      System.arraycopy(instOld.toDoubleArray(), 0, values, 0, numAttOld);

      // generate new instance
      instNew = new Instance(1.0, values);
      instNew.setDataset(result);

      // per attribute?
      if (!getDetectionPerAttribute()) {
        // outlier?
        if (isOutlier(instOld)) instNew.setValue(m_OutlierAttributePosition[0], 1);
        // extreme value?
        if (isExtremeValue(instOld)) {
          instNew.setValue(m_OutlierAttributePosition[0] + 1, 1);
          // tag extreme values also as outliers?
          if (getExtremeValuesAsOutliers()) instNew.setValue(m_OutlierAttributePosition[0], 1);
        }
      } else {
        for (i = 0; i < m_AttributeIndices.length; i++) {
          // non-numeric attribute?
          if (m_AttributeIndices[i] == NON_NUMERIC) continue;

          // outlier?
          if (isOutlier(instOld, m_AttributeIndices[i]))
            instNew.setValue(m_OutlierAttributePosition[i], 1);
          // extreme value?
          if (isExtremeValue(instOld, m_AttributeIndices[i])) {
            instNew.setValue(m_OutlierAttributePosition[i] + 1, 1);
            // tag extreme values also as outliers?
            if (getExtremeValuesAsOutliers()) instNew.setValue(m_OutlierAttributePosition[i], 1);
          }
          // add multiplier?
          if (getOutputOffsetMultiplier())
            instNew.setValue(
                m_OutlierAttributePosition[i] + 2,
                calculateMultiplier(instOld, m_AttributeIndices[i]));
        }
      }

      // copy possible strings, relational values...
      copyValues(instNew, false, instOld.dataset(), getOutputFormat());

      // add to output
      result.add(instNew);
    }

    return result;
  }
예제 #28
0
  public static void main(String[] args) throws Exception {
    // NaiveBayesSimple nb = new NaiveBayesSimple();

    //		BufferedReader br_train = new BufferedReader(new FileReader("src/train.arff.txt"));
    //		String s = null;
    //		long st_time = System.currentTimeMillis();
    //		Instances inst_train = new Instances(br_train);
    //		System.out.println(inst_train.numAttributes());
    //		inst_train.setClassIndex(inst_train.numAttributes()-1);
    //		System.out.println("train time"+(System.currentTimeMillis()-st_time));
    // NaiveBayes nb1 = new NaiveBayes();
    // nb1.buildClassifier(inst_train);
    // br_train.close();
    long st_time = System.currentTimeMillis();
    st_time = System.currentTimeMillis();

    Classifier classifier = (Classifier) SerializationHelper.read("NaiveBayes.model");

    //		BufferedReader br_test = new BufferedReader(new FileReader("src/test.arff.txt"));
    //		Instances inst_test = new Instances(br_test);
    //		inst_test.setClassIndex(inst_test.numAttributes()-1);
    //		System.out.println("test time"+(System.currentTimeMillis()-st_time));
    //

    ArffLoader testLoader = new ArffLoader();
    testLoader.setSource(new File("src/test.arff"));
    testLoader.setRetrieval(Loader.BATCH);
    Instances testDataSet = testLoader.getDataSet();

    Attribute testAttribute = testDataSet.attribute("class");
    testDataSet.setClass(testAttribute);

    int correct = 0;
    int incorrect = 0;
    FastVector attInfo = new FastVector();
    attInfo.addElement(new Attribute("Id"));
    attInfo.addElement(new Attribute("Category"));

    Instances outputInstances = new Instances("predict", attInfo, testDataSet.numInstances());

    Enumeration testInstances = testDataSet.enumerateInstances();
    int index = 1;
    while (testInstances.hasMoreElements()) {
      Instance instance = (Instance) testInstances.nextElement();
      double classification = classifier.classifyInstance(instance);
      Instance predictInstance = new Instance(outputInstances.numAttributes());
      predictInstance.setValue(0, index++);
      predictInstance.setValue(1, (int) classification + 1);
      outputInstances.add(predictInstance);
    }

    System.out.println("Correct Instance: " + correct);
    System.out.println("IncCorrect Instance: " + incorrect);
    double accuracy = (double) (correct) / (double) (correct + incorrect);
    System.out.println("Accuracy: " + accuracy);
    CSVSaver predictedCsvSaver = new CSVSaver();
    predictedCsvSaver.setFile(new File("predict.csv"));
    predictedCsvSaver.setInstances(outputInstances);
    predictedCsvSaver.writeBatch();

    System.out.println("Prediciton saved to predict.csv");
  }
  public static void classify(Instances train, File file) throws Exception {
    FastVector atts = new FastVector();
    String[] classes = {"classical", "hiphop", "pop", "rock"};
    double[] val;
    FastVector attValsRel = new FastVector();

    // Setting attributes for the test data
    Attribute attributeZero = new Attribute("Zero_Crossings");
    atts.addElement(attributeZero);
    Attribute attributeLPC = new Attribute("LPC");
    atts.addElement(attributeLPC);
    Attribute attributeCentroid = new Attribute("Spectral_Centroid");
    atts.addElement(attributeCentroid);
    Attribute attributeRollOff = new Attribute("Spectral_Rolloff_Point");
    atts.addElement(attributeRollOff);
    Attribute attributePeakDetection = new Attribute("Peak_Detection");
    atts.addElement(attributePeakDetection);
    Attribute attributeStrongestBeat = new Attribute("Strongest_Beat");
    atts.addElement(attributeStrongestBeat);
    Attribute attributeBeatSum = new Attribute("Beat_Sum");
    atts.addElement(attributeBeatSum);
    Attribute attributeRMS = new Attribute("RMS");
    atts.addElement(attributeRMS);
    Attribute attributeConstantQ = new Attribute("ConstantQ");
    atts.addElement(attributeConstantQ);
    Attribute attributeMFT = new Attribute("MagnitudeFFT");
    atts.addElement(attributeMFT);
    Attribute attributeMFCC = new Attribute("MFCC");
    atts.addElement(attributeMFCC);

    for (int i = 0; i < classes.length; i++) attValsRel.addElement(classes[i]);
    atts.addElement(new Attribute("class", attValsRel));
    // Adding instances to the test dataset

    Instances test = new Instances("AudioSamples", atts, 0);
    val = makeData(file, null, attValsRel, test.numAttributes());
    Instance instance = new Instance(test.numAttributes());
    instance.setValue(attributeZero, val[0]);
    instance.setValue(attributeLPC, val[1]);
    instance.setValue(attributeCentroid, val[2]);
    instance.setValue(attributeRollOff, val[3]);
    instance.setValue(attributePeakDetection, val[4]);
    instance.setValue(attributeStrongestBeat, val[5]);
    instance.setValue(attributeBeatSum, val[6]);
    instance.setValue(attributeRMS, val[7]);
    instance.setValue(attributeConstantQ, val[8]);
    instance.setValue(attributeMFT, val[9]);
    instance.setValue(attributeMFCC, val[10]);
    test.add(instance);
    // Setting the class attribute
    test.setClassIndex(test.numAttributes() - 1);
    System.out.println(test);
    // Trainging the classifier with train dataset
    classifier = new FilteredClassifier();
    classifier.buildClassifier(train);

    // Classifying the test data with the train data
    for (int i = 0; i < test.numInstances(); i++) {
      double pred = classifier.classifyInstance(test.instance(i));
      System.out.println("===== Classified instance =====");
      System.out.println("Class predicted: " + test.classAttribute().value((int) pred));
    }
  }
예제 #30
0
파일: CNode.java 프로젝트: Waikato/meka
 public void updateTransform(Instance t_, double ypred[]) throws Exception {
   for (int pa : this.paY) {
     t_.setValue(this.map[pa], ypred[pa]);
   }
 }