Skip to content

Commit

Permalink
ENH: Only convert a list to a numpy array once.
Browse files Browse the repository at this point in the history
  • Loading branch information
pvanmulbregt committed Apr 8, 2018
1 parent ffb38ed commit d40dd94
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions scipy/stats/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -1784,22 +1784,22 @@ def percentileofscore(a, score, kind='rank'):
"""
if np.isnan(score):
return np.nan
a = np.asarray(a)
n = len(a)
if n == 0:
return 100.0

if kind == 'rank':
# Use np.array(score) to avoid converting a to an np.array
left = np.count_nonzero(a < np.array(score))
right = np.count_nonzero(a <= np.array(score))
left = np.count_nonzero(a < score)
right = np.count_nonzero(a <= score)
pct = (right + left + (1 if right > left else 0)) * 50.0/n
return pct
elif kind == 'strict':
return np.count_nonzero(a < np.array(score)) / float(n) * 100
return np.count_nonzero(a < score) / float(n) * 100
elif kind == 'weak':
return np.count_nonzero(a <= np.array(score)) / float(n) * 100
return np.count_nonzero(a <= score) / float(n) * 100
elif kind == 'mean':
pct = (np.count_nonzero(a < np.array(score)) + np.count_nonzero(a <= np.array(score))) / float(n) * 50
pct = (np.count_nonzero(a < score) + np.count_nonzero(a <= score)) / float(n) * 50
return pct
else:
raise ValueError("kind can only be 'rank', 'strict', 'weak' or 'mean'")
Expand Down

0 comments on commit d40dd94

Please sign in to comment.