average

np.mean() vs np.average() in Python NumPy?

余生长醉 提交于 2019-11-28 03:07:14
I notice that In [30]: np.mean([1, 2, 3]) Out[30]: 2.0 In [31]: np.average([1, 2, 3]) Out[31]: 2.0 However, there should be some differences, since after all they are two different functions. What are the differences between them? np.average takes an optional weight parameter. If it is not supplied they are equivalent. Take a look at the source code: Mean , Average np.mean: try: mean = a.mean except AttributeError: return _wrapit(a, 'mean', axis, dtype, out) return mean(axis, dtype, out) np.average: ... if weights is None : avg = a.mean(axis) scl = avg.dtype.type(a.size/avg.size) else: #code

Average time in hours,minutes and seconds in php

时间秒杀一切 提交于 2019-11-28 01:40:58
问题 I have some total sums of hours and need to calculate the average. For example, my total hour value is 2452:43:44 (H:m:s) and the total count is 15. I would like to get the average time in the same format, that is hours:minutes:seconds. How can we do it in PHP ? 回答1: function average_time($total, $count, $rounding = 0) { $total = explode(":", strval($total)); if (count($total) !== 3) return false; $sum = $total[0]*60*60 + $total[1]*60 + $total[2]; $average = $sum/(float)$count; $hours = floor

Calculate the average of fields in embedded documents/array

两盒软妹~` 提交于 2019-11-28 00:53:36
问题 I want to calculate the rating_average field of this object with the rating fields inside the array ratings. Can you help me to understand how to use aggregation with $avg? { "title": "The Hobbit", "rating_average": "???", "ratings": [ { "title": "best book ever", "rating": 5 }, { "title": "good book", "rating": 3.5 } ] } 回答1: The aggregation framework in MongoDB 3.4 and newer offers the $reduce operator which efficiently calculates the total without the need for extra pipelines. Consider

Django ORM how to Round an Avg result

有些话、适合烂在心里 提交于 2019-11-27 23:42:20
I have a model in which I use Django ORM to extract Avg of values from the table. I want to Round that Avg value, how do I do this? See below I am extracting Avg price from Prices model grouped by date in format YYYY-MM, I want to automatically extract the average values rounded to the closest number. rs = Prices.objects.all.extra(select={ 'for_date': 'CONCAT(CONCAT(extract( YEAR from for_date ), "-"), LPAD(extract(MONTH from for_date), 2, "00"))' }).values('for_date').annotate(price=Avg('price')).order_by('-for_date') Use Func() expressions . Here's an example using the Book model from Django

Compute monthly averages from daily data

我与影子孤独终老i 提交于 2019-11-27 23:19:09
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-01-22 0 3821 2010-01-23 0 3822 2010-01-24 0 3823 2010-01-25 0 3824 2010-01-26 0 3825 2010-01-27 4.5

R calculate the average of one column corresponding to each bin of another column [duplicate]

江枫思渺然 提交于 2019-11-27 22:49:21
问题 This question already has an answer here: R aggregate data in one column based on 2 other columns 1 answer I have these data that has two columns. As you can see in the graph, the data has too much noise. So, I want to discretize column "r" with size 5, and assign each row to its corresponding bin, then calculate the average of f for each bin. > dr r f 1 65.06919 21.796 2 62.36986 22.836 3 59.81639 22.980 4 57.42822 22.061 5 55.22681 21.012 6 53.23533 21.274 7 51.47815 21.594 8 49.98000 22

Calculating average of an array list?

醉酒当歌 提交于 2019-11-27 22:39:13
I'm trying to use the below code to calculate the average of a set of values that a user enters and display it in a jTextArea but it does not work properly. Say, a user enters 7, 4, and 5, the program displays 1 as the average when it should display 5.3 ArrayList <Integer> marks = new ArrayList(); Collections.addAll(marks, (Integer.parseInt(markInput.getText()))); private void analyzeButtonActionPerformed(java.awt.event.ActionEvent evt) { analyzeTextArea.setText("Class average:" + calculateAverage(marks)); } private int calculateAverage(List <Integer> marks) { int sum = 0; for (int i=0; i<

Avg of a Sum in one query

无人久伴 提交于 2019-11-27 20:36:58
I would like to know if I can get the average of a sum in one single SQL SERVER request, Have tried to do it with the following request but it doesn't work: SELECT t.client, AVG(SUM(t.asset)) AS Expr1 FROM TABLE t GROUP BY t.client Lukasz Lysik I think your question needs a bit of explanation. If you want to take the sums grouped by t.client you can use: SELECT t.client, SUM(t.asset) FROM the-table t GROUP BY t.client Then, if you want to take the average of this sume, just make: SELECT AVG(asset_sums) FROM ( SELECT t.client, SUM(t.asset) AS asset_sums FROM the-table t GROUP BY t.client ) as

Getting the average of a certain hour on weekdays over several years in a pandas dataframe

半世苍凉 提交于 2019-11-27 20:14:49
I have an hourly dataframe in the following format over several years: Date/Time Value 01.03.2010 00:00:00 60 01.03.2010 01:00:00 50 01.03.2010 02:00:00 52 01.03.2010 03:00:00 49 . . . 31.12.2013 23:00:00 77 I would like to average the data so I can get the average of hour 0, hour 1... hour 23 of each of the years. So the output should look somehow like this: Year Hour Avg 2010 00 63 2010 01 55 2010 02 50 . . . 2013 22 71 2013 23 80 Does anyone know how to obtain this in pandas? Note: Now that Series have the dt accessor it's less important that date is the index, though Date/Time still needs

MATLAB Accumarray weighted mean

∥☆過路亽.° 提交于 2019-11-27 18:34:47
问题 So I am currently using 'accumarray' to find the averages of a range of numbers wich correspond to matching ID's. Ex Input: ID----Value 1 215 1 336 1 123 2 111 2 246 2 851 My current code finds the unweighted average of the above values, using the ID as the 'seperator' so that I don't get the average for all of the values together as one number, but rather seperate results for just values which have corresponding ID's. EX Output: ID----Value 1 224.66 2 402.66 To achieve this I am using this