如何在Python使用 NumPy 随机选择数组的元素?
先决条件: Numpy
随机值在机器学习、统计和概率等数据相关领域很有用。这 numpy.random.choice() 函数用于从 NumPy 数组中获取随机元素。它是Python的 NumPy 包中的内置函数。
Syntax: numpy.random.choice( a , size = None, replace = True, p = None)
Parameters:
- a: a one-dimensional array/list (random sample will be generated from its elements) or an integer (random samples will be generated in the range of this integer)
- size: int or tuple of ints (default is None where a single random value is returned). If the given shape is (m,n), then m x n random samples are drawn.
- replace: (optional); the Boolean value that specifies whether the sample is drawn with or without replacement. When sample is larger than the population of the list, replace cannot be False.
- p: (optional); a 1-D array containing probabilities associated with each entry in a. If not given then sample assumes uniform distribution over all entries in a.
方法
- 导入模块
- 创建样本数组
- 从创建的数组中随机选择值
- 打印如此生成的数组。
下面给出的是一维和二维数组的实现。
生成一维随机样本列表
示例 1:
Python3
import numpy as np
prog_langs = ['python', 'c++', 'java', 'ruby']
# generating random samples
print(np.random.choice(prog_langs, size=8))
# generating random samples without replacement
print(np.random.choice(prog_langs, size=3, replace=False))
# generating random samples with probabilities
print(np.random.choice(prog_langs, size=10,
replace=True, p=[0.3, 0.5, 0.0, 0.2]))
Python3
import numpy as np
samples = 5
# generating random samples
print(np.random.choice(samples, size=10))
# generating random samples without replacement
print(np.random.choice(samples, size=5, replace=False))
# generating random samples with probablities
print(np.random.choice(samples, size=5, replace=True))
# generating with probabilities
print(np.random.choice(samples, size=15,
replace=True, p=[0.2, 0.1, 0.1, 0.3, 0.3]))
Python3
import numpy as np
prog_langs = ['python', 'c++', 'java', 'ruby']
# generating random samples
print(np.random.choice(prog_langs, size=(4, 5)))
# generating random samples with probabilities
print('\n')
print(np.random.choice(prog_langs, size=(10, 2),
replace=True, p=[0.3, 0.5, 0.0, 0.2]))
Python3
import numpy as np
samples = 5
# generating random samples
print(np.random.choice(samples, size=(5, 5)))
# generating with probabilities
print('\n')
print(np.random.choice(samples, size=(8, 3),
replace=True,
p=[0.2, 0.1, 0.1, 0.3, 0.3]))
输出 :
示例 2:
蟒蛇3
import numpy as np
samples = 5
# generating random samples
print(np.random.choice(samples, size=10))
# generating random samples without replacement
print(np.random.choice(samples, size=5, replace=False))
# generating random samples with probablities
print(np.random.choice(samples, size=5, replace=True))
# generating with probabilities
print(np.random.choice(samples, size=15,
replace=True, p=[0.2, 0.1, 0.1, 0.3, 0.3]))
输出:
生成随机样本的二维列表
例子:
蟒蛇3
import numpy as np
prog_langs = ['python', 'c++', 'java', 'ruby']
# generating random samples
print(np.random.choice(prog_langs, size=(4, 5)))
# generating random samples with probabilities
print('\n')
print(np.random.choice(prog_langs, size=(10, 2),
replace=True, p=[0.3, 0.5, 0.0, 0.2]))
输出:
示例 2:
蟒蛇3
import numpy as np
samples = 5
# generating random samples
print(np.random.choice(samples, size=(5, 5)))
# generating with probabilities
print('\n')
print(np.random.choice(samples, size=(8, 3),
replace=True,
p=[0.2, 0.1, 0.1, 0.3, 0.3]))
输出: