本文整理汇总了Python中scipy.stats.scoreatpercentile方法的典型用法代码示例。如果您正苦于以下问题:Python stats.scoreatpercentile方法的具体用法?Python stats.scoreatpercentile怎么用?Python stats.scoreatpercentile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.stats
的用法示例。
在下文中一共展示了stats.scoreatpercentile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: scoreatpercentile
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import scoreatpercentile [as 别名]
def scoreatpercentile(a, per, limit=(), interpolation_method='lower'):
"""
This function is grabbed from scipy
"""
values = np.sort(a, axis=0)
if limit:
values = values[(limit[0] <= values) & (values <= limit[1])]
idx = per /100. * (values.shape[0] - 1)
if (idx % 1 == 0):
score = values[int(idx)]
else:
if interpolation_method == 'fraction':
score = _interpolate(values[int(idx)], values[int(idx) + 1],
idx % 1)
elif interpolation_method == 'lower':
score = values[int(np.floor(idx))]
elif interpolation_method == 'higher':
score = values[int(np.ceil(idx))]
else:
raise ValueError("interpolation_method can only be 'fraction', " \
"'lower' or 'higher'")
return score
示例2: makeadmask
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import scoreatpercentile [as 别名]
def makeadmask(cdat,min=True,getsum=False):
nx,ny,nz,Ne,nt = cdat.shape
mask = np.ones((nx,ny,nz),dtype=np.bool)
if min:
mask = cdat[:,:,:,:,:].prod(axis=-1).prod(-1)!=0
return mask
else:
#Make a map of longest echo that a voxel can be sampled with,
#with minimum value of map as X value of voxel that has median
#value in the 1st echo. N.b. larger factor leads to bias to lower TEs
emeans = cdat[:,:,:,:,:].mean(-1)
medv = emeans[:,:,:,0] == stats.scoreatpercentile(emeans[:,:,:,0][emeans[:,:,:,0]!=0],33,interpolation_method='higher')
lthrs = np.squeeze(np.array([ emeans[:,:,:,ee][medv]/3 for ee in range(Ne) ]))
if len(lthrs.shape)==1: lthrs = np.atleast_2d(lthrs).T
lthrs = lthrs[:,lthrs.sum(0).argmax()]
mthr = np.ones([nx,ny,nz,ne])
for ee in range(Ne): mthr[:,:,:,ee]*=lthrs[ee]
mthr = np.abs(emeans[:,:,:,:])>mthr
masksum = np.array(mthr,dtype=np.int).sum(-1)
mask = masksum!=0
if getsum: return mask,masksum
else: return mask
示例3: scoreatpercentile
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import scoreatpercentile [as 别名]
def scoreatpercentile(a, per, limit=(), interpolation_method='lower'):
"""
This function is grabbed from scipy
"""
values = np.sort(a, axis=0)
if limit:
values = values[(limit[0] <= values) & (values <= limit[1])]
idx = per /100. * (values.shape[0] - 1)
if (idx % 1 == 0):
score = values[idx]
else:
if interpolation_method == 'fraction':
score = _interpolate(values[int(idx)], values[int(idx) + 1],
idx % 1)
elif interpolation_method == 'lower':
score = values[np.floor(idx)]
elif interpolation_method == 'higher':
score = values[np.ceil(idx)]
else:
raise ValueError("interpolation_method can only be 'fraction', " \
"'lower' or 'higher'")
return score
示例4: _select_sigma
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import scoreatpercentile [as 别名]
def _select_sigma(X):
"""
Returns the smaller of std(X, ddof=1) or normalized IQR(X) over axis 0.
References
----------
Silverman (1986) p.47
"""
# normalize = norm.ppf(.75) - norm.ppf(.25)
normalize = 1.349
# IQR = np.subtract.reduce(percentile(X, [75,25],
# axis=axis), axis=axis)/normalize
IQR = (sap(X, 75) - sap(X, 25))/normalize
return np.minimum(np.std(X, axis=0, ddof=1), IQR)
## Univariate Rule of Thumb Bandwidths ##
示例5: test_fraction
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import scoreatpercentile [as 别名]
def test_fraction(self):
scoreatperc = stats.scoreatpercentile
# Test defaults
assert_equal(scoreatperc(list(range(10)), 50), 4.5)
assert_equal(scoreatperc(list(range(10)), 50, (2,7)), 4.5)
assert_equal(scoreatperc(list(range(100)), 50, limit=(1, 8)), 4.5)
assert_equal(scoreatperc(np.array([1, 10,100]), 50, (10,100)), 55)
assert_equal(scoreatperc(np.array([1, 10,100]), 50, (1,10)), 5.5)
# explicitly specify interpolation_method 'fraction' (the default)
assert_equal(scoreatperc(list(range(10)), 50, interpolation_method='fraction'),
4.5)
assert_equal(scoreatperc(list(range(10)), 50, limit=(2, 7),
interpolation_method='fraction'),
4.5)
assert_equal(scoreatperc(list(range(100)), 50, limit=(1, 8),
interpolation_method='fraction'),
4.5)
assert_equal(scoreatperc(np.array([1, 10,100]), 50, (10, 100),
interpolation_method='fraction'),
55)
assert_equal(scoreatperc(np.array([1, 10,100]), 50, (1,10),
interpolation_method='fraction'),
5.5)
示例6: test_lower_higher
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import scoreatpercentile [as 别名]
def test_lower_higher(self):
scoreatperc = stats.scoreatpercentile
# interpolation_method 'lower'/'higher'
assert_equal(scoreatperc(list(range(10)), 50,
interpolation_method='lower'), 4)
assert_equal(scoreatperc(list(range(10)), 50,
interpolation_method='higher'), 5)
assert_equal(scoreatperc(list(range(10)), 50, (2,7),
interpolation_method='lower'), 4)
assert_equal(scoreatperc(list(range(10)), 50, limit=(2,7),
interpolation_method='higher'), 5)
assert_equal(scoreatperc(list(range(100)), 50, (1,8),
interpolation_method='lower'), 4)
assert_equal(scoreatperc(list(range(100)), 50, (1,8),
interpolation_method='higher'), 5)
assert_equal(scoreatperc(np.array([1, 10, 100]), 50, (10, 100),
interpolation_method='lower'), 10)
assert_equal(scoreatperc(np.array([1, 10, 100]), 50, limit=(10, 100),
interpolation_method='higher'), 100)
assert_equal(scoreatperc(np.array([1, 10, 100]), 50, (1, 10),
interpolation_method='lower'), 1)
assert_equal(scoreatperc(np.array([1, 10, 100]), 50, limit=(1, 10),
interpolation_method='higher'), 10)
示例7: test_sequence_per
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import scoreatpercentile [as 别名]
def test_sequence_per(self):
x = arange(8) * 0.5
expected = np.array([0, 3.5, 1.75])
res = stats.scoreatpercentile(x, [0, 100, 50])
assert_allclose(res, expected)
assert_(isinstance(res, np.ndarray))
# Test with ndarray. Regression test for gh-2861
assert_allclose(stats.scoreatpercentile(x, np.array([0, 100, 50])),
expected)
# Also test combination of 2-D array, axis not None and array-like per
res2 = stats.scoreatpercentile(np.arange(12).reshape((3,4)),
np.array([0, 1, 100, 100]), axis=1)
expected2 = array([[0, 4, 8],
[0.03, 4.03, 8.03],
[3, 7, 11],
[3, 7, 11]])
assert_allclose(res2, expected2)
示例8: test_axis
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import scoreatpercentile [as 别名]
def test_axis(self):
scoreatperc = stats.scoreatpercentile
x = arange(12).reshape(3, 4)
assert_equal(scoreatperc(x, (25, 50, 100)), [2.75, 5.5, 11.0])
r0 = [[2, 3, 4, 5], [4, 5, 6, 7], [8, 9, 10, 11]]
assert_equal(scoreatperc(x, (25, 50, 100), axis=0), r0)
r1 = [[0.75, 4.75, 8.75], [1.5, 5.5, 9.5], [3, 7, 11]]
assert_equal(scoreatperc(x, (25, 50, 100), axis=1), r1)
x = array([[1, 1, 1],
[1, 1, 1],
[4, 4, 3],
[1, 1, 1],
[1, 1, 1]])
score = stats.scoreatpercentile(x, 50)
assert_equal(score.shape, ())
assert_equal(score, 1.0)
score = stats.scoreatpercentile(x, 50, axis=0)
assert_equal(score.shape, (3,))
assert_equal(score, [1, 1, 1])
示例9: run_isolation_forest
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import scoreatpercentile [as 别名]
def run_isolation_forest(features, id_list, fraction_of_outliers=.3):
"""Performs anomaly detection based on Isolation Forest."""
rng = np.random.RandomState(1984)
num_samples = features.shape[0]
iso_f = IsolationForest(max_samples=num_samples,
contamination=fraction_of_outliers,
random_state=rng)
iso_f.fit(features)
pred_scores = iso_f.decision_function(features)
threshold = stats.scoreatpercentile(pred_scores, 100 * fraction_of_outliers)
outlying_ids = id_list[pred_scores < threshold]
return outlying_ids
示例10: __call__
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import scoreatpercentile [as 别名]
def __call__(self, y, pred, sample_weight=None):
pred = pred.ravel()
diff = y - pred
gamma = self.gamma
if gamma is None:
if sample_weight is None:
gamma = stats.scoreatpercentile(np.abs(diff), self.alpha * 100)
else:
gamma = _weighted_percentile(np.abs(diff), sample_weight, self.alpha * 100)
gamma_mask = np.abs(diff) <= gamma
if sample_weight is None:
sq_loss = np.sum(0.5 * diff[gamma_mask] ** 2.0)
lin_loss = np.sum(gamma * (np.abs(diff[~gamma_mask]) - gamma / 2.0))
loss = (sq_loss + lin_loss) / y.shape[0]
else:
sq_loss = np.sum(0.5 * sample_weight[gamma_mask] * diff[gamma_mask] ** 2.0)
lin_loss = np.sum(gamma * sample_weight[~gamma_mask] *
(np.abs(diff[~gamma_mask]) - gamma / 2.0))
loss = (sq_loss + lin_loss) / sample_weight.sum()
return loss
示例11: _get_support_mask
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import scoreatpercentile [as 别名]
def _get_support_mask(self):
check_is_fitted(self, 'scores_')
# Cater for NaNs
if self.percentile == 100:
return np.ones(len(self.scores_), dtype=np.bool)
elif self.percentile == 0:
return np.zeros(len(self.scores_), dtype=np.bool)
scores = _clean_nans(self.scores_)
treshold = stats.scoreatpercentile(scores,
100 - self.percentile)
mask = scores > treshold
ties = np.where(scores == treshold)[0]
if len(ties):
max_feats = int(len(scores) * self.percentile / 100)
kept_ties = ties[:max_feats - mask.sum()]
mask[kept_ties] = True
return mask
示例12: find_percent_point
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import scoreatpercentile [as 别名]
def find_percent_point(percents, y):
"""
可视化技术线比例分割的区域, 针对输入的比例迭代操作后
分别使用stats.scoreatpercentile和 (y.max() - y.min()) * pt + y.min()两种
方式进行计算的分割值, 返回对象为比例值为key的字典对象
eg:
input:
percents = (0.1, 0.9)
output:
{0.1: (15.732749999999999, 15.5075), 0.9: (31.995000000000005, 34.387500000000003)}
:param percents: 可迭代序列,eg: (0.1, 0.9), [0.3, 0,4, 0.8]
:param y: 计算分割线的序列
:return: 比例值为key的字典对象
"""
percent_point_dict = {pt: (stats.scoreatpercentile(y, np.round(pt * 100, 1)), (y.max() - y.min()) * pt + y.min())
for pt in percents}
return percent_point_dict
# noinspection PyTypeChecker
示例13: find_golden_point_ex
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import scoreatpercentile [as 别名]
def find_golden_point_ex(x, y, show=False):
"""统计黄金分割计算方法,以及对应简单可视化操作"""
sp382 = stats.scoreatpercentile(y, 38.2)
sp618 = stats.scoreatpercentile(y, 61.8)
sp50 = stats.scoreatpercentile(y, 50.0)
if show:
with plt_show():
# 可视化操作
plt.plot(x, y)
plt.axhline(sp50, color='c')
plt.axhline(sp618, color='r')
plt.axhline(sp382, color='g')
_ = plt.setp(plt.gca().get_xticklabels(), rotation=30)
plt.legend(['TLine', 'sp50', 'sp618', 'sp382'],
bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
return sp382, sp50, sp618
示例14: sample_571_1
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import scoreatpercentile [as 别名]
def sample_571_1():
"""
5.7.1 黄金分割线的定义方式
:return:
"""
# 收盘价格序列中的最大值
cs_max = tsla_df.close.max()
# 收盘价格序列中的最小值
cs_min = tsla_df.close.min()
sp382 = (cs_max - cs_min) * 0.382 + cs_min
sp618 = (cs_max - cs_min) * 0.618 + cs_min
print('视觉上的382: ' + str(round(sp382, 2)))
print('视觉上的618: ' + str(round(sp618, 2)))
sp382_stats = stats.scoreatpercentile(tsla_df.close, 38.2)
sp618_stats = stats.scoreatpercentile(tsla_df.close, 61.8)
print('统计上的382: ' + str(round(sp382_stats, 2)))
print('统计上的618: ' + str(round(sp618_stats, 2)))
# noinspection PyTypeChecker
示例15: get_trim_mean
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import scoreatpercentile [as 别名]
def get_trim_mean(values, percentage=20):
"""
Get the trimmed mean of a list of values.
Explanation: This function finds the arithmetic mean of given values,
ignoring values outside the given limits.
Args:
values (list): The list of values
percentage (int): The percentage to be trimmed
Returns:
float: Trimmed mean. In case trimmed mean calculation fails,
the regular mean average is returned
"""
lower_limit = scoreatpercentile(values, percentage)
upper_limit = scoreatpercentile(values, 100 - percentage)
try:
return tmean(values, limits=(lower_limit, upper_limit))
except ValueError:
log.warning(
f"Failed to calculate the trimmed mean of {values}. The "
f"Regular mean average will be calculated instead"
)
return sum(values) / len(values)