我想生成0-1,1-2,2-3,4-5,5-6,6-7,7-8,8-9,9-10 (5个浮点数)范围内的5个随机数,然后取这5个浮点数的平均值(所以我总共得到10个数字)。稍后我将把这十个数字作为我的图的x值。有没有人推荐我如何使用numpy的方法来做这件事?
发布于 2021-11-26 04:18:25
temp=[]
average=[]
for i in range(10):
temp.append(np.random.rand(5)*i)
average.append(numpy.average(temp[i])
发布于 2021-11-26 04:36:28
IIUC,您希望从10个可能的范围生成5个浮点数,其中每个浮点数位于不同的存储桶中。
一种有效的矢量解决方案是在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
输出示例:
>>> c
array([6.92992694, 7.64156516, 3.39305146, 2.11970796, 9.67578794])
发布于 2021-11-26 07:09:36
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)
将数字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)
计算每列的平均值:
[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()
https://stackoverflow.com/questions/70119518
复制相似问题