本文整理汇总了Python中numpy.nanmedian方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.nanmedian方法的具体用法?Python numpy.nanmedian怎么用?Python numpy.nanmedian使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.nanmedian方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_equ_med_far_vector
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import nanmedian [as 别名]
def test_equ_med_far_vector(self):
"""
Test that median reddening is correct in the far limit, using a vector
of coordinates as input.
"""
l = [d['l']*units.deg for d in self._test_data]
b = [d['b']*units.deg for d in self._test_data]
dist = [1.e3*units.kpc for bb in b]
c = coords.SkyCoord(l, b, distance=dist, frame='galactic')
ebv_data = np.array([np.nanmedian(d['samples'][:,-1]) for d in self._test_data])
ebv_calc = self._bayestar(c, mode='median')
# print 'vector:'
# print r'% residual:'
# for ed,ec in zip(ebv_data, ebv_calc):
# print ' {: >8.3f}'.format((ec - ed) / (0.02 + 0.02 * ed))
np.testing.assert_allclose(ebv_data, ebv_calc, atol=0.001, rtol=0.0001)
示例2: test_equ_med_scalar
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import nanmedian [as 别名]
def test_equ_med_scalar(self):
"""
Test that median reddening is correct in at arbitary distances, using
individual coordinates as input.
"""
for d in self._test_data:
l = d['l']*units.deg
b = d['b']*units.deg
for reps in range(10):
dm = 3. + (25.-3.)*np.random.random()
dist = 10.**(dm/5.-2.)
c = coords.SkyCoord(l, b, distance=dist*units.kpc, frame='galactic')
ebv_samples = self._interp_ebv(d, dist)
ebv_data = np.nanmedian(ebv_samples)
ebv_calc = self._bayestar(c, mode='median')
np.testing.assert_allclose(ebv_data, ebv_calc, atol=0.001, rtol=0.0001)
示例3: _nanmedian
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import nanmedian [as 别名]
def _nanmedian(a, axis=None, out=None, overwrite_input=False):
"""
Private function that doesn't support extended axis or keepdims.
These methods are extended to this function using _ureduce
See nanmedian for parameter usage
"""
if axis is None or a.ndim == 1:
part = a.ravel()
if out is None:
return _nanmedian1d(part, overwrite_input)
else:
out[...] = _nanmedian1d(part, overwrite_input)
return out
else:
# for small medians use sort + indexing which is still faster than
# apply_along_axis
# benchmarked with shuffled (50, 50, x) containing a few NaN
if a.shape[axis] < 600:
return _nanmedian_small(a, axis, out, overwrite_input)
result = np.apply_along_axis(_nanmedian1d, axis, a, overwrite_input)
if out is not None:
out[...] = result
return result
示例4: test_allnans
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import nanmedian [as 别名]
def test_allnans(self):
mat = np.array([np.nan]*9).reshape(3, 3)
for axis in [None, 0, 1]:
with suppress_warnings() as sup:
sup.record(RuntimeWarning)
assert_(np.isnan(np.nanmedian(mat, axis=axis)).all())
if axis is None:
assert_(len(sup.log) == 1)
else:
assert_(len(sup.log) == 3)
# Check scalar
assert_(np.isnan(np.nanmedian(np.nan)))
if axis is None:
assert_(len(sup.log) == 2)
else:
assert_(len(sup.log) == 4)
示例5: _nanmedian1d
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import nanmedian [as 别名]
def _nanmedian1d(arr1d, overwrite_input=False):
"""
Private function for rank 1 arrays. Compute the median ignoring NaNs.
See nanmedian for parameter usage
"""
c = np.isnan(arr1d)
s = np.where(c)[0]
if s.size == arr1d.size:
warnings.warn("All-NaN slice encountered", RuntimeWarning, stacklevel=3)
return np.nan
elif s.size == 0:
return np.median(arr1d, overwrite_input=overwrite_input)
else:
if overwrite_input:
x = arr1d
else:
x = arr1d.copy()
# select non-nans at end of array
enonan = arr1d[-s.size:][~c[-s.size:]]
# fill nans in beginning of array with non-nans of end
x[s[:enonan.size]] = enonan
# slice nans away
return np.median(x[:-s.size], overwrite_input=True)
示例6: test_out
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import nanmedian [as 别名]
def test_out(self):
mat = np.random.rand(3, 3)
nan_mat = np.insert(mat, [0, 2], np.nan, axis=1)
resout = np.zeros(3)
tgt = np.median(mat, axis=1)
res = np.nanmedian(nan_mat, axis=1, out=resout)
assert_almost_equal(res, resout)
assert_almost_equal(res, tgt)
# 0-d output:
resout = np.zeros(())
tgt = np.median(mat, axis=None)
res = np.nanmedian(nan_mat, axis=None, out=resout)
assert_almost_equal(res, resout)
assert_almost_equal(res, tgt)
res = np.nanmedian(nan_mat, axis=(0, 1), out=resout)
assert_almost_equal(res, resout)
assert_almost_equal(res, tgt)
示例7: _nanmedian1d
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import nanmedian [as 别名]
def _nanmedian1d(arr1d, overwrite_input=False):
"""
Private function for rank 1 arrays. Compute the median ignoring NaNs.
See nanmedian for parameter usage
"""
c = np.isnan(arr1d)
s = np.where(c)[0]
if s.size == arr1d.size:
warnings.warn("All-NaN slice encountered", RuntimeWarning)
return np.nan
elif s.size == 0:
return np.median(arr1d, overwrite_input=overwrite_input)
else:
if overwrite_input:
x = arr1d
else:
x = arr1d.copy()
# select non-nans at end of array
enonan = arr1d[-s.size:][~c[-s.size:]]
# fill nans in beginning of array with non-nans of end
x[s[:enonan.size]] = enonan
# slice nans away
return np.median(x[:-s.size], overwrite_input=True)
示例8: _nanmedian
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import nanmedian [as 别名]
def _nanmedian(a, axis=None, out=None, overwrite_input=False):
"""
Private function that doesn't support extended axis or keepdims.
These methods are extended to this function using _ureduce
See nanmedian for parameter usage
"""
if axis is None or a.ndim == 1:
part = a.ravel()
if out is None:
return _nanmedian1d(part, overwrite_input)
else:
out[...] = _nanmedian1d(part, overwrite_input)
return out
else:
# for small medians use sort + indexing which is still faster than
# apply_along_axis
if a.shape[axis] < 400:
return _nanmedian_small(a, axis, out, overwrite_input)
result = np.apply_along_axis(_nanmedian1d, axis, a, overwrite_input)
if out is not None:
out[...] = result
return result
示例9: test_allnans
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import nanmedian [as 别名]
def test_allnans(self):
mat = np.array([np.nan]*9).reshape(3, 3)
for axis in [None, 0, 1]:
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
warnings.simplefilter('ignore', FutureWarning)
assert_(np.isnan(np.nanmedian(mat, axis=axis)).all())
if axis is None:
assert_(len(w) == 1)
else:
assert_(len(w) == 3)
assert_(issubclass(w[0].category, RuntimeWarning))
# Check scalar
assert_(np.isnan(np.nanmedian(np.nan)))
if axis is None:
assert_(len(w) == 2)
else:
assert_(len(w) == 4)
assert_(issubclass(w[0].category, RuntimeWarning))
示例10: compute_mean_metrics
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import nanmedian [as 别名]
def compute_mean_metrics(json_folder, compute_averages=True):
files = glob.glob(os.path.join(json_folder, "*.json"))
sdr_inst_list = None
for path in files:
#print(path)
with open(path, "r") as f:
js = json.load(f)
if sdr_inst_list is None:
sdr_inst_list = [list() for _ in range(len(js["targets"]))]
for i in range(len(js["targets"])):
sdr_inst_list[i].extend([np.float(f['metrics']["SDR"]) for f in js["targets"][i]["frames"]])
#return np.array(sdr_acc), np.array(sdr_voc)
sdr_inst_list = [np.array(sdr) for sdr in sdr_inst_list]
if compute_averages:
return [(np.nanmedian(sdr), np.nanmedian(np.abs(sdr - np.nanmedian(sdr))), np.nanmean(sdr), np.nanstd(sdr)) for sdr in sdr_inst_list]
else:
return sdr_inst_list
示例11: test_equ_med_far_scalar
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import nanmedian [as 别名]
def test_equ_med_far_scalar(self):
"""
Test that median reddening is correct in the far limit, using a single
location on the sky at a time as input.
"""
for d in self._test_data:
c = self._get_gal(d, dist=1.e3*units.kpc)
# print d['samples']
ebv_data = np.nanmedian(d['samples'][:,-1])
ebv_calc = self._bayestar(c, mode='median')
# print 'ebv_data:', ebv_data
# print 'ebv_calc:', ebv_calc
# print ''
# print r'% residual: {:.6f}'.format((ebv_calc - ebv_data) / (0.001 + 0.001 * ebv_data))
np.testing.assert_allclose(ebv_data, ebv_calc, atol=0.001, rtol=0.0001)
示例12: test_equ_med_vector
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import nanmedian [as 别名]
def test_equ_med_vector(self):
"""
Test that median reddening is correct at arbitary distances, using a
vector of coordinates as input.
"""
for reps in range(10):
l = [d['l']*units.deg for d in self._test_data]
b = [d['b']*units.deg for d in self._test_data]
dm = 3. + (25.-3.)*np.random.random(len(self._test_data))
dist = [d*units.kpc for d in 10.**(dm/5.-2.)]
dist_unitless = [d for d in 10.**(dm/5.-2.)]
c = coords.SkyCoord(l, b, distance=dist, frame='galactic')
ebv_samples = np.array([
self._interp_ebv(datum, d)
for datum,d in zip(self._test_data, dist_unitless)
])
ebv_data = np.nanmedian(ebv_samples, axis=1)
ebv_calc = self._bayestar(c, mode='median')
# print 'vector arbitrary distance:'
# print r'% residual:'
# for ed,ec in zip(ebv_data, ebv_calc):
# print ' {: >8.3f}'.format((ec - ed) / (0.02 + 0.02 * ed))
np.testing.assert_allclose(ebv_data, ebv_calc, atol=0.001, rtol=0.0001)
示例13: _rsp_rrv_time
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import nanmedian [as 别名]
def _rsp_rrv_time(bbi):
diff_bbi = np.diff(bbi)
out = {} # Initialize empty dict
# Mean based
out["RMSSD"] = np.sqrt(np.mean(diff_bbi ** 2))
out["MeanBB"] = np.nanmean(bbi)
out["SDBB"] = np.nanstd(bbi, ddof=1)
out["SDSD"] = np.nanstd(diff_bbi, ddof=1)
out["CVBB"] = out["SDBB"] / out["MeanBB"]
out["CVSD"] = out["RMSSD"] / out["MeanBB"]
# Robust
out["MedianBB"] = np.nanmedian(bbi)
out["MadBB"] = mad(bbi)
out["MCVBB"] = out["MadBB"] / out["MedianBB"]
# # Extreme-based
# nn50 = np.sum(np.abs(diff_rri) > 50)
# nn20 = np.sum(np.abs(diff_rri) > 20)
# out["pNN50"] = nn50 / len(rri) * 100
# out["pNN20"] = nn20 / len(rri) * 100
#
# # Geometrical domain
# bar_y, bar_x = np.histogram(rri, bins=range(300, 2000, 8))
# bar_y, bar_x = np.histogram(rri, bins="auto")
# out["TINN"] = np.max(bar_x) - np.min(bar_x) # Triangular Interpolation of the NN Interval Histogram
# out["HTI"] = len(rri) / np.max(bar_y) # HRV Triangular Index
return out
示例14: mad
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import nanmedian [as 别名]
def mad(x, constant=1.4826):
"""Median Absolute Deviation: a "robust" version of standard deviation.
Parameters
----------
x : Union[list, np.array, pd.Series]
A vector of values.
constant : float
Scale factor. Use 1.4826 for results similar to default R.
Returns
----------
float
The MAD.
Examples
----------
>>> import neurokit2 as nk
>>> nk.mad([2, 8, 7, 5, 4, 12, 5, 1])
3.7064999999999997
References
-----------
- https://en.wikipedia.org/wiki/Median_absolute_deviation
"""
median = np.nanmedian(np.ma.array(x).compressed())
mad_value = np.nanmedian(np.abs(x - median))
mad_value = mad_value * constant
return mad_value
示例15: _standardize
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import nanmedian [as 别名]
def _standardize(data, robust=False, window=None, **kwargs):
# Compute standardized on whole data
if window is None:
if robust is False:
z = (data - np.nanmean(data, axis=0)) / np.nanstd(data, axis=0, ddof=1)
else:
z = (data - np.nanmedian(data, axis=0)) / mad(data)
# Rolling standardization on windows
else:
df = pd.DataFrame(data) # Force dataframe
if robust is False:
z = (df - df.rolling(window, min_periods=0, **kwargs).mean()) / df.rolling(
window, min_periods=0, **kwargs
).std(ddof=1)
else:
z = (df - df.rolling(window, min_periods=0, **kwargs).median()) / df.rolling(
window, min_periods=0, **kwargs
).apply(mad)
# Fill the created nans
z = z.fillna(method="bfill")
# Restore to vector or array
if z.shape[1] == 1:
z = z[0].values
else:
z = z.values
return z