-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscore_function.py
More file actions
75 lines (61 loc) · 3.18 KB
/
Copy pathscore_function.py
File metadata and controls
75 lines (61 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------ #
# INPUT
# predicted_label : 예측 답안지 파일 경로
# actual_label : 실제 답안지 파일 경로
#
# OUTPUT : 유저 기대이익 총합
#
#
# - 예측 답안지를 실제 답안과 비교하여 유저 기대이익 총합을 계산하는 함수
# - 함수의 계산방식은 문제 설명서에 기술된 기대이익 산출식과 동일
# ------------------------------------------------------------------------ #
#필요한 모듈 import
import pandas as pd
import numpy as np
import sys
def score_function(predict_label, actual_label):
predict = pd.read_csv(predict_label, engine='python') # 예측 답안 파일 불러오기
actual = pd.read_csv(actual_label,engine='python') # 실제 답안 파일 불러오기
predict.acc_id = predict.acc_id.astype('int')
predict = predict.sort_values(by =['acc_id'], axis = 0) # 예측 답안을 acc_id 기준으로 정렬
predict = predict.reset_index(drop = True)
actual.acc_id = actual.acc_id.astype('int')
actual = actual.sort_values(by =['acc_id'], axis = 0) # 실제 답안을 acc_id 기준으로 정렬
actual =actual.reset_index(drop=True)
if predict.acc_id.equals(actual.acc_id) == False:
print('acc_id of predicted and actual label does not match')
sys.exit() # 예측 답안의 acc_id와 실제 답안의 acc_id가 다른 경우 에러처리
else:
S, alpha, L, sigma = 30, 0.01, 0.1, 15
cost, gamma, add_rev = 0,0,0
profit_result = []
survival_time_pred = list(predict.survival_time)
amount_spent_pred = list(predict.amount_spent)
survival_time_actual = list(actual.survival_time)
amount_spent_actual = list(actual.amount_spent)
for i in range(len(survival_time_pred)):
if survival_time_pred[i] == 64 :
cost = 0
optimal_cost = 0
else:
cost = alpha * S * amount_spent_pred[i] #비용 계산
optimal_cost = alpha * S * amount_spent_actual[i] #적정비용 계산
if optimal_cost == 0:
gamma = 0
elif cost / optimal_cost < L:
gamma = 0
elif cost / optimal_cost >= 1:
gamma = 1
else:
gamma = (cost)/((1-L)*optimal_cost) - L/(1-L) #반응률 계산
if survival_time_pred[i] == 64 or survival_time_actual[i] == 64:
T_k = 0
else:
T_k = S * np.exp(-((survival_time_pred[i] - survival_time_actual[i])**2)/(2*(sigma)**2)) #추가 생존기간 계산
add_rev = T_k * amount_spent_actual[i] #잔존가치 계산
profit = gamma * add_rev - cost #유저별 기대이익 계산
profit_result.append(profit)
score = sum(profit_result) #기대이익 총합 계산
print(score)
return score