- PHP Jaccard 유사성 지수 구현
- PHP 자카드 유사도
1 함수[ | ]
PHP
Copy
function jaccard_index($set1, $set2) {
$union_cnt = count(array_unique(array_merge($set1, $set2)));
if($union_cnt < 1) return 1;
$intersect_cnt = count(array_intersect($set1, $set2));
return $intersect_cnt / $union_cnt;
}
2 예제[ | ]
PHP
Copy
<?php
function jaccard_index($set1, $set2) {
$union_cnt = count(array_unique(array_merge($set1, $set2)));
if($union_cnt < 1)return 1;
$intersect_cnt = count(array_intersect($set1, $set2));
return $intersect_cnt / $union_cnt;
}
$sets = array();
$sets['A'] = array(3,5,6,7,8);
$sets['B'] = array(1,2,3,4,5,6,8,9,10);
$sets['C'] = array(2,3,9);
$sets['D'] = array(1,2,3,4,5,6,8);
$cnt = count($sets);
echo "<table>";
echo "<th>구분</th>";
foreach(array_keys($sets) as $i) {
echo "<th>$i</th>";
}
foreach($sets as $i => $set1) {
echo "<tr>";
echo "<th>$i</th>";
foreach($sets as $j => $set2) {
if($i == $j) {
$j_index = '-';
} else {
$j_index = jaccard_index($set1, $set2);
$j_index = number_format($j_index, 2);
}
echo "<td>$j_index</td>";
}
echo "</tr>";
}
echo "</table>";
text
Copy
구분 A B C D
A - 0.40 0.14 0.50
B 0.40 - 0.33 0.78
C 0.14 0.33 - 0.25
D 0.50 0.78 0.25 -
- → 집합 B와 D는 유사성 0.78 → 상당히 유사함
3 같이 보기[ | ]
편집자 Jmnote Jmnote bot
로그인하시면 댓글을 쓸 수 있습니다.