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. A column named "a*b" holds an engineered product and is fitted as a cross-term coefficient whose factors a and b become the spec’s inputs. 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

Factors of the fitted columns, in first-seen order.

output_variable

Column name of the fitted output.

input_units

Units of every input variable, keyed by its 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 factor of X’s columns (a plain column is its own factor; "a*b" needs a and b). 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 a factor 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 the terms, their factors are the input_variables keys, and every parameter is fixed.

Raises:

FlexDataError – If fit() has not been called.