View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
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. of terms are even]
Parameters :
arr : [array_like]input array.
axis : [int or tuples of int]axis along which we want to calculate the median. 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 median.Results : Median of the array [a scalar value if axis is none] or array with median values along specified axis.
Code #1:
import
numpy as np
arr
=
[
20
,
2
,
7
,
1
,
34
]
print
[
"arr : "
, arr]
print
[
"median of arr : "
, np.median[arr]]
Output :
arr : [20, 2, 7, 1, 34] median of arr : 7.0
Code #2:
import
numpy as np
arr
=
[[
14
,
17
,
12
,
33
,
44
],
[
15
,
6
,
27
,
8
,
19
],
[
23
,
2
,
54
,
1
,
4
, ]]
print
[
"\nmedian of arr, axis = None : "
, np.median[arr]]
print
[
"\nmedian of arr, axis = 0 : "
, np.median[arr, axis
=
0
]]
print
[
"\nmedian of arr, axis = 1 : "
, np.median[arr, axis
=
1
]]
out_arr
=
np.arange[
3
]
print
[
"\nout_arr : "
, out_arr]
print
[
"median of arr, axis = 1 : "
,
np.median[arr, axis
=
1
, out
=
out_arr]]
Output :
median of arr, axis = None : 15.0 median of arr, axis = 0 : [15. 6. 27. 8. 19.] median of arr, axis = 1 : [17. 15. 4.] out_arr : [0 1 2] median of arr, axis = 1 : [17 15 4]
Compute the median along the specified axis.
Returns the median of the array elements.
Parametersaarray_likeInput array or object that can be converted to an array.
axis{int, sequence of int, None}, optionalAxis or axes along which the medians are computed. The default is to compute the median along a flattened version of the array. A sequence of axes is supported since version 1.9.0.
outndarray, optionalAlternative output array in which to place the result. It must have the same shape and buffer length as the expected output, but the type [of the output] will be cast if necessary.
overwrite_inputbool, optionalIf True, then allow use of memory of input array a for calculations. The input array will be modified by the call to
median
. This will save memory when you do not need to preserve the contents of the input array. Treat the input as undefined, but it will probably be fully or partially sorted. Default is False. If overwrite_input is True
and a is not already an
ndarray
, an error will be raised.
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.
New in version 1.9.0.
Returns medianndarrayA new array holding the result. If the input contains integers or floats smaller than float64
, then the output data-type is np.float64
. Otherwise, the data-type of the output is the same as that of the input. If out is specified, that array is returned instead.
Notes
Given a vector V
of length N
, the median of V
is the middle value of a sorted copy of V
,
V_sorted
- i e., V_sorted[[N-1]/2]
, when N
is odd, and the average of the two middle values of V_sorted
when N
is even.
Examples
>>> a = np.array[[[10, 7, 4], [3, 2, 1]]] >>> a array[[[10, 7, 4], [ 3, 2, 1]]] >>> np.median[a] 3.5 >>> np.median[a, axis=0] array[[6.5, 4.5, 2.5]] >>> np.median[a, axis=1] array[[7., 2.]] >>> m = np.median[a, axis=0] >>> out = np.zeros_like[m] >>> np.median[a, axis=0, out=m] array[[6.5, 4.5, 2.5]] >>> m array[[6.5, 4.5, 2.5]] >>> b = a.copy[] >>> np.median[b, axis=1, overwrite_input=True] array[[7., 2.]] >>> assert not np.all[a==b] >>> b = a.copy[] >>> np.median[b, axis=None, overwrite_input=True] 3.5 >>> assert not np.all[a==b]