2
$\begingroup$

Classify doesn't work when the targets are lists, so you need to encode the labels. However, there must be a nicer built-in way to do label encoding than this, but I can't find it:

n = 4;
t = Table[
   r = RandomInteger[1, {n}];
   d = Thread[
     Range[n] -> Map[FromCharacterCode, Range[97, 97 + n - 1]]];
   {r, (Flatten@Position[r, 1] /. d)}, {100}];
ut = Union[t[[All, 2]]];
en = Thread[ut -> Range[Length[ut]]];
de = Reverse /@ en;
x = t[[All, 1]];
y = t[[All, 2]] /. en;
c = Classify[x -> y];

Example:

In[2]:= c[{1, 0, 0, 1}] /. de
Out[2]= {"a", "d"}
$\endgroup$
2
$\begingroup$

Since Classify accepts associations, I would use their functionality to encode the classes. First, group your data by class

at = KeySort@GroupBy[t, Last -> First];

then set up the encoding and the reverse lookup for later

ena = Association@MapIndexed[#1 -> #2[[1]]&, Keys@at]
dea = AssociationMap[Reverse]@ena

The actual encoding is then straightforward

endata = KeyMap[ena]@at;
c = Classify@endata

Unfortunately, I do not know of a built-in way to do the reverse lookup, although your code works as is with associations, e.g.

In[211]:= c[{1, 0, 0, 1}] /. dea
Out[211]= {"a", "d"}
$\endgroup$

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.