Skip to content

Instantly share code, notes, and snippets.

@satomacoto
Created February 18, 2016 08:47
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 satomacoto/b70d2b8fd81c04017826 to your computer and use it in GitHub Desktop.
Save satomacoto/b70d2b8fd81c04017826 to your computer and use it in GitHub Desktop.
Pinv2Regressor
class Pinv2Regressor:
def __init__(self, estimators, cv=5):
self.estimators = estimators
self.cv = cv
def fit(self, X, y, verbose=False):
preds = {}
for k, estimator in self.estimators.items():
if verbose:
print(k, estimator)
preds[k] = cross_val_predict(estimator, X, y, cv=self.cv)
estimator.fit(X, y)
preds = np.vstack(preds.values()).T
self._weights = np.dot(pinv2(preds), y)
return self
def predict(self, X):
preds = {}
for k, estimator in self.estimators.items():
preds[k] = estimator.predict(X)
preds = np.vstack(preds.values()).T
return np.dot(preds, self._weights)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment