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:
- regressor
object A regressor implementing
fit(X, y)andpredict(X).- proba_func
strorCallable,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 problemsCallable : a custom probability function.
Default is
'softmax'.- encoder
OneHotEncoder Encoder for transforming class labels to one-hot vectors.
- classes_
numpy.ndarrayofshape(n_classes,)orNone Array of class labels seen during fitting.
- regressors_
listofobjectorNone Fitted regressors. Contains a single regressor if multi-output regression is supported, otherwise one regressor per class.
- regressor
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)
Methods
__init__(regressor[, proba_func])fit(X, y)Fit the regressor on one-hot encoded targets.
predict(X)Predict class labels.
Predict class probabilities.
- fit(X, y)[source]#
Fit the regressor on one-hot encoded targets.
- Parameters:
- Xarray_like
ofshape(n_samples,n_features) Training input features.
- yarray_like
ofshape(n_samples,) Target class labels.
- Xarray_like
- Returns:
RegressorToClassifierFitted instance.
- Return type:
- predict_proba(X)[source]#
Predict class probabilities.
- Parameters:
- Xarray_like
ofshape(n_samples,n_features) Input features.
- Xarray_like
- Returns:
numpy.ndarrayofshape(n_samples,n_classes)Predicted class probabilities.
- Return type:
- predict(X)[source]#
Predict class labels.
- Parameters:
- Xarray_like
ofshape(n_samples,n_features) Input features.
- Xarray_like
- Returns:
- labels
numpy.ndarrayofshape(n_samples,) Predicted class labels.
- labels
- Return type: