473,383 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Problem with Numpy Standard Deviation

Can anyone please tell me where I am going wrong with this for loop which is meant to take as input a specific corpus, sample size and number of samples and then give the averages of the expected sentiment tokens, normalised lexical diversity and probability of short sentences. It is also meant to give me the standard deviation of these three statistics too. I'm a real beginner with Python so not really sure where I've gone wrong Thanks a lot in advance.

Expand|Select|Wrap|Line Numbers
  1. def test_iterate(corpus_reader, sample_size, number_of_samples):
  2. for i in xrange(number_of_samples):
  3. tokens = corpus_reader.sample_words_by_sents(sample_size)
  4. sents = corpus_reader.sample_sents(sample_size)
  5. expected_sentiment_tokens(tokens)
  6. normalised_lexical_diversity(tokens)
  7. prob_short_sents(sents)
  8. stats = expected_sentiment_tokens(tokens)
  9. stats_two = normalised_lexical_diversity(tokens)
  10. stats_three = prob_short_sents(sents)
  11. print "Average expected no of sentiment tokens: %s" % average(stats)
  12. print "Average normalised lexical diversity: %s" % average(stats_two)
  13. print "Average probability of short sentences: %s" % average(stats_three)
  14. print "Standard deviation of sentiment tokens: %s" % std(stats)
  15. print "Standard deviation of normalised lexical diversity: %s" % std(stats_two)
  16. print "Standard deviation of probability of short sentences: %s" % std(stats_three)
When I call for example
Expand|Select|Wrap|Line Numbers
  1. test_iterate(tcr, 500, 3)
, the following output is given:

Expand|Select|Wrap|Line Numbers
  1. 127.333333333 
  2. 2.08398681196 
  3. 0.506 
  4. 116.25 
  5. 2.21737363871 
  6. 0.518 
  7. 123.333333333 
  8. 1.9821801535 
  9. 0.534 
  10. Average expected no of sentiment tokens: 110.416666667 
  11. Average normalised lexical diversity: 2.89485940038 
  12. Average probability of short sentences: 0.518 
  13. Standard deviation of sentiment tokens: 0.0 
  14. Standard deviation of normalised lexical diversity: 0.0 
  15. Standard deviation of probability of short sentences: 0.0
Oct 14 '12 #1
6 3759
dwblas
626 Expert 512MB
I'm a real beginner with Python so not really sure where I've gone wrong
What is wrong with what you have? What do you expect vs. what is printed? It looks like you are not saving the results from each iteration in the for() loop so are only printing the final pass but I can't tell from what you have submitted. Add some print statements similar to this
Expand|Select|Wrap|Line Numbers
  1. def test_iterate(corpus_reader, sample_size, number_of_samples):
  2.      for i in xrange(number_of_samples):
  3.          tokens = corpus_reader.sample_words_by_sents(sample_size)
  4.          sents = corpus_reader.sample_sents(sample_size)
  5.          expected_sentiment_tokens(tokens)
  6.          normalised_lexical_diversity(tokens)
  7.          prob_short_sents(sents)
  8.  
  9.          stats = expected_sentiment_tokens(tokens)
  10.          print "stats in for loop =", stats
  11.  
  12.          stats_two = normalised_lexical_diversity(tokens)
  13.          stats_three = prob_short_sents(sents)
  14.  
  15.      print "using stats =", stats, type(stats)
  16.      print "Average expected no of sentiment tokens: %s" % average(stats) 
If "stats" is anything other than a numpy array then you are probably averaging one number instead of a list of numbers.
Oct 14 '12 #2
Thanks for the quick reply. I just tried that and this is what outputted
Expand|Select|Wrap|Line Numbers
  1. stats in for loop = 191.473684211
  2. stats in for loop = 186.277777778
  3. stats in for loop = 182.473684211
  4. stats in for loop = 182.611111111
  5. using stats = 182.611111111 <type 'float'>
  6. Average expected no of sentiment tokens: 182.611111111
Do you know how I could fix my code to make it work correctly? Just need the averages and standard deviations for each statistic.

Thanks again.
Oct 14 '12 #3
dwblas
626 Expert 512MB
stats in for loop = 182.611111111 <----- same value
using stats = 182.611111111 <type 'float'> on both lines
You have to append each of the values in the loop to a numpy array and average the array (some definitions). Start with a simple average only and then expand the code. I am sure there are many tutuorials/examples on the web for arrays, average, and standard deviation.
Oct 15 '12 #4
Could you please tell me what's wrong with this following code?
Expand|Select|Wrap|Line Numbers
  1. def test_iterate(corpus_reader, sample_size, number_of_samples):
  2. for i in xrange(number_of_samples):
  3. tokens = corpus_reader.sample_words_by_sents(sample_size)
  4. sents = corpus_reader.sample_sents(sample_size)
  5. print expected_sentiment_tokens(tokens)
  6. s = ([expected_sentiment_tokens(tokens)])
  7. s.append(expected_sentiment_tokens(tokens))
  8. print "Average expected no of sentiment tokens: %s" % average(s)
Expand|Select|Wrap|Line Numbers
  1. test_iterate(rcr, 500, 3)
gives output

Expand|Select|Wrap|Line Numbers
  1. 191.823529412
  2. 185.117647059
  3. 185.166666667
  4. Average expected no of sentiment tokens: 185.166666667
the average is being assigned just the last value
Oct 15 '12 #5
Rabbit
12,516 Expert Mod 8TB
You're just getting the last value because that's all you have available. In line 6, you replace whatever you had with the most current value. Then in line 7, you append the most current value. The result being an average of the most current value. Then in the next iteration, you do it all over again. You will only ever have 2 of the most current values in there.
Oct 15 '12 #6
Rabbit
12,516 Expert Mod 8TB
I am reverting your posts for posterity.

Please don't edit your posts and remove all traces of your question. If someone were to visit this thread to see the answer, we would like them to be able to view everything. Also, if you've solved the issue, please post the answer so that others facing the same issue may benefit from your solution.
Oct 15 '12 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: T.Venkatesh | last post by:
Hi friends, I just got stuck up in JDBC,JSP. My problem is iam unable to display the output of Standard deviation , variance sum, of a column in a JSP page. But it complies- but no display of...
3
by: mat | last post by:
Je l'ai cherché partout, je ne l'ai pas trouvé, alors je l'ai écrite. Si ça peut servir à d'autres ... echo "Calcul d'un ecart type"; echo "<br>"; $datas =...
3
by: Scott | last post by:
I know in some other languages there is a simple standard deviation function (sdev(1,2,3,4,5,etc...)). But I'm unable to find a suitable alternative for vb.net I'm assuming it's there...
2
by: Quentin | last post by:
How would I calculate standard deviation of a csv file? Any code examples would be great! Thank you!
10
by: Verbal Kint | last post by:
DEAR ALL, I just have a very short question. In the FAQ list question 13.20 the following text is mentioned: "These methods all generate numbers with mean 0 and standard deviation 1. (To adjust...
0
by: aboxylica | last post by:
do v have a function to calculate variance, average and standard deviation in python??
6
by: kumarboston | last post by:
Hi All, I am trying to get an average value for my data, here is my data file DATA FILE EP1934.PDB 250 250 11.27 EP1934.PDB 251 251 12.7332 EP1934.PDB 252 252 6.38341 EP1934.PDB 253 253...
6
by: shibujohn82 | last post by:
Hi, My data set is followed like this... (as tab delimited input.csv) First is column is gene name, second column is bn count, third colums is sh count. ENSRNOG00000000417 42 102...
3
by: yawsam | last post by:
How can i use aggregate function like count, sum and other to calculate standard deviation in SQL
0
by: Dave Smith | last post by:
I’m hoping someone can help me with my newest rank issue. I’m trying to get a rank my standard deviation so it will be a number below 1, the numbers I'm trying to rank are below. Example: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.