머신러닝 이미지 분류 플젝(2)

seonghyeon·2022년 1월 14일
0

오늘은 프론트에 기능을 연결시키는 작업들만 무수하게했다

그중에서 가장많이 나온 값 1 2 3등과 전체 대비 백분율을 계산해서 넘겨줌

    result_list = list(db.compared_face_result.find({},{'_id':False}))

    all_result= len(result_list)

    top_list = []
    for i in result_list:
        name = i['result']
        top_list.append(name)
    
    #첫번째 높은순위 
    counts = collections.Counter(top_list)

    a = counts.most_common(1)[0][0]
    a_number = counts.most_common(1)[0][1]

    top_len = len(top_list)
    
    # 전체 값에서 특정값의 백분율 계산
    a_per = "%.2f" % (a_number * 100 / top_len)
    
    # 2번쨰 높은 순위 
    b_list = top_list
    while a in b_list:
        
        b_list.remove(a)

    counts = collections.Counter(b_list)

    b = counts.most_common(1)[0][0]
    b_number = counts.most_common(1)[0][1]
    b_per = "%.2f" % (b_number * 100 / top_len)
    


    # 3번째 높은 순위 
    c_list = top_list
    while b in c_list:
        c_list.remove(b)

    counts = collections.Counter(c_list)

    c = counts.most_common(1)[0][0]
    c_number = counts.most_common(1)[0][1]
    c_per = "%.2f" % (c_number * 100 / top_len)

기존 리스트에 백분율 계산을 해줘야했기에 b_list , c_list를 생성해줬다

import collections

# steam 3개, xbox 2개, pc 는 4개
list = ['steam', 'steam', 'steam', 'xbox', 'xbox',
'pc', 'pc', 'pc', 'pc']

# 빈도수를 세려면?
counts = collections.Counter(list)
print(counts)
# 최대 빈도를 가진 첫번째 값을 most_common(1)로 추출
# most_common(1) => [('pc',4)] _리스트
# most_common(1)[0] => ('pc', 4) _ 튜플
# most_common(1)[0][0] => pc _ 튜플에서 첫번째 추출
print('가장 많이 나온 것?: ', counts.most_common(1)[0][0])

이 방식을 사용하여 최빈값을 구하였다

0개의 댓글