B
    	\#                 @   st   d dl mZ d dlZd dlm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ddZdddZdS )    )divisionN)check_random_state   )sample_without_replacement)
deprecatedr   choicezsklearn.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.Tc             C   s6   |dk	r t |}|| |||S tj| |||S dS )a
  
    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)r   r   npZrandom)asizereplaceprandom_state r   3lib/python3.7/site-packages/sklearn/utils/random.pyr      s    [c             C   s*  t  d}t  d}t  ddg}xtt|D ]}t|| ||< || jjdkrltd|| j || jtj	dd||< |dkrtj
|| jd d}|d|| jd   nt|| }tt|d	std
||jd || jd kr&td||| jd |jd d|| krXt|| dd||< t|dd}t|}	|| jd dkrd||| dk  }
t| |
 }t| ||d}|| || dk}|| }|t| }t| |	|}||| | |  |t| q2W tj|||f| t|ftdS )a  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)

    ir   zclass dtype %s is not supportedF)copyN)shaper   g      ?z2Probability array at index {0} does not sum to onezXclasses[{0}] (length {1}) and class_probability[{0}] (length {2}) have different length.g        )Zn_population	n_samplesr   )dtype)arrayrangelenr   Zasarrayr   Zkind
ValueErrorZastypeZint64emptyr   ZfillZisclosesumformatinsertr   intr   extendZsearchsortedZcumsumZrandappendspZ
csc_matrix)r   classesZclass_probabilityr   dataindicesZindptrjZclass_prob_jrngZ	p_nonzeroZnnzZ
ind_sampleZclasses_j_nonzeroZclass_probability_nzZclass_probability_nz_normZclasses_indr   r   r   random_choice_csct   sV    




r&   )NTNN)NN)Z
__future__r   Znumpyr   Zscipy.sparseZsparser    r   Zsklearn.utilsr   Z_randomr   Zdeprecationr   __all__r   r&   r   r   r   r   <module>   s   _ 