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
LinearRegression
¶
Bases: SupervisedLearner
Methods:¶
Accuracy
¶
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
¶
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
¶
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
|
MaxError
¶
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
¶
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
¶
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
|
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¶
Methods:¶
predict_proba
¶
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
¶
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
|
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
|
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)
Methods:¶
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
|
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)
Methods:¶
Metrics¶
metrics
¶
Classification and regression metrics backed by scikit-learn.
Classes¶
MultiOutputMixin
¶
Accuracy
¶
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
¶
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
¶
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
|
PrecisionScore
¶
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
¶
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
¶
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'
|