12
$\begingroup$

For neural networks mathematica likes 2 ways to structure training sets:

listFormatData = {
<|"Input" -> input1, "OutputKey1" -> OutputKey1Input1Output, ...|>,
<|"Input" -> input2, "OutputKey1" -> OutputKey1Input2Output, ...|>,
...
}

and

assocFormatData = <|
"Input" -> {input1, input2, ...}, 
"OutputKey1" -> {OutputKey1Input1Output, OutputKey1Input2Output, ...}
, ...
|>

Is the a stock way to convert these 2 formats properly?

Concrete examples:

listData = {
<|"Input" -> 1, "double" -> 2, "squared" -> 1|>,
<|"Input" -> 2, "double" -> 4, "squared" -> 4|>,
<|"Input" -> 3, "double" -> 6, "squared" -> 9|>
}

and

assocData = <|
"Input" -> {1, 2, 3}, 
"double" -> {2, 4, 6}, 
"squared" -> {1, 4, 9}
|>
$\endgroup$
  • 1
    $\begingroup$ What does Normal[Transpose[Dataset[listData]]] return for you? (Also if you replace listData with assocData?) $\endgroup$ – J. M. will be back soon Oct 19 '18 at 14:43
12
$\begingroup$

You can use the undocumented AllowedHeads option of Transpose to do this:

tr = Transpose[listData, AllowedHeads -> {Association, List}]

<|"Input" -> {1, 2, 3}, "double" -> {2, 4, 6}, "squared" -> {1, 4, 9}|>

and back:

Transpose[tr, AllowedHeads -> {Association, List}]

{<|"Input" -> 1, "double" -> 2, "squared" -> 1|>, <|"Input" -> 2, "double" -> 4, "squared" -> 4|>, <|"Input" -> 3, "double" -> 6, "squared" -> 9|>}

$\endgroup$
8
$\begingroup$
Merge[listData, Identity]

<|"Input" -> {1, 2, 3}, "double" -> {2, 4, 6}, "squared" -> {1, 4, 9}|>

listFormatData = {<|"Input" -> input1,  "OutputKey1" -> OutputKey1Input1Output|>,
   <|"Input" -> input2, "OutputKey1" -> OutputKey1Input2Output|>};
Merge[listFormatData , Identity]

<|"Input" -> {input1, input2}, "OutputKey1" -> {OutputKey1Input1Output, OutputKey1Input2Output}|>

$\endgroup$
8
$\begingroup$

A function that is of great use here, is AssociationTranspose in the GeneralUtilities package. It does the same thing as regular transpose, but works with List and Associations alike (and mixtures of the two):

listData = {
  <|"Input" -> 1, "double" -> 2, "squared" -> 1|>, 
  <|"Input" -> 2, "double" -> 4, "squared" -> 4|>,
  <|"Input" -> 3, "double" -> 6, "squared" -> 9|>
};
assocData = <|"Input" -> {1, 2, 3}, "double" -> {2, 4, 6}, "squared" -> {1, 4, 9}|>;
GeneralUtilities`AssociationTranspose[listData]  === assocData
GeneralUtilities`AssociationTranspose[assocData] === listData

Out[11]= True

Out[12]= True

$\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.