-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: change to sklearn-compatible interface
- Loading branch information
Showing
4 changed files
with
76 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,7 @@ requires = [ | |
"setuptools>=42", | ||
"wheel", | ||
"pybind11>=2.6.0", | ||
"scipy", | ||
"sklearn", | ||
"numpy" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from .slope import slope | ||
from .estimators import Slope |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import _sortedl1 as sl1 | ||
import numpy as np | ||
from sklearn.base import BaseEstimator, RegressorMixin | ||
from sklearn.utils.validation import check_array, check_is_fitted, check_X_y | ||
|
||
|
||
class Slope(BaseEstimator, RegressorMixin): | ||
""" | ||
Sorted L-One Penalized Estimation | ||
Parameters | ||
---------- | ||
lam : array_like | ||
The lambda parameter vector for the Sorted L1 Penalty | ||
alph : array_like | ||
A multiplier for the Sorted L1 Penalty | ||
Attributes | ||
---------- | ||
coef_ : ndarray | ||
The estimated regression coefficients. | ||
""" | ||
|
||
def __init__(self, lam=None, alph=1.0): | ||
self.lam = lam | ||
self.alph = alph | ||
|
||
def fit(self, X, y): | ||
""" | ||
Fit the model according to the given training data. | ||
Parameters | ||
---------- | ||
X : array_like, shape (n_samples, n_features) | ||
Training vector, where n_samples is the number of samples and | ||
n_features is the number of features. | ||
y : array_like, shape (n_samples,) | ||
Target vector relative to X. | ||
Returns | ||
------- | ||
self : object | ||
""" | ||
X, y = check_X_y(X, y, accept_sparse=True, order="F", y_numeric=True) | ||
|
||
self.X_ = X | ||
self.y_ = y | ||
|
||
self.coef_ = sl1.fit_slope( | ||
self.X_, self.y_, np.array(self.lam), np.array(self.alph) | ||
) | ||
|
||
return self | ||
|
||
def predict(self, X): | ||
""" | ||
Predict using the linear model. | ||
Parameters | ||
---------- | ||
X : array_like, shape (n_samples, n_features) | ||
Samples. | ||
Returns | ||
------- | ||
C : array, shape (n_samples,) | ||
Returns predicted values. | ||
""" | ||
check_is_fitted(self) | ||
|
||
X = check_array(X, accept_sparse=True, order="F") | ||
|
||
return np.dot(X, self.coef_) |
This file was deleted.
Oops, something went wrong.