Skip to main content

list manipulation - Turning table into association for Classify


I have the following sample data for 20 users. I know their sex, and the response for three yes/no questions. I am trying to building a classifier to guess their response to the third question, based on their sex, and the response to the first two questions. It is probably obvious but this is all made up data.


Here is the data:


{{{"M", "Y", "N"} -> "Y"}, {{"F", "Y", "N"} -> 
"N"}, {{"F", "N", "Y"} -> "Y"}, {{"M", "Y", "Y"} ->
"Y"}, {{"M", "Y", "Y"} -> "Y"}, {{"M", "N", "Y"} ->

"Y"}, {{"M", "Y", "Y"} -> "Y"}, {{"M", "N", "Y"} ->
"Y"}, {{"M", "Y", "N"} -> "Y"}, {{"F", "Y", "Y"} ->
"N"}, {{"F", "Y", "Y"} -> "N"}, {{"M", "Y", "N"} ->
"Y"}, {{"F", "Y", "N"} -> "N"}, {{"F", "Y", "N"} ->
"N"}, {{"M", "Y", "N"} -> "Y"}, {{"M", "Y", "N"} ->
"Y"}, {{"M", "Y", "Y"} -> "Y"}, {{"F", "Y", "Y"} ->
"N"}, {{"F", "N", "Y"} -> "N"}}

Then I make a training set from the first 10 elements of the data.


training = data[[1 ;; 10]]


and finally I try to use Classify using built in defaults


cf = Classify[training]

Which gives:


Argument {{{M,Y,N}->Y},{{F,Y,N}->N},{{F,N,Y}->Y},<<4>>,{{M,N,Y}->Y},{{M,Y,N}->Y},{{F,Y,Y}->N}} should be a rule, a list of rules, or an association. >>

I went through Iris data as practice, but since they uses built-in dataset, I'm not sure how the Associations were build behind the scenes.


I would like to understand:


1) How to convert my table, into associations? \ 2) In general, for Classifying is it better to use Mathematica's new Dataset structure?



This question does seems related to : Transform Dataset so it can be used as training set for Classify



Answer



You must change your list structure. right now it is in the form of a list of lists of associations. If you remove the lists, you will have a list of associations, which the classifier function can work with.


cf = Classify[Flatten[training,1]]

I would look at the documentation for AssociationMap and AssociationThread, for good examples of how to build associations.


Comments