LinearRegressor

class flexparameterize.regression.linear.LinearRegressor

Fits a unit’s output as an ordinary-least-squares function of its inputs.

One output column, any number of input columns, degree 1 in each (no automatic cross terms – engineer one into an X column named "a*b" before fitting if the relationship needs one). Rows with any null across X/y are dropped before fitting.

coefficients

Fitted column name -> coefficient, plus "intercept".

n_samples

Number of rows the fit used, after dropping nulls.

metrics

{"r2": ..., "rmse": ...} of the fitted line against y.

data_window

(first, last) index value of the rows used.

input_variables

Column names of the fitted inputs, in order.

output_variable

Column name of the fitted output.

input_units

Units of every fitted input column, keyed by its column name.

output_units

Units of the fitted output column.

fit(X: DataFrame, y: DataFrame, *, input_units: dict[str, str], output_units: str) → LinearRegressor

Fit an OLS line of y against X’s columns.

Parameters:
  • X – One or more input columns.

  • y – A one-column DataFrame holding the output.

  • input_units – Units of every fitted input column, keyed by its column name (every column in X must have an entry). Recorded for to_surrogate_spec().

  • output_units – Units of the fitted output column. Recorded for to_surrogate_spec().

Returns:

self, fitted.

Raises:
  • FlexConfigError – If scikit-learn is not installed, or if input_units is missing an entry for one of X’s columns.

  • FlexDataError – If X or y is not a DataFrame, if y does not hold exactly one column, or if fewer rows survive dropping nulls than X.shape[1] + 1 (the minimum an OLS fit with an intercept needs to be determined).

to_fit_result() → FitResult

Return this fit as the shared FitResult shape.

Raises:

FlexDataError – If fit() has not been called.

to_surrogate_spec() → SurrogateSpec

Return the fit as a multilinear SurrogateSpec.

Uses the input_units/output_units recorded by fit().

Returns:

A SurrogateType.MULTILINEAR spec built by make_spec(): the fitted column names, verbatim, are both the input_variables keys and the (single-factor) terms, and every parameter is fixed.

Raises:

FlexDataError – If fit() has not been called.