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 factorsaandbbecome the spec’s inputs. Rows with any null acrossX/yare 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 againsty.
- 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
yagainstX’s columns.- Parameters:
X – One or more input columns.
y – A one-column
DataFrameholding the output.input_units – Units of every factor of
X’s columns (a plain column is its own factor;"a*b"needsaandb). Recorded forto_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_unitsis missing an entry for a factor ofX’s columns.FlexDataError – If
Xoryis not aDataFrame, ifydoes not hold exactly one column, or if fewer rows survive dropping nulls thanX.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
FitResultshape.- Raises:
FlexDataError – If
fit()has not been called.
- to_surrogate_spec() SurrogateSpec¶
Return the fit as a
multilinearSurrogateSpec.Uses the
input_units/output_unitsrecorded byfit().- Returns:
A
SurrogateType.MULTILINEARspec built bymake_spec(): the fitted column names, verbatim, are the terms, their factors are theinput_variableskeys, and every parameter is fixed.- Raises:
FlexDataError – If
fit()has not been called.