median

How do I find the median of numbers in linear time using heaps?

巧了我就是萌 提交于 2019-11-26 18:47:39
问题 Wikipedia says: Selection algorithms: Finding the min, max, both the min and max, median , or even the k-th largest element can be done in linear time using heaps. All it says is that it can be done, and not how. Can you give me some start on how this can be done using heaps? 回答1: You would use a min-max-median heap to find the min, max and median in constant time (and take linear time to build the heap). You can use order-statistics trees to find the kth smallest/largest value. Both of these

how to calculate mean/median per group in a dataframe in r [duplicate]

社会主义新天地 提交于 2019-11-26 17:48:21
This question already has an answer here: Mean per group in a data.frame [duplicate] 8 answers I have a dataframe recording how much money a costomer spend in detail like the following: custid, value 1, 1 1, 3 1, 2 1, 5 1, 4 1, 1 2, 1 2, 10 3, 1 3, 2 3, 5 How to calcuate the charicteristics using mean,max,median,std, etc like the following? Use some apply function? And how? custid, mean, max,min,median,std 1, .... 2,.... 3,.... To add to the alternatives, here's summaryBy from the "doBy" package, with which you can specify a list of functions to apply. library(doBy) summaryBy(value ~ custid,

Median of Medians in Java

大兔子大兔子 提交于 2019-11-26 16:38:53
问题 I am trying to implement Median of Medians in Java for a method like this: Select(Comparable[] list, int pos, int colSize, int colMed) list is a list of values of which to find a specified position pos is the specified position colSize is the size of the columns that I create in the first stage colMed is the position in those columns that I use as the medX I am not sure which sorting algorithm would be the best to use or how to implement this exactly.. 回答1: I don't know if you still need this

What is the right approach when using STL container for median calculation?

吃可爱长大的小学妹 提交于 2019-11-26 15:31:39
问题 Let's say I need to retrieve the median from a sequence of 1000000 random numeric values. If using anything but std::list , I have no (built-in) way to sort sequence for median calculation. If using std::list , I can't randomly access values to retrieve middle (median) of sorted sequence. Is it better to implement sorting myself and go with e.g. std::vector , or is it better to use std::list and use std::list::iterator to for-loop-walk to the median value? The latter seems less overheadish,

C++ Efficiently Calculating a Running Median [duplicate]

雨燕双飞 提交于 2019-11-26 12:40:06
问题 This question already has answers here : Find running median from a stream of integers (8 answers) Closed last year . Those of you that have read my previous questions know about my work at understanding and implementing quicksort and quickselect, as well as some other basic algorithms. Quickselect is used to calculate the kth smallest element in an unsorted list, and this concept can also be used to find the median in an unsorted list. This time, I need aid in devising an efficient technique

Map each list value to its corresponding percentile

混江龙づ霸主 提交于 2019-11-26 12:22:02
问题 I\'d like to create a function that takes a (sorted) list as its argument and outputs a list containing each element\'s corresponding percentile. For example, fn([1,2,3,4,17]) returns [0.0, 0.25, 0.50, 0.75, 1.00] . Can anyone please either: Help me correct my code below? OR Offer a better alternative than my code for mapping values in a list to their corresponding percentiles? My current code: def median(mylist): length = len(mylist) if not length % 2: return (mylist[length / 2] + mylist

Finding median of list in Python

╄→гoц情女王★ 提交于 2019-11-26 11:14:01
How do you find the median of a list in Python? The list can be of any size and the numbers are not guaranteed to be in any particular order. If the list contains an even number of elements, the function should return the average of the middle two. Here are some examples (sorted for display purposes): median([1]) == 1 median([1, 1]) == 1 median([1, 1, 2, 4]) == 1.5 median([0, 2, 5, 6, 8, 9, 9]) == 6 median([0, 0, 0, 0, 4, 4, 6, 8]) == 2 Python 3.4 has statistics.median : Return the median (middle value) of numeric data. When the number of data points is odd, return the middle data point. When

Calculate median in c#

无人久伴 提交于 2019-11-26 10:34:53
问题 I need to write function that will accept array of decimals and it will find the median. Is there a function in the .net Math library? 回答1: Is there a function in the .net Math library? No. It's not hard to write your own though. The naive algorithm sorts the array and picks the middle (or the average of the two middle) elements. However, this algorithm is O(n log n) while its possible to solve this problem in O(n) time. You want to look at selection algorithms to get such an algorithm. 回答2:

How to calculate or approximate the median of a list without storing the list

半城伤御伤魂 提交于 2019-11-26 09:17:39
问题 I\'m trying to calculate the median of a set of values, but I don\'t want to store all the values as that could blow memory requirements. Is there a way of calculating or approximating the median without storing and sorting all the individual values? Ideally I would like to write my code a bit like the following var medianCalculator = new MedianCalculator(); foreach (var value in SourceData) { medianCalculator.Add(value); } Console.WriteLine(\"The median is: {0}\", medianCalculator.Median);

“On-line” (iterator) algorithms for estimating statistical median, mode, skewness, kurtosis?

余生颓废 提交于 2019-11-26 08:57:37
问题 Is there an algorithm to estimate the median, mode, skewness, and/or kurtosis of set of values, but that does NOT require storing all the values in memory at once? I\'d like to calculate the basic statistics: mean: arithmetic average variance: average of squared deviations from the mean standard deviation: square root of the variance median: value that separates larger half of the numbers from the smaller half mode: most frequent value found in the set skewness: tl; dr kurtosis: tl; dr The