Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python deprecation warning about sum function

I coded an algorithm and it worked properly till 2 weeks ago. I get this warning and I cannot understand why I get it. The warning is:

"C:/Users/Administrator/Documents/Python/sezg_1_diffne.py:147: DeprecationWarning: Calling np.sum(generator) is deprecated, and in the future will give a different result. Use np.sum(np.from_iter(generator)) or the python sum builtin instead. obje_1=detmas.objVal+sum(hopen[i]*fixedCost for i in Fset)"

A part of my code is:

obje_1=detmas.objVal+sum(hopen[i]*fixedCost for i in Fset)

I tried something which I found in internet such as removing numpy and reinstall it. However these solutions did not work for my code. How I can solve it? Thanks in advance...

like image 885
nsener Avatar asked Jan 27 '23 19:01

nsener


1 Answers

Don't import sum from numpy. Look for from numpy import sum or from numpy import * in your code and delete those lines. Otherwise, you will override the built-in sum. np.sum and built-in sum are independent functions with different requirements.

The warning suggests while your code may work now, it may not work in the future. Notice you do in fact use a generator implicitly. These lines are equivalent:

sum(hopen[i]*fixedCost for i in Fset)
sum((hopen[i]*fixedCost for i in Fset))

In Python, extra parentheses are not required to explicitly denote a generator. Your error will disappear when you avoid importing sum from the NumPy library.

like image 138
jpp Avatar answered Jan 31 '23 07:01

jpp