📌  相关文章
📜  如何在Python使用 NumPy 随机选择数组的元素?

📅  最后修改于: 2022-05-13 01:54:29.321000             🧑  作者: Mango

如何在Python使用 NumPy 随机选择数组的元素?

先决条件: Numpy

随机值在机器学习、统计和概率等数据相关领域很有用。这 numpy.random.choice() 函数用于从 NumPy 数组中获取随机元素。它是Python的 NumPy 包中的内置函数。

方法

  • 导入模块
  • 创建样本数组
  • 从创建的数组中随机选择值
  • 打印如此生成的数组。

下面给出的是一维和二维数组的实现。



生成一维随机样本列表

示例 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]))

输出: