use of hex.gram.Gram in project h2o-3 by h2oai.
the class LinearAlgebraUtils method computeR.
/**
* Get R = L' from Cholesky decomposition Y'Y = LL' (same as R from Y = QR)
* @param jobKey Job key for Gram calculation
* @param yinfo DataInfo for Y matrix
* @param transpose Should result be transposed to get L?
* @return L or R matrix from Cholesky of Y Gram
*/
public static double[][] computeR(Key<Job> jobKey, DataInfo yinfo, boolean transpose) {
// Calculate Cholesky of Y Gram to get R' = L matrix
// Gram is Y'Y/n where n = nrow(Y)
Gram.GramTask gtsk = new Gram.GramTask(jobKey, yinfo);
gtsk.doAll(yinfo._adaptedFrame);
// Gram.Cholesky chol = gtsk._gram.cholesky(null); // If Y'Y = LL' Cholesky, then R = L'
Matrix ygram = new Matrix(gtsk._gram.getXX());
CholeskyDecomposition chol = new CholeskyDecomposition(ygram);
double[][] L = chol.getL().getArray();
// Must scale since Cholesky of Y'Y/n where nobs = nrow(Y)
ArrayUtils.mult(L, Math.sqrt(gtsk._nobs));
return transpose ? L : ArrayUtils.transpose(L);
}
use of hex.gram.Gram in project h2o-3 by h2oai.
the class MakeGLMModelHandler method computeGram.
public GramV3 computeGram(int v, GramV3 input) {
if (DKV.get(input.X.key()) == null)
throw new IllegalArgumentException("Frame " + input.X.key() + " does not exist.");
Frame fr = input.X.key().get();
Frame frcpy = new Frame(fr._names.clone(), fr.vecs().clone());
String wname = null;
Vec weight = null;
if (input.W != null && !input.W.column_name.isEmpty()) {
wname = input.W.column_name;
if (fr.find(wname) == -1)
throw new IllegalArgumentException("Did not find weight vector " + wname);
weight = frcpy.remove(wname);
}
DataInfo dinfo = new DataInfo(frcpy, null, 0, input.use_all_factor_levels, input.standardize ? TransformType.STANDARDIZE : TransformType.NONE, TransformType.NONE, input.skip_missing, false, !input.skip_missing, /* weight */
false, /* offset */
false, /* fold */
false, /* intercept */
true);
DKV.put(dinfo);
if (weight != null)
dinfo.setWeights(wname, weight);
Gram.GramTask gt = new Gram.GramTask(null, dinfo, false, true).doAll(dinfo._adaptedFrame);
double[][] gram = gt._gram.getXX();
dinfo.remove();
String[] names = water.util.ArrayUtils.append(dinfo.coefNames(), "Intercept");
Vec[] vecs = new Vec[gram.length];
Key[] keys = new VectorGroup().addVecs(vecs.length);
for (int i = 0; i < vecs.length; ++i) vecs[i] = Vec.makeVec(gram[i], keys[i]);
input.destination_frame = new KeyV3.FrameKeyV3();
String keyname = input.X.key().toString();
if (keyname.endsWith(".hex"))
keyname = keyname.substring(0, keyname.lastIndexOf("."));
keyname = keyname + "_gram";
if (weight != null)
keyname = keyname + "_" + wname;
Key k = Key.make(keyname);
if (DKV.get(k) != null) {
int cnt = 0;
while (cnt < 1000 && DKV.get(k = Key.make(keyname + "_" + cnt)) != null) cnt++;
if (cnt == 1000)
throw new IllegalArgumentException("unable to make unique key");
}
input.destination_frame.fillFromImpl(k);
DKV.put(new Frame(k, names, vecs));
return input;
}
Aggregations