numpy.random.choice和zeros的用法
2018-01-05 10:38
591 查看
numpy.random.choice
numpy.random.choice(a, size=None, replace=True, p=None)Generates a random sample from a given 1-D array
New in version 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. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. 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. |
---|---|
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 |
randint, shuffle, permutation
Ex
4000
amples
Generate a uniform random sample from np.arange(5) of size 3:
>>>
>>> np.random.choice(5, 3) 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]) 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) array([3,1,0]) >>> #This is equivalent to np.random.permutation(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]) 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]) array(['pooh', 'pooh', 'pooh', 'Christopher', 'piglet'], dtype='|S11')
用法:zeros(shape, dtype=float, order='C')返回:返回来一个给定形状和类型的用0填充的数组;参数:shape:形状 dtype:数据类型,可选参数,默认numpy.float64 dtype类型:t ,位域,如t4代表4位 b,布尔值,true or false i,整数,如i8(64位) u,无符号整数,u8(64位) f,浮点数,f8(64位) c,浮点负数, o,对象, s,a,字符串,s24 u,unicode,u24 order:可选参数,c代表与c语言类似,行优先;F代表列优先
相关文章推荐
- numpy.random.seed()和numpy.random.RandomState用法笔记
- numpy.random.choice
- numpy.random.shuffle(x)的用法
- numpy中的random.choice()函数
- numpy.random.RandomState()函数用法
- numpy.random 的函数基本用法
- enumerate(),flatnonzero(),argsort(),array_split(),numpy.random.choice()
- numpy中random的用法
- numpy.zeros((5,))中(5,)的含义 顺便复习Python中tuple的用法
- 【机器学习】【数据处理】numpy.random.RandomState函数用法
- numpy.random.randn()用法
- numpy.random.choice 中replace 的含义
- [numpy]random.choice()随机选取内容
- np.random.choice的用法
- [numpy]random.choice()随机选取内容
- o-c中随机数的用法 (3种:arc4random() 、random()、CCRANDOM_0_1() )
- numpy.zeros_like
- python-numpy的基本用法03
- Java RandomAccessFile用法
- numpy.ceil()用法