swectral.modelcombiners.RegressorToClassifier#

class swectral.modelcombiners.RegressorToClassifier(regressor, proba_func='softmax')[source]#

Wrap a sklearn-style regressor into a sklearn-style classifier using one-hot encoding and probability regression.

This class converts a regressor that predicts continuous outputs into a classifier by one-hot encoding the targets and applying a probability conversion function to the regressor outputs.

Attributes:
regressorobject

A regressor implementing fit(X, y) and predict(X).

proba_funcstr or Callable, optional

Function to convert raw regressor outputs to probabilities. Choose between:

  • 'softmax' : row-wise softmax for single-label, mutually exclusive classes

  • 'sigmoid' : per-class sigmoid for multi-label problems

  • Callable : a custom probability function.

Default is 'softmax'.

encoderOneHotEncoder

Encoder for transforming class labels to one-hot vectors.

classes_numpy.ndarray of shape (n_classes,) or None

Array of class labels seen during fitting.

regressors_list of object or None

Fitted regressors. Contains a single regressor if multi-output regression is supported, otherwise one regressor per class.

Methods

fit(X, y)

Fit the regressor(s) on one-hot encoded class targets.

predict_proba(X)

Predict class probabilities for each sample.

predict(X)

Predict class labels for each sample.

Examples

>>> import numpy as np
>>> from sklearn.linear_model import LinearRegression
>>> X = np.array([[1], [2], [3], [4]])
>>> y = np.array([0, 1, 0, 1])
>>> reg = LinearRegression()
>>> clf = RegressorToClassifier(regressor=reg, proba_func='softmax')
>>> clf.fit(X, y)
>>> clf.predict(X)
>>> clf.predict_proba(X)
__init__(regressor, proba_func='softmax')[source]#

Methods

__init__(regressor[, proba_func])

fit(X, y)

Fit the regressor on one-hot encoded targets.

predict(X)

Predict class labels.

predict_proba(X)

Predict class probabilities.

fit(X, y)[source]#

Fit the regressor on one-hot encoded targets.

Parameters:
Xarray_like of shape (n_samples, n_features)

Training input features.

yarray_like of shape (n_samples,)

Target class labels.

Returns:
RegressorToClassifier

Fitted instance.

Return type:

object

predict_proba(X)[source]#

Predict class probabilities.

Parameters:
Xarray_like of shape (n_samples, n_features)

Input features.

Returns:
numpy.ndarray of shape (n_samples, n_classes)

Predicted class probabilities.

Return type:

ndarray

predict(X)[source]#

Predict class labels.

Parameters:
Xarray_like of shape (n_samples, n_features)

Input features.

Returns:
labelsnumpy.ndarray of shape (n_samples,)

Predicted class labels.

Return type:

ndarray