average

Averaging over every n elements of a numpy array

空扰寡人 提交于 2019-12-17 17:45:13
问题 I have a numpy array. I want to create a new array which is the average over every consecutive triplet of elements. So the new array will be a third of the size as the original. As an example: np.array([1,2,3,1,2,3,1,2,3]) should return the array: np.array([2,2,2]) Can anyone suggest an efficient way of doing this? I'm drawing blanks. 回答1: If your array arr has a length divisible by 3: np.mean(arr.reshape(-1, 3), axis=1) Reshaping to a higher dimensional array and then performing some form of

Calculating the averages for each KEY in a Pairwise (K,V) RDD in Spark with Python

≯℡__Kan透↙ 提交于 2019-12-17 07:05:09
问题 I want to share this particular Apache Spark with Python solution because documentation for it is quite poor. I wanted to calculate the average value of K/V pairs (stored in a Pairwise RDD), by KEY. Here is what the sample data looks like: >>> rdd1.take(10) # Show a small sample. [(u'2013-10-09', 7.60117302052786), (u'2013-10-10', 9.322709163346612), (u'2013-10-10', 28.264462809917358), (u'2013-10-07', 9.664429530201343), (u'2013-10-07', 12.461538461538463), (u'2013-10-09', 20.76923076923077)

How to compute the sum and average of elements in an array?

∥☆過路亽.° 提交于 2019-12-17 01:45:28
问题 Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted. I am having problems adding all the elements of an array as well as averaging them out. How would I do this and implement it with the code I currently have? The elements are supposed to be defined as I have it below. <script type="text/javascript"> //<![CDATA[ var i; var elmt = new Array(); elmt[0] =

Calculating the average of object properties in array

喜欢而已 提交于 2019-12-16 18:03:11
问题 I'm trying to calculate the average temperature of one week, but I do not quite know how I would do this. I've tried out some things but the end result would be either 'NaN' or 'Infinity'. Definitely doing something wrong here.. Here's the code I need to work with: var temperatures; temperatures = new Array(); temperatures["monday"] = 23.5; temperatures["tuesday"] = 22.3; temperatures["wednesday"] = 28.5; temperatures["thursday"] = 23.5; temperatures["friday"] = 22.3; temperatures["saturday"]

SQLite Exists keyword : How to query the highest average?

拥有回忆 提交于 2019-12-14 03:49:54
问题 In an SQLite database table with two columns 'mID', and 'stars', I have to return 'mID's with highest average values of 'stars'. Having the following data: Rating mID stars 101 2 101 4 106 4 103 2 108 4 108 2 101 3 103 3 104 2 108 4 107 3 106 5 107 5 104 3 I would first take average of 'stars' of each 'mID' by grouping it by 'mID', such as select mID, avg(stars) theAvg from Rating group by mID; As a result, I would get the table of average 'stars' values for each 'mID'. mID avg(stars) 101 3.0

Either an aggregate function or the GROUP BY clause

邮差的信 提交于 2019-12-14 03:46:11
问题 I used the following query: select Patients.LastName, avg (PatientVisits.Pulse)as pulse, avg (patientvisits.depressionlevel)as depressionLevel from Patients left join PatientVisits on Patients.PatientKey=PatientVisits.PatientKey But I get the following error: Msg 8120, Level 16, State 1, Line 1 Column 'Patients.LastName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. 回答1: You need to add a GROUP BY to your query: select

SQL Server - monthly avg of count

Deadly 提交于 2019-12-14 03:34:35
问题 I want to be able to find out the monthly average of a count My code at the moment is SELECT company, COUNT(company) AS 'count' FROM Information GROUP BY company I basically need it to be SELECT company, count(company) as 'count' avg(count(company)) per month as 'average' FROM Information group by company I want the result to look something like this company count monthly average a 5 6 b 13 14 c 2 2 d 45 45 e 23 21 f 6 5 回答1: A very simple approach would be to count per company and month

MySQL where clause and ordering by avg() as a sub query

Deadly 提交于 2019-12-14 02:18:47
问题 Although I can group and order by on an aliased sub query, I can't use the alias in a where clause. Do I need to use a join instead? Works: SELECT entries.*, (SELECT avg(value) FROM `ratings` WHERE ratings.entry_id = entries.id) as avg_rating FROM `entries` ORDER BY avg_rating DESC Fails ("unknown column 'avg_rating' in where clause"): SELECT entries.*, (SELECT avg(value) FROM `ratings` WHERE ratings.entry_id = entries.id) as avg_rating FROM `entries` WHERE avg_rating < '4.5000' ORDER BY avg

AVG() not accurate in SQL Server

我与影子孤独终老i 提交于 2019-12-13 23:50:03
问题 I am trying to calculate an Avg() for an column with where condition using SQL Server Management Studio 2008. When I try the AVG(column-name) in a SQL query it gives me a rounded value and not a decimal value. When I copy the dataset into MS Excel I get the accurate value with 4 decimal (example: 10.74) in SQL I am getting just 10. Any help is appreciated. My query: SELECT Item, AVG(POOrdrQty) FROM [tableWR] where Item ='737' AND POOrdrQty <((select AVG([POOrdrQty]) FROM [tableWR]) * 2) group

Calculate Average from a Textfile C#

余生颓废 提交于 2019-12-13 21:59:51
问题 private void button1_Click(object sender, EventArgs e) { try { var WriteToFile = new System.IO.StreamWriter("student.txt"); //create textfile in default directory WriteToFile.Write(txtStudNum.Text + ", " + txtStudName.Text + ", " + txtModCode.Text + ", " + txtModMark.Text); WriteToFile.Close(); this.Close(); } catch (System.IO.DirectoryNotFoundException ex) { //add error message } } private void button3_Click(object sender, EventArgs e) { File.AppendAllText("student.txt", "\r\n" + txtStudNum