numpy.nansum()function is used when we want to compute the sum of array elements over a given axis treating Not a Numbers (NaNs) as zero.
Syntax : numpy.nansum(arr, axis=None, dtype=None, out=None, keepdims=’no value’)
arr : [array_like] Array containing numbers whose sum is desired. If arr is not an array, a conversion is attempted.
axis : Axis or axes along which the sum is computed. The default is to compute the sum of the flattened array.
dtype : The type of the returned array and of the accumulator in which the elements are summed. By default, the dtype of arr is used.
out : [ndarray, optional] A location into which the result is stored.
-> If provided, it must have a shape that the inputs broadcast to.
-> If not provided or None, a freshly-allocated array is returned.
keepdims : bool, optional
-> If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original arr.
-> If the value is anything but the default, then keepdims will be passed through to the mean or sum methods of sub-classes of ndarray.
-> If the sub-classes methods does not implement keepdims any exceptions will be raised.
Return : A new array holding the result is returned unless out is specified, in which it is returned. The result has the same size as arr, and the same shape as arr, if axis is not None or arr, is a 1-d array.
Code #1 : Working
Input number : 10 sum of array element : 10
Code #2 :
Input array : [[ 2. 2. 2.] [ 2. 2. nan]] sum of array elements: 10.0
Code #3 :
Input array : [[ 2. 2. 2.] [ 2. 2. nan]] sum of array elements taking axis 1: [ 6. 4.]
Note : If both positive and negative infinity are present, the sum will be Not A Number (NaN). If one of positive and negative infinity are present, the sum will be positive or negative infinity, which is present.
Code #4 :
1st array elements: [ 2. -5. nan inf] 2nd array elements: [ 1. 4. inf -inf] sum of 1st array elements: inf sum of 2nd array elements: nan
- Python - Read blob object in python using wand library
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Important differences between Python 2.x and Python 3.x with examples
- Python | Index of Non-Zero elements in Python list
- MySQL-Connector-Python module in Python
- Python | PRAW - Python Reddit API Wrapper
- twitter-text-python (ttp) module - Python
- Python | Convert list to Python array
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Add Logging to Python Libraries
- Python | Add Logging to a Python Script
- Python | Visualizing O(n) using Python
- Use of min() and max() in Python
- Python Set | pop()
- Python vs PHP
- max() and min() in Python
- try-except vs If in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.