コード例 #1
0
ファイル: FM.java プロジェクト: wubin7019088/JavaFM
  /**
   * Predict the value of an instance.
   *
   * @param x instance
   * @return value of prediction
   */
  public double prediction(I x) {
    double pred = b;

    DoubleMatrix1D xm = new DenseDoubleMatrix1D(m.columns());
    pred +=
        x.operate(
            (i, xi) -> {
              double wi = w.getQuick(i);
              DoubleMatrix1D mi = m.viewRow(i);

              xm.assign(mi, (r, s) -> r + xi * s);

              return xi * wi - 0.5 * xi * xi * mi.zDotProduct(mi);
            },
            (v1, v2) -> v1 + v2);

    pred += 0.5 * xm.zDotProduct(xm);

    return pred;
  }
コード例 #2
0
ファイル: FM.java プロジェクト: wubin7019088/JavaFM
  /**
   * Feature-specific contribution to the prediction of the value of an instance.
   *
   * @param x instance
   * @param i index of the feature of interest
   * @param xi value of the feature of interest
   * @return value of the contribution of the feature to the prediction
   */
  public double prediction(I x, int i, double xi) {
    double wi = w.getQuick(i);
    DoubleMatrix1D mi = m.viewRow(i);

    double pred = 0.0;

    pred += xi * wi;

    pred +=
        x.operate(
            (j, xj) -> {
              DoubleMatrix1D mj = m.viewRow(j);

              return xi * xj * mi.zDotProduct(mj);
            },
            (v1, v2) -> v1 + v2);

    return pred;
  }
コード例 #3
0
 /**
  * Returns the Euclidean distance between two points. It is used to compute the similarity degree
  * of these ones.
  *
  * @param x the first point
  * @param y the second point
  * @return the Euclidean distance between the points
  */
 protected static double distnorm2(DoubleMatrix1D x, DoubleMatrix1D y) {
   DoubleMatrix1D z = x.copy();
   z.assign(y, Functions.minus);
   return z.zDotProduct(z);
 }