Message Boards Message Boards

0
|
4662 Views
|
7 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Compare all sublists of two different lists using PartitionMap?

Posted 5 years ago

I have an list of values and I would like to cut it into into sublists of 5 elements and compare each sublist with each sublist of another list which will also be 5 elements in length using CanonicalWarpingDistance. I have been successful in using "Partition" to cut the lists into sublists and I have also been successful in using CanonicalWarpingDistance to compare two single sublists. However, I cannot seem to combine the two to output an array of distances between every sublist in the first list and every sublist in the second list. This is what I have tried:

seq = {52.000, 52.289, 39.000, 
   0.000, -10.121, -19.000, -22.000, -28.203, -18.000, -6.000, -5.615,
    0.000, 0.000, 1.770, 2.000, 4.000, 3.903, 
   2.000, -4.000, -3.729, -3.000, 4.000, 20.378, 20.000, 47.000, 
   74.958, 58.000, 54.000, 68.563, 40.000, 17.000, 16.861, 
   2.000, -4.000, -11.492, -11.000, -20.000, -21.798, -14.000, 6.000, 
   7.932, 10.000, -1.000, -4.688, -7.000, -7.000, -10.321, -7.000, \
-7.000, -10.321, -7.000, -7.000, -10.321, -7.000, -7.000, -10.321, \
-7.000, -7.000, -10.321, -7.000};
seq2 = {52.000, 52.289, 39.000, 
    0.000, -10.121, -19.000, -22.000, -28.203, -18.000, -6.000, \
-5.615, 0.000, 0.000, 1.770, 2.000, 4.000, 3.903, 
    2.000, -4.000, -3.729, -3.000, 4.000, 20.378, 20.000, 47.000, 
    74.958, 58.000, 54.000, 68.563, 40.000, 17.000, 16.861, 
    2.000, -4.000, -11.492, -11.000, -20.000, -21.798, -14.000, 6.000,
     7.932, 10.000, -1.000, -4.688, -7.000, -7.000, -10.321, -7.000, \
-7.000, -10.321, -7.000, -7.000, -10.321, -7.000, -7.000, -10.321, \
-7.000, -7.000, -10.321, -7.000};
dist = CanonicalWarpingDistance[Partition[seq, 5], Partition[seq2, 5],
   Automatic, {"SlantedBand", 950}, 
  Method -> {"MatchingInterval" -> "Flexible"}, 
  DistanceFunction -> EuclideanDistance]

This does run but it does not give me an array of distances. Instead it gives me a huge number which is probably the sum of all of the distances. How can I get the output in an array showing distances between each sublist of "seq" and each sublist of "seq2?"

POSTED BY: Jamie Dixson
7 Replies
Posted 5 years ago

This works well for the example data but my real data is of variable length so the number of columns and rows of the table will also be variable so I will need for the table to expand and contract dynamically based on the input. Thanks you for your help, this will get me started.

POSTED BY: Jamie Dixson
Posted 5 years ago

Hi Jamie,

Here is a start. The grouping and header numbers are hardcoded but that is relatively easy to fix.

columnHeader = Table["seq1(" <> ToString[x] <> "-" <> ToString[x + 9] <> ")", {x, 1, 21, 10}];
rowHeader = Table["seq2(" <> ToString[x] <> "-" <> ToString[x + 9] <> ")", {x, 1, 21, 5}];
rowHeader = Join[{""}, rowHeader];
grid = MapThread[Prepend, {Prepend[ArrayReshape[outtable, {5, 3}], columnHeader], rowHeader}];
Grid[grid, ItemStyle -> {{1 -> Bold}, {1 -> Bold}}, Background -> {{LightYellow, None}, {LightYellow, None}}, Frame -> All]

enter image description here

POSTED BY: Rohit Namjoshi
Posted 5 years ago

Thank you both for your help thus far. Below is an example of what I am looking for as far as final output. The code that I have that produces these values albeit in one list is:

seq1 = {52.000, 52.289, 39.000, 
   0.000, -10.121, -19.000, -22.000, -28.203, -18.000, -6.000, -5.615,
    0.000, 0.000, 1.770, 2.000, 4.000, 3.903, 
   2.000, -4.000, -3.729, -3.000, 4.000, 20.378, 20.000, 47.000, 
   74.958, 58.000, 54.000, 68.563, 40.000};
seq2 = {52.000, 52.289, 39.000, -12.000, -1.792, -6.000, 49.000, 
   69.931, 59.000, 24.000, 27.989, 7.000, 7.000, 21.857, 18.000, 
   41.000, 55.666, 41.000, 18.000, 33.127, 18.000, 41.000, 57.813, 
   43.000, 25.000, 28.099, 11.000, 2.000, -2.715, -6.000};
dist1 = Tuples[{Partition[seq1, 10, 10], Partition[seq2, 10, 5]}];
outtable = 
 Table [CanonicalWarpingDistance[dist1[[i, 1]], dist1[[i, 2]], 
   Automatic, {"SlantedBand", 950}, 
   Method -> {"MatchingInterval" -> "Flexible"}, 
   DistanceFunction -> CosineDistance], {i, 1, Length[dist1]}]

The output is currently:

{2936.43, 2010.95, 805.145, 1277.23, 646.763, 40.0572, 11.9107, \
28.5548, 52.7854, 14.1702, 1305.69, 1775.71, 412.422, 773.669, \
726.661}

but I need this: Example Output

POSTED BY: Jamie Dixson
Posted 5 years ago

Hi Jamie,

I'm not sure what you mean by in x, y form perhaps if you were to give an example of how you need to see the output I or some one else might be able to help.

POSTED BY: Paul Cleary
Posted 5 years ago

Jamie,

Not sure what you mean by x, y form. You can Partition by 2 to generate sublists of pairs. If you want a mapping from sublist pairs to distance you can try this.

distances = Table[{dist1[[i, 1]], dist1[[i, 2]]} -> 
  CanonicalWarpingDistance[dist1[[i, 1]], dist1[[i, 2]], 
   Automatic, {"SlantedBand", 950}, 
   Method -> {"MatchingInterval" -> "Flexible"}, 
   DistanceFunction -> EuclideanDistance], {i, 1, Length[dist1]}]

The first few elements of the result

Column[Take[distances, 4]]

{
 {{{52., 52.289, 39., 0., -10.121}, {52., 52.289, 39., 0., -10.121}} -> 0.},
 {{{52., 52.289, 39., 0., -10.121}, {-19., -22., -28.203, -18., -6.}} -> 87.0858},
 {{{52., 52.289, 39., 0., -10.121}, {-5.615, 0., 0., 1.77, 2.}} -> 13.7236},
 {{{52., 52.289, 39., 0., -10.121}, {4., 3.903, 2., -4., -3.729}} -> 0.0414335}
}
POSTED BY: Rohit Namjoshi
Posted 5 years ago

This works great Paul! Thank you. The only other thing that I need to incorporate is that I need the output in x,y form rather than one long list. It looks like I might be able to use "Array" in place of table to make that happen. Do you have any advice for that?

POSTED BY: Jamie Dixson
Posted 5 years ago

I have made a few changes. dist1 are the tuples of your sub list of 5 elements. Have a look and see if this is what you are looking for.

seq = {52.000, 52.289, 39.000, 
   0.000, -10.121, -19.000, -22.000, -28.203, -18.000, -6.000, -5.615,
    0.000, 0.000, 1.770, 2.000, 4.000, 3.903, 
   2.000, -4.000, -3.729, -3.000, 4.000, 20.378, 20.000, 47.000, 
   74.958, 58.000, 54.000, 68.563, 40.000, 17.000, 16.861, 
   2.000, -4.000, -11.492, -11.000, -20.000, -21.798, -14.000, 6.000, 
   7.932, 10.000, -1.000, -4.688, -7.000, -7.000, -10.321, -7.000, \
-7.000, -10.321, -7.000, -7.000, -10.321, -7.000, -7.000, -10.321, \
-7.000, -7.000, -10.321, -7.000};
seq2 = {52.000, 52.289, 39.000, 
  0.000, -10.121, -19.000, -22.000, -28.203, -18.000, -6.000, -5.615, 
  0.000, 0.000, 1.770, 2.000, 4.000, 3.903, 
  2.000, -4.000, -3.729, -3.000, 4.000, 20.378, 20.000, 47.000, 
  74.958, 58.000, 54.000, 68.563, 40.000, 17.000, 16.861, 
  2.000, -4.000, -11.492, -11.000, -20.000, -21.798, -14.000, 6.000, 
  7.932, 10.000, -1.000, -4.688, -7.000, -7.000, -10.321, -7.000, \
-7.000, -10.321, -7.000, -7.000, -10.321, -7.000, -7.000, -10.321, \
-7.000, -7.000, -10.321, -7.000}; dist1 = 
 Tuples[{Partition[seq, 5], Partition[seq2, 5]}];
Table[CanonicalWarpingDistance[dist1[[i, 1]], dist1[[i, 2]], 
  Automatic, {"SlantedBand", 950}, 
  Method -> {"MatchingInterval" -> "Flexible"}, 
  DistanceFunction -> EuclideanDistance], {i, 1, Length[dist1]}]
POSTED BY: Paul Cleary
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract