Skip to content

scikit-learn

flowcean.sklearn provides regression learners, scikit-learn model wrappers, and commonly used metrics. The complete classification and regression metric API, including MultiOutputMixin, is available from flowcean.sklearn.metrics.

sklearn

Classes

AdaBoost

AdaBoost(estimator: object = None, *, n_estimators: int = 50, learning_rate: float = 1.0, random_state: int | None = None, threshold: float = 0.5)

Bases: SupervisedLearner

Wrapper class for sklearn's AdaBoostClassifier.

Reference: https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.AdaBoostClassifier.html

Initialize the AdaBoost classifier learner.

Reference: https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.AdaBoostClassifier.html

Attributes
classifier instance-attribute
classifier: AdaBoostClassifier = AdaBoostClassifier(estimator=estimator, n_estimators=n_estimators, learning_rate=learning_rate, random_state=random_state or get_seed())
threshold instance-attribute
threshold = threshold
Methods:
learn
learn(inputs: LazyFrame, outputs: LazyFrame) -> Model

LinearRegression

Bases: SupervisedLearner

Methods:
learn
learn(inputs: LazyFrame | DataFrame, outputs: LazyFrame | DataFrame) -> SciKitModel

Accuracy

Accuracy(features: list[str] | None = None)

Bases: SelectMixin, LazyMixin, Metric

Accuracy classification score.

As defined by scikit-learn.

Initialize metric.

Parameters:

Name Type Description Default
features list[str] | None

The features to calculate the metric for. If None, the metric uses all features in the data.

None

ClassificationReport

ClassificationReport(features: list[str] | None = None)

Bases: SelectMixin, LazyMixin, Metric

Build a text report showing the main classification metrics.

As defined by scikit-learn.

Initialize metric.

Parameters:

Name Type Description Default
features list[str] | None

The features to calculate the metric for. If None, the metric uses all features in the data.

None

FBetaScore

FBetaScore(*, beta: float = 1.0, features: list[str] | None = None)

Bases: SelectMixin, LazyMixin, Metric

F-beta score.

As defined by scikit-learn.

Initialize metric.

Parameters:

Name Type Description Default
beta float

The beta parameter.

1.0
features list[str] | None

The features to calculate the metric for. If None, the metric uses all features in the data.

None
Attributes
beta instance-attribute
beta = beta

MaxError

MaxError(feature: str | None = None)

Bases: SelectMixin, LazyMixin, Metric

Max error regression loss.

As defined by scikit-learn.

Initialize MaxError metric.

Parameters:

Name Type Description Default
feature str | None

The feature to calculate the metric for. If None, the metric expects a single feature in the data.

None

MeanAbsoluteError

MeanAbsoluteError(features: list[str] | None = None, multioutput: Literal['raw_values', 'uniform_average'] = 'raw_values')

Bases: SelectMixin, LazyMixin, MultiOutputMixin, Metric

Mean absolute error (MAE) regression loss.

As defined by scikit-learn.

Initialize metric.

Parameters:

Name Type Description Default
features list[str] | None

The features to calculate the metric for. If None, the metric uses all features in the data.

None
multioutput Literal['raw_values', 'uniform_average']

Defines how to aggregate multiple output values. See scikit-learn documentation for details.

'raw_values'

MeanAbsolutePercentageError

MeanAbsolutePercentageError(features: list[str] | None = None, multioutput: Literal['raw_values', 'uniform_average'] = 'raw_values')

Bases: SelectMixin, LazyMixin, MultiOutputMixin, Metric

Mean absolute percentage error (MAPE) regression loss.

As defined by scikit-learn.

Initialize metric.

Parameters:

Name Type Description Default
features list[str] | None

The features to calculate the metric for. If None, the metric uses all features in the data.

None
multioutput Literal['raw_values', 'uniform_average']

Defines how to aggregate multiple output values. See scikit-learn documentation for details.

'raw_values'

MeanSquaredError

MeanSquaredError(features: list[str] | None = None, multioutput: Literal['raw_values', 'uniform_average'] = 'raw_values')

Bases: SelectMixin, LazyMixin, MultiOutputMixin, Metric

Mean squared error (MSE) regression loss.

As defined by scikit-learn.

Initialize metric.

Parameters:

Name Type Description Default
features list[str] | None

The features to calculate the metric for. If None, the metric uses all features in the data.

None
multioutput Literal['raw_values', 'uniform_average']

Defines how to aggregate multiple output values. See scikit-learn documentation for details.

'raw_values'

PrecisionScore

PrecisionScore(features: list[str] | None = None)

Bases: SelectMixin, LazyMixin, Metric

