Last active
June 6, 2016 19:41
-
-
Save mehdidc/d162da18cef9b93c7bf4af51eece471c 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.ensemble import RandomForestRegressor | |
from sklearn.base import clone | |
import numpy as np | |
class GAM(object): | |
""" | |
Simple generalized additive model which fits each feature w.r.t | |
the output independently using a model specified by | |
base_estimator. the final predictions can be done by regressing linearly | |
the predictions of the model of each features by using a pipeline. | |
Example: | |
from sklearn.pipeline import make_pipeline | |
from sklearn.linear_model import LinearRegression | |
reg = make_pipeline(GAM(), LinearRegression()) | |
reg.fit(X, y) | |
""" | |
def __init__(self, base_estimator=RandomForestRegressor()): | |
self.base_estimator = base_estimator | |
self.estimators_ = None | |
self.agg = None | |
def fit(self, X, y=None): | |
self.estimators_ = [clone(self.base_estimator) | |
for i in range(X.shape[1])] | |
for i in range(X.shape[1]): | |
self.estimators_[i].fit(X[:, i:i+1], y) | |
return self | |
def transform(self, X): | |
y_pred = np.empty_like(X) | |
for i in range(X.shape[1]): | |
y_pred[:, i] = self.estimators_[i].predict(X[:, i:i+1]) | |
return y_pred | |
def predict(self, X): | |
return self.transform(X).mean(axis=1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment