1
$\begingroup$

I'm trying to use Classify to predict heads in a Mathematica expression from the heads of its arguments, which may be functions whose heads are not on the current $ContextPath. With the default method "LogisticRegression", the behavior is as expected-- output does not depend on $ContextPath.

a = Test`testA;
b = Test`testB;

clf = Classify[{{a}->1, {b}->2}];

clf[{a}, "Probabilities"]
Block[{$ContextPath = {"Test`"}}, clf[{a}, "Probabilities"]]
clf[{"foo"}, "Probabilities"]

(*
<|1 -> 0.999679, 2 -> 0.000321061|>
<|1 -> 0.999679, 2 -> 0.000321061|>
<|1 -> 0.999997, 2 -> 3.21752*10^-6|>
*)

However, when I set "Method" -> "Markov", the symbol is treated as equivalent to an unknown symbol when its context is on ContextPath.

clf2 = Classify[{{a}->1, {b}->2}, "Method" -> "Markov"];

clf2[{a}, "Probabilities"]
Block[{$ContextPath = {"Test`"}}, clf2[{a}, "Probabilities"]]
clf2[{"foo"}, "Probabilities"]

(*
<|1 -> 0.916597, 2 -> 0.0834028|>
<|1 -> 0.5, 2 -> 0.5|>
<|1 -> 0.5, 2 -> 0.5|>
*)

Since SequencePredict always uses Method -> Markov, it always has the second behavior. This seems to be an issue, since $ContextPath changes when loading packages.

  • Is this a bug? (I have already contacted Wolfram Research.)
  • What's the root cause? Hash doesn't depend on $ContextPath, and overloading ToString has no effect.
  • Is there an elegant workaround other than hashing the input manually (which doesn't work well for SequencePredict)? Enclosing every use of the ClassifierFunction in Block[{$ContextPath = {"System`"}},...] doesn't work.
$\endgroup$

Your Answer

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

Browse other questions tagged or ask your own question.