示例#1
0
  /**
   * Standardize the matrix entries by row- or column-wise z-scores (z=(x-u)/sigma)
   *
   * @param isByRow standardize by row if true; otherwise by column
   */
  public void standardize(boolean isByRow) {

    int iters = isByRow ? numRows : numColumns;
    for (int iter = 0; iter < iters; iter++) {
      SparseVector vec = isByRow ? row(iter) : column(iter);

      if (vec.getCount() > 0) {

        double[] data = vec.getData();
        double mu = Stats.mean(data);
        double sigma = Stats.sd(data, mu);

        for (VectorEntry ve : vec) {
          int idx = ve.index();
          double val = ve.get();
          double z = (val - mu) / sigma;

          if (isByRow) this.set(iter, idx, z);
          else this.set(idx, iter, z);
        }
      }
    }
  }
  @Override
  public Vector transMultAdd(double alpha, Vector x, Vector y) {
    if (!(x instanceof DenseVector) || !(y instanceof DenseVector))
      return super.transMultAdd(alpha, x, y);

    checkTransMultAdd(x, y);

    double[] xd = ((DenseVector) x).getData(), yd = ((DenseVector) y).getData();

    // y = 1/alpha * y
    y.scale(1. / alpha);

    // y = A'x + y
    for (int i = 0; i < numRows; ++i) {
      SparseVector v = rowD[i];
      int[] index = v.getIndex();
      double[] data = v.getData();
      int length = v.getUsed();
      for (int j = 0; j < length; ++j) yd[index[j]] += data[j] * xd[i];
    }

    // y = alpha*y = alpha * A'x + y
    return y.scale(alpha);
  }