# Python scipy.stats.scoreatpercentile() Examples

The following are code examples for showing how to use scipy.stats.scoreatpercentile(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
 Project: Anamoly-Detection   Author: msmsk05   File: base.py    BSD 2-Clause "Simplified" License 6 votes
```def _process_decision_scores(self):
"""Internal function to calculate key attributes:

- threshold_: used to decide the binary label
- labels_: binary labels of training data

Returns
-------
self
"""

self.threshold_ = scoreatpercentile(self.decision_scores_,
100 * (1 - self.contamination))
self.labels_ = (self.decision_scores_ > self.threshold_).astype(
'int').ravel()

# calculate for predict_proba()

self._mu = np.mean(self.decision_scores_)
self._sigma = np.std(self.decision_scores_)

return self

# noinspection PyMethodParameters ```
Example 2
 Project: me-ica   Author: ME-ICA   File: tedana.py    GNU Lesser General Public License v2.1 6 votes
```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 ```
Example 3
 Project: me-ica   Author: ME-ICA   File: tedana.py    GNU Lesser General Public License v2.1 6 votes
```def makeadmask(cdat,min=True,getsum=False):

nx,ny,nz,Ne,nt = cdat.shape

if min:
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
Example 4
 Project: me-ica   Author: ME-ICA   File: t2smap.py    GNU Lesser General Public License v2.1 6 votes
```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 ```
Example 5
```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) ```
Example 6
```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) ```
Example 7
```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) ```
Example 8
```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]) ```
Example 9
 Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_stats.py    GNU General Public License v3.0 6 votes
```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) ```
Example 10
 Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_stats.py    GNU General Public License v3.0 6 votes
```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) ```
Example 11
 Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_stats.py    GNU General Public License v3.0 6 votes
```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) ```
Example 12
 Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_stats.py    GNU General Public License v3.0 6 votes
```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]) ```
Example 13
```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 ## ```
Example 14
```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) ```
Example 15
```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) ```
Example 16
```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) ```
Example 17
```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]) ```
Example 18
```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) ```
Example 19
```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) ```
Example 20
```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) ```
Example 21
```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) ```
Example 22
```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) ```
Example 23
```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]) ```
Example 24
```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) ```
Example 25
```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) ```
Example 26
```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) ```
Example 27
```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]) ```
Example 28
```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) ```
Example 29
```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) ```
Example 30
```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) ```
Example 31
```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]) ```
Example 32
```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)

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:
lin_loss = np.sum(gamma * sample_weight[~gamma_mask] *
loss = (sq_loss + lin_loss) / sample_weight.sum()
return loss ```
Example 33
```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)
ties = np.where(scores == treshold)[0]
if len(ties):
max_feats = len(scores) * self.percentile // 100
Example 34
```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)

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:
lin_loss = np.sum(gamma * sample_weight[~gamma_mask] *
loss = (sq_loss + lin_loss) / sample_weight.sum()
return loss ```
Example 35
```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)
ties = np.where(scores == treshold)[0]
if len(ties):
max_feats = len(scores) * self.percentile // 100
Example 36
```def Denoise(data):
"""
This function implements the denoising given in the url below:
http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=4607982&tag=1

with title "Astronomical Spectra Denoising Based on Simplifed SURE-LET Wavelet Thresholding"

The data should be a kglib.utils.DataStructures.xypoint instance.
"""
WC = mlpy.wavelet.dwt(y, 'd', 10, 0)
# Figure out the unknown parameter 'a'
sum1 = 0.0
sum2 = 0.0
numlevels = int(np.log2(WC.size))
start = 2 ** (numlevels - 1)
median = np.median(WC[start:])
sigma = np.median(np.abs(WC[start:] - median)) / 0.6745
for w in WC:
phi = w * np.exp(-w ** 2 / (12.0 * sigma ** 2))
dphi = np.exp(-w ** 2 / (12.0 * sigma ** 2)) * (1 - 2 * w ** 2 / (12 * sigma ** 2) )
sum1 += sigma ** 2 * dphi
sum2 += phi ** 2
a = -sum1 / sum2

WC = WC + a * WC * np.exp(-WC ** 2 / (12 * sigma ** 2))

# Now, do a soft threshold
threshold = scoreatpercentile(WC, 80.0)
WC[np.abs(WC) <= threshold] = 0.0
WC[np.abs(WC) > threshold] -= threshold * np.sign(WC[np.abs(WC) > threshold])

#Transform back
y2 = mlpy.wavelet.idwt(WC, 'd', 10)
data.y = y2[boolarr]
return data

# Kept for legacy support, since I was using Denoise3 in several codes in the past. ```
Example 37
```def mad_outliers(data, genes, threshold, percentile=95, as_json=True):
res = dr.get_dataset_ensembl_info()
outliers_id = []
if as_json:
yield ("{\"outliers\":[")
for g in genes:
row_values = data.loc[g, :]
cut_row_values = row_values
med = cut_row_values.median()

filtered = (cut_row_values - med) / row_mad
support = len(filtered[filtered > threshold])

if scoreatpercentile(filtered, 95) > threshold:

info = [gene for gene in res if gene.ensemblgeneid == g][0]
formatted_info = {"id": g, "name": info.genename, "type": info.genetype, "samples": str(support),
"distance": "NA"}
jinfo = json.dumps(formatted_info)
jinfo += ","
outliers_id.append(g)
print("outlier found :" + g)
if as_json:
yield (jinfo)
else:
yield (formatted_info)
if len(outliers_id) > 0:
pr.save_outliers(1, outliers_id)

if as_json:
yield ("]}") ```
Example 38
 Project: Anamoly-Detection   Author: msmsk05   File: utility.py    BSD 2-Clause "Simplified" License 5 votes
