comparison DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/lib/arraysetops.py @ 87:2a2c65a20a8b

Add Python libs and headers
author Chris Cannam
date Wed, 25 Feb 2015 14:05:22 +0000
parents
children
comparison
equal deleted inserted replaced
86:413a9d26189e 87:2a2c65a20a8b
1 """
2 Set operations for 1D numeric arrays based on sorting.
3
4 :Contains:
5 ediff1d,
6 unique,
7 intersect1d,
8 setxor1d,
9 in1d,
10 union1d,
11 setdiff1d
12
13 :Notes:
14
15 For floating point arrays, inaccurate results may appear due to usual round-off
16 and floating point comparison issues.
17
18 Speed could be gained in some operations by an implementation of
19 sort(), that can provide directly the permutation vectors, avoiding
20 thus calls to argsort().
21
22 To do: Optionally return indices analogously to unique for all functions.
23
24 :Author: Robert Cimrman
25
26 """
27 from __future__ import division, absolute_import, print_function
28
29 import numpy as np
30
31
32 __all__ = [
33 'ediff1d', 'intersect1d', 'setxor1d', 'union1d', 'setdiff1d', 'unique',
34 'in1d'
35 ]
36
37
38 def ediff1d(ary, to_end=None, to_begin=None):
39 """
40 The differences between consecutive elements of an array.
41
42 Parameters
43 ----------
44 ary : array_like
45 If necessary, will be flattened before the differences are taken.
46 to_end : array_like, optional
47 Number(s) to append at the end of the returned differences.
48 to_begin : array_like, optional
49 Number(s) to prepend at the beginning of the returned differences.
50
51 Returns
52 -------
53 ediff1d : ndarray
54 The differences. Loosely, this is ``ary.flat[1:] - ary.flat[:-1]``.
55
56 See Also
57 --------
58 diff, gradient
59
60 Notes
61 -----
62 When applied to masked arrays, this function drops the mask information
63 if the `to_begin` and/or `to_end` parameters are used.
64
65 Examples
66 --------
67 >>> x = np.array([1, 2, 4, 7, 0])
68 >>> np.ediff1d(x)
69 array([ 1, 2, 3, -7])
70
71 >>> np.ediff1d(x, to_begin=-99, to_end=np.array([88, 99]))
72 array([-99, 1, 2, 3, -7, 88, 99])
73
74 The returned array is always 1D.
75
76 >>> y = [[1, 2, 4], [1, 6, 24]]
77 >>> np.ediff1d(y)
78 array([ 1, 2, -3, 5, 18])
79
80 """
81 ary = np.asanyarray(ary).flat
82 ed = ary[1:] - ary[:-1]
83 arrays = [ed]
84 if to_begin is not None:
85 arrays.insert(0, to_begin)
86 if to_end is not None:
87 arrays.append(to_end)
88
89 if len(arrays) != 1:
90 # We'll save ourselves a copy of a potentially large array in
91 # the common case where neither to_begin or to_end was given.
92 ed = np.hstack(arrays)
93
94 return ed
95
96 def unique(ar, return_index=False, return_inverse=False, return_counts=False):
97 """
98 Find the unique elements of an array.
99
100 Returns the sorted unique elements of an array. There are two optional
101 outputs in addition to the unique elements: the indices of the input array
102 that give the unique values, and the indices of the unique array that
103 reconstruct the input array.
104
105 Parameters
106 ----------
107 ar : array_like
108 Input array. This will be flattened if it is not already 1-D.
109 return_index : bool, optional
110 If True, also return the indices of `ar` that result in the unique
111 array.
112 return_inverse : bool, optional
113 If True, also return the indices of the unique array that can be used
114 to reconstruct `ar`.
115 return_counts : bool, optional
116 .. versionadded:: 1.9.0
117 If True, also return the number of times each unique value comes up
118 in `ar`.
119
120 Returns
121 -------
122 unique : ndarray
123 The sorted unique values.
124 unique_indices : ndarray, optional
125 The indices of the first occurrences of the unique values in the
126 (flattened) original array. Only provided if `return_index` is True.
127 unique_inverse : ndarray, optional
128 The indices to reconstruct the (flattened) original array from the
129 unique array. Only provided if `return_inverse` is True.
130 unique_counts : ndarray, optional
131 .. versionadded:: 1.9.0
132 The number of times each of the unique values comes up in the
133 original array. Only provided if `return_counts` is True.
134
135 See Also
136 --------
137 numpy.lib.arraysetops : Module with a number of other functions for
138 performing set operations on arrays.
139
140 Examples
141 --------
142 >>> np.unique([1, 1, 2, 2, 3, 3])
143 array([1, 2, 3])
144 >>> a = np.array([[1, 1], [2, 3]])
145 >>> np.unique(a)
146 array([1, 2, 3])
147
148 Return the indices of the original array that give the unique values:
149
150 >>> a = np.array(['a', 'b', 'b', 'c', 'a'])
151 >>> u, indices = np.unique(a, return_index=True)
152 >>> u
153 array(['a', 'b', 'c'],
154 dtype='|S1')
155 >>> indices
156 array([0, 1, 3])
157 >>> a[indices]
158 array(['a', 'b', 'c'],
159 dtype='|S1')
160
161 Reconstruct the input array from the unique values:
162
163 >>> a = np.array([1, 2, 6, 4, 2, 3, 2])
164 >>> u, indices = np.unique(a, return_inverse=True)
165 >>> u
166 array([1, 2, 3, 4, 6])
167 >>> indices
168 array([0, 1, 4, 3, 1, 2, 1])
169 >>> u[indices]
170 array([1, 2, 6, 4, 2, 3, 2])
171
172 """
173 ar = np.asanyarray(ar).flatten()
174
175 optional_indices = return_index or return_inverse
176 optional_returns = optional_indices or return_counts
177
178 if ar.size == 0:
179 if not optional_returns:
180 ret = ar
181 else:
182 ret = (ar,)
183 if return_index:
184 ret += (np.empty(0, np.bool),)
185 if return_inverse:
186 ret += (np.empty(0, np.bool),)
187 if return_counts:
188 ret += (np.empty(0, np.intp),)
189 return ret
190
191 if optional_indices:
192 perm = ar.argsort(kind='mergesort' if return_index else 'quicksort')
193 aux = ar[perm]
194 else:
195 ar.sort()
196 aux = ar
197 flag = np.concatenate(([True], aux[1:] != aux[:-1]))
198
199 if not optional_returns:
200 ret = aux[flag]
201 else:
202 ret = (aux[flag],)
203 if return_index:
204 ret += (perm[flag],)
205 if return_inverse:
206 iflag = np.cumsum(flag) - 1
207 iperm = perm.argsort()
208 ret += (np.take(iflag, iperm),)
209 if return_counts:
210 idx = np.concatenate(np.nonzero(flag) + ([ar.size],))
211 ret += (np.diff(idx),)
212 return ret
213
214 def intersect1d(ar1, ar2, assume_unique=False):
215 """
216 Find the intersection of two arrays.
217
218 Return the sorted, unique values that are in both of the input arrays.
219
220 Parameters
221 ----------
222 ar1, ar2 : array_like
223 Input arrays.
224 assume_unique : bool
225 If True, the input arrays are both assumed to be unique, which
226 can speed up the calculation. Default is False.
227
228 Returns
229 -------
230 intersect1d : ndarray
231 Sorted 1D array of common and unique elements.
232
233 See Also
234 --------
235 numpy.lib.arraysetops : Module with a number of other functions for
236 performing set operations on arrays.
237
238 Examples
239 --------
240 >>> np.intersect1d([1, 3, 4, 3], [3, 1, 2, 1])
241 array([1, 3])
242
243 """
244 if not assume_unique:
245 # Might be faster than unique( intersect1d( ar1, ar2 ) )?
246 ar1 = unique(ar1)
247 ar2 = unique(ar2)
248 aux = np.concatenate((ar1, ar2))
249 aux.sort()
250 return aux[:-1][aux[1:] == aux[:-1]]
251
252 def setxor1d(ar1, ar2, assume_unique=False):
253 """
254 Find the set exclusive-or of two arrays.
255
256 Return the sorted, unique values that are in only one (not both) of the
257 input arrays.
258
259 Parameters
260 ----------
261 ar1, ar2 : array_like
262 Input arrays.
263 assume_unique : bool
264 If True, the input arrays are both assumed to be unique, which
265 can speed up the calculation. Default is False.
266
267 Returns
268 -------
269 setxor1d : ndarray
270 Sorted 1D array of unique values that are in only one of the input
271 arrays.
272
273 Examples
274 --------
275 >>> a = np.array([1, 2, 3, 2, 4])
276 >>> b = np.array([2, 3, 5, 7, 5])
277 >>> np.setxor1d(a,b)
278 array([1, 4, 5, 7])
279
280 """
281 if not assume_unique:
282 ar1 = unique(ar1)
283 ar2 = unique(ar2)
284
285 aux = np.concatenate((ar1, ar2))
286 if aux.size == 0:
287 return aux
288
289 aux.sort()
290 # flag = ediff1d( aux, to_end = 1, to_begin = 1 ) == 0
291 flag = np.concatenate(([True], aux[1:] != aux[:-1], [True]))
292 # flag2 = ediff1d( flag ) == 0
293 flag2 = flag[1:] == flag[:-1]
294 return aux[flag2]
295
296 def in1d(ar1, ar2, assume_unique=False, invert=False):
297 """
298 Test whether each element of a 1-D array is also present in a second array.
299
300 Returns a boolean array the same length as `ar1` that is True
301 where an element of `ar1` is in `ar2` and False otherwise.
302
303 Parameters
304 ----------
305 ar1 : (M,) array_like
306 Input array.
307 ar2 : array_like
308 The values against which to test each value of `ar1`.
309 assume_unique : bool, optional
310 If True, the input arrays are both assumed to be unique, which
311 can speed up the calculation. Default is False.
312 invert : bool, optional
313 If True, the values in the returned array are inverted (that is,
314 False where an element of `ar1` is in `ar2` and True otherwise).
315 Default is False. ``np.in1d(a, b, invert=True)`` is equivalent
316 to (but is faster than) ``np.invert(in1d(a, b))``.
317
318 .. versionadded:: 1.8.0
319
320 Returns
321 -------
322 in1d : (M,) ndarray, bool
323 The values `ar1[in1d]` are in `ar2`.
324
325 See Also
326 --------
327 numpy.lib.arraysetops : Module with a number of other functions for
328 performing set operations on arrays.
329
330 Notes
331 -----
332 `in1d` can be considered as an element-wise function version of the
333 python keyword `in`, for 1-D sequences. ``in1d(a, b)`` is roughly
334 equivalent to ``np.array([item in b for item in a])``.
335
336 .. versionadded:: 1.4.0
337
338 Examples
339 --------
340 >>> test = np.array([0, 1, 2, 5, 0])
341 >>> states = [0, 2]
342 >>> mask = np.in1d(test, states)
343 >>> mask
344 array([ True, False, True, False, True], dtype=bool)
345 >>> test[mask]
346 array([0, 2, 0])
347 >>> mask = np.in1d(test, states, invert=True)
348 >>> mask
349 array([False, True, False, True, False], dtype=bool)
350 >>> test[mask]
351 array([1, 5])
352 """
353 # Ravel both arrays, behavior for the first array could be different
354 ar1 = np.asarray(ar1).ravel()
355 ar2 = np.asarray(ar2).ravel()
356
357 # This code is significantly faster when the condition is satisfied.
358 if len(ar2) < 10 * len(ar1) ** 0.145:
359 if invert:
360 mask = np.ones(len(ar1), dtype=np.bool)
361 for a in ar2:
362 mask &= (ar1 != a)
363 else:
364 mask = np.zeros(len(ar1), dtype=np.bool)
365 for a in ar2:
366 mask |= (ar1 == a)
367 return mask
368
369 # Otherwise use sorting
370 if not assume_unique:
371 ar1, rev_idx = np.unique(ar1, return_inverse=True)
372 ar2 = np.unique(ar2)
373
374 ar = np.concatenate((ar1, ar2))
375 # We need this to be a stable sort, so always use 'mergesort'
376 # here. The values from the first array should always come before
377 # the values from the second array.
378 order = ar.argsort(kind='mergesort')
379 sar = ar[order]
380 if invert:
381 bool_ar = (sar[1:] != sar[:-1])
382 else:
383 bool_ar = (sar[1:] == sar[:-1])
384 flag = np.concatenate((bool_ar, [invert]))
385 indx = order.argsort(kind='mergesort')[:len(ar1)]
386
387 if assume_unique:
388 return flag[indx]
389 else:
390 return flag[indx][rev_idx]
391
392 def union1d(ar1, ar2):
393 """
394 Find the union of two arrays.
395
396 Return the unique, sorted array of values that are in either of the two
397 input arrays.
398
399 Parameters
400 ----------
401 ar1, ar2 : array_like
402 Input arrays. They are flattened if they are not already 1D.
403
404 Returns
405 -------
406 union1d : ndarray
407 Unique, sorted union of the input arrays.
408
409 See Also
410 --------
411 numpy.lib.arraysetops : Module with a number of other functions for
412 performing set operations on arrays.
413
414 Examples
415 --------
416 >>> np.union1d([-1, 0, 1], [-2, 0, 2])
417 array([-2, -1, 0, 1, 2])
418
419 """
420 return unique(np.concatenate((ar1, ar2)))
421
422 def setdiff1d(ar1, ar2, assume_unique=False):
423 """
424 Find the set difference of two arrays.
425
426 Return the sorted, unique values in `ar1` that are not in `ar2`.
427
428 Parameters
429 ----------
430 ar1 : array_like
431 Input array.
432 ar2 : array_like
433 Input comparison array.
434 assume_unique : bool
435 If True, the input arrays are both assumed to be unique, which
436 can speed up the calculation. Default is False.
437
438 Returns
439 -------
440 setdiff1d : ndarray
441 Sorted 1D array of values in `ar1` that are not in `ar2`.
442
443 See Also
444 --------
445 numpy.lib.arraysetops : Module with a number of other functions for
446 performing set operations on arrays.
447
448 Examples
449 --------
450 >>> a = np.array([1, 2, 3, 2, 4, 1])
451 >>> b = np.array([3, 4, 5, 6])
452 >>> np.setdiff1d(a, b)
453 array([1, 2])
454
455 """
456 if not assume_unique:
457 ar1 = unique(ar1)
458 ar2 = unique(ar2)
459 aux = in1d(ar1, ar2, assume_unique=True)
460 if aux.size == 0:
461 return aux
462 else:
463 return np.asarray(ar1)[aux == 0]