use of water.util.TwoDimTable in project h2o-3 by h2oai.
the class NaiveBayes method createModelSummaryTable.
private TwoDimTable createModelSummaryTable(NaiveBayesOutput output) {
List<String> colHeaders = new ArrayList<>();
List<String> colTypes = new ArrayList<>();
List<String> colFormat = new ArrayList<>();
colHeaders.add("Number of Response Levels");
colTypes.add("long");
colFormat.add("%d");
colHeaders.add("Min Apriori Probability");
colTypes.add("double");
colFormat.add("%.5f");
colHeaders.add("Max Apriori Probability");
colTypes.add("double");
colFormat.add("%.5f");
double apriori_min = output._apriori_raw[0];
double apriori_max = output._apriori_raw[0];
for (int i = 1; i < output._apriori_raw.length; i++) {
if (output._apriori_raw[i] < apriori_min)
apriori_min = output._apriori_raw[i];
else if (output._apriori_raw[i] > apriori_max)
apriori_max = output._apriori_raw[i];
}
final int rows = 1;
TwoDimTable table = new TwoDimTable("Model Summary", null, new String[rows], colHeaders.toArray(new String[0]), colTypes.toArray(new String[0]), colFormat.toArray(new String[0]), "");
int row = 0;
int col = 0;
table.set(row, col++, output._apriori_raw.length);
table.set(row, col++, apriori_min);
table.set(row, col, apriori_max);
return table;
}
use of water.util.TwoDimTable in project h2o-3 by h2oai.
the class ConfusionMatrix method toTable.
// Do the work making a TwoDimTable
private TwoDimTable toTable() {
if (tooLarge())
return null;
if (_cm == null || _domain == null)
return null;
for (double[] cm : _cm) assert (_cm.length == cm.length);
// Sum up predicted & actuals
double[] acts = new double[_cm.length];
double[] preds = new double[_cm[0].length];
boolean isInt = true;
for (int a = 0; a < _cm.length; a++) {
double sum = 0;
for (int p = 0; p < _cm[a].length; p++) {
sum += _cm[a][p];
preds[p] += _cm[a][p];
isInt &= (_cm[a][p] == (long) _cm[a][p]);
}
acts[a] = sum;
}
String[] adomain = createConfusionMatrixHeader(acts, _domain);
String[] pdomain = createConfusionMatrixHeader(preds, _domain);
assert adomain.length == pdomain.length : "The confusion matrix should have the same length for both directions.";
String[] rowHeader = Arrays.copyOf(adomain, adomain.length + 1);
rowHeader[adomain.length] = "Totals";
String[] colHeader = Arrays.copyOf(pdomain, pdomain.length + 2);
colHeader[colHeader.length - 2] = "Error";
colHeader[colHeader.length - 1] = "Rate";
String[] colType = new String[colHeader.length];
String[] colFormat = new String[colHeader.length];
for (int i = 0; i < colFormat.length - 1; ++i) {
colType[i] = isInt ? "long" : "double";
colFormat[i] = isInt ? "%d" : "%.2f";
}
colType[colFormat.length - 2] = "double";
colFormat[colFormat.length - 2] = "%.4f";
colType[colFormat.length - 1] = "string";
// pass 1: compute width of last column
double terr = 0;
int width = 0;
for (int a = 0; a < _cm.length; a++) {
if (adomain[a] == null)
continue;
double correct = 0;
for (int p = 0; p < pdomain.length; p++) {
if (pdomain[p] == null)
continue;
boolean onDiag = adomain[a].equals(pdomain[p]);
if (onDiag)
correct = _cm[a][p];
}
double err = acts[a] - correct;
terr += err;
width = isInt ? Math.max(width, String.format("%,d / %,d", (long) err, (long) acts[a]).length()) : Math.max(width, String.format("%.4f / %.4f", err, acts[a]).length());
}
double nrows = 0;
for (double n : acts) nrows += n;
width = isInt ? Math.max(width, String.format("%,d / %,d", (long) terr, (long) nrows).length()) : Math.max(width, String.format("%.4f / %.4f", terr, nrows).length());
// set format width
colFormat[colFormat.length - 1] = "= %" + width + "s";
TwoDimTable table = new TwoDimTable("Confusion Matrix", "vertical: actual; across: predicted", rowHeader, colHeader, colType, colFormat, null);
// Main CM Body
for (int a = 0; a < _cm.length; a++) {
if (adomain[a] == null)
continue;
double correct = 0;
for (int p = 0; p < pdomain.length; p++) {
if (pdomain[p] == null)
continue;
boolean onDiag = adomain[a].equals(pdomain[p]);
if (onDiag)
correct = _cm[a][p];
if (isInt)
table.set(a, p, (long) _cm[a][p]);
else
table.set(a, p, _cm[a][p]);
}
double err = acts[a] - correct;
table.set(a, pdomain.length, err / acts[a]);
table.set(a, pdomain.length + 1, isInt ? String.format("%,d / %,d", (long) err, (long) acts[a]) : String.format("%.4f / %.4f", err, acts[a]));
}
// Last row of CM
for (int p = 0; p < pdomain.length; p++) {
if (pdomain[p] == null)
continue;
if (isInt)
table.set(adomain.length, p, (long) preds[p]);
else
table.set(adomain.length, p, preds[p]);
}
table.set(adomain.length, pdomain.length, (float) terr / nrows);
table.set(adomain.length, pdomain.length + 1, isInt ? String.format("%,d / %,d", (long) terr, (long) nrows) : String.format("%.2f / %.2f", terr, nrows));
return table;
}
use of water.util.TwoDimTable in project h2o-3 by h2oai.
the class ModelMetricsBinomialV3 method fillFromImpl.
@Override
public S fillFromImpl(ModelMetricsBinomial modelMetrics) {
super.fillFromImpl(modelMetrics);
// sigma = modelMetrics._sigma;
r2 = modelMetrics.r2();
logloss = modelMetrics._logloss;
mean_per_class_error = modelMetrics._mean_per_class_error;
AUC2 auc = modelMetrics._auc;
if (null != auc) {
AUC = auc._auc;
Gini = auc._gini;
// Fill TwoDimTable
String[] thresholds = new String[auc._nBins];
for (int i = 0; i < auc._nBins; i++) thresholds[i] = Double.toString(auc._ths[i]);
AUC2.ThresholdCriterion[] crits = AUC2.ThresholdCriterion.VALUES;
String[] colHeaders = new String[crits.length + 2];
String[] colHeadersMax = new String[crits.length - 8];
String[] types = new String[crits.length + 2];
String[] formats = new String[crits.length + 2];
colHeaders[0] = "Threshold";
types[0] = "double";
formats[0] = "%f";
int i;
for (i = 0; i < crits.length; i++) {
if (colHeadersMax.length > i)
colHeadersMax[i] = "max " + crits[i].toString();
colHeaders[i + 1] = crits[i].toString();
types[i + 1] = crits[i]._isInt ? "long" : "double";
formats[i + 1] = crits[i]._isInt ? "%d" : "%f";
}
colHeaders[i + 1] = "idx";
types[i + 1] = "int";
formats[i + 1] = "%d";
TwoDimTable thresholdsByMetrics = new TwoDimTable("Metrics for Thresholds", "Binomial metrics as a function of classification thresholds", new String[auc._nBins], colHeaders, types, formats, null);
for (i = 0; i < auc._nBins; i++) {
int j = 0;
thresholdsByMetrics.set(i, j, Double.valueOf(thresholds[i]));
for (j = 0; j < crits.length; j++) {
// Note: casts to Object are NOT redundant
double d = crits[j].exec(auc, i);
thresholdsByMetrics.set(i, 1 + j, crits[j]._isInt ? (Object) ((long) d) : d);
}
thresholdsByMetrics.set(i, 1 + j, i);
}
this.thresholds_and_metric_scores = new TwoDimTableV3().fillFromImpl(thresholdsByMetrics);
// Fill TwoDimTable
TwoDimTable maxMetrics = new TwoDimTable("Maximum Metrics", "Maximum metrics at their respective thresholds", colHeadersMax, new String[] { "Threshold", "Value", "idx" }, new String[] { "double", "double", "long" }, new String[] { "%f", "%f", "%d" }, "Metric");
for (i = 0; i < colHeadersMax.length; i++) {
int idx = crits[i].max_criterion_idx(auc);
maxMetrics.set(i, 0, idx == -1 ? Double.NaN : auc._ths[idx]);
maxMetrics.set(i, 1, idx == -1 ? Double.NaN : crits[i].exec(auc, idx));
maxMetrics.set(i, 2, idx);
}
max_criteria_and_metric_scores = new TwoDimTableV3().fillFromImpl(maxMetrics);
}
if (modelMetrics._gainsLift != null) {
TwoDimTable t = modelMetrics._gainsLift.createTwoDimTable();
if (t != null)
this.gains_lift_table = new TwoDimTableV3().fillFromImpl(t);
}
return (S) this;
}
use of water.util.TwoDimTable in project h2o-3 by h2oai.
the class ModelMetricsClusteringV3 method fillFromImpl.
@Override
public ModelMetricsClusteringV3 fillFromImpl(ModelMetricsClustering impl) {
ModelMetricsClusteringV3 mm = super.fillFromImpl(impl);
TwoDimTable tdt = impl.createCentroidStatsTable();
if (tdt != null)
mm.centroid_stats = new TwoDimTableV3().fillFromImpl(tdt);
return mm;
}
use of water.util.TwoDimTable in project h2o-3 by h2oai.
the class ModelMetricsMultinomialV3 method fillFromImpl.
@Override
public S fillFromImpl(I modelMetrics) {
super.fillFromImpl(modelMetrics);
logloss = modelMetrics._logloss;
r2 = modelMetrics.r2();
if (modelMetrics._hit_ratios != null) {
TwoDimTable table = getHitRatioTable(modelMetrics._hit_ratios);
hit_ratio_table = (TwoDimTableV3) SchemaServer.schema(this.getSchemaVersion(), table).fillFromImpl(table);
}
if (null != modelMetrics._cm) {
// Fill in lazy table, for icing
modelMetrics._cm.table();
cm = (ConfusionMatrixV3) SchemaServer.schema(this.getSchemaVersion(), modelMetrics._cm).fillFromImpl(modelMetrics._cm);
}
return (S) this;
}
Aggregations