```def score_to_label(pred_scores, outliers_fraction=0.1):
"""Turn raw outlier outlier scores to binary labels (0 or 1).

Parameters
----------
pred_scores : list or numpy array of shape (n_samples,)
Raw outlier scores. Outliers are assumed have larger values.

outliers_fraction : float in (0,1)
Percentage of outliers.

Returns
-------
outlier_labels : numpy array of shape (n_samples,)
For each observation, tells whether or not
it should be considered as an outlier according to the
fitted model. Return the outlier probability, ranging
in [0,1].
"""
# check input values
pred_scores = column_or_1d(pred_scores)
check_parameter(outliers_fraction, 0, 1)

threshold = scoreatpercentile(pred_scores, 100 * (1 - outliers_fraction))
pred_labels = (pred_scores > threshold).astype('int')
return pred_labels ```
Example 39
 Project: pystran   Author: stijnvanhoey   File: evaluationfunctions.py    BSD 3-Clause "New" or "Revised" License 5 votes
```def SFDCE(self):
'''
Slope of the flow duration curve error

Notes
-------
Based on the FDC, measures how well the model captures the
distribution of mid-level flow. The slope of a watershed's flow
duration curve indicates the variability, or flashiness, of its
flow magnitudes. The SFDCE metric is thus simply the absolute
error in the slope of the flow duration curve between the 30
and 70 percentile flows.

References
------------
..  [E14] van Werkhoven, Kathryn, Thorsten Wagener, Patrick Reed,
and Yong Tang. Sensitivity-guided Reduction of Parametric
Dimensionality for Multi-objective Calibration of Watershed
Models. Advances in Water Resources 32, no. 8 (2009): 1154â€“1169.

'''
QMeas30=stats.scoreatpercentile(self.observed, 30.)
QMeas70=stats.scoreatpercentile(self.observed, 70.)

QMod30=stats.scoreatpercentile(self.modelled, 30.)
QMod70=stats.scoreatpercentile(self.modelled, 70.)

objStat = abs((QMod70-QMod30)/40-(QMeas70-QMeas30)/40)
return objStat ```
Example 40
 Project: me-ica   Author: ME-ICA   File: tedana.py    GNU Lesser General Public License v2.1 5 votes
