ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 25 Jul 2015 15:06:39 +0200Find median of [2, 5, 2, 6, 7, 3, 7, 5, 7, 0, 6, 10]https://ask.sagemath.org/question/27308/find-median-of-2-5-2-6-7-3-7-5-7-0-6-10/ Ok, I'm encountering this weird result when performing:
l = [2, 5, 2, 6, 7, 3, 7, 5, 7, 0, 6, 10]
r = median(l)
The r becomes 11/2 which is 5.5. The median here is clearly an integer and it is 5!
Why does it return 5.5?Fri, 10 Jul 2015 20:56:30 +0200https://ask.sagemath.org/question/27308/find-median-of-2-5-2-6-7-3-7-5-7-0-6-10/Comment by fidbc for <p>Ok, I'm encountering this weird result when performing:</p>
<pre><code>l = [2, 5, 2, 6, 7, 3, 7, 5, 7, 0, 6, 10]
r = median(l)
</code></pre>
<p>The r becomes 11/2 which is 5.5. The median here is clearly an integer and it is 5!</p>
<p>Why does it return 5.5?</p>
https://ask.sagemath.org/question/27308/find-median-of-2-5-2-6-7-3-7-5-7-0-6-10/?comment=27311#post-id-27311The list has length 12 (even). I think what sage is returning is the average of elements at positions 6 and 7, which are 5 and 6.Fri, 10 Jul 2015 21:26:28 +0200https://ask.sagemath.org/question/27308/find-median-of-2-5-2-6-7-3-7-5-7-0-6-10/?comment=27311#post-id-27311Answer by kcrisman for <p>Ok, I'm encountering this weird result when performing:</p>
<pre><code>l = [2, 5, 2, 6, 7, 3, 7, 5, 7, 0, 6, 10]
r = median(l)
</code></pre>
<p>The r becomes 11/2 which is 5.5. The median here is clearly an integer and it is 5!</p>
<p>Why does it return 5.5?</p>
https://ask.sagemath.org/question/27308/find-median-of-2-5-2-6-7-3-7-5-7-0-6-10/?answer=27315#post-id-27315 if hasattr(v, 'median'): return v.median()
if len(v) == 0:
# Median of empty set defined as NaN
return NaN
values = sorted(v)
if len(values) % 2 == 1:
return values[((len(values))+1)/2-1]
else:
lower = values[(len(values)+1)/2-1]
upper = values[len(values)/2]
return (lower + upper)/ZZ(2)
Sat, 11 Jul 2015 00:02:33 +0200https://ask.sagemath.org/question/27308/find-median-of-2-5-2-6-7-3-7-5-7-0-6-10/?answer=27315#post-id-27315Comment by rws for <pre><code>if hasattr(v, 'median'): return v.median()
if len(v) == 0:
# Median of empty set defined as NaN
return NaN
values = sorted(v)
if len(values) % 2 == 1:
return values[((len(values))+1)/2-1]
else:
lower = values[(len(values)+1)/2-1]
upper = values[len(values)/2]
return (lower + upper)/ZZ(2)
</code></pre>
https://ask.sagemath.org/question/27308/find-median-of-2-5-2-6-7-3-7-5-7-0-6-10/?comment=27316#post-id-27316For completeness: "If there is an even number of observations, then there is no single middle value; the median is then usually defined to be the mean of the two middle values..." see https://en.wikipedia.org/wiki/MedianSat, 11 Jul 2015 08:55:41 +0200https://ask.sagemath.org/question/27308/find-median-of-2-5-2-6-7-3-7-5-7-0-6-10/?comment=27316#post-id-27316Answer by Mellie for <p>Ok, I'm encountering this weird result when performing:</p>
<pre><code>l = [2, 5, 2, 6, 7, 3, 7, 5, 7, 0, 6, 10]
r = median(l)
</code></pre>
<p>The r becomes 11/2 which is 5.5. The median here is clearly an integer and it is 5!</p>
<p>Why does it return 5.5?</p>
https://ask.sagemath.org/question/27308/find-median-of-2-5-2-6-7-3-7-5-7-0-6-10/?answer=28690#post-id-28690 How do you guys read the answer? I don't understand this. I am new to this website.Thu, 23 Jul 2015 02:33:33 +0200https://ask.sagemath.org/question/27308/find-median-of-2-5-2-6-7-3-7-5-7-0-6-10/?answer=28690#post-id-28690Comment by kcrisman for <p>How do you guys read the answer? I don't understand this. I am new to this website.</p>
https://ask.sagemath.org/question/27308/find-median-of-2-5-2-6-7-3-7-5-7-0-6-10/?comment=28700#post-id-28700See the comment under the answer - I posted the code itself but the comment is probably easier to get!Sat, 25 Jul 2015 15:06:39 +0200https://ask.sagemath.org/question/27308/find-median-of-2-5-2-6-7-3-7-5-7-0-6-10/?comment=28700#post-id-28700