5
$\begingroup$

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} ... }
$\endgroup$
8
$\begingroup$

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}

$\endgroup$
  • $\begingroup$ You beat me to the punch but my formulation is slightly different so I posted anyway. +1 of course. $\endgroup$ – Mr.Wizard Apr 14 '15 at 16:47
  • $\begingroup$ @Mr.Wizard +1, it's good to have both approaches represented. $\endgroup$ – C. E. Apr 14 '15 at 17:00
  • $\begingroup$ More learnt about dataset. Thanx to both. This seems the most "datasety" answer so accepted $\endgroup$ – Gordon Coale Apr 14 '15 at 20:09
4
$\begingroup$
{#age, #weight} -> #result & /@ ds // Normal
{{4, 10} -> 1.4, {4, 8} -> 2.3, {5, 15} -> 3.5, {7, 30} -> 5.1}
$\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.