Skip to content

Instantly share code, notes, and snippets.

@koreyou
Created December 3, 2016 05:36
Show Gist options
  • Save koreyou/5f0e22cd6113beb8aed0a72626504a39 to your computer and use it in GitHub Desktop.
Save koreyou/5f0e22cd6113beb8aed0a72626504a39 to your computer and use it in GitHub Desktop.
# Written by Yuta Koreeda
# CC-BY
import numpy as np
class ExtremeLearningMachine(object):
def __init__(self, n_unit, activation=None):
self._activation = self._sig if activation is None else activation
self._n_unit = n_unit
@staticmethod
def _sig(x):
return 1. / (1 + np.exp(-x))
@staticmethod
def _add_bias(x):
return np.hstack((x, np.ones((x.shape[0], 1))))
def fit(self, X, y):
self.W0 = np.random.random((X.shape[1], self._n_unit))
z = self._add_bias(self._activation(X.dot(self.W0)))
self.W1 = np.linalg.lstsq(z, y)[0]
def transform(self, X):
if not hasattr(self, 'W0'):
raise UnboundLocalError('must fit before transform')
z = self._add_bias(self._activation(X.dot(self.W0)))
return z.dot(self.W1)
def fit_transform(self, X, y):
self.W0 = np.random.random((X.shape[1], self._n_unit))
z = self._add_bias(self._activation(X.dot(self.W0)))
self.W1 = np.linalg.lstsq(z, y)[0]
return z.dot(self.W1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment