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. - def test_iterate(corpus_reader, sample_size, number_of_samples):
-
for i in xrange(number_of_samples):
-
tokens = corpus_reader.sample_words_by_sents(sample_size)
-
sents = corpus_reader.sample_sents(sample_size)
-
expected_sentiment_tokens(tokens)
-
normalised_lexical_diversity(tokens)
-
prob_short_sents(sents)
-
stats = expected_sentiment_tokens(tokens)
-
stats_two = normalised_lexical_diversity(tokens)
-
stats_three = prob_short_sents(sents)
-
print "Average expected no of sentiment tokens: %s" % average(stats)
-
print "Average normalised lexical diversity: %s" % average(stats_two)
-
print "Average probability of short sentences: %s" % average(stats_three)
-
print "Standard deviation of sentiment tokens: %s" % std(stats)
-
print "Standard deviation of normalised lexical diversity: %s" % std(stats_two)
-
print "Standard deviation of probability of short sentences: %s" % std(stats_three)
When I call for example - test_iterate(tcr, 500, 3)
, the following output is given: -
127.333333333
-
2.08398681196
-
0.506
-
116.25
-
2.21737363871
-
0.518
-
123.333333333
-
1.9821801535
-
0.534
-
Average expected no of sentiment tokens: 110.416666667
-
Average normalised lexical diversity: 2.89485940038
-
Average probability of short sentences: 0.518
-
Standard deviation of sentiment tokens: 0.0
-
Standard deviation of normalised lexical diversity: 0.0
-
Standard deviation of probability of short sentences: 0.0
6 3759
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 - def test_iterate(corpus_reader, sample_size, number_of_samples):
-
for i in xrange(number_of_samples):
-
tokens = corpus_reader.sample_words_by_sents(sample_size)
-
sents = corpus_reader.sample_sents(sample_size)
-
expected_sentiment_tokens(tokens)
-
normalised_lexical_diversity(tokens)
-
prob_short_sents(sents)
-
-
stats = expected_sentiment_tokens(tokens)
-
print "stats in for loop =", stats
-
-
stats_two = normalised_lexical_diversity(tokens)
-
stats_three = prob_short_sents(sents)
-
-
print "using stats =", stats, type(stats)
-
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.
Thanks for the quick reply. I just tried that and this is what outputted - stats in for loop = 191.473684211
-
stats in for loop = 186.277777778
-
stats in for loop = 182.473684211
-
stats in for loop = 182.611111111
-
using stats = 182.611111111 <type 'float'>
-
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.
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.
Could you please tell me what's wrong with this following code? - def test_iterate(corpus_reader, sample_size, number_of_samples):
-
for i in xrange(number_of_samples):
-
tokens = corpus_reader.sample_words_by_sents(sample_size)
-
sents = corpus_reader.sample_sents(sample_size)
-
print expected_sentiment_tokens(tokens)
-
s = ([expected_sentiment_tokens(tokens)])
-
s.append(expected_sentiment_tokens(tokens))
-
print "Average expected no of sentiment tokens: %s" % average(s)
- test_iterate(rcr, 500, 3)
gives output - 191.823529412
-
185.117647059
-
185.166666667
-
Average expected no of sentiment tokens: 185.166666667
the average is being assigned just the last value
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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 =...
|
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...
|
by: Quentin |
last post by:
How would I calculate standard deviation of a csv file? Any code
examples would be great!
Thank you!
|
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...
|
by: aboxylica |
last post by:
do v have a function to calculate variance, average and standard deviation in python??
|
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...
|
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...
|
by: yawsam |
last post by:
How can i use aggregate function like count, sum and other to calculate standard deviation in SQL
|
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:
...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |