NumPy | Python Methods and Functions

** **

** numpy.nansum() ** is used when we want to calculate the sum of array elements along a given axis, treating Not Numbers (NaNs) as zero.

Syntax:numpy.nansum (arr, axis = None, dtype = None, out = None, keepdims = `no value`)

Parameters:

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, thedtypeof arr is used.

out:[ndarray, optional] A location into which the result is stored.

- & gt ; If provided, it must have a shape that the inputs broadcast to.

- & gt; If not provided or None, a freshly-allocated array is returned.

keepdims:bool, optional

- & gt; 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.

- & gt; 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.

- & gt; 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: Work **

` `

` ` ` # Python program explaining `

` # numpy.nansum () function `

` import `

` numpy as geek `

` in_num `

` = `

` 10 `

` print `

` (`

`" Input number: "`

`, in_num) `

` out_sum `

` = `

` geek.nansum (in_num) `

< code class = "functions"> print ` (`

` "sum of array element:" `

`, out_sum) `

** Output: **

Input number: 10 sum of array element: 10

** Code # 2: **

` ` |

** Output: **

Input array: [[2. 2. 2.] [2 . 2. nan]] sum of array elements: 10.0

** Code # 3: **

` `

` ` ` # Program Python, explaining `

` # numpy.nansum function `

` import `

` numpy as geek `

` `

` ` ` in_arr `

` = `

` geek.array ([[`

` 2 `

`, `

` 2 `

`, `

` 2 `

`], [`

` 2 `

`, `

` 2 `

`, geek.nan]]) `

` print `

` (`

` "Input array:" `

`, in_arr) `

` out_sum `

` = `

` geek.nansum (in_arr, axis `

` = `

` 1 `

`) `

` print `

` (`

` "sum of array elements taking axis 1: "`

`, out_sum) `

** Output: **

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 not be a number (NaN). If one of positive and negative infinity is present, the sum will be positive or negative infinity, which is present.

** Code # 4: **

` # Python program explaining `

` # numpy.nansum () function `

` import `

` numpy as geek `

` in_arr1 `

` = `

` geek.array ([`

` 2 `

`, `

` - `` 5 `

`, geek.nan, geek.inf]) `

` ` ` in_arr2 `

` = `

` geek.array ([`

` 1 `

`, `

` 4 `

`, geek.inf, `` - `

` geek.inf]) `

` ` ` `

` ` ` print `

` (`

` "1st array elements:" `

`, in_arr1) `

` print `

` (`

` "2nd array elements:" `

`, in_arr2) `

` out_sum1 `

` = `

` geek.nansum (in_arr1) `

` out_sum2 `

` = `

` geek.nansum (in_arr2 ) `

` print `

` (`

`" sum of 1st array elements: "`

`, out_sum1) `

` print `

` (`

` "sum of 2nd array elements:" `

`, out_sum2) `

** Output: **

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

Google BigQuery: The Definitive Guide PDF download. Data Warehousing, Analytics, and Machine Learning at Scale, 1st Edition, 2019. Work with petabyte-scale datasets while building a collaborative a...

31/08/2021

A Practical Approach to Computer Algorithms Using Python® and C# Rod Stephens started out as a mathematician, but while studying at MIT, he discovered how much fun algorithms are. He took every al...

23/09/2020

If you can program, you are ready to grapple with Bayesian statistics. In this book, you'll learn how to solve statistical problems using Python code instead of math formulas, using discrete probabili...

28/08/2021

The Pragmatic Programmer: Your Journey To Mastery, 20th Anniversary Edition (2nd Edition). The Pragmatic Programmer is one of those rare technical books that you will read, reread, and re-read over...

23/09/2021

X
# Submit new EBook