Documentation
¶
Overview ¶
Package tabular implements a small, deterministic, single-predictor OLS workflow. All observations must be finite. No rows are silently dropped or imputed.
Example ¶
d, _ := ReadCSV(strings.NewReader("x,y\n1,3\n2,5\n3,7\n4,9\n5,11\n"), "x", "y")
train, test, _ := Split(d, 3)
model, _ := Fit(train)
defer model.Close()
predicted, _ := model.Predict(test.X)
score, _ := Evaluate(test.Y, predicted)
var saved bytes.Buffer
_ = model.Save(&saved)
restored, _ := Load(&saved)
defer restored.Close()
again, _ := restored.Predict(test.X)
fmt.Printf("held-out predictions: %v; restored: %v\n", predicted, again)
fmt.Printf("MAE %.1f RMSE %.1f R2 %.1f\n", score.MAE, score.RMSE, score.R2)
Output: held-out predictions: [9 11]; restored: [9 11] MAE 0.0 RMSE 0.0 R2 1.0
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Dataset ¶
type Dataset struct{ X, Y []float64 }
Dataset pairs one numeric predictor X with a numeric response Y. Callers own the slices and must not mutate them during a function call.
func ReadCSV ¶
ReadCSV extracts two distinct named numeric columns. Other columns are ignored. Headers must be nonempty and unique; every row must match the header width. Blank records are errors; quoted embedded newlines are supported. Values follow strconv.ParseFloat syntax; missing and nonfinite values are errors. The caller is responsible for bounding the reader for untrusted CSV input.
type Metrics ¶
Metrics reports mean absolute error, root mean squared error and coefficient of determination. For constant actual values, R2 is 1 for exact predictions, otherwise 0. Other R2 values may be negative.
type Model ¶
type Model struct {
// contains filtered or unexported fields
}
Model is a fitted single-predictor ordinary least squares model. It must not be copied. Predict, Save and Close may be called concurrently. The zero value is unusable. Close is idempotent and invalidates further Predict and Save calls.
func Fit ¶
Fit learns from d only. It requires at least two observations and a varying predictor. Scaled centered sums with compensated accumulation limit float64 overflow and rounding error. Numerically degenerate or unrepresentable slopes are rejected. No training slices are retained.
func Load ¶
Load reads one model of at most 64 KiB, including whitespace. Missing, unknown, duplicate, nonfinite or invalid fields and trailing JSON are rejected. The caller owns and closes r. Version 1 describes scaled centered float64 OLS.