dongyu4908 2013-01-02 17:12
浏览 359

如何计算php中两个不同长度数组的相似度百分比

I have retrieved records of two users from a database in two arrays respectively. The records are of people who follow other people just like in twitter. Since two users may be following different number of people. So the length of the two arrays is different. i have created a new array that stores the common people ( people followed by both users). How can I get % similarity of the two users. Lets say if two users have 5 common followers they have more similarity than 2 users having 2 in common.

foreach($common as $row){
//do events
echo $row['name']."<br>";
$count_common++;
}
echo "total common ".$count_common;
$similarity = (count($common)/(count($user1_follows))*100);

The above formula does not calculate correct result since it is based for arrays having same length. Here is a related question to mine get the percentage of similarity of two arrays in php

  • 写回答

1条回答 默认 最新

  • doushi9856 2013-01-02 17:14
    关注
    $p1 = array("foo", "bar", "grep");
    $p2 = array("foo", "buzz", "fizz", "bar");
    $similar = array_intersect($p1, $p2);
    $p1_perc = count($similar) / count($p1); // 0.66..
    $p2_perc = count($similar) / count($p2); // 0.5
    

    To get one number:

    $perc = 2 * count($similar) / (count($p1) + count($p2)); // 0.5714..
    
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办