Logistic regression using SciPy

前端 未结 3 1487
伪装坚强ぢ
伪装坚强ぢ 2021-01-02 12:54

I am trying to code up logistic regression in Python using the SciPy fmin_bfgs function, but am running into some issues. I wrote functions for the logistic (si

相关标签:
3条回答
  • 2021-01-02 13:06

    Here is the answer I sent back to the SciPy list where this question was cross-posted. Thanks to @tiago for his answer. Basically, I reparametrized the likelihood function. Also, added a call to the check_grad function.

    #=====================================================
    # purpose: logistic regression 
    import numpy as np
    import scipy as sp
    import scipy.optimize
    
    import matplotlib as mpl
    import os
    
    # prepare the data
    data = np.loadtxt('data.csv', delimiter=',', skiprows=1)
    vY = data[:, 0]
    mX = data[:, 1:]
    # mX = (mX - np.mean(mX))/np.std(mX)  # standardize the data; if required
    
    intercept = np.ones(mX.shape[0]).reshape(mX.shape[0], 1)
    mX = np.concatenate((intercept, mX), axis = 1)
    iK = mX.shape[1]
    iN = mX.shape[0]
    
    # logistic transformation
    def logit(mX, vBeta):
        return((np.exp(np.dot(mX, vBeta))/(1.0 + np.exp(np.dot(mX, vBeta)))))
    
    # test function call
    vBeta0 = np.array([-.10296645, -.0332327, -.01209484, .44626211, .92554137, .53973828, 
        1.7993371, .7148045  ])
    logit(mX, vBeta0)
    
    # cost function
    def logLikelihoodLogit(vBeta, mX, vY):
        return(-(np.sum(vY*np.log(logit(mX, vBeta)) + (1-vY)*(np.log(1-logit(mX, vBeta))))))
    logLikelihoodLogit(vBeta0, mX, vY) # test function call
    
    # different parametrization of the cost function
    def logLikelihoodLogitVerbose(vBeta, mX, vY):
        return(-(np.sum(vY*(np.dot(mX, vBeta) - np.log((1.0 + np.exp(np.dot(mX, vBeta))))) +
                        (1-vY)*(-np.log((1.0 + np.exp(np.dot(mX, vBeta))))))))
    logLikelihoodLogitVerbose(vBeta0, mX, vY)  # test function call
    
    # gradient function
    def likelihoodScore(vBeta, mX, vY):
        return(np.dot(mX.T, 
                      (logit(mX, vBeta) - vY)))
    likelihoodScore(vBeta0, mX, vY).shape # test function call
    sp.optimize.check_grad(logLikelihoodLogitVerbose, likelihoodScore, 
                           vBeta0, mX, vY)  # check that the analytical gradient is close to 
                                                    # numerical gradient
    
    # optimize the function (without gradient)
    optimLogit = scipy.optimize.fmin_bfgs(logLikelihoodLogitVerbose, 
                                      x0 = np.array([-.1, -.03, -.01, .44, .92, .53,
                                                1.8, .71]), 
                                      args = (mX, vY), gtol = 1e-3)
    
    # optimize the function (with gradient)
    optimLogit = scipy.optimize.fmin_bfgs(logLikelihoodLogitVerbose, 
                                      x0 = np.array([-.1, -.03, -.01, .44, .92, .53,
                                                1.8, .71]), fprime = likelihoodScore, 
                                      args = (mX, vY), gtol = 1e-3)
    #=====================================================
    
    0 讨论(0)
  • 2021-01-02 13:16

    I was facing the same issues. When I experimented with different algorithms implementation in scipy.optimize.minimize , I found that for finding optimal logistic regression parameters for my data set , Newton Conjugate Gradient proved helpful. Call can be made to it like:

    Result = scipy.optimize.minimize(fun = logLikelihoodLogit, 
                                     x0 = np.array([-.1, -.03, -.01, .44, .92, .53,1.8, .71]), 
                                     args = (mX, vY),
                                     method = 'TNC',
                                     jac = likelihoodScore);
    optimLogit = Result.x;
    
    0 讨论(0)
  • 2021-01-02 13:19

    Your problem is that the function you are trying to minimise, logLikelihoodLogit, will return NaN with values very close to your initial estimate. And it will also try to evaluate negative logarithms and encounter other problems. fmin_bfgs doesn't know about this, will try to evaluate the function for such values and run into trouble.

    I suggest using a bounded optimisation instead. You can use scipy's optimize.fmin_l_bfgs_b for this. It uses a similar algorithm to fmin_bfgs, but it supports bounds in the parameter space. You call it similarly, just add a bounds keyword. Here's a simple example on how you'd call fmin_l_bfgs_b:

    from scipy.optimize import fmin_bfgs, fmin_l_bfgs_b
    
    # list of bounds: each item is a tuple with the (lower, upper) bounds
    bd = [(0, 1.), ...] 
    
    test = fmin_l_bfgs_b(logLikelihoodLogit, x0=x0, args=(mX, vY), bounds=bd,
                          approx_grad=True)
    

    Here I'm using an approximate gradient (seemed to work fine with your data), but you can pass fprime as in your example (I don't have time to check its correctness). You'll know your parameter space better than me, just make sure to build the bounds array for all the meaningful values that your parameters can take.

    0 讨论(0)
提交回复
热议问题