Python deprecation warning about sum function

gensim calling np sum generator is deprecated
python sum runtime
sum vs np sum python
numpy sum implementation
numpy fromiter 2d
numpy array from generator

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

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.

DeprecationWarning: numpy sum :: np.sum(generator) is , from_iter(generator)) or the python sum builtin instead. score += np.sum(cnt * logsumexp(Elogthetad + Elogbeta[:, int(id)]) for id, cnt in doc)  From documentation of the warnings module: #!/usr/bin/env python -W ignore::DeprecationWarning If you're on Windows: pass -W ignore::DeprecationWarning as an argument to Python. Better though to resolve the issue, by casting to int. (Note that in Python 3.2, deprecation warnings are ignored by default.)

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.

Deprecation Warning while performing topic modelling · Issue , DeprecationWarning: Calling np.sum(generator) is deprecated, and in Use np.sum(np.from_iter(generator)) or the python sum builtin  27.4.2. The Warnings Filter¶. The warnings filter controls whether warnings are ignored, displayed, or turned into errors (raising an exception). Conceptually, the warnings filter maintains an ordered list of filter specifications; any specific warning is matched against each filter specification in the list in turn until a match is found; the match determines the disposition of the match.

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.

Release Notes, The Python versions supported by this release are 2.7, 3.4-3.7. The wheels are numpy.ma.stack , the numpy.stack array-joining function generalized to masked arrays. Giving a generator to numpy.sum is now deprecated. A deprecation warning will be emitted if those calls are not used when needed. Fix deprecation warning `np.sum(generator)` #2296 menshikh-iv merged 1 commit into RaRe-Technologies : develop from rsdel2007 : rsdel2 Dec 15, 2018 Conversation 5 Commits 1 Checks 0 Files changed

9. Warnings, PendingDeprecationWarning) return a + b def sum(a, b): return a + b sumuj(1, 2) sum(1, **kwargs): warnings.warn_explicit( f"Call to deprecated function {func. sum(a) a is the list , it adds up all the numbers in the list a and takes start to be 0, so returning only the sum of the numbers in the list. sum(a, start) this returns the sum of the list + start

Python Essentials, 43 string value using 37 string values, Python bytes 37 unicode 37 sum() function 102 syntax warning classes DeprecationWarning 172 ImportWarning 172  Hi, when I build numpy 1.16.3 in Fedora with Python 3.8.0a3, I get the following test failures with DeprecationWarning: an integer is required (got type float). Implicit conversion to integers using __int__ is deprecated, and may be remo

PEP 289 -- Generator Expressions, Generator expressions are especially useful with functions like sum(), Python 2.4 and beyond should issue a deprecation warning if a list  The Warnings Filter¶. The warnings filter controls whether warnings are ignored, displayed, or turned into errors (raising an exception). Conceptually, the warnings filter maintains an ordered list of filter specifications; any specific warning is matched against each filter specification in the list in turn until a match is found; the filter determines the disposition of the match.

Comments
  • 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.
  • 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
  • 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]])
  • 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.
  • How about np.sum([a[i] for i in range]), just a new set of []?
  • @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.