Submit
Path:
~
/
/
proc
/
thread-self
/
root
/
opt
/
alt
/
python35
/
lib64
/
python3.5
/
site-packages
/
sklearn
/
utils
/
__pycache__
/
File Content:
random.cpython-35.opt-1.pyc
��(X) � @ s� d d l m Z d d l Z d d l j Z d d l Z d d l Z d d l m Z d d l m Z d d l m Z d d g Z d d d d d d � Z d d d d � Z d S) � )�divisionN)�check_random_state)�astype� )�sample_without_replacementr �choiceTc C s> t | � } t j | d d �} | j d k r� y t j | j � � } Wn t k ri t d � � Yn X| d k r� t d � � nC | j d k r� t d � � n% | j d } | d k r� t d � � | d k rst j | d t j d d d d �} | j d k rt d � � | j | k r.t d � � t j | d k � rOt d � � t j | j � d � sst d � � | } | d k r�t j | d t j �} n d } | r)| d k r| j � } | | d } | j | � } | j | d d �} t j | d d �} q�| j d | d | �} n�| | k rAt d � � | d k r�t j | d k � | k rtt d � � d } | j � } t j | d t j �} | j � } x� | | k ry| j | | � } | d k r�d | | d | � <t j | � } | | d } | j | d d �} t j | d d �\ } } | j � | j | � } | | | | | j � <| | j 7} q�W| } n. | j | � d | � } | d k r�| | _ | d k r�t | t j � r�| j d � } | j d k r�| S| d k r6| j d k r6t j f d | j! �} | | | f <| S| | 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') �copyFr z%a must be 1-dimensional or an integerza must be greater than 0r za must be 1-dimensionalza must be non-emptyN�dtypeZndminzp must be 1-dimensionalza and p must have same sizez"probabilities are not non-negativezprobabilities do not sum to 1Zside�right�sizez@Cannot take a larger sample than population when 'replace=False'z%Fewer non-zero entries in p than sizeZreturn_indexT���r )"r �np�array�ndim�operator�index�item� TypeError� ValueError�shapeZdoubler �anyZallclose�sumZprodZintp�cumsumZ random_sample�searchsorted�randintr Zzeros�intZravel�rand�unique�sortZtakeZpermutation� isinstanceZndarray�emptyr )�ar �replace�p�random_stateZpop_sizer ZcdfZuniform_samples�idxZn_uniq�foundZ flat_found�x�new�_Zunique_indices�res� r+ � /random.pyr s� X $ c C s� t j d � } t j d � } t j d d g � } x}t t | � � D]i} t j | | � | | <| | j j d k r� t d | | j � � t | | t j d d �| | <| d k r� t j d | | j d � } | j d | | j d � n t j | | � } t j | � d k r5t d j | � � � | j d | | j d k r�t d j | | | j d | j d � � � d | | k r�t j | | d d � | | <t j | d d � } t | � } | | j d d k r�d | | | d k } t | | � } t d | d | d | � } | j | � | | d k } | | } | t j | � } t j | j � | j | � � } | j | | | | � | j t | � � qF Wt j | | | f | t | � f d t �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 zclass dtype %s is not supportedr FNr r g �?z2Probability array at index {0} does not sum to onezXclasses[{0}] (length {1}) and class_probability[{0}] (length {2}) have different length.g Zn_population� n_samplesr$ r )r �range�lenr Zasarrayr �kindr r Zint64r r Zfillr �format�insertr r r �extendr r r �append�spZ csc_matrix)r. �classesZclass_probabilityr$ �data�indicesZindptr�jZclass_prob_j�rngZ p_nonzeroZnnzZ ind_sampleZclasses_j_nonzeroZclass_probability_nzZclass_probability_nz_normZclasses_indr+ r+ r, �random_choice_csc� sV r<