median

How to find Median [duplicate]

让人想犯罪 __ 提交于 2019-11-27 13:47:29
问题 This question already has an answer here: Finding median of list in Python 19 answers I have data like this. Ram,500 Sam,400 Test,100 Ram,800 Sam,700 Test,300 Ram,900 Sam,800 Test,400 What is the shortest way to fine the "median" from above data. My result should be something like... Median = 1/2(n+1), where n is the number of data values in the sample. Test 500 Sam 700 Ram 800 回答1: Python 3.4 includes statistics built-in, so you can use the method statistics.median: >>> from statistics

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

你。 提交于 2019-11-27 11:19:52
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, but also feels more ugly.. Or are there more and better alternatives for me? Any random-access container

C++ Efficiently Calculating a Running Median [duplicate]

橙三吉。 提交于 2019-11-27 06:59:53
This question already has an answer here: Find running median from a stream of integers 8 answers 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 to calculate the running median , because quickselect isn't a good choice as it needs to re-calculate every time the list

Incremental median computation with max memory efficiency

无人久伴 提交于 2019-11-27 03:15:36
问题 I have a process that generates values and that I observe. When the process terminates, I want to compute the median of those values. If I had to compute the mean, I could just store the sum and the number of generated values and thus have O(1) memory requirement. How about the median? Is there a way to save on the obvious O(n) coming from storing all the values? Edit: Interested in 2 cases: 1) the stream length is known, 2) it's not. 回答1: You are going to need to store at least ceil(n/2)

Map each list value to its corresponding percentile

北战南征 提交于 2019-11-27 00:23:21
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[length / 2 - 1]) / 2.0 return mylist[length / 2] ###########################################################

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

﹥>﹥吖頭↗ 提交于 2019-11-27 00:03:02
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); All I need is the actual MedianCalculator code! Update: Some people have asked if the values I'm trying to

Finding the median of an unsorted array

天涯浪子 提交于 2019-11-26 22:02:10
To find the median of an unsorted array, we can make a min-heap in O(nlogn) time for n elements, and then we can extract one by one n/2 elements to get the median. But this approach would take O(nlogn) time. Can we do the same by some method in O(n) time? If we can, then please tell or suggest some method. dasblinkenlight You can use the Median of Medians algorithm to find median of an unsorted array in linear time. I have already upvoted the @dasblinkenlight answer since the Median of Medians algorithm in fact solves this problem in O(n) time. I only want to add that this problem could be

O(n) algorithm to find the median of a collection of numbers

空扰寡人 提交于 2019-11-26 20:20:26
Problem: input is a (not necessarily sorted) sequence S = k1, k2, ..., kn of n arbitrary numbers. Consider the collection C of n² numbers of the form min{ki,kj}, for 1 <=i, j<=n. Present an O(n) time and O(n) space algorithm to find the median of C. So far I've found by examining C for different sets S that the number of instances of the smallest number in S in C is equal to (2n-1), the next smallest number: (2n-3) and so on until you only have one instance of the largest number. Is there a way to use this information to find the median of C? There are a number of possibilities. One I like is

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

隐身守侯 提交于 2019-11-26 19:15:12
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 basic formulas for calculating any of these is grade-school arithmetic, and I do know them. There are many

Fastest way of finding the middle value of a triple?

元气小坏坏 提交于 2019-11-26 19:02:28
问题 Given is an array of three numeric values and I'd like to know the middle value of the three. The question is, what is the fastest way of finding the middle of the three ? My approach is this kind of pattern - as there are three numbers there are six permutations: if (array[randomIndexA] >= array[randomIndexB] && array[randomIndexB] >= array[randomIndexC]) It would be really nice, if someone could help me out finding a more elegant and faster way of doing this. 回答1: If you are looking for the