我想生成 0-1, 1-2, 2-3, 4-5,5-6,6-7,7-8,8-9,9-10 范圍內的五個亂數(五個浮點數)然后取這五個浮點數的平均值(所以我總共會得到十個數字)。我稍后將繪制這十個數字作為我的繪圖的 x 值。有沒有人對我如何使用 numpy 使用其方法進行此操作有任何建議?
uj5u.com熱心網友回復:
temp=[]
average=[]
for i in range(10):
temp.append(np.random.rand(5)*i)
average.append(numpy.average(temp[i])
uj5u.com熱心網友回復:
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])
uj5u.com熱心網友回復:
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://www.uj5u.com/caozuo/367144.html
標籤:Python 麻木的 matplotlib 随机的
