Skip to content

Commit

Permalink
refactor: rename estimator_on_state from state_fn_from_estimator
Browse files Browse the repository at this point in the history
  • Loading branch information
hollandjg committed Aug 25, 2023
1 parent e48b387 commit 1a1c897
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 53 deletions.
4 changes: 2 additions & 2 deletions docs/cycle/Basic Introduction to Functions and States.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,9 @@
"outputs": [],
"source": [
"from sklearn.linear_model import LinearRegression\n",
"from autora.state import state_fn_from_estimator\n",
"from autora.state import estimator_on_state\n",
"\n",
"theorist = state_fn_from_estimator(LinearRegression(fit_intercept=True))"
"theorist = estimator_on_state(LinearRegression(fit_intercept=True))"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@
"### Defining The Theorist\n",
"\n",
"Now we define a theorist, which does a linear regression on the polynomial of degree 5. We define a regressor and a\n",
"method to return its feature names and coefficients, and then the theorist to handle it. Here, we use a different wrapper `state_fn_from_estimator` that wraps the regressor and returns a function with the same functionality, but operating on `State` fields. In this case, we want to use the `State` field `experiment_data` and extend the `State` field `models`."
"method to return its feature names and coefficients, and then the theorist to handle it. Here, we use a different wrapper `estimator_on_state` that wraps the regressor and returns a function with the same functionality, but operating on `State` fields. In this case, we want to use the `State` field `experiment_data` and extend the `State` field `models`."
]
},
{
Expand All @@ -278,13 +278,13 @@
"outputs": [],
"source": [
"from sklearn.linear_model import LinearRegression\n",
"from autora.state import state_fn_from_estimator\n",
"from autora.state import estimator_on_state\n",
"from sklearn.pipeline import make_pipeline as make_theorist_pipeline\n",
"from sklearn.preprocessing import PolynomialFeatures\n",
"\n",
"# Completely standard scikit-learn pipeline regressor\n",
"regressor = make_theorist_pipeline(PolynomialFeatures(degree=5), LinearRegression())\n",
"theorist = state_fn_from_estimator(regressor)\n",
"theorist = estimator_on_state(regressor)\n",
"\n",
"def get_equation(r):\n",
" t = r.named_steps['polynomialfeatures'].get_feature_names_out()\n",
Expand Down
50 changes: 2 additions & 48 deletions src/autora/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@ def model(self):
XY = TypeVar("XY")


def state_fn_from_estimator(estimator: BaseEstimator) -> StateFunction:
def estimator_on_state(estimator: BaseEstimator) -> StateFunction:
"""
Convert a scikit-learn compatible estimator into a function on a `State` object.
Expand All @@ -1287,7 +1287,7 @@ def state_fn_from_estimator(estimator: BaseEstimator) -> StateFunction:
Examples:
Initialize a function which operates on the state, `state_fn` and runs a LinearRegression.
>>> from sklearn.linear_model import LinearRegression
>>> state_fn = state_fn_from_estimator(LinearRegression())
>>> state_fn = estimator_on_state(LinearRegression())
Define the state on which to operate (here an instance of the `StandardState`):
>>> from autora.state import StandardState
Expand Down Expand Up @@ -1319,52 +1319,6 @@ def theorist(
return theorist


def state_fn_from_x_to_y_fn_df(f: Callable[[X], Y]) -> StateFunction:
"""Wrapper for experiment_runner of the form $f(x) \rarrow y$, where `f` returns just the $y$
values, with inputs and outputs as a DataFrame or Series with correct column names.
Examples:
The conditions are some x-values in a StandardState object:
>>> from autora.state import StandardState
>>> s = StandardState(conditions=pd.DataFrame({"x": [1, 2, 3]}))
The function can be defined on a DataFrame (allowing the explicit inclusion of
metadata like column names).
>>> def x_to_y_fn(c: pd.DataFrame) -> pd.Series:
... result = pd.Series(2 * c["x"] + 1, name="y")
... return result
We apply the wrapped function to `s` and look at the returned experiment_data:
>>> state_fn_from_x_to_y_fn_df(x_to_y_fn)(s).experiment_data
x y
0 1 3
1 2 5
2 3 7
We can also define functions of several variables:
>>> def xs_to_y_fn(c: pd.DataFrame) -> pd.Series:
... result = pd.Series(c["x0"] + c["x1"], name="y")
... return result
With the relevant variables as conditions:
>>> t = StandardState(conditions=pd.DataFrame({"x0": [1, 2, 3], "x1": [10, 20, 30]}))
>>> state_fn_from_x_to_y_fn_df(xs_to_y_fn)(t).experiment_data
x0 x1 y
0 1 10 11
1 2 20 22
2 3 30 33
"""

@on_state()
def experiment_runner(conditions: pd.DataFrame, **kwargs):
x = conditions
y = f(x, **kwargs)
experiment_data = pd.DataFrame.merge(x, y, left_index=True, right_index=True)
return Delta(experiment_data=experiment_data)

return experiment_runner


def state_fn_from_x_to_xy_fn_df(f: Callable[[X], XY]) -> StateFunction:
"""Wrapper for experiment_runner of the form $f(x) \rarrow (x,y)$, where `f`
returns both $x$ and $y$ values in a complete dataframe.
Expand Down

0 comments on commit 1a1c897

Please sign in to comment.