average

How to efficiently compute average on the fly (moving average)?

为君一笑 提交于 2019-11-27 10:28:20
问题 I come up with this n=1; curAvg = 0; loop{ curAvg = curAvg + (newNum - curAvg)/n; n++; } I think highlights of this way are: - It avoids big numbers (and possible overflow if you would sum and then divide) - you save one register (not need to store sum) The trouble might be with summing error - but I assume that generally there shall be balanced numbers of round up and round down so the error shall not sum up dramatically. Do you see any pitfalls in this solution? Have you any better proposal

Calculate average in java

倾然丶 夕夏残阳落幕 提交于 2019-11-27 07:05:44
问题 EDIT: Ive written code for the average, but i dont know how to make it so that it also uses ints from my args.length rather than the array I need to write a java program that can calculate: 1. the number of integers read in 2. the average value—which need not be an integer! NOTE! i dont want to calculate the average from the array but the integers in the args. Currently i have written this: int count = 0; for (int i = 0; i<args.length -1; ++i) count++; System.out.println(count); } int nums[]

Average of grouped rows in Sql Server

妖精的绣舞 提交于 2019-11-27 06:37:38
问题 I have an Sql Server Table.. it's something like this: Id ...... Column1 ...... Column2 ```````````````````````````````` 1 ........ 1 ............. 34 2 ........ 1 ............. 44 3 ........ 2 ............. 45 4 ........ 2 ............. 36 5 ........ 2 ............. 23 6 ........ 3 ............. 68 7 ........ 3 ............. 26 So, I need to select the average of Column2,but group with column1 before doing that. I mean, if I say Avg(Column2) it just returns a single row with the average of

Averaging dates in oracle sql

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 06:17:27
问题 Is there a way to average multiple dates in oracle? avg doesn't do any good. Thanks. 回答1: The definition of an "average date" is subjective, but you could convert your dates to a Julian number, then average those, round it off, then convert back to a date. create table dates (dt DATE); insert into dates values ('24-APR-2012'); insert into dates values ('01-JAN-2012'); insert into dates values ('01-JAN-2013'); insert into dates values ('25-DEC-1900'); select to_date(round(avg(to_number(to_char

SQL query with avg and group by

醉酒当歌 提交于 2019-11-27 04:36:59
问题 I have some problems with writing a SQL query for MySQL. I have a table with the following structure: mysql> select id, pass, val from data_r1 limit 10; +------------+--------------+----------------+ | id | pass | val | +------------+--------------+----------------+ | DA02959106 | 5.0000000000 | 44.4007000000 | | 08A5969201 | 1.0000000000 | 182.4100000000 | | 08A5969201 | 2.0000000000 | 138.7880000000 | | DA02882103 | 5.0000000000 | 44.7265000000 | | DA02959106 | 1.0000000000 | 186.1470000000

Average time for datetime list

强颜欢笑 提交于 2019-11-27 03:30:00
问题 Looking for fastest solution of time averaging problem. I've got a list of datetime objects. Need to find average value of time (excluding year, month, day). Here is what I got so far: import datetime as dtm def avg_time(times): avg = 0 for elem in times: avg += elem.second + 60*elem.minute + 3600*elem.hour avg /= len(times) rez = str(avg/3600) + ' ' + str((avg%3600)/60) + ' ' + str(avg%60) return dtm.datetime.strptime(rez, "%H %M %S") 回答1: Here's a better way to approach this problem

How can I get xtabs to calculate means instead of sums in R?

a 夏天 提交于 2019-11-27 02:19:32
问题 I have a data frame where each line represents an individual. That data frame has two variables: age and year. I want to make a table of average ages per year. How can I do it? The best I could come up with was xtabs(age ~ year, dataframe) , but this gives me the sum of ages per year. 回答1: Use aggregate : xtabs(hp~cyl+gear,aggregate(hp~cyl+gear,mtcars,mean)) gear cyl 3 4 5 4 97.0000 76.0000 102.0000 6 107.5000 116.5000 175.0000 8 194.1667 0.0000 299.5000 回答2: Have a look at the plyr package,

Calculate cumulative average (mean)

人走茶凉 提交于 2019-11-27 02:06:36
I would like to know how to calculate the cumulative average for some numbers. I will give a simple example to describe what I am looking for. I have the following numbers vec <- c(1, 2, 3, 4, 5) If I do the average of these numbers I will get 3 as a result. Now, how to do the cumulative average of these numbers. In analogy to the cumulative sum of a list I propose this: The cumulative average avg of a vector x would contain the averages from 1st position till position i . One method is just to compute the the mean for each position by summing over all previous values and dividing by their

Compute monthly averages from daily data

我怕爱的太早我们不能终老 提交于 2019-11-26 23:19:06
问题 I have this dataframe "df1" as example which is actually part of a much larger one (15 years): X1 X2 3798 2009-12-29 0 3799 2009-12-30 0 3800 2009-12-31 0 3802 2010-01-02 0 3803 2010-01-03 2.1 3804 2010-01-04 0 3805 2010-01-05 0 3806 2010-01-06 0 3807 2010-01-07 0 3808 2010-01-08 0 3809 2010-01-09 0 3810 2010-01-10 6.8 3811 2010-01-12 0 3812 2010-01-13 0 3813 2010-01-14 17.7 3814 2010-01-16 0 3815 2010-01-17 0 3816 2010-01-18 1.5 3817 2010-01-19 0 3818 2010-01-20 0 3819 2010-01-21 0 3820 2010

Finding moving average from data points in Python

扶醉桌前 提交于 2019-11-26 21:39:43
I am playing in Python a bit again, and I found a neat book with examples. One of the examples is to plot some data. I have a .txt file with two columns and I have the data. I plotted the data just fine, but in the exercise it says: Modify your program further to calculate and plot the running average of the data, defined by: $Y_k=\frac{1}{2r}\sum_{m=-r}^r y_{k+m}$ where r=5 in this case (and the y_k is the second column in the data file). Have the program plot both the original data and the running average on the same graph. So far I have this: from pylab import plot, ylim, xlim, show, xlabel