<s> import numpy <EOL> import math <EOL> from Solver import Solver , ReturnValue <EOL> class FullRankMajorizationSolver ( Solver ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( <EOL> self , X , y , <EOL> classZeroLabel , <EOL> regularizer , <EOL> tol , <EOL> maxIters , <EOL> splitLimit = <NUM_LIT> ) : <EOL> fSum = FullRankMajorizationSolver . computeFSum ( <EOL> X , y , classZeroLabel ) <EOL> self . X = X <EOL> self . regularizer = float ( regularizer ) <EOL> self . tol = tol <EOL> self . maxIters = maxIters <EOL> self . classZeroLabel = classZeroLabel <EOL> self . fSum = fSum <EOL> self . nFeatures = X . shape [ <NUM_LIT:1> ] <EOL> self . nSamples = X . shape [ <NUM_LIT:0> ] <EOL> self . splitLimit = splitLimit <EOL> @ staticmethod <EOL> def computeFSum ( X , y , classZeroLabel ) : <EOL> nRows = X . shape [ <NUM_LIT:0> ] <EOL> def columnSum ( column ) : <EOL> def f ( rowIx ) : <EOL> if y [ rowIx ] == classZeroLabel : <EOL> return float ( column [ rowIx ] ) <EOL> return <NUM_LIT:0.0> <EOL> return sum ( f ( rowIx ) for rowIx in xrange ( nRows ) ) <EOL> return numpy . array ( <EOL> [ columnSum ( column ) for column in X . columns ( ) ] <EOL> ) <EOL> def solve ( self , theta = None ) : <EOL> if theta is None : <EOL> oldTheta = numpy . zeros ( self . nFeatures ) <EOL> else : <EOL> oldTheta = theta <EOL> newTheta = self . updateTheta ( oldTheta ) <EOL> iters = <NUM_LIT:1> <EOL> while iters < self . maxIters and sum ( ( newTheta - oldTheta ) ** <NUM_LIT:2> ) > self . tol * self . tol : <EOL> oldTheta = newTheta <EOL> newTheta = self . updateTheta ( oldTheta ) <EOL> iters = iters + <NUM_LIT:1> <EOL> return ReturnValue ( newTheta , iters ) <EOL> def updateTheta ( self , theta ) : <EOL> thetaDotX = self . X . dot ( theta , self . splitLimit ) <EOL> sigma = self . computeSigma ( thetaDotX ) <EOL> muSum = self . computeMuSum ( thetaDotX ) <EOL> A = sigma + _diagonal ( <EOL> sigma . shape [ <NUM_LIT:0> ] , self . X . shape [ <NUM_LIT:0> ] * self . regularizer ) <EOL> b = muSum - self . fSum + theta * ( self . regularizer * self . X . shape [ <NUM_LIT:0> ] ) <EOL> return theta - numpy . linalg . solve ( A , b ) <EOL> def computeSigma ( self , thetaDotX , start = <NUM_LIT:0> , end = None ) : <EOL> return _computeSigma ( <EOL> thetaDotX , self . X . _columns , self . splitLimit , start , end ) <EOL> def computeMuSum ( self , thetaDotX ) : <EOL> def rowWiseScalarMultiplier ( ix ) : <EOL> return <NUM_LIT:1.0> / ( <NUM_LIT:1.0> + math . exp ( - thetaDotX [ ix ] ) ) <EOL> columns = self . X . columns ( ) <EOL> def computeWeightedColumnSum ( columnIx ) : <EOL> column = columns [ columnIx ] <EOL> return sum ( <EOL> rowWiseScalarMultiplier ( ix ) * column [ ix ] for ix in xrange ( self . X . shape [ <NUM_LIT:0> ] ) <EOL> ) <EOL> return numpy . array ( [ <EOL> computeWeightedColumnSum ( columnIx ) for columnIx in xrange ( self . X . shape [ <NUM_LIT:1> ] ) <EOL> ] ) <EOL> def _computeSigma ( thetaDotX , columns , splitLimit , start = <NUM_LIT:0> , end = None ) : <EOL> if end is None : <EOL> end = len ( thetaDotX ) <EOL> if ( end - start ) < splitLimit : <EOL> numColumns = len ( columns ) <EOL> def unsymmetrizedElementAt ( row , col ) : <EOL> if col < row : <EOL> return <NUM_LIT:0.0> <EOL> right = columns [ col ] [ start : end ] <EOL> left = columns [ row ] [ start : end ] <EOL> weights = thetaDotX [ start : end ] <EOL> return _weightedDotProduct ( right , left , weights ) <EOL> unsymmetrizedValues = [ <EOL> [ unsymmetrizedElementAt ( row , col ) for col in xrange ( numColumns ) ] for row in xrange ( numColumns ) <EOL> ] <EOL> symmetrizedValues = [ ] <EOL> for row in xrange ( numColumns ) : <EOL> for col in xrange ( numColumns ) : <EOL> if row <= col : <EOL> elt = unsymmetrizedValues [ row ] [ col ] <EOL> else : <EOL> elt = unsymmetrizedValues [ col ] [ row ] <EOL> symmetrizedValues = symmetrizedValues + [ elt ] <EOL> tr = numpy . array ( symmetrizedValues ) <EOL> return tr . reshape ( ( numColumns , numColumns ) ) <EOL> mid = ( start + end ) / <NUM_LIT:2> <EOL> return _computeSigma ( thetaDotX , columns , splitLimit , start , mid ) + _computeSigma ( thetaDotX , columns , splitLimit , mid , end ) <EOL> def _weightedDotProduct ( x1 , x2 , x3 ) : <EOL> res = <NUM_LIT:0.0> <EOL> ix = <NUM_LIT:0> <EOL> top = len ( x1 ) <EOL> while ix < top : <EOL> res = res + x1 [ ix ] * x2 [ ix ] * _weightFun ( x3 [ ix ] ) <EOL> ix = ix + <NUM_LIT:1> <EOL> return res <EOL> def _weightFun ( x ) : <EOL> exp_x = math . exp ( x ) <EOL> if abs ( x ) < <NUM_LIT> : <EOL> return ( <NUM_LIT:0.5> - <NUM_LIT> * x ) / ( exp_x + <NUM_LIT:1.0> ) <EOL> return ( exp_x - <NUM_LIT:1.0> ) / ( exp_x + <NUM_LIT:1.0> ) / ( <NUM_LIT> * x ) <EOL> def _diagonal ( n , value ) : <EOL> value = float ( value ) <EOL> def eltAtIx ( ix ) : <EOL> if ix % n == ix / n : <EOL> return value <EOL> else : <EOL> return <NUM_LIT:0.0> <EOL> vals = [ eltAtIx ( ix ) for ix in xrange ( n * n ) ] <EOL> tr = numpy . array ( vals ) <EOL> tr = tr . reshape ( ( n , n ) ) <EOL> return tr </s>
