Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to convert a dataset to a list of features and a list of target values, for use in Predict and also more generally. I know Predict can accept datasets natively but there are some "features" with Predict in V10.1, that require dropping back to lists and rules. The dataset is :

ds = 
  {<|"age" -> 4, "weight" -> 10, "gender" -> "M", 
     "comment" -> "mummy says I eat like a horse", "result" -> 1.4|>,
   <|"age" -> 4, "weight" -> 8, "gender" -> "M", 
     "comment" -> "throws his porridge on the floor", "result" -> 2.3|>,
   <|"age" -> 5, "weight" -> 15, "gender" -> "F", 
     "comment" -> "loves green vegetables", "result" -> 3.5|>,
   <|"age" -> 7, "weight" -> 30, "gender" -> "F", 
    "comment" -> "Thinks chocolate is fantastic", "result" -> 5.1|>} //
  Dataset

I thought MapThread might work :

MapThread[Rule, {Values[Normal[ds[All, {"age", "weight"}]]], 
Values[Normal[ds[All, {"result"}]]]}, 2]

The end result I am Looking for is of the form

{{4, 10}  -> 1.4}, {4, 8} -> 2.3} ... }
share|improve this question

2 Answers 2

up vote 8 down vote accepted

This should do it:

ds[All, {#age, #weight, #gender} -> #result &] // Normal

{{4, 10, "M"} -> 1.4, {4, 8, "M"} -> 2.3, {5, 15, "F"} -> 3.5, {7, 30, "F"} -> 5.1}

share|improve this answer
    
You beat me to the punch but my formulation is slightly different so I posted anyway. +1 of course. –  Mr.Wizard yesterday
    
@Mr.Wizard +1, it's good to have both approaches represented. –  Pickett yesterday
    
More learnt about dataset. Thanx to both. This seems the most "datasety" answer so accepted –  Gordon Coale yesterday
{#age, #weight} -> #result & /@ ds // Normal
{{4, 10} -> 1.4, {4, 8} -> 2.3, {5, 15} -> 3.5, {7, 30} -> 5.1}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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