Template <class T1, class t2>
Double Pearson (std::vector<t1> &inst1, std::vector<t2> &inst2) {
if (inst1.size () = Inst2.size ()) {
std::cout<< "The size of the vectors is not the same\n";
return 0;
}
size_t n=inst1.size ();
Double pearson=n*inner_product (Inst1.begin (), Inst1.end (), Inst2.begin (), 0.0)-accumulate (Inst1.begin (), Inst1.end (), 0.0) *accumulate (Inst2.begin (), Inst2.end (), 0.0);
Double temp1=n*inner_product (Inst1.begin (), Inst1.end (), Inst1.begin (), 0.0)-pow (Accumulate (Inst1.begin (), Inst1.end (), 0.0), 2.0);
Double temp2=n*inner_product (Inst2.begin (), Inst2.end (), Inst2.begin (), 0.0)-pow (Accumulate (Inst2.begin (), Inst2.end (), 0.0), 2.0);
temp1=sqrt (TEMP1);
temp2=sqrt (TEMP2);
pearson=pearson/(TEMP1*TEMP2);
return Pearson;
}
Pearson correlation coefficient is the most common algorithm for similarity calculation in collaborative filtering. The Pearson correlation coefficient algorithm can be used to measure the two variables
The degree of correlation, the result is a value between 1 and 1, 1 indicates a fully positive correlation, 0 is irrelevant, and 1 indicates a complete negative correlation.
Calculation of Pearson correlation coefficients in collaborative filtering algorithm C + +