Mean with nan python

numpy.nanmean#

Compute the arithmetic mean along the specified axis, ignoring NaNs.

Returns the average of the array elements. The average is taken over the flattened array by default, otherwise over the specified axis. float64 intermediate and return values are used for integer inputs.

For all-NaN slices, NaN is returned and a RuntimeWarning is raised.

Array containing numbers whose mean is desired. If a is not an array, a conversion is attempted.

axis , optional

Axis or axes along which the means are computed. The default is to compute the mean of the flattened array.

dtype data-type, optional

Type to use in computing the mean. For integer inputs, the default is float64 ; for inexact inputs, it is the same as the input dtype.

out ndarray, optional

Alternate output array in which to place the result. The default is None ; if provided, it must have the same shape as the expected output, but the type will be cast if necessary. See Output type determination for more details.

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

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.

where array_like of bool, optional

Elements to include in the mean. See reduce for details.

If out=None, returns a new array containing the mean values, otherwise a reference to the output array is returned. Nan is returned for slices that contain only NaNs.

Arithmetic mean taken while not ignoring NaNs

The arithmetic mean is the sum of the non-NaN elements along the axis divided by the number of non-NaN elements.

Note that for floating-point input, the mean is computed using the same precision the input has. Depending on the input data, this can cause the results to be inaccurate, especially for float32 . Specifying a higher-precision accumulator using the dtype keyword can alleviate this issue.

>>> a = np.array([[1, np.nan], [3, 4]]) >>> np.nanmean(a) 2.6666666666666665 >>> np.nanmean(a, axis=0) array([2., 4.]) >>> np.nanmean(a, axis=1) array([1., 3.5]) # may vary 

Источник

Mean of Numpy Array with NaN Values

The Numpy library in Python comes with a number of useful built-in functions for computing common descriptive statistics like mean, median, standard deviation, etc. In this tutorial, we will look at how to get the mean value of a Numpy array containing one or more NaN values.

Can you use the numpy.mean() function on an array with NaN values?

We use the numpy.mean() function to get the mean (or the average) value of an array in Numpy. But what happens if the array contains one or more NaN values?

📚 Discover Online Data Science Courses & Programs (Enroll for Free)

Introductory ⭐

Intermediate ⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

import numpy as np # create array ar = np.array([1, 2, np.nan, 3]) # get array mean print(np.mean(ar))

Here, we created a one-dimensional Numpy array containing some numbers and a NaN value. We then applied the numpy.mean() function which resulted in nan . This happened because the numpy.mean() function wasn’t able to handle the nan value present in the array when computing the mean.

Thus, you cannot use the numpy.mean() function to calculate the mean of an array with NaN values.

How to ignore NaN values when calculating the mean of a Numpy array?

mean of numpy array with nan values

You can use the numpy.nanmean() function to calculate the mean of a Numpy array containing NaN values. Pass the array as an argument.

The following is the syntax –

# mean of array with nan values numpy.nanmean(ar)

It returns the mean value in the array ignoring all the NaN values.

Let’s look at some examples of using the numpy.nanmean() function.

Example 1 – Mean of one-dimensional array with NaN values

Let’s apply the numpy.nanmean() function on the same array used in the example above.

# create array ar = np.array([1, 2, np.nan, 3]) # get array mean print(np.nanmean(ar))

We get the mean in the above array as 2.0. The numpy.nanmean() function ignores the NaN values when computing the mean ((1+2+3)/3 = 2).

Example 2 – Mean of multi-dimensional array with NaN values

The numpy.nanmean() function is very similar to the numpy.mean() function in its arguments. For example, use the axis parameter to specify the axis along which to compute the mean.

First, let’s create a 2-D Numpy array.

# create 2-D numpy array ar = np.array([[1, np.nan, 3], [np.nan, 5, np.nan]]) # display the array print(ar)

Here, we used the numpy.array() function to create a Numpy array with two rows and three columns. You can see that there are some NaN values present in the array.

If you use the Numpy nanmean() function on an array without specifying the axis, it will return the mean of all the values inside the array.

# mean of array print(np.nanmean(ar))

We get the mean of all the values inside the 2-D array.

Use the numpy.nanmean() function with axis=1 to get the mean value for each row in the array.

# mean of each row in array print(np.nanmean(ar, axis=1))

We get the mean of each row in the above 2-D array. The mean of values in the first row is (1+3)/2 = 2 and the mean of values in the second row is 5/1 = 5.

Use the numpy.nanmean() function with axis=0 to get the mean of each column in the array.

# mean of each column in array print(np.nanmean(ar, axis=0))

We get the mean of each column in the above 2-D array. In this example, each column has one NaN value and one non-NaN value (which naturally becomes the mean since it’s the only value in the column).

Summary – Mean of Numpy array with NaN values

The following is a short summary of the important points mentioned in this tutorial.

  1. Using the numpy.mean() function on an array with NaN values results in NaN.
  2. Use the numpy.nanmean() function to get the mean value in an array containing one or more NaN values. It computes the mean by taking into account only the non-NaN values in the array.
  3. Similar to the numpy.mean() function, you can specify the axis along which you want to compute the mean with the numpy.nanmean() function.

You might also be interested in –

Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.

Author

Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects. View all posts

Data Science Parichay is an educational website offering easy-to-understand tutorials on topics in Data Science with the help of clear and fun examples.

Источник

NumPy nanmean() – Get Mean ignoring NAN Values

Python NumPy nanmean() function is used to compute the arithmetic mean or average of the array ignoring the NaN value. If the array has a NaN value and we can find out the average without being influenced by the NaN value. The mean/average is taken over the flattened array by default, otherwise over the specified axis.

In this article, I will explain how to use numpy.nanmean() function in Python to return the average of the array elements by ignoring NaN with examples.

1. Quick Examples of Python NumPy nanmean() Function

If you are in a hurry, below are some quick examples of how to use nanmean() function in NumPy Python.

2. Syntax of numpy.nanmean() Function

Following is the syntax of numpy.nanmean() function.

2.1 Parameter of numpy.nanmean()

  • arr : Array containing numbers whose mean is desired. If arr is not an array, a conversion is attempted.
  • axis : Axis or axes along which the means are computed. we can use axis=1 means row wise or axis=0 means column wise.
  • dtype : Type to used during the calculation of the arithmetic mean. For integer inputs, the default is float64.
  • out : Alternate output array in which to place the result.
  • keepdims : 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.

2.2 Return Value of numpy.nanmean()

It returns the average of the array elements, otherwise, a reference to the output array is returned. Nan is returned for slices that contain only NaNs.

3. Usage of NumPy nanmean() Function

Use numpy.nanmean() function is used to get the mean value in an array containing one or more NaN values. It calculates the mean by taking into account only the non-NaN values in the array.

The numpy.nanmean() function is very similar to the numpy.mean() function in its arguments. When we use numpy.nanmean() function is used to calculate the mean of a Numpy array containing NaN values. By not specifying the axis, it will return the mean of all the values inside the array.

4. Get the nanmean() Values of 2-D Array along Axis = 0

We can calculate the mean value of an array by ignoring NaN along with a specified axis using numpy.nanmean() function. Use axis=0 param to get the mean of each column in the array.

5. Get the nanmean() Values of 2-D Array along Axis = 1

We can also use the np.nanmean() function with axis=1 to get the mean value for each row in the array.

6. Conclusion

In this article, I have explained how to use NumPy nanmean() function in Python which calculates the arithmetic mean/average along with the specified axis and by ignoring NaN values with examples.

References

You may also like reading:

Источник

Читайте также:  Глобальные стили
Оцените статью