Precision classification score.

As defined by scikit-learn.

Initialize metric.

Parameters:

Name Type Description Default
features list[str] | None

The features to calculate the metric for. If None, the metric uses all features in the data.

None

R2Score

R2Score(features: list[str] | None = None, multioutput: Literal['raw_values', 'uniform_average'] = 'raw_values')

Bases: SelectMixin, LazyMixin, MultiOutputMixin, Metric

R^2 (coefficient of determination) regression score.

As defined by scikit-learn.

Initialize metric.

Parameters:

Name Type Description Default
features list[str] | None

The features to calculate the metric for. If None, the metric uses all features in the data.

None
multioutput Literal['raw_values', 'uniform_average']

Defines how to aggregate multiple output values. See scikit-learn documentation for details.

'raw_values'

Recall

Recall(features: list[str] | None = None)

Bases: SelectMixin, LazyMixin, Metric

Recall classification score.

As defined by scikit-learn.

Initialize metric.

Parameters:

Name Type Description Default
features list[str] | None

The features to calculate the metric for. If None, the metric uses all features in the data.

None

SciKitClassifierModel

SciKitClassifierModel(estimator: SupportsPredict, *, output_names: Iterable[str], threshold: float = 0.5, name: str | None = None)

Bases: SciKitModel

A SciKit model for classifiers with probability predictions.

Supports threshold-based predictions via the threshold attribute and exposes class probabilities via predict_proba. The estimator must implement predict_proba.

Initialize the classifier model.

Parameters:

Name Type Description Default
estimator SupportsPredict

The scikit-learn classifier (must support predict_proba).

required
output_names Iterable[str]

The names of the output columns.

required
threshold float

Decision threshold for the positive class (default: 0.5).

0.5
name str | None

The name of the model.

None
Attributes
threshold instance-attribute
threshold: float = threshold
Methods:
predict_proba
predict_proba(input_features: DataFrame | LazyFrame) -> LazyFrame

Predict class probabilities, applying preprocessing transforms.

Parameters:

Name Type Description Default
input_features DataFrame | LazyFrame

The inputs for which to predict probabilities.

required

Returns:

Type Description
LazyFrame

The predicted probabilities for the positive class.

SciKitModel

SciKitModel(estimator: SupportsPredict, *, output_names: Iterable[str], name: str | None = None)

Bases: Model

A model that wraps a scikit-learn estimator.

Initialize the model.

Parameters:

Name Type Description Default
estimator SupportsPredict

The scikit-learn estimator.

required
output_names Iterable[str]

The names of the output columns.

required
name str | None

The name of the model.

None
Attributes
estimator instance-attribute
estimator: SupportsPredict = estimator
output_names instance-attribute
output_names: list[str] = list(output_names)

RandomForestRegressorLearner

RandomForestRegressorLearner(n_estimators: int = 100, *, criterion: str = 'squared_error', max_depth: int | None = None, min_samples_split: int = 2, min_samples_leaf: int = 1, min_weight_fraction_leaf: float = 0.0, max_features: float = 1.0, max_leaf_nodes: int | None = None, min_impurity_decrease: float = 0.0, bootstrap: bool = True, oob_score: bool = False, n_jobs: int | None = None, random_state: int | None = None, verbose: int = 0, warm_start: bool = False, ccp_alpha: float = 0.0, max_samples: int | float | None = None, monotonic_cst: NDArray | None = None, callbacks: list[LearnerCallback] | LearnerCallback | None = None)

Bases: SupervisedLearner

Wrapper class for sklearn's RandomForestRegressor.

Reference: https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestRegressor.html

Initialize the random forest learner.

Reference: https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestRegressor.html

Parameters:

Name Type Description Default
n_estimators int

Number of trees in the forest.

100
criterion str

Function to measure the quality of a split.

'squared_error'
max_depth int | None

Maximum depth of the tree.

None
min_samples_split int

Minimum number of samples required to split an internal node.

2
min_samples_leaf int

Minimum number of samples required to be at a leaf node.

1
min_weight_fraction_leaf float

Minimum weighted fraction of the sum total of weights required to be at a leaf node.

0.0
max_features float

Number of features to consider when looking for the best split.

1.0
max_leaf_nodes int | None

Grow trees with max_leaf_nodes in best-first fashion.

None
min_impurity_decrease float

A node will be split if this split induces a decrease of the impurity greater than or equal to this value.

0.0
bootstrap bool

Whether bootstrap samples are used when building trees.

True
oob_score bool

Whether to use out-of-bag samples to estimate the R^2 on unseen data.

False
n_jobs int | None

Number of jobs to run in parallel.

