numpy.mean() in Python (original) (raw)
Last Updated : 28 Nov, 2018
numpy.mean(arr, axis = None)
: Compute the arithmetic mean (average) of the given data (array elements) along the specified axis.
Parameters :
arr : [array_like]input array.
axis : [int or tuples of int]axis along which we want to calculate the arithmetic mean. Otherwise, it will consider arr to be flattened(works on all
the axis). axis = 0 means along the column and axis = 1 means working along the row.
out : [ndarray, optional]Different array in which we want to place the result. The array must have the same dimensions as expected output.
dtype : [data-type, optional]Type we desire while computing mean.Results : Arithmetic mean of the array (a scalar value if axis is none) or array with mean values along specified axis.
Code #1:
import
numpy as np
arr
=
[
20
,
2
,
7
,
1
,
34
]
print
(
"arr : "
, arr)
print
(
"mean of arr : "
, np.mean(arr))
Output :
arr : [20, 2, 7, 1, 34] mean of arr : 12.8
Code #2:
import
numpy as np
arr
=
[[
14
,
17
,
12
,
33
,
44
],
`` [
15
,
6
,
27
,
8
,
19
],
`` [
23
,
2
,
54
,
1
,
4
, ]]
print
(
"\nmean of arr, axis = None : "
, np.mean(arr))
print
(
"\nmean of arr, axis = 0 : "
, np.mean(arr, axis
=
0
))
print
(
"\nmean of arr, axis = 1 : "
, np.mean(arr, axis
=
1
))
out_arr
=
np.arange(
3
)
print
(
"\nout_arr : "
, out_arr)
print
(
"mean of arr, axis = 1 : "
,
`` np.mean(arr, axis
=
1
, out
=
out_arr))
Output :
mean of arr, axis = None : 18.6
mean of arr, axis = 0 : [17.33333333 8.33333333 31. 14. 22.33333333]
mean of arr, axis = 1 : [24. 15. 16.8]
out_arr : [0 1 2] mean of arr, axis = 1 : [24 15 16]
Similar Reads
- numpy.median() in Python numpy.median(arr, axis = None) : Compute the median of the given data (array elements) along the specified axis. How to calculate median? Given data points. Arrange them in ascending order Median = middle term if total no. of terms are odd. Median = Average of the terms in the middle (if total no. o 2 min read
- numpy.nanvar() in Python numpy.nanvar(arr, axis = None) : Compute the variance of the given data (array elements) along the specified axis(if any), while ignoring NaN values. Example : x = 1 1 1 1 1 Standard Deviation = 0 . Variance = 0 y = 9, 2, 5, 4, 12, 7, 8, 11, 9, 3, 7, 4, 12, 5, 4, 10, 9, 6, 9, 4 Step 1 : Mean of dist 3 min read
- numpy.nansum() in Python 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') Parameters : arr : [array_like] Array containing numbers whose sum is desired. If 3 min read
- Python | numpy.nanmean() function numpy.nanmean() function can be used to calculate the mean of array ignoring the NaN value. If array have NaN value and we can find out the mean without effect of NaN value. Syntax: numpy.nanmean(a, axis=None, dtype=None, out=None, keepdims=))Parameters: a: [arr_like] input array axis: we can use ax 2 min read
- numpy.nancumsum() in Python numpy.nancumsum() function is used when we want to compute the cumulative sum of array elements over a given axis treating Not a Numbers (NaNs) as zero. The cumulative sum does not change when NaNs are encountered and leading NaNs are replaced by zeros. Zeros are returned for slices that are all-NaN 3 min read
- numpy.std() in Python numpy.std() is a function provided by the NumPy library that calculates the standard deviation of an array or a set of values. Standard deviation is a measure of the amount of variation or dispersion of a set of values. [Tex]\text{Standard Deviation} = \sqrt{\text{mean} \left( (x - x.\text{mean}())^ 4 min read
- numpy.var() in Python numpy.var(arr, axis = None) : Compute the variance of the given data (array elements) along the specified axis(if any). Example : x = 1 1 1 1 1 Standard Deviation = 0 . Variance = 0 y = 9, 2, 5, 4, 12, 7, 8, 11, 9, 3, 7, 4, 12, 5, 4, 10, 9, 6, 9, 4 Step 1 : Mean of distribution 4 = 7 Step 2 : Summat 3 min read
- numpy.nanquantile() in Python numpy.nanquantile(arr, q, axis = None) : Compute the qth quantile of the given data (array elements) along the specified axis, ignoring the nan values. Quantiles plays a very important role in statistics. In the figure given above, Q2 is the median and Q3 - Q1 represents the Interquartile Range of t 4 min read
- numpy.nanstd() function - Python numpy.nanstd() function compute the standard deviation along the specified axis, while ignoring NaNs. Syntax : numpy.nanstd(arr, axis = None, dtype = None, out = None, ddof = 0, keepdims) Parameters : arr : [array_like] Calculate the standard deviation of the non-NaN values. axis : [{int, tuple of i 2 min read
- Numpy recarray.mean() function | Python In numpy, arrays may have a data-types containing fields, analogous to columns in a spreadsheet. An example is [(a, int), (b, float)], where each entry in the array is a pair of (int, float). Normally, these attributes are accessed using dictionary lookups such as arr['a'] and arr['b']. Record array 4 min read