```def eimask(dd,ees=None):
if ees==None: ees=range(dd.shape[1])
for ee in ees:
print ee
lthr = 0.001*scoreatpercentile(dd[:,ee,:].flatten(),98)
hthr = 5*scoreatpercentile(dd[:,ee,:].flatten(),98)
print lthr,hthr
Example 41
 Project: me-ica   Author: ME-ICA   File: t2smap.py    GNU Lesser General Public License v2.1 5 votes
```def makemask(cdat,min=True,getsum=False):

nx,ny,nz,Ne,nt = cdat.shape

if min:
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
Example 42
 Project: DRCOG_Urbansim   Author: apdjustino   File: functions.py    GNU Affero General Public License v3.0 5 votes
```def scoreatpercentile(v, percentile):
return stats.scoreatpercentile(v, percentile) ```
Example 43
 Project: DRCOG_Urbansim   Author: apdjustino   File: functions.py    GNU Affero General Public License v3.0 5 votes
```def test_scoreatpercentile(self):
import numpy as np
numbers = np.random.permutation(100) + 1
self.assert_( scoreatpercentile(numbers, 0), 0)
self.assert_( scoreatpercentile(numbers, 5), 5)
self.assert_( scoreatpercentile(numbers, 21), 21)
self.assert_( scoreatpercentile(numbers, 100), 100)

# the function_tester is just set up for unary functions, and safe_array_divide takes two numpy arguments,
# so it is tested separately ```
Example 44
```def test_basic(self):
x = arange(8) * 0.5
assert_equal(stats.scoreatpercentile(x, 0), 0.)
assert_equal(stats.scoreatpercentile(x, 100), 3.5)
assert_equal(stats.scoreatpercentile(x, 50), 1.75) ```
Example 45
```def test_empty(self):
assert_equal(stats.scoreatpercentile([], 50), np.nan)
assert_equal(stats.scoreatpercentile(np.array([[], []]), 50), np.nan)
assert_equal(stats.scoreatpercentile([], [50, 99]), [np.nan, np.nan]) ```
Example 46
 Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_stats.py    GNU General Public License v3.0 5 votes
```def test_basic(self):
x = arange(8) * 0.5
assert_equal(stats.scoreatpercentile(x, 0), 0.)
assert_equal(stats.scoreatpercentile(x, 100), 3.5)
assert_equal(stats.scoreatpercentile(x, 50), 1.75) ```
Example 47
 Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: test_stats.py    GNU General Public License v3.0 5 votes
```def test_empty(self):
assert_equal(stats.scoreatpercentile([], 50), np.nan)
assert_equal(stats.scoreatpercentile(np.array([[], []]), 50), np.nan)
assert_equal(stats.scoreatpercentile([], [50, 99]), [np.nan, np.nan]) ```
Example 48
```def calculate(self,data,axis=0):
values=[]
for stat in self.stats:
if stat.startswith('mean'):
ndata=np.mean(data,axis=axis)
elif stat.startswith('std'):
ndata=np.std(data,axis=axis)
elif stat.startswith('median'):
ndata=np.median(data,axis=axis)
elif stat.startswith('q1'):
ndata=[scoreatpercentile(data[:,col],25)
for col in range(data.shape[1]) ]
elif stat.startswith('q2'):
ndata=[scoreatpercentile(data[:,col],50)
for col in range(data.shape[1]) ]
elif stat.startswith('q3'):
ndata=[scoreatpercentile(data[:,col],75)
for col in range(data.shape[1]) ]
elif stat.startswith('skew'):
ndata=[skew(data[:,col])
for col in range(data.shape[1]) ]
elif stat.startswith('kurtosis'):
ndata=[kurtosis(data[:,col])
for col in range(data.shape[1]) ]
elif stat.startswith('max'):
ndata=np.amax(data,axis=axis)
elif stat.startswith('min'):
ndata=np.amin(data,axis=axis)
values.append(ndata)
return np.hstack(values) ```
Example 49
 Project: tedana   Author: ME-ICA   File: _utils.py    GNU Lesser General Public License v2.1 5 votes
