0

I am trying to create a program that will rate a bunch of arrays to find the one that most closely matches a given array. So say the given array is

[1, 80, 120, 155, 281, 301]

And one of the array to compare against is

[-6, 78, 108, 121, 157, 182, 218, 256, 310, 408, 410]

How can I match up the values in the first array to their closes values in the second array that will give it the lowest total difference. In case this is unclear

1 => -6, 80 => 78, 120 => 121, 155 => 157

Than 281 should match up to 310 since it is closer than 256 however this would force 301 to match to 256. So the best overall match would be

281=>256 and 301=> 310

Then the program would simply calculate a rating by doing

abs(-6 - 1) + abs(78-80) etc for all matches. And the array with the lowest rating is the best match

*******NOTE*******

The given array will be the same size or smaller than the matching array and will only have positive values. The matching array can have negative values. I was thinking of using cosine similarity but I am unsure how to implement that for this problem.

James Notaro
  • 137
  • 1
  • 3
  • 10
  • You can compute the distance between arrays to determine the similarity. Is there noise in the data? Is the data linear? – Darius May 21 '16 at 15:41
  • There are no values that should be ignored (noise), and the arrays will be sorted to be linear. How do you calculate the distance? – James Notaro May 21 '16 at 15:45

1 Answers1

0

In general a computed distance is more accurate. There are different approaches with advantages and disadvantages. In your example you compute the sum of one dimension euclidean distances. But there are more extended comparisons like the dynamic time warping. It's an algorithm, which finds the best alignment between two 'arrays' and computes the optimal distance.

You can install and use this package. Here you can see a visual example. One other advantage of the DTW is, that the length of the arrays don't have to be matched.

Darius
  • 10,762
  • 2
  • 29
  • 50
  • I am trying to write this code in ruby is there a ruby gem that gives DTW calculations – James Notaro May 21 '16 at 16:14
  • Sorry, I didn't recognize the programming language. (In detail you use the word `array` instead of `list`.) Anyway I found this rubygem: https://rubygems.org/gems/dtw – Darius May 21 '16 at 16:18