本文整理汇总了Python中numpy.ma.median方法的典型用法代码示例。如果您正苦于以下问题:Python ma.median方法的具体用法?Python ma.median怎么用?Python ma.median使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy.ma
的用法示例。
在下文中一共展示了ma.median方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: regridToCoarse
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import median [as 别名]
def regridToCoarse(fine,fac,mode,missValue):
nr,nc = np.shape(fine)
coarse = np.zeros(nr/fac * nc / fac).reshape(nr/fac,nc/fac) + MV
nr,nc = np.shape(coarse)
for r in range(0,nr):
for c in range(0,nc):
ar = fine[r * fac : fac * (r+1),c * fac: fac * (c+1)]
m = np.ma.masked_values(ar,missValue)
if ma.count(m) == 0:
coarse[r,c] = MV
else:
if mode == 'average':
coarse [r,c] = ma.average(m)
elif mode == 'median':
coarse [r,c] = ma.median(m)
elif mode == 'sum':
coarse [r,c] = ma.sum(m)
elif mode =='min':
coarse [r,c] = ma.min(m)
elif mode == 'max':
coarse [r,c] = ma.max(m)
return coarse
示例2: sen_seasonal_slopes
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import median [as 别名]
def sen_seasonal_slopes(x):
x = ma.array(x, subok=True, copy=False, ndmin=2)
(n,_) = x.shape
# Get list of slopes per season
szn_slopes = ma.vstack([(x[i+1:]-x[i])/np.arange(1,n-i)[:,None]
for i in range(n)])
szn_medslopes = ma.median(szn_slopes, axis=0)
medslope = ma.median(szn_slopes, axis=None)
return szn_medslopes, medslope
示例3: stde_median
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import median [as 别名]
def stde_median(data, axis=None):
"""Returns the McKean-Schrader estimate of the standard error of the sample
median along the given axis. masked values are discarded.
Parameters
----------
data : ndarray
Data to trim.
axis : {None,int}, optional
Axis along which to perform the trimming.
If None, the input array is first flattened.
"""
def _stdemed_1D(data):
data = np.sort(data.compressed())
n = len(data)
z = 2.5758293035489004
k = int(np.round((n+1)/2. - z * np.sqrt(n/4.),0))
return ((data[n-k] - data[k-1])/(2.*z))
data = ma.array(data, copy=False, subok=True)
if (axis is None):
return _stdemed_1D(data)
else:
if data.ndim > 2:
raise ValueError("Array 'data' must be at most two dimensional, "
"but got data.ndim = %d" % data.ndim)
return ma.apply_along_axis(_stdemed_1D, axis, data)
示例4: scoreatpercentile
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import median [as 别名]
def scoreatpercentile(data, per, limit=(), alphap=.4, betap=.4):
"""Calculate the score at the given 'per' percentile of the
sequence a. For example, the score at per=50 is the median.
This function is a shortcut to mquantile
"""
if (per < 0) or (per > 100.):
raise ValueError("The percentile should be between 0. and 100. !"
" (got %s)" % per)
return mquantiles(data, prob=[per/100.], alphap=alphap, betap=betap,
limit=limit, axis=0).squeeze()
示例5: compare_medians_ms
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import median [as 别名]
def compare_medians_ms(group_1, group_2, axis=None):
"""
Compares the medians from two independent groups along the given axis.
The comparison is performed using the McKean-Schrader estimate of the
standard error of the medians.
Parameters
----------
group_1 : array_like
First dataset. Has to be of size >=7.
group_2 : array_like
Second dataset. Has to be of size >=7.
axis : int, optional
Axis along which the medians are estimated. If None, the arrays are
flattened. If `axis` is not None, then `group_1` and `group_2`
should have the same shape.
Returns
-------
compare_medians_ms : {float, ndarray}
If `axis` is None, then returns a float, otherwise returns a 1-D
ndarray of floats with a length equal to the length of `group_1`
along `axis`.
"""
(med_1, med_2) = (ma.median(group_1,axis=axis), ma.median(group_2,axis=axis))
(std_1, std_2) = (mstats.stde_median(group_1, axis=axis),
mstats.stde_median(group_2, axis=axis))
W = np.abs(med_1 - med_2) / ma.sqrt(std_1**2 + std_2**2)
return 1 - norm.cdf(W)
示例6: sen_seasonal_slopes
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import median [as 别名]
def sen_seasonal_slopes(x):
x = ma.array(x, subok=True, copy=False, ndmin=2)
(n,_) = x.shape
# Get list of slopes per season
szn_slopes = ma.vstack([(x[i+1:]-x[i])/np.arange(1,n-i)[:,None]
for i in range(n)])
szn_medslopes = ma.median(szn_slopes, axis=0)
medslope = ma.median(szn_slopes, axis=None)
return szn_medslopes, medslope
#####--------------------------------------------------------------------------
#---- --- Inferential statistics ---
#####--------------------------------------------------------------------------
示例7: stde_median
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import median [as 别名]
def stde_median(data, axis=None):
"""Returns the McKean-Schrader estimate of the standard error of the sample
median along the given axis. masked values are discarded.
Parameters
----------
data : ndarray
Data to trim.
axis : {None,int}, optional
Axis along which to perform the trimming.
If None, the input array is first flattened.
"""
def _stdemed_1D(data):
data = np.sort(data.compressed())
n = len(data)
z = 2.5758293035489004
k = int(np.round((n+1)/2. - z * np.sqrt(n/4.),0))
return ((data[n-k] - data[k-1])/(2.*z))
#
data = ma.array(data, copy=False, subok=True)
if (axis is None):
return _stdemed_1D(data)
else:
if data.ndim > 2:
raise ValueError("Array 'data' must be at most two dimensional, but got data.ndim = %d" % data.ndim)
return ma.apply_along_axis(_stdemed_1D, axis, data)
#####--------------------------------------------------------------------------
#---- --- Normality Tests ---
#####--------------------------------------------------------------------------
示例8: scoreatpercentile
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import median [as 别名]
def scoreatpercentile(data, per, limit=(), alphap=.4, betap=.4):
"""Calculate the score at the given 'per' percentile of the
sequence a. For example, the score at per=50 is the median.
This function is a shortcut to mquantile
"""
if (per < 0) or (per > 100.):
raise ValueError("The percentile should be between 0. and 100. !"
" (got %s)" % per)
return mquantiles(data, prob=[per/100.], alphap=alphap, betap=betap,
limit=limit, axis=0).squeeze()
示例9: compare_medians_ms
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import median [as 别名]
def compare_medians_ms(group_1, group_2, axis=None):
"""
Compares the medians from two independent groups along the given axis.
The comparison is performed using the McKean-Schrader estimate of the
standard error of the medians.
Parameters
----------
group_1 : array_like
First dataset.
group_2 : array_like
Second dataset.
axis : int, optional
Axis along which the medians are estimated. If None, the arrays are
flattened. If `axis` is not None, then `group_1` and `group_2`
should have the same shape.
Returns
-------
compare_medians_ms : {float, ndarray}
If `axis` is None, then returns a float, otherwise returns a 1-D
ndarray of floats with a length equal to the length of `group_1`
along `axis`.
"""
(med_1, med_2) = (ma.median(group_1,axis=axis), ma.median(group_2,axis=axis))
(std_1, std_2) = (mstats.stde_median(group_1, axis=axis),
mstats.stde_median(group_2, axis=axis))
W = np.abs(med_1 - med_2) / ma.sqrt(std_1**2 + std_2**2)
return 1 - norm.cdf(W)
示例10: test_median_axis_none_mask_none
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import median [as 别名]
def test_median_axis_none_mask_none(set_random_seed):
for i in range(25):
size = np.random.randint(1, 10000)
mean = np.random.uniform(-1000, 1000)
sigma = np.random.uniform(0, 1000)
a = np.random.normal(mean, sigma, size)
expected = np.median(a.astype(np.float32))
actual = stats.median(a)
assert np.float32(expected) == actual
示例11: test_median_2d_axis_none_mask_none
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import median [as 别名]
def test_median_2d_axis_none_mask_none(set_random_seed):
for i in range(5):
size1 = np.random.randint(1, 300)
size2 = np.random.randint(1, 300)
mean = np.random.uniform(-1000, 1000)
sigma = np.random.uniform(0, 1000)
a = np.random.normal(mean, sigma, size=(size1, size2))
expected = np.median(a.astype(np.float32))
actual = stats.median(a)
assert np.float32(expected) == actual
示例12: test_median_3d_axis_none_mask_none
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import median [as 别名]
def test_median_3d_axis_none_mask_none(set_random_seed):
for i in range(5):
size1 = np.random.randint(1, 50)
size2 = np.random.randint(1, 50)
size3 = np.random.randint(1, 50)
mean = np.random.uniform(-1000, 1000)
sigma = np.random.uniform(0, 1000)
a = np.random.normal(mean, sigma, size=(size1, size2, size3))
expected = np.median(a.astype(np.float32))
actual = stats.median(a)
assert np.float32(expected) == actual
示例13: test_median_2d_axis_0_mask_none
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import median [as 别名]
def test_median_2d_axis_0_mask_none(set_random_seed):
for i in range(5):
size1 = np.random.randint(1, 300)
size2 = np.random.randint(1, 300)
mean = np.random.uniform(-1000, 1000)
sigma = np.random.uniform(0, 1000)
a = np.random.normal(mean, sigma, size=(size1, size2))
expected = np.median(a.astype(np.float32), axis=0)
actual = stats.median(a, axis=0)
np.testing.assert_allclose(actual, expected.astype(np.float32), atol=1e-6)
示例14: test_median_2d_axis_1_mask_none
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import median [as 别名]
def test_median_2d_axis_1_mask_none(set_random_seed):
for i in range(5):
size1 = np.random.randint(1, 300)
size2 = np.random.randint(5, 300)
mean = np.random.uniform(-1000, 1000)
sigma = np.random.uniform(0, 1000)
a = np.random.normal(mean, sigma, size=(size1, size2))
expected = np.median(a.astype(np.float32), axis=1)
actual = stats.median(a, axis=1)
np.testing.assert_allclose(actual, expected.astype(np.float32), atol=1e-6)
示例15: test_median_3d_axis_1_mask_none
# 需要导入模块: from numpy import ma [as 别名]
# 或者: from numpy.ma import median [as 别名]
def test_median_3d_axis_1_mask_none(set_random_seed):
for i in range(5):
size1 = np.random.randint(1, 50)
size2 = np.random.randint(5, 50)
size3 = np.random.randint(1, 50)
mean = np.random.uniform(-1000, 1000)
sigma = np.random.uniform(0, 1000)
a = np.random.normal(mean, sigma, size=(size1, size2, size3))
expected = np.median(a.astype(np.float32), axis=1)
actual = stats.median(a, axis=1)
np.testing.assert_allclose(actual, expected.astype(np.float32), atol=1e-6)