#计算分数函数
def get_score(,woe,B):
scores=[]
for w in woe:
score=round(*w*B,0)
scores.append(score)
return scores
#根据变量计算分数
def compute_score(series,cut,score):
list = []
i = 0
while i < len(series):
value = series[i]
j = len(cut) - 2
m = len(cut) - 2
while j > 0: # 有的代码写的是“>=”,但实际转换后发现有的变量出现错误
if value >= cut[j]:
j = -1
else:
j -= 1
m -= 1
list.append(score[m])
i += 1
return list
#计算分数
#coe为逻辑回归模型的系数
coe=[]
# 我们取600分为基础分值,PDO为20(每高20分好坏比翻一倍),好坏比取60。
B = 20 / math.log(2)
A = 600 -B*log(60)
# B= 20 / math.log(2)
#A= 600 + B*math.log(1/60)
baseScore = round(A+ B * coe[0], 0)
#baseScore = round(A+ B * coe[0], 0)
注:若odds是 坏客户概率/好客户概率,odds应取倒数,再经过ln转换则B前面是‘—’。woe也要改变。
x1 = get_score(coe[1], woex1, B) #,woe, 要对应
x2 = get_score(coe[2], woex2, B)
x3 = get_score(coe[3], woex3, B)
#计算分数
test1 = pd.read_csv('TestData.csv')
test1['BaseScore']=Series(np.zeros(len(test1)))+baseScore
test1['x1'] = Series(compute_score(test1['变量名'], cutx1, x1))
test1['x2'] = Series(compute_score(test1['变量名'], cutx2, x2))
test1['x3'] = Series(compute_score(test1['变量名'], cutx3, x3))
test1['Score'] = test1['x1'] + test1['x2'] + test1['x3'] + baseScore