????????numpy.median() 是 Numpy 库中的一个函数,用于计算给定数据集或数组的中位数。
numpy.median(a, axis=None, out=None, overwrite_input=False, keepdims=False)
????????参数:
????????????????a:输入数组。
????????????????axis:沿着哪个轴计算中位数。默认是 None,表示在所有维度上计算中位数。
????????????????out:可选参数,用于指定输出结果的位置,一般用于输出到某个维度相同的数组中。
????????????????overwrite_input:布尔值,如果为 True,则允许修改输入数组以节省内存。
????????????????keepdims:布尔值,如果为 True,则结果数组的维度与输入数组一致。
import numpy as np
a = [1, 3, 1, 2, 3, 4, 5, 6, 7, 8, 2]
b = np.median(a)
print(b)
a = [[1, 3, 1, 2, 3, 4, 5, 6, 7, 8, 2],
[2, 4, 5, 6, 2, 1, 8, 1, 2,3, 9],
[10, 2, 5, 0, 1, 1, 6, 4, 3, 2, 2]]
b = np.median(a, axis=0)
print(b)
a = [[1, 3, 1, 2, 3, 4, 5, 6, 7, 8, 2],
[2, 4, 5, 6, 2, 1, 8, 1, 2,3, 9],
[10, 2, 5, 0, 1, 1, 6, 4, 3, 2, 2]]
b = np.median(a, axis=1)
print(b)
temp_array = np.empty((3,))
a = [[1, 3, 1, 2, 3, 4, 5, 6, 7, 8, 2],
[2, 4, 5, 6, 2, 1, 8, 1, 2,3, 9],
[10, 2, 5, 0, 1, 1, 6, 4, 3, 2, 2]]
np.median(a, axis=1, out=temp_array)
print(temp)
a = [[1, 3, 1, 2, 3, 4, 5, 6, 7, 8, 2],
[2, 4, 5, 6, 2, 1, 8, 1, 2,3, 9],
[10, 2, 5, 0, 1, 1, 6, 4, 3, 2, 2]]
b = np.median(a, axis=1, overwrite_input=True)
print(b)
a = [[1, 3, 1, 2, 3, 4, 5, 6, 7, 8, 2],
[2, 4, 5, 6, 2, 1, 8, 1, 2,3, 9],
[10, 2, 5, 0, 1, 1, 6, 4, 3, 2, 2]]
b = np.median(a, axis=1, keepdims=True)
print(b)