I am working on a little machine learning theoretical algorithm using nodeJs. My goal is to compare many array patterns with a single source pattern , and then return them as a percentage. For example, pattern1 could be 80%, similar to the source pattern.
What could be the best method for determining percent similarity for one array to another?
What have I done so far ..
var soureSequence = [0.53,0.55,0.50,0.40,0.50,0.52,0.58,0.60]
var sequence1 = [0.53,0.54,0.49,0.40,0.50,0.52,0.58,0.60]
var sequence2 = [0.53,0.55,0.50,0.42,0.50,0.53,0.57,0.62]
Since I chose the percentage result, I decided that I should base the original template from the percentage change from the first value to the second value in the array.
var percentChange = (firstVal, secondVal) => {
var pChange = ((parseFloat(secondVal) - firstVal) /
Math.abs(firstVal)) * 100.00;
if(!pChange || pChange == 0){
return 0.00000001
}
return pChange;
}
Here I will generate the source template from the source sequence
var storePattern = function(sequence){
var pattern = [];
for(var i = 0 ; i < sequence.length ; i++){
let $change = percentChange(sequence[i] , sequence[i + 1]);
if(i != sequence.length && $change ){
pattern.push($change)
}
}
return pattern;
}
var sourcePattern = storePattern(soureSequence);
Now I will create more models for comparison
var testPattern1 = storePattern(sequence1);
var testPattern2 = storePattern(sequence2);
Below is my comparison function
var processPattern = function(source , target){
var simularityArray = [];
for(var i = 0 ; i < target.length ; i++){
let change = Math.abs(percentChange(target[i] , source[i]));
simularityArray.push(100.00 - change);
}
var rating = simularityArray.reduce((a,b) => {
return a + b
});
rating = rating / parseFloat(source.length + ".00");
return rating;
}
Now I can try to appreciate the similarities
var similarityOfTest1 = processPattern(sourcePattern , testPattern1)
, .. 0.50, 0.52.. 0,20, 0,22, , .. → 0,02
, .
. !