None
random_state int | None

Controls the randomness of the estimator.

None
verbose int

Controls the verbosity when fitting and predicting.

0
warm_start bool

When set to True, reuse the solution of the previous call to fit.

False
ccp_alpha float

Complexity parameter used for Minimal Cost-Complexity Pruning.

0.0
max_samples int | float | None

If bootstrap is True, the number of samples to draw from X to train each base estimator.

None
monotonic_cst NDArray | None

Monotonicity constraints.

None
callbacks list[LearnerCallback] | LearnerCallback | None

Optional callbacks for progress feedback. Use None for silent learning.

None
Attributes
regressor instance-attribute
regressor: RandomForestRegressor = RandomForestRegressor(n_estimators=n_estimators, criterion=criterion, max_depth=max_depth, min_samples_split=min_samples_split, min_samples_leaf=min_samples_leaf, min_weight_fraction_leaf=min_weight_fraction_leaf, max_features=max_features, max_leaf_nodes=max_leaf_nodes, min_impurity_decrease=min_impurity_decrease, bootstrap=bootstrap, oob_score=oob_score, n_jobs=n_jobs, random_state=random_state or get_seed(), verbose=verbose, warm_start=warm_start, ccp_alpha=ccp_alpha, max_samples=max_samples, monotonic_cst=monotonic_cst)
callback_manager instance-attribute
callback_manager = create_callback_manager(callbacks)
Methods:
learn
learn(inputs: LazyFrame, outputs: LazyFrame) -> Model

Fit the random forest regressor on the given inputs and outputs.

RegressionTree

RegressionTree(*, dot_graph_export_path: str | None = None, criterion: str = 'squared_error', splitter: str = 'best', max_depth: int | None = None, min_samples_split: int = 2, min_samples_leaf: int = 1, min_weight_fraction_leaf: float = 0.0, max_features: float | None = None, random_state: int | None = None, max_leaf_nodes: int | None = None, min_impurity_decrease: float = 0.0, ccp_alpha: float = 0.0, monotonic_cst: NDArray | None = None, callbacks: list[LearnerCallback] | LearnerCallback | None = None)

Bases: SupervisedLearner

Wrapper class for sklearn's DecisionTreeRegressor.

Reference: https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeRegressor.html

Initialize the regression tree learner.

Reference: https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeRegressor.html

Parameters:

Name Type Description Default
dot_graph_export_path str | None

Path to export the decision tree graph in Graphviz DOT format.

None
criterion str

Function to measure the quality of a split.

'squared_error'
splitter str

Strategy used to choose the split at each node.

'best'
max_depth int | None

Maximum depth of the tree.

None
min_samples_split int

Minimum number of samples required to split an internal node.

2
min_samples_leaf int

Minimum number of samples required to be at a leaf node.

1
min_weight_fraction_leaf float

Minimum weighted fraction of the sum total of weights required to be at a leaf node.

0.0
max_features float | None

Number of features to consider when looking for the best split.

None
random_state int | None

Controls the randomness of the estimator.

None
max_leaf_nodes int | None

Grow a tree with max_leaf_nodes in best-first fashion.

None
min_impurity_decrease float

A node will be split if this split induces a decrease of the impurity greater than or equal to this value.

0.0
ccp_alpha float

Complexity parameter used for Minimal Cost-Complexity Pruning.

0.0
monotonic_cst NDArray | None

Monotonicity constraints.

None
callbacks list[LearnerCallback] | LearnerCallback | None

Optional callbacks for progress feedback. Use None for silent learning.

None
Attributes
regressor instance-attribute
regressor: DecisionTreeRegressor = DecisionTreeRegressor(criterion=criterion, splitter=splitter, max_depth=max_depth, min_samples_split=min_samples_split, min_samples_leaf=min_samples_leaf, min_weight_fraction_leaf=min_weight_fraction_leaf, max_features=max_features, max_leaf_nodes=max_leaf_nodes, min_impurity_decrease=min_impurity_decrease, random_state=random_state or get_seed(), ccp_alpha=ccp_alpha, monotonic_cst=monotonic_cst)
dot_graph_export_path instance-attribute
dot_graph_export_path = dot_graph_export_path
callback_manager instance-attribute
callback_manager = create_callback_manager(callbacks)
Methods:
learn
learn(inputs: LazyFrame, outputs: LazyFrame) -> Model

Metrics

metrics

Classification and regression metrics backed by scikit-learn.

Classes

MultiOutputMixin

MultiOutputMixin(multioutput: Literal['raw_values', 'uniform_average', 'variance_weighted'] = 'raw_values', **kwargs: Any)

Mixin to handle sklearn-style multioutput regression metrics.

Attributes
multioutput instance-attribute
multioutput = multioutput

Accuracy

Accuracy(features: list[str] | None = None)

Bases: SelectMixin, LazyMixin, Metric

Accuracy classification score.

As defined by scikit-learn.

Initialize metric.

Parameters:

Name Type Description Default
features list[str] | None

The features to calculate the metric for. If None, the metric uses all features in the data.

None

ClassificationReport

ClassificationReport(features: list[str] | None = None)

Bases: SelectMixin, LazyMixin, Metric

Build a text report showing the main classification metrics.

As defined by scikit-learn.

Initialize metric.

Parameters:

Name Type Description Default
features list[str] | None

The features to calculate the metric for. If None, the metric uses all features in the data.

None

FBetaScore

FBetaScore(*, beta: float = 1.0, features: list[str] | None = None)

Bases: SelectMixin, LazyMixin, Metric

F-beta score.

As defined by scikit-learn.

Initialize metric.

Parameters:

Name Type Description Default
beta float

The beta parameter.

1.0
features list[str] | None

The features to calculate the metric for. If None, the metric uses all features in the data.

None
Attributes
beta instance-attribute
beta = beta

PrecisionScore

PrecisionScore(features: list[str] | None = None)

Bases: SelectMixin, LazyMixin, Metric

Precision classification score.

As defined by scikit-learn.

Initialize metric.

Parameters:

Name Type Description Default
features list[str] | None

The features to calculate the metric for. If None, the metric uses all features in the data.

None

Recall

Recall(features: list[str] | None = None)

Bases: SelectMixin, LazyMixin, Metric

Recall classification score.

As defined by scikit-learn.

Initialize metric.

Parameters:

Name Type Description Default
features list[str] | None

The features to calculate the metric for. If None, the metric uses all features in the data.

None

MaxError

MaxError(feature: str | None = None)

Bases: SelectMixin, LazyMixin, Metric

Max error regression loss.

As defined by scikit-learn.

Initialize MaxError metric.

Parameters:

Name Type Description Default
feature str | None

The feature to calculate the metric for. If None, the metric expects a single feature in the data.

None

MeanAbsoluteError

MeanAbsoluteError(features: list[str] | None = None, multioutput: Literal['raw_values', 'uniform_average'] = 'raw_values')

Bases: SelectMixin, LazyMixin, MultiOutputMixin, Metric

Mean absolute error (MAE) regression loss.

As defined by scikit-learn.

Initialize metric.

Parameters:

Name Type Description Default
features list[str] | None

The features to calculate the metric for. If None, the metric uses all features in the data.

None
multioutput Literal['raw_values', 'uniform_average']

Defines how to aggregate multiple output values. See scikit-learn documentation for details.

'raw_values'

MeanAbsolutePercentageError

MeanAbsolutePercentageError(features: list[str] | None = None, multioutput: Literal['raw_values', 'uniform_average'] = 'raw_values')

Bases: SelectMixin, LazyMixin, MultiOutputMixin, Metric

Mean absolute percentage error (MAPE) regression loss.

As defined by scikit-learn.

Initialize metric.

Parameters:

Name Type Description Default
features list[str] | None

The features to calculate the metric for. If None, the metric uses all features in the data.

None
multioutput Literal['raw_values', 'uniform_average']

Defines how to aggregate multiple output values. See scikit-learn documentation for details.

'raw_values'

MeanSquaredError

MeanSquaredError(features: list[str] | None = None, multioutput: Literal['raw_values', 'uniform_average'] = 'raw_values')

Bases: SelectMixin, LazyMixin, MultiOutputMixin, Metric

Mean squared error (MSE) regression loss.

As defined by scikit-learn.

Initialize metric.

Parameters:

Name Type Description Default
features list[str] | None

The features to calculate the metric for. If None, the metric uses all features in the data.

None
multioutput Literal['raw_values', 'uniform_average']

Defines how to aggregate multiple output values. See scikit-learn documentation for details.

'raw_values'

R2Score

R2Score(features: list[str] | None = None, multioutput: Literal['raw_values', 'uniform_average'] = 'raw_values')

Bases: SelectMixin, LazyMixin, MultiOutputMixin, Metric

R^2 (coefficient of determination) regression score.

As defined by scikit-learn.

Initialize metric.

Parameters:

Name Type Description Default
features list[str] | None

The features to calculate the metric for. If None, the metric uses all features in the data.

None
multioutput Literal['raw_values', 'uniform_average']

Defines how to aggregate multiple output values. See scikit-learn documentation for details.

'raw_values'