Exemplo n.º 1
0
 /** Writes the example set into the given output stream. */
 public void writeSupportVectors(ObjectOutputStream out) throws IOException {
   out.writeInt(getNumberOfSupportVectors());
   out.writeDouble(b);
   out.writeInt(dim);
   if ((meanVarianceMap == null) || (meanVarianceMap.size() == 0)) {
     out.writeUTF("noscale");
   } else {
     out.writeUTF("scale");
     out.writeInt(meanVarianceMap.size());
     Iterator i = meanVarianceMap.keySet().iterator();
     while (i.hasNext()) {
       Integer index = (Integer) i.next();
       MeanVariance meanVariance = meanVarianceMap.get(index);
       out.writeInt(index.intValue());
       out.writeDouble(meanVariance.getMean());
       out.writeDouble(meanVariance.getVariance());
     }
   }
   for (int e = 0; e < train_size; e++) {
     if (alphas[e] != 0.0d) {
       out.writeInt(atts[e].length);
       for (int a = 0; a < atts[e].length; a++) {
         out.writeInt(index[e][a]);
         out.writeDouble(atts[e][a]);
       }
       out.writeDouble(alphas[e]);
       out.writeDouble(ys[e]);
     }
   }
 }
Exemplo n.º 2
0
  /**
   * Creates a fresh example set of the given size from the RapidMiner example reader. The alpha
   * values and b are zero, the label will be set if it is known.
   */
  public SVMExamples(
      com.rapidminer.example.ExampleSet exampleSet,
      Attribute labelAttribute,
      Map<Integer, MeanVariance> meanVariances) {
    this(exampleSet.size(), 0.0d);
    this.meanVarianceMap = meanVariances;

    Iterator<com.rapidminer.example.Example> reader = exampleSet.iterator();
    Attribute idAttribute = exampleSet.getAttributes().getId();
    int exampleCounter = 0;
    while (reader.hasNext()) {
      com.rapidminer.example.Example current = reader.next();
      Map<Integer, Double> attributeMap = new LinkedHashMap<Integer, Double>();
      int a = 0;
      for (Attribute attribute : exampleSet.getAttributes()) {
        double value = current.getValue(attribute);
        if (!com.rapidminer.example.Tools.isDefault(attribute.getDefault(), value)) {
          attributeMap.put(a, value);
        }
        if ((a + 1) > dim) {
          dim = (a + 1);
        }
        a++;
      }
      atts[exampleCounter] = new double[attributeMap.size()];
      index[exampleCounter] = new int[attributeMap.size()];
      Iterator<Map.Entry<Integer, Double>> i = attributeMap.entrySet().iterator();
      int attributeCounter = 0;
      while (i.hasNext()) {
        Map.Entry<Integer, Double> e = i.next();
        Integer indexValue = e.getKey();
        Double attributeValue = e.getValue();
        index[exampleCounter][attributeCounter] = indexValue.intValue();
        double value = attributeValue.doubleValue();
        MeanVariance meanVariance = meanVarianceMap.get(indexValue);
        if (meanVariance != null) {
          if (meanVariance.getVariance() == 0.0d) {
            value = 0.0d;
          } else {
            value = (value - meanVariance.getMean()) / Math.sqrt(meanVariance.getVariance());
          }
        }
        atts[exampleCounter][attributeCounter] = value;
        attributeCounter++;
      }
      if (labelAttribute != null) {
        double label = current.getValue(labelAttribute);
        if (labelAttribute.isNominal()) {
          ys[exampleCounter] = (label == labelAttribute.getMapping().getPositiveIndex() ? 1 : -1);
        } else {
          ys[exampleCounter] = label;
        }
      }
      if (idAttribute != null) {
        ids[exampleCounter] = current.getValueAsString(idAttribute);
      }
      exampleCounter++;
    }
  }