# 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

## 1 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.

• Worked like a charm. Thank you. Too be honest, I still get confused about the basic building blocks for Mathematica. I do like the built-in documentation. Is there another resource for getting a better handle of this kind of issue. I feel like it always trips me up. Thanks again. – tau1777 Oct 2 '15 at 19:47
• No problem- List manipulation In the upper-right corner of this guide, there is a button called "tutorials." Spending some time with these will definitely help. – Peter Roberge Oct 2 '15 at 19:53
• Pro-tip: Flatten does not flatten sub-expressions of non-Lists, i.e. you can simply use Flatten@training and save some key-strokes. – rcollyer Oct 2 '15 at 20:07