한 걸음 두 걸음
Jaccard Index 구하는 JAVA 소스코드 본문
반응형
Jaccardindex (Jaccardsimilarity coefficient)
• 두 집합 사이의 유사도를 표현하는 지표. 0과 1 사이의 값을 가짐
• J(A, B) = |A ∩B| / |A ∪B|
• 스캔된AP 각각을 집합의 원소로 봄.
2개의AP 리스트가 있을 때 이를 각 집합으로 보고 Jaccard index 값 계산
쉬운 예시
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
HashSet<String> hs01 = new HashSet<>();
HashSet<String> hs02 = new HashSet<>();
HashSet<String> hsSum = new HashSet<>();
hs01.add("1");
hs01.add("2");
hs01.add("3");
hs01.add("4");
hs02.add("1");
hs02.add("2");
//합집합 버퍼의 기준 정하기
hsSum = (HashSet)hs01.clone();
int common = 0;
//합집합 만들기 & 교집합 갯수 구하기
for(String hs2Element: hs02)
if(hs01.contains(hs2Element)) common++;
else hsSum.add(hs2Element);
//결과값 출력
System.out.println((double)common/hsSum.size());
}
}
관련 문제
https://www.acmicpc.net/problem/15466
15466번: Similarity Computation
The Jaccard similarity coefficient is usually used for measuring the similarity of two sets. Give two sets A and B, the Jaccard similarity coefficient, J(A, B), is defined as the size of the intersection divided by the size of the union of the two sets. Th
www.acmicpc.net
소스코드
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
HashSet<Integer> hs01 = new HashSet<>();
HashSet<Integer> hs02 = new HashSet<>();
HashSet<Integer> hsSum = new HashSet<>();
int testCase = sc.nextInt();
for(int i = 0 ; i < testCase; i ++) {
int size01 = sc.nextInt();
int size02 = sc.nextInt();
for(int j = 0 ; j < size01; j++)
hs01.add(sc.nextInt());
for(int j = 0 ; j < size02; j++)
hs02.add(sc.nextInt());
//합집합 버퍼의 기준 정하기
hsSum = (HashSet)hs01.clone();
int common = 0;
//합집합 만들기 & 교집합 갯수 구하기
for(int hs2Element: hs02)
if(hs01.contains(hs2Element)) common++;
else hsSum.add(hs2Element);
System.out.println((double)common/(double)hsSum.size() > 0.5?1:0);
hs01.clear(); hs02.clear(); hsSum.clear();
}
}
}
반응형
'CSE > baekjoon & swexpert' 카테고리의 다른 글
백준 10872 팩토리얼 / java (0) | 2019.04.27 |
---|---|
백준 1676 팩토리얼 0의 개수 / java (0) | 2019.04.27 |
백준 11723 집합 ] HashSet Collection 사용 (0) | 2019.04.13 |
백준 아호코라식 문제 다음에 풀어보자!! 개념공부가 좀 필요해요~ (0) | 2019.04.13 |
백준 14425 문자열집합 ] 자료구조 HashSet 이용! (Set) (0) | 2019.04.13 |