Example #1
0
  /**
   * initializes the algorithm
   *
   * @param data the data to work with
   * @throws Exception if m_SVM is null
   */
  protected void init(Instances data) throws Exception {
    if (m_SVM == null) {
      throw new Exception("SVM not initialized in optimizer. Use RegOptimizer.setSVMReg()");
    }
    m_C = m_SVM.getC();
    m_data = data;
    m_classIndex = data.classIndex();
    m_nInstances = data.numInstances();

    // Initialize kernel
    m_kernel = Kernel.makeCopy(m_SVM.getKernel());
    m_kernel.buildKernel(data);

    // init m_target
    m_target = new double[m_nInstances];
    for (int i = 0; i < m_nInstances; i++) {
      m_target[i] = data.instance(i).classValue();
    }

    m_random = new Random(m_nSeed);

    //		initialize alpha and alpha* array to all zero
    m_alpha = new double[m_target.length];
    m_alphaStar = new double[m_target.length];

    m_supportVectors = new SMOset(m_nInstances);

    m_b = 0.0;
    m_nEvals = 0;
    m_nCacheHits = -1;
  }
Example #2
0
  /**
   * Builds a model using the current Kernel using the given data and returns the produced output.
   *
   * @param data the instances to test the Kernel on
   * @return a String containing the output of the Kernel.
   */
  protected String useKernel(Instances data) throws Exception {
    Kernel kernel = null;
    StringBuffer text = new StringBuffer();

    try {
      kernel = Kernel.makeCopy(m_Kernel);
    } catch (Exception e) {
      e.printStackTrace();
      fail("Problem setting up to use Kernel: " + e);
    }

    kernel.buildKernel(data);
    for (int n = 0; n < data.numInstances(); n++) {
      for (int i = n; i < data.numInstances(); i++) {
        text.append((n + 1) + "-" + (i + 1) + ": " + kernel.eval(n, i, data.instance(i)) + "\n");
      }
    }

    return text.toString();
  }