#計(jì)算分?jǐn)?shù)函數(shù)
def get_score(,woe,B):
? ? scores=[]
? ? for w in woe:
? ? ? ? score=round(*w*B,0)
? ? ? ? scores.append(score)
? ? return scores
#根據(jù)變量計(jì)算分?jǐn)?shù)
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:??# 有的代碼寫的是“>=”求泰,但實(shí)際轉(zhuǎn)換后發(fā)現(xiàn)有的變量出現(xiàn)錯(cuò)誤
? ? ? ? ? ? if value >= cut[j]:
? ? ? ? ? ? ? ? j = -1
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? j -= 1
? ? ? ? ? ? ? ? m -= 1
? ? ? ? list.append(score[m])
? ? ? ? i += 1
? ? return list
#計(jì)算分?jǐn)?shù)
? ? #coe為邏輯回歸模型的系數(shù)
? ? coe=[]
? ? # 我們?nèi)?00分為基礎(chǔ)分值计盒,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應(yīng)取倒數(shù)咕村,再經(jīng)過ln轉(zhuǎn)換則B前面是‘—’。woe也要改變惹悄。
x1 = get_score(coe[1], woex1, B)? ?#,woe,?要對(duì)應(yīng)
x2 = get_score(coe[2], woex2, B)
x3 = get_score(coe[3], woex3, B)
#計(jì)算分?jǐn)?shù)
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