Created
April 10, 2021 17:46
-
-
Save conormm/e1c688be69bb31a32ce2b28e029f175d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from sklearn.base import BaseEstimator, RegressorMixin | |
import statsmodels.api as sm | |
class QuantileRegression(BaseEstimator, RegressorMixin): | |
"""Sklearn wrapper for statsmodels Quantile Regression | |
""" | |
def __init__(self, quantile=0.5, **kwargs): | |
self.quantile = quantile | |
self.kwargs = kwargs | |
self.model = None | |
self.fitted = None | |
def fit(self, X, y=None): | |
X = sm.add_constant(X) | |
self.model = sm.QuantReg(endog=y, exog=X, **self.kwargs) | |
self.fitted = self.model.fit(q=self.quantile) | |
def predict(self, X, y=None): | |
X = sm.add_constant(X) | |
return self.fitted.predict(X) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment