Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 20th, 2012  |  syntax: None  |  size: 1.76 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ###########################################
  2. # Note: this requires PR # 245 in numpy
  3. # https://github.com/numpy/numpy/pull/245
  4. #
  5. # Author: Skipper Seabold
  6. # License: BSD
  7.  
  8. import numpy as np
  9.  
  10. def cut(x, bins, right=True):
  11.     """
  12.     Return indices of half-open bins to which each value of `x` belongs.
  13.  
  14.     Parameters
  15.     ----------
  16.     x : array-like
  17.         Input array to be binned. It has to be 1-dimensional.
  18.     bins : int or sequence of scalars
  19.         If `bins` is an int, it defines the number of equal-width bins in the
  20.         range of `x`. The range of `x`, however, is extended by .1% on each
  21.         side to include the min or max values of `x`. If `bins` is a sequence
  22.         it defines the bin edges allowing for non-uniform bin width.
  23.     right : bool
  24.         Indicates whether the bins include the rightmost edge or not. If
  25.         right == True (the default), then the bins [1,2,3,4] indicate
  26.         (1,2], (2,3], (3,4].
  27.  
  28.     Returns
  29.     -------
  30.     out : ndarray of ints
  31.         Output array of indices, of same shape as `x`.
  32.     """
  33.     if not np.iterable(bins):
  34.         if np.isscalar(bins) and bins < 1:
  35.             raise ValueError("`bins` should be a positive integer.")
  36.         if x.size == 0:
  37.             # handle empty arrays. Can't determine range, so use 0-1.
  38.             range = (0, 1)
  39.         else:
  40.             range = (x.min(), x.max())
  41.         mn, mx = [mi+0.0 for mi in range]
  42.         if mn == mx:
  43.             mn -= 0.5
  44.             mx += 0.5
  45.         bins = np.linspace(mn, mx, bins+1, endpoint=True)
  46.         bins[0] -= .1*mn
  47.         bins[-1] += .1*mx
  48.     else:
  49.         bins = np.asarray(bins)
  50.         if (np.diff(bins) < 0).any():
  51.             raise AttributeError(
  52.                     'bins must increase monotonically.')
  53.  
  54.     return np.digitize(x, bins, right)