@Override
 public Label predict(Instance instance) {
   Label l = null;
   if (instance.getLabel() instanceof ClassificationLabel || instance.getLabel() == null) {
     // ----------------- declare variables ------------------
     double lambda = 0.0;
     RealVector x_instance = new ArrayRealVector(matrixX.getColumnDimension(), 0);
     double result = 0.0;
     // -------------------------- initialize xi -------------------------
     for (int idx = 0; idx < matrixX.getColumnDimension(); idx++) {
       x_instance.setEntry(idx, instance.getFeatureVector().get(idx + 1));
     }
     // ------------------ get lambda -----------------------
     for (int j = 0; j < alpha.getDimension(); j++) {
       lambda += alpha.getEntry(j) * kernelFunction(matrixX.getRowVector(j), x_instance);
     }
     // ----------------- make prediction -----------------
     Sigmoid g = new Sigmoid(); // helper function
     result = g.value(lambda);
     l = new ClassificationLabel(result < 0.5 ? 0 : 1);
   } else {
     System.out.println("label type error!");
   }
   return l;
 }
Exemplo n.º 2
0
  public static double vectorLength(RealVector v) {
    // euclidean distance (i.e. L2-norm) from point 0

    double[] v0 = new double[v.getDimension()];
    for (int i = 0; i < v0.length; i++) v0[i] = 0;

    RealVector zero = MatrixUtils.createRealVector(v0);

    return v.getDistance(zero);
  }
Exemplo n.º 3
0
 public void setY(RealVector y) {
   this.y = y;
   n = y.getDimension();
 }