```def eimask(dd, ees=None):
"""
Returns mask for data between [0.001, 5] * 98th percentile of dd

Parameters
----------
dd : (S x E x T) array_like
Input data, where `S` is samples, `E` is echos, and `T` is time
ees : (N,) :obj:`list`
Indices of echos to assess from `dd` in calculating output

Returns
-------
imask : (S x N) :obj:`numpy.ndarray`
Boolean array denoting
"""

if ees is None:
ees = range(dd.shape[1])
for ee in ees:
if len(ees) == 1:
else:
perc98 = stats.scoreatpercentile(dd[:, ee, :].flatten(), 98,
interpolation_method='lower')
lthr, hthr = 0.001 * perc98, 5 * perc98
'{:.03f} {:.03f}'.format(lthr, hthr))
m = dd[:, ee, :].mean(axis=1)
imask[np.logical_and(m > lthr, m < hthr), ee] = True

Example 50
```def bestval (q):
if not isinstance (q, Uval):
return q

# http://www.roma1.infn.it/~dagos/asymmetric/node2.html says that the
# "best" value is the barycenter of the distribution, i.e. the mean of our
# samples (right?), not the median. TODO: investigate.

return sap (q.d, 50.000) ```
Example 51
```def stdval (q):
if not isinstance (q, Uval):
return 0.0

# TODO: figure out how to handle asymmetric error distributions! Should
# pay attention to where this function is actually used to think about
# the semantics that'd be the most helpful.

return 0.5 * (sap (q.d, 84.134) - sap (q.d, 15.866)) ```
Example 52
```def _percentiles(self,x):
p = [stats.scoreatpercentile(x,per) for per in
(1,5,10,25,50,75,90,95,99)]
return p ```
Example 53
```def fitquantilesgmm(distfn, x, start=None, pquant=None, frozen=None):
if pquant is None:
pquant = np.array([0.01, 0.05,0.1,0.4,0.6,0.9,0.95,0.99])
if start is None:
if hasattr(distfn, '_fitstart'):
start = distfn._fitstart(x)
else:
start = [1]*distfn.numargs + [0.,1.]
#TODO: vectorize this:
xqs = [stats.scoreatpercentile(x, p) for p in pquant*100]
mom2s = None
parest = optimize.fmin(lambda params:np.sum(
momentcondquant(distfn, params, mom2s,(pquant,xqs), shape=None)**2), start)
return parest ```
Example 54
```def __init__(self, endog, exog, instrument, **kwds):
#TODO: something wrong with super
super(DistQuantilesGMM, self).__init__(endog, exog, instrument)
#self.func = func
self.epsilon_iter = 1e-5

self.distfn = kwds['distfn']
#done by super doesn't work yet
#TypeError: super does not take keyword arguments
self.endog = endog

#make this optional for fit
if not 'pquant' in kwds:
self.pquant = pquant = np.array([0.01, 0.05,0.1,0.4,0.6,0.9,0.95,0.99])
else:
self.pquant = pquant = kwds['pquant']

#TODO: vectorize this: use edf
self.xquant = np.array([stats.scoreatpercentile(endog, p) for p
in pquant*100])
self.nmoms = len(self.pquant)

#TODOcopied from GMM, make super work
self.endog = endog
self.exog = exog
self.instrument = instrument
self.results = GMMResults(model=self)
#self.__dict__.update(kwds)
self.epsilon_iter = 1e-6 ```
Example 55
```def test_basic(self):
x = arange(8) * 0.5
assert_equal(stats.scoreatpercentile(x, 0), 0.)
assert_equal(stats.scoreatpercentile(x, 100), 3.5)
assert_equal(stats.scoreatpercentile(x, 50), 1.75) ```
Example 56
```def test_empty(self):
assert_equal(stats.scoreatpercentile([], 50), np.nan)
assert_equal(stats.scoreatpercentile(np.array([[], []]), 50), np.nan)
assert_equal(stats.scoreatpercentile([], [50, 99]), [np.nan, np.nan]) ```
Example 57
 Project: mapper   Author: timothy-mcroy   File: _mapper.py    GNU General Public License v2.0 5 votes
```def crop(f, a, b):
from scipy.stats import scoreatpercentile
s1 = scoreatpercentile(f, a)
s2 = scoreatpercentile(f, 100-b)
assert s1<=s2
return np.logical_and(f>=s1, f<=s2) ```
Example 58
```def filter_data(x, percentile, no_zeros=True):
"""Remove data from an array which is below a certain
percentile value. Optionally, if no_zeros is specified,
also remove any zeros from the array.

If removing values would result in returning an empty array,
do nothing.

:param x: Output values are taken from this array
:type x: :class:`~numpy.ndarray`
:param float percentile: Percentile at which to remove values \
(e.g. if percentile=95.0, only the top 5% of values \
are retained).
:param bool no_zeros: If True, also discard any values equal \
to zero from the output array.
:returns: New array contining values from x that pass the filter.
"""

