ó
‡ˆ\c           @   s§   d  d l  m Z d  d l Z d  d l j Z d  d l Z d  d l m	 Z	 d d l
 m Z d d l m Z d d g Z e d	 ƒ d e d d d
 „ ƒ Z d d d „ Z d S(   iÿÿÿÿ(   t   divisionN(   t   check_random_statei   (   t   sample_without_replacement(   t
   deprecatedR   t   choices•   sklearn.utils.random.choice was deprecated in version 0.19 and will be removed in 0.21. Use np.random.choice or np.random.RandomState.choice instead.c         C   sK   | d k	 r. t | ƒ } | j |  | | | ƒ St j j |  | | | ƒ Sd S(   sÅ
  
    choice(a, size=None, replace=True, p=None)

    Generates a random sample from a given 1-D array

    .. versionadded:: 1.7.0

    Parameters
    -----------
    a : 1-D array-like or int
        If an ndarray, a random sample is generated from its elements.
        If an int, the random sample is generated as if a was np.arange(n)

    size : int or tuple of ints, optional
        Output shape. Default is None, in which case a single value is
        returned.

    replace : boolean, optional
        Whether the sample is with or without replacement.

    p : 1-D array-like, optional
        The probabilities associated with each entry in a.
        If not given the sample assumes a uniform distribution over all
        entries in a.

    random_state : int, RandomState instance or None, optional (default=None)
        If int, random_state is the seed used by the random number generator;
        If RandomState instance, random_state is the random number generator;
        If None, the random number generator is the RandomState instance used
        by `np.random`.


    Returns
    --------
    samples : 1-D ndarray, shape (size,)
    The generated random samples

    Raises
    -------
    ValueError
    If a is an int and less than zero, if a or p are not 1-dimensional,
    if a is an array-like of size 0, if p is not a vector of
    probabilities, if a and p have different lengths, or if
    replace=False and the sample size is greater than the population
    size

    See Also
    ---------
    randint, shuffle, permutation

    Examples
    ---------
    Generate a uniform random sample from np.arange(5) of size 3:

    >>> np.random.choice(5, 3)  # doctest: +SKIP
    array([0, 3, 4])
    >>> #This is equivalent to np.random.randint(0,5,3)

    Generate a non-uniform random sample from np.arange(5) of size 3:

    >>> np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])  # doctest: +SKIP
    array([3, 3, 0])

    Generate a uniform random sample from np.arange(5) of size 3 without
    replacement:

    >>> np.random.choice(5, 3, replace=False)  # doctest: +SKIP
    array([3,1,0])
    >>> #This is equivalent to np.random.shuffle(np.arange(5))[:3]

    Generate a non-uniform random sample from np.arange(5) of size
    3 without replacement:

    >>> np.random.choice(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0])
    ... # doctest: +SKIP
    array([2, 3, 0])

    Any of the above can be repeated with an arbitrary array-like
    instead of just integers. For instance:

    >>> aa_milne_arr = ['pooh', 'rabbit', 'piglet', 'Christopher']
    >>> np.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3])
    ... # doctest: +SKIP
    array(['pooh', 'pooh', 'pooh', 'Christopher', 'piglet'],
    dtype='|S11')

    N(   t   NoneR   R   t   npt   random(   t   at   sizet   replacet   pt   random_state(    (    s3   lib/python2.7/site-packages/sklearn/utils/random.pyR      s    [c         C   só  t  j  d ƒ } t  j  d ƒ } t  j  d d g ƒ } x’t t | ƒ ƒ D]~} t j | | ƒ | | <| | j j d k r“ t d | | j ƒ ‚ n  | | j t j	 d t
 ƒ| | <| d k rû t j d | | j d ƒ } | j d | | j d ƒ n t j | | ƒ } t j t j | ƒ d ƒ sAt d j | ƒ ƒ ‚ n  | j d | | j d k rt d	 j | | | j d | j d ƒ ƒ ‚ n  d | | k rÔt j | | d d ƒ | | <t j | d d
 ƒ } n  t | ƒ }	 | | j d d k r±d | | | d k }
 t |  |
 ƒ } t d |  d | d | ƒ } | j | ƒ | | d k } | | } | t j | ƒ } t j | j ƒ  |	 j | ƒ ƒ } | j | | | | ƒ n  | j t | ƒ ƒ qF Wt j | | | f |  t | ƒ f d t ƒS(   s‘  Generate a sparse random matrix given column class distributions

    Parameters
    ----------
    n_samples : int,
        Number of samples to draw in each column.

    classes : list of size n_outputs of arrays of size (n_classes,)
        List of classes for each column.

    class_probability : list of size n_outputs of arrays of size (n_classes,)
        Optional (default=None). Class distribution of each column. If None the
        uniform distribution is assumed.

    random_state : int, RandomState instance or None, optional (default=None)
        If int, random_state is the seed used by the random number generator;
        If RandomState instance, random_state is the random number generator;
        If None, the random number generator is the RandomState instance used
        by `np.random`.

    Returns
    -------
    random_matrix : sparse csc matrix of size (n_samples, n_outputs)

    t   ii    s   class dtype %s is not supportedt   copyt   shapei   g      ð?s2   Probability array at index {0} does not sum to onesX   classes[{0}] (length {1}) and class_probability[{0}] (length {2}) have different length.g        t   n_populationt	   n_samplesR   t   dtypeN(   t   arrayt   ranget   lenR   t   asarrayR   t   kindt
   ValueErrort   astypet   int64t   FalseR   t   emptyR   t   fillt   iscloset   sumt   formatt   insertR   t   intR   t   extendt   searchsortedt   cumsumt   randt   appendt   spt
   csc_matrix(   R   t   classest   class_probabilityR   t   datat   indicest   indptrt   jt   class_prob_jt   rngt	   p_nonzerot   nnzt
   ind_samplet   classes_j_nonzerot   class_probability_nzt   class_probability_nz_normt   classes_ind(    (    s3   lib/python2.7/site-packages/sklearn/utils/random.pyt   random_choice_csct   sV     			
(   t
   __future__R    t   numpyR   t   scipy.sparset   sparseR(   R   t   sklearn.utilsR   t   _randomR   t   deprecationR   t   __all__R   t   TrueR   R9   (    (    (    s3   lib/python2.7/site-packages/sklearn/utils/random.pyt   <module>   s   	_