1

i want to create a program which calculates the average of N random numbers taken from a uniform random number distribution.The program must run for Ν=10,100,1000,10000,100000,1000000 random numbers.Then,i have to plot the mean value as a function of N.

I did this:

from scitools.std import *
import matplotlib.pyplot as plt


N=10

distribution=[]
for i in range(1,7):   
    N*=10
    random_numbers=[random.uniform(0,1,size=N)]   
    distribution.append(random_numbers)


plt.semilogx(array(range(N)),array(distribution).mean())
plt.xlabel('N')
plt.grid(True)
plt.show()

It gives me the error in the title in the line where i do the plot. Also,if there is another ,more pythonic way of doing this i'll appreciate it.

Thank you.

3
  • It is in the title:setting an array element with a sequence (Valueerror) Commented Oct 31, 2011 at 16:08
  • 1
    Take the mean of random_numbers before adding it to distribution. Commented Oct 31, 2011 at 16:09
  • Thank you all for your answers.I chose the most pythonic.Thanks again! Commented Oct 31, 2011 at 16:20

3 Answers 3

1
import scipy
import matplotlib.pyplot as plt

Ns=[10**i for i in range(1,7)]
means=[scipy.random.uniform(0,1,size=N).mean() for N in Ns]

plt.semilogx(Ns,means)
plt.xlabel('N')
plt.grid(True)
plt.show()
  1. As has already been mentioned, distribution is a list of arrays, with the inner arrays having different shapes. You can't form a numpy array out of such an object by calling np.array(distribution).
  2. At least for plotting purposes, you don't need to save the entire distribution. Just compute and store the means. A succinct way to do that is to use a list comprehension.
  3. Don't use from module import * in scripts. It makes it hard to trace where variables come from. The from module import * was made (mainly) for use in interactive sessions, but generally not recommended for scripts.
Sign up to request clarification or add additional context in comments.

2 Comments

Ok,thanks for the tips!I use import * because some times its difficult to know what method goes where.
It pays to take the time to sort it out now, and save examples of how you use various functions so you can cut-and-paste later. Doing from scipy import * is particularly dangerous because scipy.any is a different function than Python's builtin any. from scipy import * overwrites Python's builtin any, making it harder to access (and a potential source of bugs).
1

Compute the mean() before adding the random numbers to the distribution list. Then you have a simple plot of an n-element list. The problem is that you needed a list of scalars, not a list of arrays:

distribution=[]
for i in range(1,7):   
    N*=10
    random_numbers=[random.uniform(0,1,size=N)]   
    distribution.append(array(random_numbers).mean())

4 Comments

+1. Just for the sake of completeness, I'll add a bit of explanation: The OP's conversion array(distribution) didn't work because it tried to convert a list of one-dimensional arrays of different lengths to a NumPy array, which is not possible. map(numpy.mean, distribution) would also work in the original code.
The problem with the above code is that the "mean" method doesn't work because random_numbers is a list.If i make it array,then it gives me error:must have same first dimension
A list of scalars should be convertible to an array so you can take the mean.
This works fine: >>> numpy.random.uniform(0,1,10).mean() --> 0.48908919057971734
0

You seem to be appending a list in a list, like this:

a = [[1], [2], [3]]

Try this code instead:

for i in range(1, 7):   
    N*=10
    distribution.append(random.uniform(0,1,size=N))

@Raymond suggests to take the mean of the sequence:

plt.semilogx(array(range(N)),array(sum(distribution) / float(len(distribution))))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.