percentile_score = scoreatpercentile(x, percentile)
less_than_percentile = list(x < percentile_score)

if no_zeros:
not_a_zero = x > 0

# only keep points which are both less than percentile AND not a zero
points_to_keep = list(map(all, list(zip(less_than_percentile, not_a_zero))))

else:
points_to_keep = less_than_percentile

out_data = x[points_to_keep]

if out_data.size:

return out_data

if no_zeros:

return x[not_a_zero]

return x ```
Example 59
```def test_basic(self):
x = arange(8) * 0.5
assert_equal(stats.scoreatpercentile(x, 0), 0.)
assert_equal(stats.scoreatpercentile(x, 100), 3.5)
assert_equal(stats.scoreatpercentile(x, 50), 1.75) ```
Example 60
```def test_2D(self):
x = array([[1, 1, 1],
[1, 1, 1],
[4, 4, 3],
[1, 1, 1],
[1, 1, 1]])
assert_array_equal(stats.scoreatpercentile(x, 50), [1, 1, 1]) ```
Example 61
```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) ```
Example 62
```def test_exception(self):
assert_raises(ValueError, stats.scoreatpercentile, [1, 2], 56,
interpolation_method='foobar')
assert_raises(ValueError, stats.scoreatpercentile, [1], 101)
assert_raises(ValueError, stats.scoreatpercentile, [1], -1) ```
Example 63
```def test_basic(self):
x = arange(8) * 0.5
assert_equal(stats.scoreatpercentile(x, 0), 0.)
assert_equal(stats.scoreatpercentile(x, 100), 3.5)
assert_equal(stats.scoreatpercentile(x, 50), 1.75) ```
Example 64
```def test_empty(self):
assert_equal(stats.scoreatpercentile([], 50), np.nan)
assert_equal(stats.scoreatpercentile(np.array([[], []]), 50), np.nan)
assert_equal(stats.scoreatpercentile([], [50, 99]), [np.nan, np.nan]) ```
Example 65
 Project: physical_validation   Author: shirtsgroup   File: trajectory.py    GNU Lesser General Public License v2.1 5 votes
```def cut_tails(traj, cut, verbose=False, name=None):
traj = np.array(traj)
dc = 100 * cut
if traj.ndim == 1:
with warnings.catch_warnings():
# With some combination of python version / scipy version,
# scoreatpercentile throws a warning
warnings.filterwarnings('ignore', category=FutureWarning)
tmax = stats.scoreatpercentile(traj, 100 - dc)
tmin = stats.scoreatpercentile(traj, dc)
t = traj[(tmin <= traj) * (traj <= tmax)]
n0 = traj.size
n = t.size
elif traj.ndim == 2:
with warnings.catch_warnings():
# With some combination of python version / scipy version,
# scoreatpercentile throws a warning
warnings.filterwarnings('ignore', category=FutureWarning)
tmax = stats.scoreatpercentile(traj, 100 - dc, axis=1)
tmin = stats.scoreatpercentile(traj, dc, axis=1)
t = traj[:,
(tmin[0] <= traj[0]) * (tmin[1] <= traj[1]) *
(tmax[0] >= traj[0]) * (tmax[1] >= traj[1])]
n0 = traj.shape[1]
n = t.shape[1]
else:
raise NotImplementedError('trajectory.cut_tails() is not implemented for '
'trajectories with more than 2 dimension.')

