简体   繁体   English

使用 numpy 在 python 中生成随机数

[英]Generating random number in python using numpy

I want to generate five random numbers within the range of 0-1, 1-2, 2-3, 4-5,5-6,6-7,7-8,8-9,9-10 (five floats) and then take the average of those five floats (so I will get ten numbers in total).我想生成 0-1、1-2、2-3、4-5、5-6、6-7、7-8、8-9、9-10 范围内的五个随机数(五个浮点数)然后取这五个浮点数的平均值(所以我总共会得到十个数字)。 I'll later plot those ten numbers as the x-value of my plot.稍后我将 plot 这十个数字作为我的 plot 的 x 值。 Does anyone have any recommendation as how i can do this with numpy using its methods?有没有人有任何建议,我如何使用 numpy 使用它的方法来做到这一点?

temp=[] 
average=[]
for i in range(10):
   temp.append(np.random.rand(5)*i)
   average.append(numpy.average(temp[i])

IIUC, you want to generate 5 floats from 10 possible ranges, where each float is in a different bucket. IIUC,您想从 10 个可能的范围中生成 5 个浮点数,其中每个浮点数位于不同的存储桶中。

An efficient vectorial solution would be to generate 5 floats between 0-1, and to pick 5 different integers in the range 0-9, then to sum them一个有效的矢量解决方案是在 0-1 之间生成 5 个浮点数,并在 0-9 范围内选择 5 个不同的整数,然后将它们相加

a = np.random.random(size=5)
b = np.random.choice(np.arange(10), size=5, replace=False)
c = a+b

Example output:示例 output:

>>> c
array([6.92992694, 7.64156516, 3.39305146, 2.11970796, 9.67578794])

np.random.rand(5, 10) generates 5 rows of 10 random values between 0 and 1: np.random.rand(5, 10)生成 5 行,每行 10 个介于 0 和 1 之间的随机值:

[[0.60597828, 0.73336936, 0.13894716, 0.31267308, 0.99724328, 0.12816238, 0.17899311, 0.75292543, 0.66216051, 0.78431013],
 [0.0968944 , 0.05857129, 0.96239599, 0.61655744, 0.08662996, 0.56127236, 0.61652471, 0.96384302, 0.57430429, 0.37116085],
 [0.45214524, 0.20185025, 0.56930512, 0.19509597, 0.58370402, 0.47631347, 0.5178144 , 0.82309863, 0.73222503, 0.06905627],
 [0.67212894, 0.64348481, 0.82801437, 0.20446939, 0.61748895, 0.61770101, 0.30106862, 0.87174059, 0.58965408, 0.98177009]]

np.random.rand(5, 10) + np.arange(10) adds the numbers 0..9 to each column: np.random.rand(5, 10) + np.arange(10)将数字 0..9 添加到每一列:

[[0.60597828 1.73336936 2.13894716 3.31267308 4.99724328 5.12816238 6.17899311 7.75292543 8.66216051 9.78431013],
 [0.0968944  1.05857129 2.96239599 3.61655744 4.08662996 5.56127236 6.61652471 7.96384302 8.57430429 9.37116085],
 [0.45214524 1.20185025 2.56930512 3.19509597 4.58370402 5.47631347 6.5178144  7.82309863 8.73222503 9.06905627],
 [0.67212894 1.64348481 2.82801437 3.20446939 4.61748895 5.61770101 6.30106862 7.87174059 8.58965408 9.98177009],
 [0.44223223 1.12631769 2.5088309  3.43178618 4.91593956 5.70901564 6.89065539 7.58888561 8.63682992 9.34220894]]

np.mean(data, axis=0) calculates the mean of each column: np.mean(data, axis=0)计算每列的平均值:

[0.45387582, 1.35271868, 2.60149871, 3.35211642, 4.64020116, 5.49849297, 6.50101124, 7.80009866, 8.63903477, 9.50970126]
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(2021)
data = np.random.rand(5, 10) + np.arange(10)
data_means = np.mean(data, axis=0)
plt.figure(figsize=(12, 2))
# show the means on the x-axis, let y-axis be zeros
plt.scatter(data_means, np.zeros(data_means.size), color='b', s=50, marker='+')
# for reference, show the original data
plt.scatter(data, np.zeros(data.size), color='r', marker='.', alpha=0.5)
# for reference, mark the 10 zones
plt.vlines(np.arange(11), -1, 1, ls='--', color='grey')
# set the x ticks
plt.xticks(np.arange(11))
# no y ticks needed
plt.yticks([])
plt.tight_layout()
plt.show()

绘制 10 个随机均值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM