median

Get median from AVL tree?

为君一笑 提交于 2019-12-05 11:55:33
If you have an AVL tree, what's the best way to get the median from it? The median would be defined as the element with index ceil(n/2) (index starts with 1) in the sorted list. So if the list was 1 3 5 7 8 the median is 5. If the list was 1 3 5 7 8 10 the median is 5. If you can augment the tree, I think it's best to let each node know the size (number of nodes) of the subtree, (i.e. 1 + left.size + right.size). Using this, the best way I can think of makes median searching O(lg n) time because you can traverse by comparing indexes. Is there a better way? Augmenting the AVL tree to store

Tensorflow median value

一个人想着一个人 提交于 2019-12-05 07:05:01
How can I calculate the median value of a list in tensorflow? Like node = tf.median(X) X is the placeholder In numpy, I can directly use np.median to get the median value. How can I use the numpy operation in tensorflow? edit: This answer is outdated, use Lucas Venezian Povoa's solution instead. It is simpler and faster. You can calculate the median inside tensorflow using: def get_median(v): v = tf.reshape(v, [-1]) mid = v.get_shape()[0]//2 + 1 return tf.nn.top_k(v, mid).values[-1] If X is already a vector you can skip the reshaping. If you care about the median value being the mean of the

Count median grouped by day

前提是你 提交于 2019-12-05 07:03:06
I have a script which counts median value for all table data: SELECT avg(t1.price) as median_val FROM ( SELECT @rownum:=@rownum+1 as `row_number`, d.price FROM mediana d, (SELECT @rownum:=0) r WHERE 1 ORDER BY d.price ) as t1, ( SELECT count(*) as total_rows FROM mediana d WHERE 1 ) as t2 AND t1.row_number>=total_rows/2 and t1.row_number<=total_rows/2+1; Now I need to get median value not for all table values, but grouped by date. Is it possible? http://sqlfiddle.com/#!2/7cf27 - so as result I will get 2013-03-06 - 1.5 , 2013-03-05 - 3.5. I hope I didn't loose myself and overcomplicate things,

How to calculate median of a numeric sequence in Google BigQuery efficiently?

送分小仙女□ 提交于 2019-12-05 05:35:10
I need to calculate median value of a numeric sequence in Google BigQuery efficiently. Is the same possible? Yeah it's possible with PERCENTILE_CONT window function. Returns values that are based upon linear interpolation between the values of the group, after ordering them per the ORDER BY clause. must be between 0 and 1. This window function requires ORDER BY in the OVER clause. So an example query would be like (the max() is there just to work across the group by but it's not being used as a math logic, should not confuse you) SELECT room, max(median) FROM (SELECT room, percentile_cont(0.5)

interviewstreet median challenge

女生的网名这么多〃 提交于 2019-12-05 04:49:25
Problem The median of M numbers is defined as the 1) if M is odd middle number after sorting them in order 2) if M is even the average number of the middle 2 numbers (again after sorting) You have an empty number list at first. Then you can add or remove some number from the list. For each add or remove operation, output the median of numbers in the list. Example : For a set of m = 5 numbers, { 9, 2, 8, 4, 1 } the median is the third number in sorted set { 1, 2, 4, 8, 9 } which is 4. Similarly for set of m = 4, { 5, 2, 10, 4 }, the median is the average of second and the third element in the

SQL ranking query to compute ranks and median in sub groups

时光毁灭记忆、已成空白 提交于 2019-12-05 03:03:52
I want to compute the Median of y in sub groups of this simple xy_table : x | y --groups--> gid | x | y --medians--> gid | x | y ------- ------------- ------------- 0.1 | 4 0.0 | 0.1 | 4 0.0 | 0.1 | 4 0.2 | 3 0.0 | 0.2 | 3 | | 0.7 | 5 1.0 | 0.7 | 5 1.0 | 0.7 | 5 1.5 | 1 2.0 | 1.5 | 1 | | 1.9 | 6 2.0 | 1.9 | 6 | | 2.1 | 5 2.0 | 2.1 | 5 2.0 | 2.1 | 5 2.7 | 1 3.0 | 2.7 | 1 3.0 | 2.7 | 1 In this example every x is unique and the table is already sorted by x . I now want to GROUP BY round(x) and get the tuple that holds the median of y in each group. I can already compute the median for the whole

Find the median of an unsorted array without sorting [duplicate]

不问归期 提交于 2019-12-05 02:06:32
问题 This question already has answers here : O(n) algorithm to find the median of a collection of numbers (3 answers) Closed 4 years ago . is there a way to find the Median of an unsorted array: 1- without sorting it. 2- without using the select algorithm, nor the median of medians I found a lot of other questions similar to mine. But the solutions, most of them, if not all of them, discussed the SelectProblem and the MedianOfMedians 回答1: You can certainly find the median of an array without

Calculating median - javascript

£可爱£侵袭症+ 提交于 2019-12-04 23:13:12
I've been trying to calculate median but still I've got some mathematical issues I guess as I couldn't get the correct median value and couldn't figure out why. Here's the code; class StatsCollector { constructor() { this.inputNumber = 0; this.average = 0; this.timeout = 19000; this.frequencies = new Map(); for (let i of Array(this.timeout).keys()) { this.frequencies.set(i, 0); } } pushValue(responseTimeMs) { let req = responseTimeMs; if (req > this.timeout) { req = this.timeout; } this.average = (this.average * this.inputNumber + req) / (this.inputNumber + 1); console.log(responseTimeMs /

Calculating Median in Ruby

牧云@^-^@ 提交于 2019-12-04 09:50:28
问题 How do I calculate the median of an array of numbers using Ruby? I am a beginner and within the progress of my learning I am trying to stick to what has already been taught. Thus the other questions that I've found are beyond my scope. Here are my notes and my attempt: sort the array in ascending order. figure out if it is odd or even in length. if odd, divide the sorted array length +1 in half. That is the index of the median. Return this value. if even, find the middle two numbers of the

Optimal 9-element sorting network that reduces to an optimal median-of-9 network?

非 Y 不嫁゛ 提交于 2019-12-04 08:42:24
问题 I am looking into sorting and median-selection networks for nine elements based exclusively on two-input minimum / maximum operations. Knuth, TAOCP Vol. 3 , 2nd ed. states (page 226) that a nine-element sorting network requires at least 25 comparisons, which translates into an equal number of SWAP() primitives or 50 min / max operations. Obviously a sorting network can be converted into a median-selection network by eliminating redundant operations. The conventional wisdom seems to be that