Home  >  Article  >  Backend Development  >  How to process numpy.median Python data

How to process numpy.median Python data

php中世界最好的语言
php中世界最好的语言Original
2018-04-09 11:32:273005browse

This time I will bring you PythonHow to process numpy.median data, what are the precautions for Python data processing numpy.median, the following is a practical case, let’s take a look .

The function of median under the numpy module is:

Calculate the median along the specified axis

Return the median of the array elements Median

The function interface is:

median(a, 
axis=None, 
out=None,
overwrite_input=False, 
keepdims=False)

The parameters are:

a: Input array;

axis: Calculate the median on which axis, for example, the input is a two-dimensional array , then axis=0 corresponds to the row and axis=1 corresponds to the column;

out: is used to place the array after calculating the median. It must have the same shape and buffer length as the expected output;

overwrite_input: A bool parameter, defaulting to Flase. If it is True, it will be calculated directly in the array memory, which means that the original array cannot be saved after calculation, but the advantage is to save memory resources, and the opposite is true for False;

keepdims: a bool Type parameter, defaults to False. If it is True, the axis for calculating the median will be retained in the result;

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

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

How to read and write txt files line by line in python

How to batch read txt files into DataFrame in python Format

The above is the detailed content of How to process numpy.median Python data. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn