Skip to content

Instantly share code, notes, and snippets.

@conormm
Created April 10, 2021 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conormm/e1c688be69bb31a32ce2b28e029f175d to your computer and use it in GitHub Desktop.
Save conormm/e1c688be69bb31a32ce2b28e029f175d to your computer and use it in GitHub Desktop.
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