Qu

corrmat

Builtin index · Plotting

result = corr_heatmap(t)

Input: t · Output: result

What it is

Computes the pairwise Pearson correlation of Table t's numeric columns. corr_heatmap/corrplot render it directly as a heatmap and return nothing; corrmat(t) instead returns a tagged Model (.matrix, an n-by-n Mat of correlation values, plus .labels, a List of the n column names) for when you want the numbers rather than a picture — calling .plot() on that Model also renders the labeled heatmap.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = corr_heatmap(t)
t Table, Model, matrix, Mat, List, or numbernot documented yet
result Model, matrix, Mat, or List
Returns a tagged Model from corrmat — .matrix, an n-by-n Mat of correlations, plus .labels, a List of the n column names; corr_heatmap/corrplot draw instead and hand back no value.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

write_csv(table(
    temp     = [21.5, 22.1, 35.0, 21.8, 36.2, 22.0, 34.5, 21.6, 35.8, 22.3],
    humidity = [45.2, 46.0, 20.1, 45.5, 19.5, 45.8, 21.0, 45.1, 20.4, 46.2],
    pressure = [1012.1, 1011.8, 1009.5, 1012.0, 1008.9, 1012.3, 1009.8, 1012.2, 1009.1, 1011.9],
    label    = ["ok", "ok", "warn", "ok", "warn", "ok", "warn", "ok", "warn", "ok"]
), "sensors.csv")

df = read_csv("sensors.csv")
print("rows = {nrow(df)}, cols = {ncol(df)}")
print(head(df, 3))

numeric = drop(df, "label")
c = corrmat(numeric)
print(c.matrix)
c.plot()
savefig("quickstart_corrmat.svg")

scaled_temp = standardize(df.temp)
print("standardized temp[0] = {scaled_temp[0]:.4f}")
print("mean(standardized) = {mean(scaled_temp):.4f}, std(standardized) = {std(scaled_temp):.4f}")
output
rows = 10, cols = 4
temp  humidity  pressure  label
----  --------  --------  -----
21.5      45.2    1012.1  ok   
22.1        46    1011.8  ok   
  35      20.1    1009.5  warn 
[1, -0.997357, -0.991317; -0.997357, 1, 0.983511; -0.991317, 0.983511, 1]
· savefig quickstart_corrmat.svg
  note: fonts render using whatever is installed on the viewer's machine (none are embedded) -- pass embed_fonts=true (fontfamily preset only) or export .tikz for guaranteed appearance
standardized temp[0] = -0.8275
mean(standardized) = -0.0000, std(standardized) = 1.0000
temp, temp: 1 1 temp, humidity: -0.997 -0.997 temp, pressure: -0.991 -0.991 humidity, temp: -0.997 -0.997 humidity, humidity: 1 1 humidity, pressure: 0.984 0.984 pressure, temp: -0.991 -0.991 pressure, humidity: 0.984 0.984 pressure, pressure: 1 1 temp humidity pressure temp humidity pressure First plot

from quickstart

How it works

data.corrmat() — the pairwise Pearson correlation matrix of data's NUMERIC columns only (text columns silently skipped, same filter corr_heatmap/describe already apply — a correlation of a text column isn't meaningful). Builds a samples-by-features Mat from those columns and delegates the actual computation to the existing corrcoef_matrix (landed at 85f1631 as the corrcoef(X) builtin) rather than a second copy of that math. Returns a Value::Model (kind="corrmat", fields matrix + labels), NOT a bare Value::Mat — so .plot() can recognize "this came from corrmat()" and render a labeled heatmap instead of falling through to the ordinary flattened-line-plot path an arbitrary Mat gets (see the "plot" arm below). The alternative from the spec — treat ANY square Mat with a diagonal of 1s as "must be a correlation matrix, render as heatmap" — was rejected: an unrelated square matrix that happens to have 1s on its diagonal (eye(n), a similarity/adjacency matrix, ...) would then silently misrender as a heatmap with no way to opt back into a normal plot. A Model tag can never collide with an unrelated value by accident, matching how kmeans/pca/ every other tagged-result builtin here already works.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.

See also

corr_heatmap, corrplot — documented in the same entry.