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). I'll later plot those ten numbers as the x-value of my plot. Does anyone have any recommendation as how i can do this with numpy using its methods?
CodePudding user response:
temp=[]
average=[]
for i in range(10):
temp.append(np.random.rand(5)*i)
average.append(numpy.average(temp[i])
CodePudding user response:
IIUC, you want to generate 5 floats from 10 possible ranges, where each float is in a different bucket.
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
a = np.random.random(size=5)
b = np.random.choice(np.arange(10), size=5, replace=False)
c = a b
Example output:
>>> c
array([6.92992694, 7.64156516, 3.39305146, 2.11970796, 9.67578794])
CodePudding user response:
np.random.rand(5, 10)
generates 5 rows of 10 random values between 0 and 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:
[[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:
[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()