Friend recommendations for social networks
Not long ago I wrote how you can recommend a product in online stores or other places using user information. Now I want to show an algorithm that allows you to recommend friends, for example on social networks.
The first step is to present the user information in the interval scale and recommend to the user friends using the Pearson correlation coefficient , which will measure the degree of linear relationship between two interval variables. For example, we have 4 users: Dima, Anna, Petya and Sasha. We know about them the information that we present in the form of numbers in an array (interests, blogs, age, etc.). The Pearson coefficient looks like this: where xi and yi are the compared quantitative attributes

n is the number of observations to be compared.
In the second step, we insert the value in the formula and see which users are suitable for Dima: The more the value is closer to 1, the more likely it is that their interests coincide with his interests:
The first step is to present the user information in the interval scale and recommend to the user friends using the Pearson correlation coefficient , which will measure the degree of linear relationship between two interval variables. For example, we have 4 users: Dima, Anna, Petya and Sasha. We know about them the information that we present in the form of numbers in an array (interests, blogs, age, etc.). The Pearson coefficient looks like this: where xi and yi are the compared quantitative attributes
//Dima
$name[0]=array(1,4,5,5,4);
//Anna
$name[1]=array(3,5,6,5,4);
//Petr
$name[2]=array(3,2,4,2,1);
//Sasha
$name[3]=array(5,4,3,1,1);

n is the number of observations to be compared.
In the second step, we insert the value in the formula and see which users are suitable for Dima: The more the value is closer to 1, the more likely it is that their interests coincide with his interests:
$n=5;
$x=$name[0];
$y=$name[1];
for ($j=1;$j
$x=$name[0];
$y=$name[$j];
//begin
$s_x=0;
$s_y=0;
$s_pow_x=0;
$s_pow_y=0;
$s_x_y=0;
//x*y
for ($i=0;$i<5;$xy[$i]=$x[$i]*$y[$i],$i++);
//x pow 2
for ($i=0;$i<5;$x_pow_2[$i]=$x[$i]*$x[$i],$i++);
//y pow 2
for ($i=0;$i<5;$y_pow_2[$i]=$y[$i]*$y[$i],$i++);
//Summa xi
for ($i=0;$i<5;$s_x+=$x[$i],$i++);
//Summa yi
for ($i=0;$i<5;$s_y+=$y[$i],$i++);
//Summa x pow 2
for ($i=0;$i<5;$s_pow_x+=$x_pow_2[$i],$i++);
//Summa y pow 2
for ($i=0;$i<5;$s_pow_y+=$y_pow_2[$i],$i++);
//Summa x*y
for ($i=0;$i<5;$s_x_y+=$xy[$i],$i++);
$r_x_y=(($n*$s_x_y)-($s_x*$s_y))/sqrt((($n*$s_pow_x) - ($s_x*$s_x))*(($n*$s_pow_y) - ($s_y*$s_y)));
print $r_x_y;
}
| Name | He is our friend | Output value |
| Anya | (80%) | 0.880704845928 |
| Petya | (0%) | -0.0800640769025 |
| Sasha | (0%) | -0.697424162876 |