14
All functions work best with integer numerical arrays on input
15
(e.g. indices). For floating point arrays, innacurate results may appear due to
16
usual round-off and floating point comparison issues.
16
All functions work best with integer numerical arrays on input (e.g. indices).
17
For floating point arrays, innacurate results may appear due to usual round-off
18
and floating point comparison issues.
18
20
Except unique1d, union1d and intersect1d_nu, all functions expect inputs with
19
unique elements. Speed could be gained in some operations by an implementaion
20
of sort(), that can provide directly the permutation vectors, avoiding thus
21
unique elements. Speed could be gained in some operations by an implementaion of
22
sort(), that can provide directly the permutation vectors, avoiding thus calls
23
Run test_unique1d_speed() to compare performance of numpy.unique1d() and
25
Run _test_unique1d_speed() to compare performance of numpy.unique1d() and
24
26
numpy.unique() - it should be the same.
26
28
To do: Optionally return indices analogously to unique1d for all functions.
28
Author: Robert Cimrman
30
30
created: 01.11.2005
31
last revision: 12.10.2006
31
last revision: 07.01.2007
33
:Author: Robert Cimrman
33
35
__all__ = ['ediff1d', 'unique1d', 'intersect1d', 'intersect1d_nu', 'setxor1d',
34
36
'setmember1d', 'union1d', 'setdiff1d']
39
def ediff1d(ary, to_end = None, to_begin = None):
40
"""Array difference with prefixed and/or appended value.
42
See also: unique1d, intersect1d, intersect1d_nu, setxor1d,
43
setmember1d, union1d, setdiff1d
41
def ediff1d(ary, to_end=None, to_begin=None):
42
"""The differences between consecutive elements of an array, possibly with
43
prefixed and/or appended values.
48
This array will be flattened before the difference is taken.
49
to_end : number, optional
50
If provided, this number will be tacked onto the end of the returned
52
to_begin : number, optional
53
If provided, this number will be taked onto the beginning of the
59
The differences. Loosely, this will be (ary[1:] - ary[:-1]).
45
62
ary = nm.asarray(ary).flat
46
63
ed = ary[1:] - ary[:-1]
47
65
if to_begin is not None:
48
if to_end is not None:
49
ed = nm.r_[to_begin, ed, to_end]
51
ed = nm.insert(ed, 0, to_begin)
52
elif to_end is not None:
53
ed = nm.append(ed, to_end)
66
arrays.insert(0, to_begin)
67
if to_end is not None:
71
# We'll save ourselves a copy of a potentially large array in the common
72
# case where neither to_begin or to_end was given.
73
ed = nm.hstack(arrays)
57
77
def unique1d(ar1, return_index=False):
58
"""Unique elements of 1D array. When return_index is True, return
59
also the indices indx such that ar1.flat[indx] is the resulting
60
array of unique elements.
62
See also: ediff1d, intersect1d, intersect1d_nu, setxor1d,
63
setmember1d, union1d, setdiff1d
78
"""Find the unique elements of 1D array.
80
Most of the other array set operations operate on the unique arrays
81
generated by this function.
86
This array will be flattened if it is not already 1D.
87
return_index : bool, optional
88
If True, also return the indices against ar1 that result in the unique
95
unique_indices : int array, optional
96
The indices of the unique values. Only provided if return_index is True.
100
numpy.lib.arraysetops : Module with a number of other functions
101
for performing set operations on arrays.
65
104
ar = nm.asarray(ar1).flatten()
67
106
if return_index: return nm.empty(0, nm.bool), ar
71
110
perm = ar.argsort()
73
112
flag = nm.concatenate( ([True], aux[1:] != aux[:-1]) )
74
113
return perm[flag], aux[flag]
78
117
flag = nm.concatenate( ([True], ar[1:] != ar[:-1]) )
81
def intersect1d( ar1, ar2 ):
120
def intersect1d(ar1, ar2):
82
121
"""Intersection of 1D arrays with unique elements.
84
See also: ediff1d, unique1d, intersect1d_nu, setxor1d,
85
setmember1d, union1d, setdiff1d
123
Use unique1d() to generate arrays with only unique elements to use as inputs
124
to this function. Alternatively, use intersect1d_nu() which will find the
125
unique values for you.
138
numpy.lib.arraysetops : Module with a number of other functions for
139
performing set operations on arrays.
87
142
aux = nm.concatenate((ar1,ar2))
89
144
return aux[aux[1:] == aux[:-1]]
91
def intersect1d_nu( ar1, ar2 ):
146
def intersect1d_nu(ar1, ar2):
92
147
"""Intersection of 1D arrays with any elements.
94
See also: ediff1d, unique1d, intersect1d, setxor1d,
95
setmember1d, union1d, setdiff1d
149
The input arrays do not have unique elements like intersect1d() requires.
162
numpy.lib.arraysetops : Module with a number of other functions for
163
performing set operations on arrays.
97
# Might be faster then unique1d( intersect1d( ar1, ar2 ) )?
166
# Might be faster than unique1d( intersect1d( ar1, ar2 ) )?
98
167
aux = nm.concatenate((unique1d(ar1), unique1d(ar2)))
100
169
return aux[aux[1:] == aux[:-1]]
102
def setxor1d( ar1, ar2 ):
171
def setxor1d(ar1, ar2):
103
172
"""Set exclusive-or of 1D arrays with unique elements.
105
See also: ediff1d, unique1d, intersect1d, intersect1d_nu,
106
setmember1d, union1d, setdiff1d
174
Use unique1d() to generate arrays with only unique elements to use as inputs
185
The values that are only in one, but not both, of the input arrays.
189
numpy.lib.arraysetops : Module with a number of other functions for
190
performing set operations on arrays.
108
193
aux = nm.concatenate((ar1, ar2))
109
194
if aux.size == 0:
113
198
# flag = ediff1d( aux, to_end = 1, to_begin = 1 ) == 0
114
199
flag = nm.concatenate( ([True], aux[1:] != aux[:-1], [True] ) )
116
201
flag2 = flag[1:] == flag[:-1]
117
202
return aux[flag2]
119
def setmember1d( ar1, ar2 ):
120
"""Return an array of shape of ar1 containing 1 where the elements of
121
ar1 are in ar2 and 0 otherwise.
123
See also: ediff1d, unique1d, intersect1d, intersect1d_nu, setxor1d,
204
def setmember1d(ar1, ar2):
205
"""Return a boolean array of shape of ar1 containing True where the elements
206
of ar1 are in ar2 and False otherwise.
208
Use unique1d() to generate arrays with only unique elements to use as inputs
219
The values ar1[mask] are in ar2.
223
numpy.lib.arraysetops : Module with a number of other functions for
224
performing set operations on arrays.
126
zlike = nm.zeros_like
227
ar1 = nm.asarray( ar1 )
228
ar2 = nm.asarray( ar2 )
127
229
ar = nm.concatenate( (ar1, ar2 ) )
128
tt = nm.concatenate( (zlike( ar1 ), zlike( ar2 ) + 1) )
230
b1 = nm.zeros( ar1.shape, dtype = nm.int8 )
231
b2 = nm.ones( ar2.shape, dtype = nm.int8 )
232
tt = nm.concatenate( (b1, b2) )
234
# We need this to be a stable sort, so always use 'mergesort' here. The
235
# values from the first array should always come before the values from the
237
perm = ar.argsort(kind='mergesort')
132
240
# flag = ediff1d( aux, 1 ) == 0
133
241
flag = nm.concatenate( (aux[1:] == aux[:-1], [False] ) )
135
242
ii = nm.where( flag * aux2 )[0]
137
244
perm[ii+1] = perm[ii]
140
indx = perm.argsort()[:len( ar1 )]
247
indx = perm.argsort(kind='mergesort')[:len( ar1 )]
142
249
return flag[indx]
144
def union1d( ar1, ar2 ):
145
"""Union of 1D arrays with unique elements.
147
See also: ediff1d, unique1d, intersect1d, intersect1d_nu, setxor1d,
148
setmember1d, setdiff1d
251
def union1d(ar1, ar2):
253
Union of 1D arrays with unique elements.
255
Use unique1d() to generate arrays with only unique elements to use as inputs
269
numpy.lib.arraysetops : Module with a number of other functions for
270
performing set operations on arrays.
150
273
return unique1d( nm.concatenate( (ar1, ar2) ) )
152
def setdiff1d( ar1, ar2 ):
275
def setdiff1d(ar1, ar2):
153
276
"""Set difference of 1D arrays with unique elements.
155
See also: ediff1d, unique1d, intersect1d, intersect1d_nu, setxor1d,
278
Use unique1d() to generate arrays with only unique elements to use as inputs
289
The values in ar1 that are not in ar2.
293
numpy.lib.arraysetops : Module with a number of other functions for
294
performing set operations on arrays.
158
297
aux = setmember1d(ar1,ar2)
159
298
if aux.size == 0: