9
$\begingroup$

Bug introduced in 10.0 and fixed in 10.3


I'm having trouble calculating the median of a Dataset[] in Mathematica 10.

The situation is as follows. Consider a dataset that was defined as follows:

dataset = Dataset[{<|"a"->1,"b"->2|>,<|"a"->3,"b"->4|>}];

The mean and variance of columns a and b can now be calculated by

mean = dataset[Mean, {"a","b"}]
var = dataset[Variance, {"a","b"}]

That works pefectly, but

med = dataset[Median, {"a","b"}]

returns a Failure[]! Somehow, Median[] is not compatible with a list of associations as its argument and the other functions are.

Can someone explain why this happens and maybe help with a solution?

$\endgroup$
  • 1
    $\begingroup$ I found a workaround. Entering AssociationThread[Keys[#[[1]]], Median[Values /@ #]]& instead of Median helps. But I must say that I don't like this at all, there should be a better way. $\endgroup$ – Thijs Aug 13 '14 at 12:10
  • 1
    $\begingroup$ A couple of questions. What OS are you using? Also, what do you mean by "crash"? Does the kernel go down? Or, do you get a Failure[...]? On Mac OS, I get a failure. $\endgroup$ – rcollyer Aug 13 '14 at 12:11
  • $\begingroup$ You're right. It returns a Failure. I updated the question. I'm on OS X as well. $\endgroup$ – Thijs Aug 13 '14 at 12:14
  • $\begingroup$ Looks like a bug to me. Have you reported this to support@wolfram.com? $\endgroup$ – Sjoerd C. de Vries Aug 13 '14 at 15:39
  • 3
    $\begingroup$ It's not that Median isn't compatible with associations, its that the type here is recognized as a Struct instead of an Assoc. That's not wrong, per se, but it does mean that the type inference scheme, which is a vast undertaking that can never really be finished, doesn't 'know' that this should be allowed. There is a fairly straightforward fix that may make it into 10.0.1 (and will fix many similar issues), but for now, writing Median[#]& instead of Median should get around this problem. Edit: I'm wrong, Median doesn't work properly on associations of vectors. $\endgroup$ – Taliesin Beynon Aug 13 '14 at 17:58
2
$\begingroup$

Working since version 10.3:

dataset = Dataset[{<|"a" -> 1, "b" -> 2|>, <|"a" -> 3, "b" -> 4|>}];

med = dataset[Median, {"a", "b"}]

enter image description here

|improve this answer|||||
$\endgroup$
1
$\begingroup$

Median itself doesn't work on associations of vectors:

In[9]:= Median[{<|"a" -> 1, "b" -> 2|>, <|"a" -> 3, "b" -> 4|>}]
During evaluation of In[9]:= Median::rectn: Rectangular array of real numbers is expected at position 1 in Median[{<|a->1,b->2|>,<|a->3,b->4|>}]. >>
Out[9]= Median[{<|"a" -> 1, "b" -> 2|>, <|"a" -> 3, "b" -> 4|>}]
|improve this answer|||||
$\endgroup$
  • $\begingroup$ This kind of makes sense because the median is defined based on ordering of the elements. Vectors cannot be ordered, thus mathematically the median of vectors makes no sense. Median does have the convenient behaviour that for a list of vectors Median[list] gives Median /@ Transpose[list] though. $\endgroup$ – Szabolcs Jul 2 '15 at 6:19
  • $\begingroup$ @Szabolcs yes I agree. Reported as 297160. $\endgroup$ – Taliesin Beynon Jul 2 '15 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.