daily pastebin goal
7%
SHARE
TWEET

Untitled

a guest Dec 10th, 2018 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2.  
  3.  
  4. def select(items, nsample=None, p=None, size=None):
  5.     """
  6.     Select random samples from `items`.
  7.  
  8.     The function randomly selects `nsample` items from `items` without
  9.     replacement.
  10.  
  11.     Parameters
  12.     ----------
  13.     items : sequence
  14.         The collection of items from which the selection is made.
  15.     nsample : int, optional
  16.         Number of items to select without replacement in each draw.
  17.         It must be between 0 and len(items), inclusize.
  18.     p : array-like of floats, same length as `items, optional
  19.         Probabilities of the items.  If this argument is not given,
  20.         the elements in `items` are assumed to have equal probability.
  21.     size : int, optional
  22.         Number of variates to draw.
  23.  
  24.     Notes
  25.     -----
  26.     `size=None` means "generate a single selection".
  27.  
  28.     If `size` is None, the result is equivalent to
  29.         numpy.random.choice(items, size=nsample, replace=False)
  30.  
  31.     `nsample=None` means draw one (scalar) sample.
  32.     If `nsample` is None, the functon acts (almost) like nsample=1 (see
  33.     below for more information), and the result is equivalent to
  34.         numpy.random.choice(items, size=size)
  35.     In effect, it does choice with replacement.  The case `nsample=None`
  36.     can be interpreted as each sample is a scalar, and `nsample=k`
  37.     means each sample is a sequence with length k.
  38.  
  39.     If `nsample` is not None, it must be a nonnegative integer with
  40.     0 <= nsample <= len(items).
  41.  
  42.     If `size` is not None, it must be an integer or a tuple of integers.
  43.     When `size` is an integer, it is treated as the tuple ``(size,)``.
  44.  
  45.     When both `nsample` and `size` are not None, the result
  46.     has shape ``size + (nsample,)``.
  47.  
  48.     Examples
  49.     --------
  50.     Make 6 choices with replacement from [10, 20, 30, 40].  (This is
  51.     equivalent to "Make 1 choice without replacement from [10, 20, 30, 40];
  52.     do it six times.")
  53.  
  54.     >>> select([10, 20, 30, 40], size=6)
  55.     array([20, 20, 40, 10, 40, 30])
  56.  
  57.     Choose two items from [10, 20, 30, 40] without replacement.  Do it six
  58.     times.
  59.  
  60.     >>> select([10, 20, 30, 40], nsample=2, size=6)
  61.     array([[40, 10],
  62.            [20, 30],
  63.            [10, 40],
  64.            [30, 10],
  65.            [10, 30],
  66.            [10, 20]])
  67.  
  68.     When `nsample` is an integer, there is always an axis at the end of the
  69.     result with length `nsample`, even when `nsample=1`.  For example, the
  70.     shape of the array returned in the following call is (2, 3, 1)
  71.  
  72.     >>> select([10, 20, 30, 40], nsample=1, size=(2, 3))
  73.     array([[[10],
  74.             [30],
  75.             [20]],
  76.  
  77.            [[10],
  78.             [40],
  79.             [20]]])
  80.  
  81.     When `nsample` is None, it acts like `nsample=1`, but the trivial
  82.     dimension is not included.  The shape of the array returned in the
  83.     following call is (2, 3).
  84.  
  85.     >>> select([10, 20, 30, 40], size=(2, 3))
  86.     array([[20, 40, 30],
  87.            [30, 20, 40]])
  88.  
  89.     """
  90.     # This implementation is a proof of concept, and provides a demonstration
  91.     # of a possible API.  Efficiency was not considered.  The actual
  92.     # implementation would probably use Cython or C.
  93.  
  94.     if nsample is None:
  95.         return np.random.choice(items, size=size, p=p)
  96.  
  97.     if size is None:
  98.         size = ()
  99.     elif np.isscalar(size):
  100.         size = (size,)
  101.  
  102.     tmp = np.empty(size + (0,))
  103.     func = lambda _: np.random.choice(items, size=nsample, p=p, replace=False)
  104.     result = np.apply_along_axis(func, -1, tmp)
  105.     return result
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top