6

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...

jpp
  • 159,742
  • 34
  • 281
  • 339
nsener
  • 73
  • 1
  • 8
  • 3
    What is there to solve? It is a warning and tells you that in the future your current solution (`np.sum`) may not work as expected and gives you the exact code to replace it with. You can either replace it with the suggested code now, or leave it for now - up to you. – Gavin Feb 04 '19 at 12:03
  • It looks like you must have done `from numpy import sum`. Don't, otherwise you're overwritting the built-in `sum` method, which does work with generators – yatu Feb 04 '19 at 12:03
  • Thanks for your answers. However my code include only `import numpy as np` line. I solve with `obje_1=detmas.objVal+sum(hopen.values())*fixedCost` code. However I cannot solve for `for ind in range(0,sjt): tot_dis[ind]=sum(dist[unop[ind],ophu[stz]])` – nsener Feb 04 '19 at 12:26

3 Answers3

7

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.

jpp
  • 159,742
  • 34
  • 281
  • 339
  • Thanks for your quick response, but hopen is a dictionary. So it did not work. I solved via values edit for all sums. However I did not solve how can i rewrite for ind in range(0,sjt): tot_dis[ind]=sum(dist[unop[ind],ophu[stz]]) constraint sets. – nsener Feb 04 '19 at 12:07
  • How about `np.sum([a[i] for i in range])`, just a new set of []? – hpaulj Feb 04 '19 at 14:04
  • @hpaulj, True, but I see not benefit of NumPy in this instance.. do you? It might be marginally more efficient to create a list (for small iterables) as iterating generators is expensive, but the performance difference is often marginal. – jpp Feb 04 '19 at 14:04
2

I have found an alternative solution to the one by jpp. If you wish to keep the from numpy import * you can assign the built-in object to a different variable before importing numpy, as it is shown here.

In your particular case you have two choices:

  1. Import the built-in module:

    import builtins for Python 3, or import __builtin__ for Python 2. And you call either builtins.sum(hopen[i]*fixedCost for i in Fset) or __builtin__.sum(hopen[i]*fixedCost for i in Fset).

  2. Assign the built-in sum to a different variable before importing numpy:

    bltin_sum = sum

    from numpy import *

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

I have already checked that the built-in sum behaves as expected for numpy arrays as well.

1

All you have to do is to use sum instead of np.sum. I ran into the same problem. The warning went away after I switch to the built-in sum.

You don't have to do any special imports or assign anything.

Sarah
  • 1,854
  • 17
  • 18