简体   繁体   中英

CVXPY Import Error - 'sum_entries' is not defined

I just install the latest version of cvxpy using pip install and am working my way through the examples provided at http://nbviewer.jupyter.org/github/cvxgrp/cvx_short_course/blob/master/applications/portfolio_optimization.ipynb The code does not work for me. When I run the following:

import numpy as np
np.random.seed(1)
n = 10
mu = np.abs(np.random.randn(n, 1))
Sigma = np.random.randn(n, n)
Sigma = Sigma.T.dot(Sigma)

# Long only portfolio optimization.
from cvxpy import *
w = Variable(n)
gamma = Parameter(nonneg=True)
ret = mu.T*w 
risk = quad_form(w, Sigma)
prob = Problem(Maximize(ret - gamma*risk), 
           [sum_entries(w) == 1, 
            w >= 0])

I get the following error:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-fde65e194bce> in <module>
     14 risk = quad_form(w, Sigma)
     15 prob = Problem(Maximize(ret - gamma*risk), 
---> 16                [sum_entries(w) == 1, 
     17                 w >= 0])

NameError: name 'sum_entries' is not defined

I have tried using eg, cvx.sum_entries as mentioned here , but it did not work.

Can anyone help?

sum_entries was renamed to sum in 1.0.

https://www.cvxpy.org/updates/index.html#numpy-compatibility

In [1]: from cvxpy import *                                                                           
In [2]: x = Variable(10)                                                                              
In [3]: sum(x)                                                                                        
Out[3]: Expression(AFFINE, UNKNOWN, ())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM