Qu

impute

Builtin index · Statistics & ML

impute(x, strategy)

Input: x, strategy · Output: no output

What it is

Replaces each NaN by a statistic of the observed (non-NaN) values of its own column (a vector is one column): strategy is "mean", "median" (the mean of the two middle values for an even count), "most_frequent" (ties go to the smallest value) or "zero". Columns with nothing missing are left as they are. The statistic comes from x itself: to impute a test set with training statistics, compute them on the training set and use fillna. Errors: an unknown strategy, and a column that is all NaN under "mean", "median" or "most_frequent", which has no statistic to use ("zero" still works).

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
impute(x, strategy)
x —The statistic comes from x itself: to impute a test set with training statistics, compute them on the training set and use fillna.
strategy vectorReplaces each NaN by a statistic of the observed (non-NaN) values of its own column (a vector is one column): strategy is "mean", "median" (the mean of the two middle values for an even count), "most_frequent" (ties go to the smallest value) or "zero".
not documented yetColumns with nothing missing are left as they are.

Examples

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

Xna = [1, 10; nan, 12; 3, nan; 5, 14; 7, 16; nan, 18]
yna = [0, 0, 0, 1, 1, 1]
Xf = impute(Xna, "median")
print(Xf)
m = logistic_model(Xf, yna)
print("train accuracy: {round(m.score(Xf, yna), 3)}")
print(fillna([1, nan, 3], 0))
output
[1, 10; 4, 12; 3, 14; 5, 14; 7, 16; 4, 18]
train accuracy: 0.667
[1, 0, 3]

from statistics-ml