Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugs When Using ptype='auc' #2

Open
unbreakablegao opened this issue Nov 27, 2018 · 0 comments
Open

Bugs When Using ptype='auc' #2

unbreakablegao opened this issue Nov 27, 2018 · 0 comments

Comments

@unbreakablegao
Copy link

There are several bugs in script glmnet_python / cvlognet.py when using ptype='auc' for binary classification.
Fix them by the following!

  1. Replace functions 'auc' and 'auc_mat' by the following:
#=========================    
# Helper functions
#=========================    
def auc_mat(y, prob, weights = None):
    if type(weights) == type(None) or len(weights) == 0:
        weights = scipy.ones([y.shape[0], 1])
    wweights = weights*y
    wweights = wweights.flatten('F')
    wweights = scipy.reshape(wweights, [wweights.size])
    ny= y.shape[0]
    a = scipy.zeros([ny])
    b = scipy.ones([ny])
    yy = scipy.hstack((a, b))
    pprob = scipy.hstack((prob,prob))
    result = auc(yy, pprob, wweights)
    return(result)
#=========================
def auc(y, prob, w):
    if len(w) == 0:
        mindiff = scipy.amin(scipy.diff(scipy.unique(prob)))
        pert = scipy.random.uniform(0, mindiff/3, prob.size)
        t, rprob = scipy.unique(prob + pert, return_inverse = True)
        n1 = scipy.sum(y, keepdims = True)
        n0 = y.shape[0] - n1
        u = scipy.sum(rprob[y == 1]) - n1*(n1 + 1)/2
        result = u/(n1*n0)
    else:
        op = scipy.argsort(prob)
        y = y[op]
        w = w[op]
        cw = scipy.cumsum(w)
        w1 = w[y == 1]
        cw1 = scipy.cumsum(w1)
        wauc = scipy.sum(w1*(cw[y == 1] - cw1))
        sumw = cw1[-1]
        sumw = sumw*(cw[-1] - sumw)
        result = wauc/sumw
    return(result)
#=========================    

  1. Replace line 112 to 116 by the following:
    if grouped == True and ptype != 'auc':
        cvob = cvcompute(cvraw, weights, foldid, nlams)
        cvraw = cvob['cvraw']
        weights = cvob['weights']
        N = cvob['N']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant