Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 15bcd04

Browse files
committed
Merged in sergem/numpy/digitize2 (pull request ganesh-k13#14)
Digitize2
2 parents 6b082f7 + 95ea6d1 commit 15bcd04

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

numpy/core/multiarray.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from _numpypy.multiarray import *
22
from _numpypy.multiarray import _reconstruct
3-
from pypy_impl import bincount
3+
from pypy_impl import bincount, digitize
44

55
def _fastCopyAndTranspose(a):
66
return a.T.copy()
@@ -100,7 +100,7 @@ def __init__(self, *args, **kwargs):
100100
for name in '''
101101
nested_iters
102102
broadcast empty_like fromiter fromfile frombuffer newbuffer getbuffer
103-
int_asbuffer set_numeric_ops promote_types digitize
103+
int_asbuffer set_numeric_ops promote_types
104104
lexsort compare_chararrays putmask einsum inner interp
105105
_vec_string datetime_data correlate correlate2 vdot matmul _insert
106106
datetime_as_string busday_offset busday_count is_busday busdaycalendar

numpy/core/pypy_impl/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
from digitize import digitize
12
from bincount import bincount

numpy/core/pypy_impl/digitize.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
from numpy.core.multiarray import dtype
2+
from numpy.core.multiarray import array
3+
4+
5+
def digitize(x, bins, right=False):
6+
7+
x = array(x, dtype=dtype('float'))
8+
bins = array(bins, dtype=dtype('float'))
9+
10+
if len(bins) == 0:
11+
raise ValueError("bins must have non-zero length")
12+
13+
monotonic = check_monotonic(bins)
14+
if monotonic == 0:
15+
raise ValueError("bins must be monotonically increasing or decreasing")
16+
17+
if monotonic == -1:
18+
bins = bins[::-1]
19+
result = bins.searchsorted(x, side='right' if not right else 'left')
20+
if monotonic == -1:
21+
result = len(bins) - result
22+
23+
return result
24+
25+
26+
def check_monotonic(a):
27+
"""
28+
29+
Parameters
30+
----------
31+
a
32+
input array
33+
34+
Returns
35+
-1 -- for monotonic, non-increasing
36+
0 -- for non-monotonic
37+
1 -- for monotonic, non-decreasing
38+
-------
39+
40+
>>> check_monotonic([1,2,3])
41+
1
42+
>>> check_monotonic([3,2,1])
43+
-1
44+
>>> check_monotonic([3,1,2])
45+
0
46+
>>> check_monotonic([1, 1, 1, 3, 100])
47+
1
48+
>>> check_monotonic([1, 1, 1, 0, -1])
49+
-1
50+
>>> check_monotonic([1, 1, 1, 3, 2])
51+
0
52+
>>> check_monotonic([1123123])
53+
1
54+
"""
55+
len_a = len(a)
56+
assert len_a > 0
57+
58+
last = a[0]
59+
i = 1
60+
while i < len_a and a[0] == a[i]:
61+
i += 1
62+
63+
if i == len_a:
64+
return 1
65+
66+
next = a[i]
67+
i += 1
68+
if last < next:
69+
while i < len_a:
70+
last = next
71+
next = a[i]
72+
if last > next:
73+
return 0
74+
i += 1
75+
return 1
76+
else:
77+
while i < len_a:
78+
last = next
79+
next = a[i]
80+
if last < next:
81+
return 0
82+
i += 1
83+
return -1

0 commit comments

Comments
 (0)