if verbose:
if not name:
name = 'Trajectory'
print('{:s} tails (cut = {:.2%}): {:n} frames ({:.2%} of trajectory) were cut'.format(
name, cut, n0 - n, (n0-n)/n0))

return t ```
Example 66
```def plot_betas(unshuffled, shuffled, filename=None, zoom_in_top_percentile=100, zoom_in_bottom_percentile=0, xlabel="Causal Coefficients", ylabel="Count", title="Histogram of Causal Coefficients", nbins=30):

both = np.concatenate((unshuffled, shuffled))

top_percentile = stats.scoreatpercentile(both, zoom_in_top_percentile)
bottom_percentile = stats.scoreatpercentile(both, zoom_in_bottom_percentile)

pboth = both[np.logical_and(both >= bottom_percentile, both <= top_percentile)]
punshuffled = unshuffled[np.logical_and(unshuffled >= bottom_percentile, unshuffled <= top_percentile)]
pshuffled = shuffled[np.logical_and(shuffled >= bottom_percentile, shuffled <= top_percentile)]

bins = np.linspace(min(pboth), max(pboth), nbins)

fig = plt.figure(figsize=(12,8))
plt.hist(punshuffled, alpha=0.5, color='red', label="Normal", bins=bins)
plt.hist(pshuffled, alpha=0.5, color='blue', label="Randomized", bins=bins)

plt.legend(fontsize=20)
plt.xlabel(xlabel, fontsize=25)
plt.ylabel(ylabel, fontsize=25)
plt.title(title, fontsize=25)

if filename != None:
print("Overlaid betas saved to ", filename)
fig.savefig(filename)
plt.show()
plt.close() ```
Example 67
```def test_basic(self):
x = arange(8) * 0.5
assert_equal(stats.scoreatpercentile(x, 0), 0.)
assert_equal(stats.scoreatpercentile(x, 100), 3.5)
assert_equal(stats.scoreatpercentile(x, 50), 1.75) ```
Example 68
```def test_empty(self):
assert_equal(stats.scoreatpercentile([], 50), np.nan)
assert_equal(stats.scoreatpercentile(np.array([[], []]), 50), np.nan)
assert_equal(stats.scoreatpercentile([], [50, 99]), [np.nan, np.nan]) ```
Example 69
 Project: rainfall-teleconnections   Author: niklasboers   File: events_from_data.py    GNU General Public License v3.0 5 votes
```def ec_wd(ts, perc):
th = st.scoreatpercentile(ts[ts > 1], perc)
ts = np.where(ts > th)[0]
noe = ts.shape[0]
if noe > 2 and th > 2:
return ts, noe, th
else:
return np.zeros(1), 0, 0 ```
Example 70
```def test_basic(self):
x = arange(8) * 0.5
assert_equal(stats.scoreatpercentile(x, 0), 0.)
assert_equal(stats.scoreatpercentile(x, 100), 3.5)
assert_equal(stats.scoreatpercentile(x, 50), 1.75) ```
Example 71
```def test_empty(self):
assert_equal(stats.scoreatpercentile([], 50), np.nan)
assert_equal(stats.scoreatpercentile(np.array([[], []]), 50), np.nan)
assert_equal(stats.scoreatpercentile([], [50, 99]), [np.nan, np.nan]) ```
Example 72
```def fit(self, X, y, sample_weight=None):
if sample_weight is None:
self.quantile = stats.scoreatpercentile(y, self.alpha * 100.0)
else:
self.quantile = _weighted_percentile(y, sample_weight, self.alpha * 100.0) ```
Example 73
```def negative_gradient(self, y, pred, sample_weight=None, **kargs):
pred = pred.ravel()
diff = y - pred
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)
residual = np.zeros((y.shape[0],), dtype=np.float64)
self.gamma = gamma
return residual ```
Example 74
```def fit(self, X, y, sample_weight=None):
if sample_weight is None:
self.quantile = stats.scoreatpercentile(y, self.alpha * 100.0)
else:
self.quantile = _weighted_percentile(y, sample_weight, self.alpha * 100.0) ```
Example 75
```def negative_gradient(self, y, pred, sample_weight=None, **kargs):
pred = pred.ravel()
diff = y - pred
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)
residual = np.zeros((y.shape[0],), dtype=np.float64)
self.gamma = gamma
return residual ```
Example 76
 Project: Anamoly-Detection   Author: msmsk05   File: utility.py    BSD 2-Clause "Simplified" License 4 votes
```def get_label_n(y, y_pred, n=None):
"""Function to turn raw outlier scores into binary labels by assign 1
to top n outlier scores.

Parameters
----------
y : list or numpy array of shape (n_samples,)
The ground truth. Binary (0: inliers, 1: outliers).

y_pred : list or numpy array of shape (n_samples,)
The raw outlier scores as returned by a fitted model.

n : int, optional (default=None)
The number of outliers. if not defined, infer using ground truth.

Returns
-------
labels : numpy array of shape (n_samples,)
binary labels 0: normal points and 1: outliers

Examples
--------
>>> from pyod.utils.utility import get_label_n
>>> y = [0, 1, 1, 0, 0, 0]
>>> y_pred = [0.1, 0.5, 0.3, 0.2, 0.7]
>>> get_label_n(y, y_pred)
>>> [0, 1, 0, 0, 1]

"""

# enforce formats of inputs
y = column_or_1d(y)
y_pred = column_or_1d(y_pred)

check_consistent_length(y, y_pred)
y_len = len(y)  # the length of targets

# calculate the percentage of outliers
if n is not None:
outliers_fraction = n / y_len
else:
outliers_fraction = np.count_nonzero(y) / y_len

threshold = scoreatpercentile(y_pred, 100 * (1 - outliers_fraction))
y_pred = (y_pred > threshold).astype('int')

return y_pred ```
Example 77
```def __init__( self, input_file, args ):
self.args = args
toskip = [int(l) for l in self.args.skip_rows.split(",")]  if self.args.skip_rows else []
for t in toskip:
for i,m in enumerate(mdr):
if t <= m:
else:
header = self.args.fname_row if self.args.fname_row > -1 else None
input_file, sep = self.args.sep, # skipinitialspace = True,
skiprows = sorted(toskip) if isinstance(toskip, list) else toskip,
index_col = self.args.sname_row if self.args.sname_row > -1 else None
)

def select( perc, top  ):
self.table['perc'] = self.table.apply(lambda x: stats.scoreatpercentile(x,perc),axis=1)
m = sorted(self.table['perc'])[-top]
self.table = self.table[self.table['perc'] >= m ]
del self.table['perc']

if not self.args.def_na is None:
self.table = self.table.fillna( self.args.def_na )

if self.args.ftop:
select( self.args.fperc, self.args.ftop )

if self.args.stop:
self.table = self.table.T
select( self.args.sperc, self.args.stop )
self.table = self.table.T

Example 78
 Project: skutil   Author: tgsmith61591   File: one_way_fs.py    BSD 3-Clause "New" or "Revised" License 4 votes
```def _select_features(self, all_scores, all_pvalues, feature_names):
"""This function selects the top ``percentile`` of
features from the F-scores.

Parameters
----------

all_scores : np.ndarray (float)
The scores

all_pvalues : np.ndarray (float)
The p-values

feature_names : array_like (str)
The list of names that are eligible for drop

Returns
-------

list : the features to drop
"""
percentile = self.percentile

# compute which features to keep or drop
if percentile == 100:
return []
elif percentile == 0:
return feature_names
else:
all_scores = _clean_nans(all_scores)
thresh = stats.scoreatpercentile(all_scores, 100 - percentile)

ties = np.where(all_scores == thresh)[0]
if len(ties):
max_feats = int(len(all_scores) * percentile / 100)

# inverse, since we're recording which features to DROP, not keep

# now se the drop as the inverse mask
Example 79
```def qqline(ax, line, x=None, y=None, dist=None, fmt='r-'):
"""
Plot a reference line for a qqplot.

Parameters
----------
ax : matplotlib axes instance
The axes on which to plot the line
line : str {'45','r','s','q'}
Options for the reference line to which the data is compared.:

- '45' - 45-degree line
- 's'  - standardized line, the expected order statistics are scaled by
the standard deviation of the given sample and have the mean
- 'r'  - A regression line is fit
- 'q'  - A line is fit through the quartiles.
- None - By default no reference line is added to the plot.

x : array
X data for plot. Not needed if line is '45'.
y : array
Y data for plot. Not needed if line is '45'.
dist : scipy.stats.distribution
A scipy.stats distribution, needed if line is 'q'.

Notes
-----
There is no return value. The line is plotted on the given `ax`.
"""
if line == '45':
end_pts = lzip(ax.get_xlim(), ax.get_ylim())
end_pts[0] = min(end_pts[0])
end_pts[1] = max(end_pts[1])
ax.plot(end_pts, end_pts, fmt)
ax.set_xlim(end_pts)
ax.set_ylim(end_pts)
return # does this have any side effects?
if x is None and y is None:
raise ValueError("If line is not 45, x and y cannot be None.")
elif line == 'r':
# could use ax.lines[0].get_xdata(), get_ydata(),
# but don't know axes are 'clean'
ax.plot(x,y,fmt)
elif line == 's':
m,b = y.std(), y.mean()
ref_line = x*m + b
ax.plot(x, ref_line, fmt)
elif line == 'q':
_check_for_ppf(dist)
q25 = stats.scoreatpercentile(y, 25)
q75 = stats.scoreatpercentile(y, 75)
theoretical_quartiles = dist.ppf([0.25, 0.75])
m = (q75 - q25) / np.diff(theoretical_quartiles)
b = q25 - m*theoretical_quartiles[0]
ax.plot(x, m*x + b, fmt)

#about 10x faster than plotting_position in sandbox and mstats ```
Example 80
```def _filter_ridge_lines(cwt, ridge_lines, window_size=None, min_length=None,
min_snr=1, noise_perc=10):
"""
Filter ridge lines according to prescribed criteria. Intended
to be used for finding relative maxima.

Parameters
----------
cwt : 2-D ndarray
Continuous wavelet transform from which the `ridge_lines` were defined.
ridge_lines : 1-D sequence
Each element should contain 2 sequences, the rows and columns
of the ridge line (respectively).
window_size : int, optional
Size of window to use to calculate noise floor.
Default is ``cwt.shape[1] / 20``.
min_length : int, optional
Minimum length a ridge line needs to be acceptable.
Default is ``cwt.shape[0] / 4``, ie 1/4-th the number of widths.
min_snr : float, optional
Minimum SNR ratio. Default 1. The signal is the value of
the cwt matrix at the shortest length scale (``cwt[0, loc]``), the
noise is the `noise_perc`th percentile of datapoints contained within a
window of `window_size` around ``cwt[0, loc]``.
noise_perc : float, optional
When calculating the noise floor, percentile of data points
examined below which to consider noise. Calculated using
scipy.stats.scoreatpercentile.

References
----------
Bioinformatics (2006) 22 (17): 2059-2065. doi: 10.1093/bioinformatics/btl355
http://bioinformatics.oxfordjournals.org/content/22/17/2059.long

"""
num_points = cwt.shape[1]
if min_length is None:
min_length = np.ceil(cwt.shape[0] / 4)
if window_size is None:
window_size = np.ceil(num_points / 20)
hf_window = window_size / 2

#Filter based on SNR
row_one = cwt[0, :]
noises = np.zeros_like(row_one)
for ind, val in enumerate(row_one):
window = np.arange(max([ind - hf_window, 0]), min([ind + hf_window, num_points]))
window = window.astype(int)
noises[ind] = scoreatpercentile(row_one[window], per=noise_perc)

def filt_func(line):
if len(line[0]) < min_length:
return False
snr = abs(cwt[line[0][0], line[1][0]] / noises[line[1][0]])
if snr < min_snr:
return False
return True

return list(filter(filt_func, ridge_lines)) ```