Advertisement
beegie_b

LinearMAE

Nov 11th, 2013
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.16 KB | None | 0 0
  1. __author__ = 'Miroslaw Horbal'
  2. __email__ = 'miroslaw@gmail.com'
  3. __date__ = '2013-03-09'
  4.  
  5. import numpy as np
  6. from scipy import optimize
  7.  
  8. OPTIMIZATION_FUNCTIONS = { 'cg':   optimize.fmin_cg,
  9.                            'bfgs': optimize.fmin_bfgs }
  10.    
  11. class LinearMAE(object):
  12.     """Linear model with Mean Absolute Error"""
  13.     def __init__(self, l1=0.0, l2=0.0, opt='bfgs', maxiter=1000,
  14.                  tol=1e-4, verbose=False):
  15.         """
  16.        Parameters:
  17.          l1 - magnitude of l1 penalty (default 0.0)
  18.          l2 - magnitude of l2 penalty (default 0.0)
  19.          opt - optimization algorithm to use for gardient decent
  20.                options are 'cg', 'bfgs' (default 'bfgs')
  21.          maxiter - maximum number of iterations (default 1000)
  22.          tol - terminate optimization if gradient l2 is smaller than tol (default 1e-4)
  23.          verbose - display convergence information at each iteration (default False)
  24.        """
  25.         self.opt = opt
  26.         self.maxiter = maxiter
  27.         self.tol = tol
  28.         self.l1 = l1
  29.         self.l2 = l2
  30.         self.verbose = verbose
  31.    
  32.     @property
  33.     def opt(self):
  34.         """Optimization algorithm to use for gradient decent"""
  35.         return self._opt
  36.    
  37.     @opt.setter
  38.     def opt(self, o):
  39.         """
  40.        Set the optimization algorithm for gradient decent
  41.        
  42.        Parameters:
  43.          o - 'cg' for conjugate gradient decent
  44.              'bfgs' for BFGS algorithm
  45.        """
  46.         if o not in OPTIMIZATION_FUNCTIONS:
  47.             raise Error('Unknown optimization routine %s' % o)
  48.         self._opt = o
  49.         self._optimize = OPTIMIZATION_FUNCTIONS[o]
  50.    
  51.     def score(self, X, y):
  52.         """
  53.        Compute the MAE of the linear model prediction on X against y
  54.        
  55.        Must only be run after calling fit
  56.        
  57.        Parameters:
  58.          X - data array for the linear model. Has shape (m,n)
  59.          y - output target array for the linear model. Has shape (m,o)
  60.        """
  61.         y = _2d(y)
  62.         pred = self.predict(X)
  63.         return np.mean(np.abs(pred - y))
  64.    
  65.     def predict(self, X):
  66.         """
  67.        Compute the linear model prediction on X
  68.        
  69.        Must only be run after calling fit
  70.        
  71.        Parameters:
  72.          X - data array for the linear model. Has shape (m,n)
  73.        """
  74.         return X.dot(self.coef_[1:]) + self.coef_[0]
  75.    
  76.     def fit(self, X, y, coef=None):
  77.         """
  78.        Fit the linear model using gradient decent methods
  79.        
  80.        Parameters:
  81.          X - data array for the linear model. Has shape (m,n)
  82.          y - output target array for the linear model. Has shape (m,o)
  83.          coef - None or array of size (n+1) * o
  84.        
  85.        Sets attributes:
  86.          coef_ - the weights of the linear model
  87.        """
  88.         y = _2d(y)
  89.         m, n = X.shape
  90.         m, o = y.shape
  91.         if coef is None:
  92.             coef = np.zeros((n+1, o))
  93.         elif coef.shape != (n+1, o):
  94.             raise Error('coef must be None or be shape %s' % (str((n+1, o))))
  95.         self._coef_shape = coef.shape
  96.         coef = self._optimize(f=cost,
  97.                               x0=coef.flatten(),
  98.                               fprime=grad,
  99.                               args=(X, y, self.l1, self.l2),
  100.                               gtol=self.tol,
  101.                               maxiter=self.maxiter,
  102.                               disp=0,
  103.                               callback=self._callback(X,y))
  104.         self.coef_ = np.reshape(coef, self._coef_shape)
  105.         return self
  106.  
  107.     def _callback(self, X, y):
  108.         """
  109.        Helper method that generates a callback function for the optimization
  110.        algorithm opt if verbose is set to True
  111.        """
  112.         def callback(coef):
  113.             self.i += 1
  114.             self.coef_ = np.reshape(coef, self._coef_shape)
  115.             score = self.score(X, y)
  116.             print 'iter %i | Score: %f\r' % (self.i, score)
  117.         self.i = 0
  118.         return callback if self.verbose else None
  119.  
  120. def _2d(a):
  121.     """Returns a 2d array of a if rank a <= 2"""
  122.     if len(a.shape) == 1:
  123.         a.shape = (len(a), 1)
  124.     return a
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement