bbalasub1 / glmnet_python

GNU General Public License v3.0
199 stars 94 forks source link

ValueError: operands could not be broadcast together with shapes (28,2) (1,56) using `weights` #24

Open Visdoom opened 6 years ago

Visdoom commented 6 years ago

Hey there,

I tried using weights on the samples. However, I run into this error:


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-62-c9638c455afe> in <module>()
     23 
     24     cvfit = cvglmnet(x = X_train.copy(), y = y_train.copy().astype(np.float64),ptype='deviance', nfolds=5,
---> 25                  family = 'binomial', alpha=0.8, weights=weights.copy())
     26 
     27     y_hat =  cvglmnetPredict(cvfit, newx = X_test, s='lambda_min', ptype='class').reshape(-1)

/usr/local/lib/python3.5/dist-packages/glmnet_python/glmnet_python/cvglmnet.py in cvglmnet(x, y, family, ptype, nfolds, foldid, parallel, keep, grouped, **options)
    241 
    242     # main call to glmnet
--> 243     glmfit = glmnet(x = x, y = y, family = family, **options)
    244 
    245     is_offset = glmfit['offset']

/usr/local/lib/python3.5/dist-packages/glmnet_python/glmnet_python/glmnet.py in glmnet(x, y, family, **options)
    453         fit = lognet(x, is_sparse, irs, pcs, y, weights, offset, parm,
    454                      nobs, nvars, jd, vp, cl, ne, nx, nlam, flmin, ulam,
--> 455                      thresh, isd, intr, maxit, kopt, family)
    456     elif family == 'cox':
    457         # call coxnet

/usr/local/lib/python3.5/dist-packages/glmnet_python/glmnet_python/lognet.py in lognet(x, is_sparse, irs, pcs, y, weights, offset, parm, nobs, nvars, jd, vp, cl, ne, nx, nlam, flmin, ulam, thresh, isd, intr, maxit, kopt, family)
     58             mv, ny = y.shape
     59 
---> 60         y = y*scipy.tile(weights, (1, ny))
     61 
     62     #

ValueError: operands could not be broadcast together with shapes (28,2) (1,56) 

seems sth is odd with unpacking the shape of y

Visdoom commented 6 years ago

I realized it is the same issue as #19

JoeyFaulkner commented 5 years ago

Also having this issue, any info on how to fix this?

DexGroves commented 3 years ago

Try doing: weights = weights.reshape((len(weights), 1)). It expects weights to have shape (N, 1) (a matrix with one column) rather than shape (N,) (a vector).