Ask Your Question
1

frecuency and numbers - Numpy - mean, histogram and more

asked 2013-06-25 20:01:54 +0100

mresimulator gravatar image

Hi experts!

If i have two numpy arrays: A = califications, B = number of alumns with calification 'a' (I mean: B[j] is the number of alumni in the class with test calification a[j]):

How can i calculate the MEAN VALUE and STANDARD DEVIATION of califications, and build a histogram of califications using numpy.

Waiting for your answers.

Thanks a lot!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2013-06-26 00:08:47 +0100

Eviatar Bach gravatar image

updated 2013-06-26 17:08:06 +0100

Here is how you would do it, assuming A and B are NumPy arrays:

import matplotlib.pyplot as plt
mean = A.mean()
standard_deviation = A.std()
plt.hist(B)
plt.savefig('histogram.png')

Unfortunately Sage does not yet have good histogram functionality; however, it does have mean and standard deviation, with the benefit that you can get exact output:

sage: std(range(10))
sqrt(55/6)

EDIT: Here's a way to take (integer) frequencies into account:

def mean2(data, freq):
    return sum(map(prod, zip(data, freq))) / sum(freq)

def std2(data, freq):
    data2 = []
    for index, item in enumerate(data):
        data2.extend([item] * freq[index])
    return std(data2)
edit flag offensive delete link more

Comments

Hello Eviatar Bach.

Thanks for your answer.

Please note that doing:

mean = A.mean()
standard_deviation = A.std()

like you wrote, you are not cosidering the 'frecuency' (weight) of califications (array B).

Surfing in web, I found the function np.average:

note_mean=np.average(A,weights=B).

I still can not find a function to calculate the standard deviation. Any idea?

Waiting for your answers.

Thanks a lot!

mresimulator gravatar imagemresimulator ( 2013-06-26 07:51:32 +0100 )edit

I think R will handle working with a frequency table if you set the table up correctly. Otherwise, you might need to write your own code for this.

calc314 gravatar imagecalc314 ( 2013-06-26 10:17:17 +0100 )edit

Sorry, I misunderstood the question. I edited my post with code which I think does what you want.

Eviatar Bach gravatar imageEviatar Bach ( 2013-06-26 17:02:09 +0100 )edit

Hello Eviatar Bach. Thanks for your answer.

Only remains to construct the bar graph from these two arrays (data & freq).

Any idea??

Waiting for your answers.

Thanks a lot!

mresimulator gravatar imagemresimulator ( 2013-06-27 11:55:31 +0100 )edit
0

answered 2013-06-27 14:34:36 +0100

calc314 gravatar image

You can do the bar graph using pyplot from matplotlib as follows:

import matplotlib.pyplot as plt
categories=[1,2,3,4]
frequencies=[5,2,6,10]
plt.figure()
plt.bar(categories,frequencies,width=1)
plt.savefig('tmp.png')
edit flag offensive delete link more

Comments

1

Thanks a lot!!

Case closed

mresimulator gravatar imagemresimulator ( 2013-06-28 17:24:46 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2013-06-25 20:01:54 +0100

Seen: 5,383 times

Last updated: Jun 28 '13