123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576 |
- import numpy as np
- import pandas as pd
- import json
- from datetime import datetime
- from myCreditCard.enterprise import enterprise_credit
- # 设置判断矩阵,获取各指标权重
- def get_weight():
- # 主准则:企业规模、招投标情况、企业信用情况
- print("主准则:")
- main_rule = np.array([
- [ 1, 1/2, 1/3],
- [ 2, 1, 1],
- [ 3, 1, 1]
- ])
- m = AHP(main_rule)
- main_weight = m.cal_weight__by_eigenvalue_method()
- m.test_consist()
- '''
- 1.企业规模 -->>
- actual_capital 实收注册资金
- branch_number 分支机构总数
- copy_reg_works_number 著作权数量
- estiblish_time 开业时间
- is_micro_ent 是否是小微企业
- mortgage_info_number 动产抵押数量
- patents_number 专利数量
- social_staff_num 参保人数
- '''
- print("\n企业规模--rule1:")
- rule1 = np.array([
- [ 1, 5, 3, 1/3, 3, 2, 2, 2],
- [1/5, 1, 1/2, 1/6, 1, 1/3, 1/3, 1/4],
- [1/3, 2, 1, 1/2, 2, 2, 1, 1/3],
- [ 3, 6, 2, 1, 3, 2, 2, 2],
- [1/3, 1, 1/2, 1/3, 1, 1/2, 1/3, 1/5],
- [1/2, 3, 1/2, 1/2, 2, 1, 1/2, 1/3],
- [1/2, 3, 1, 1/2, 3, 2, 1, 1/2],
- [1/2, 4, 3, 1/2, 5, 3, 2, 1]
- ])
- r1 = AHP(rule1)
- r1_weight = r1.cal_weight__by_eigenvalue_method()
- # r1.test_consist()
- '''
- 2、招投标情况 -->>
- bid_number 总的招投标数量
- 2.1、数量之比即为权重 [代理数量:招标数量:(投标数量+中标数量)]
- 2.1.1、代理情况评分
- dai_li_number 代理数量
- 2.1.2、招标情况评分
- 50%:zhao_biao_number 招标数量
- 50%:zhao_biao_money 招标金额(平均)
- 2.1.3、中投标情况评分
- 20%:中标率 :中标数量 / (中标数量 +tou_biao_number 投标数量)
- 40%:zhong_biao_number 中标数量
- 40%:zhong_biao_money :中标金额(平均)
-
- '''
- print("\n招投标情况--rule2:")
- rule2 = np.array([
- [ 1, 1/5],
- [ 5, 1]
- ])
- # rule2 = np.array([
- # [ 1, 2, 2, 2, 1/2, 1/2],
- # [1/2, 1, 1, 1, 1/4, 1/3],
- # [1/2, 1, 1, 1, 1/4, 1/3],
- # [1/2, 1, 1, 1, 1/4, 1/3],
- # [ 2, 4, 4, 4, 1, 1/2],
- # [ 2, 3, 3, 3, 2, 1]
- # ])
- r2 = AHP(rule2)
- r2_weight = r2.cal_weight__by_eigenvalue_method()
- # r2.test_consist()
- '''
- 3.企业信用情况 -->>
- abnormal_number 经营异常数量
- admin_penalties_number 行政处罚数量 xx
- bad_credit_behaviors_number 不良行为数量 xx
- court_announcement_number 法院公告数量
- credit_black_records_number 黑名单记录数量 xx
- credit_china_v2_number 行政处罚数量--信用中国 (新版) 1
- punishment_info_number 行政处罚数量(旧)x 2
- credit_red_record_number 守信红名单数量
- dishonest_number 失信人数量
- good_credit_behaviors_number 良好行为数量 xx
- illegalinfo_number 严重违法数量
- law_suit_number 法律诉讼数量
- own_tax_number 欠税公告数量
- zhixinginfo_number 被执行人数量
- '''
- print("\n企业信用情况--rule3:")
- rule3 = np.array([
- [ 1, 1, 1/3, 1/3, 1/2, 1/5, 1, 1/2, 1/2],
- [ 1, 1, 1/3, 1/2, 1, 1/4, 1, 1/2, 1/2],
- [ 3, 3, 1, 2, 2, 1/2, 2, 2, 2],
- [ 3, 2, 1/2, 1, 1, 1/3, 1, 1, 1],
- [ 2, 1, 1/2, 1, 1, 1/4, 1, 1, 1],
- [ 5, 4, 2, 3, 4, 1, 3, 2, 3],
- [ 1, 1, 1/2, 1, 1, 1/3, 1, 2, 1],
- [ 2, 2, 1/2, 1, 1, 1/2, 1/2, 1, 1/2],
- [ 2, 2, 1/2, 1, 1, 1/3, 1, 2, 1]
- ])
- r3 = AHP(rule3)
- r3_weight = r3.cal_weight__by_eigenvalue_method()
- # r3.test_consist()
- return main_weight,r1_weight,r2_weight,r3_weight
- # 信用评分卡
- class Credit_Card():
- def __init__(self):
- self.main_weight,self.r1_weight,self.r2_weight,self.r3_weight = get_weight()
- self.r1_weight = self.main_weight[0] * self.r1_weight
- self.r2_weight = self.main_weight[1] * self.r2_weight
- self.r3_weight = self.main_weight[2] * self.r3_weight
- self.pinf = float('inf') # 正无穷大
- def set_data(self,data):
- # data : 传入的数据字典
- data_dict = enterprise_credit().toDict()
- data_dict.update(data)
- self.data_dict = data_dict
- def scoring_scale(self):
- data_dict = self.data_dict
- # rule1
- capital_score = self.capital_score(data_dict.get('actual_capital'))
- branch_score = self.branch_score(data_dict.get('branch_number'))
- copy_reg_works_score = self.copy_reg_works_score(data_dict.get('copy_reg_works_number'))
- estiblish_time_score = self.estiblish_time_score(data_dict.get('estiblish_time'))
- is_micro_ent_score = self.is_micro_ent_score(data_dict.get('is_micro_ent'))
- mortgage_score = self.mortgage_score(data_dict.get('mortgage_info_number'))
- patents_score = self.patents_score(data_dict.get('patents_number'))
- staff_num_range = self.staff_num_range(data_dict.get('social_staff_num'))
- rule1_score_list = [capital_score,branch_score,copy_reg_works_score,estiblish_time_score,is_micro_ent_score,mortgage_score,patents_score,staff_num_range]
- # rule2
- bid_num_range = self.bid_num_range(data_dict.get('bid_number'))
- # daili_num_range = self.daili_num_range(data_dict.get('dai_li_number'))
- # toubiao_num_range = self.toubiao_num_range(data_dict.get('tou_biao_number'))
- # zhaobiao_num_range = self.zhaobiao_num_range(data_dict.get('zhao_biao_number'))
- # zhongbiao_num_range = self.zhongbiao_num_range(data_dict.get('zhong_biao_number'))
- # zhongbiao_money_range = self.zhongbiao_money_range(data_dict.get('zhong_biao_money'))
- zhao_tou_biao_score = self.zhao_tou_biao_range(data_dict.get("dai_li_number"),data_dict.get('zhao_biao_number'),data_dict.get('zhao_biao_money'),
- data_dict.get('tou_biao_number'),data_dict.get('zhong_biao_number'),data_dict.get('zhong_biao_money'))
- # rule2_score_list = [bid_num_range,daili_num_range,toubiao_num_range,zhaobiao_num_range,zhongbiao_num_range,zhongbiao_money_range]
- rule2_score_list = [bid_num_range,zhao_tou_biao_score]
- # rule3
- abnormal_num_range = self.abnormal_num_range(data_dict.get('abnormal_number'))
- court_announcement_num_range = self.court_announcement_num_range(data_dict.get('court_announcement_number'))
- punishment_num_range = self.punishment_num_range(data_dict.get('credit_china_v2_number'),data_dict.get('punishment_info_number'))
- credit_red_record_num_range = self.credit_red_record_num_range(data_dict.get('credit_red_record_number'))
- dishonest_num_range = self.dishonest_num_range(data_dict.get('dishonest_number'))
- illegalinfo_num_range = self.illegalinfo_num_range(data_dict.get('illegalinfo_number'))
- law_suit_num_range = self.law_suit_num_range(data_dict.get('law_suit_number'))
- own_tax_num_range = self.own_tax_num_range(data_dict.get('own_tax_number'))
- zhixinginfo_num_range = self.zhixinginfo_num_range(data_dict.get('zhixinginfo_number'))
- rule3_score_list = [abnormal_num_range,court_announcement_num_range,punishment_num_range,credit_red_record_num_range,
- dishonest_num_range,illegalinfo_num_range,law_suit_num_range,own_tax_num_range,zhixinginfo_num_range]
- return rule1_score_list,rule2_score_list,rule3_score_list
- def get_score(self,get_each_score=False):
- rule1_score_list,rule2_score_list,rule3_score_list = self.scoring_scale()
- main_score = sum([np.dot(self.r1_weight,np.array(rule1_score_list)),
- np.dot(self.r2_weight,np.array(rule2_score_list)),
- np.dot(self.r3_weight,np.array(rule3_score_list))])
- monthly_score = self.monthly_zhao_tou_biao_range(self.data_dict.get('monthly_dai_li_number'),self.data_dict.get('monthly_zhao_biao_number'),
- self.data_dict.get('monthly_tou_biao_number'),self.data_dict.get('monthly_zhong_biao_number'))
- credit_score = 0.9 * main_score + 0.1 * monthly_score
- credit_score = int(round(credit_score * 10))
- if get_each_score:
- r1_score = np.array(rule1_score_list)*self.r1_weight
- r2_score = np.array(rule2_score_list)*self.r2_weight
- r3_score = np.array(rule3_score_list)*self.r3_weight
- return r1_score,r2_score,r3_score,credit_score
- else:
- return credit_score
- # 注册资金
- def capital_score(self,value):
- try:
- self.capital_range_scale
- except:
- self.capital_range_scale = [-1,0,50,100,500,1000,5000,10000,self.pinf]
- self.capital_range_score_scale = [30,30,40,50,60,70,75,80]
- score = self.compute_score(value,self.capital_range_scale,self.capital_range_score_scale)
- return score
- # 分支机构/公司
- def branch_score(self,value):
- try:
- self.branch_range_scale
- except:
- self.branch_range_scale = [-1,0,1,10,50,100,self.pinf]
- self.branch_range_score_scale = [40,40,50,60,70,80]
- score = self.compute_score(value,self.branch_range_scale,self.branch_range_score_scale)
- return score
- # 著作权数量
- def copy_reg_works_score(self,value):
- try:
- self.copy_reg_works_range_scale
- except:
- self.copy_reg_works_range_scale = [-1,0,1,10,50,self.pinf]
- self.copy_reg_works_range_score_scale = [40,40,50,60,70]
- score = self.compute_score(value,self.copy_reg_works_range_scale,self.copy_reg_works_range_score_scale)
- return score
- # 注册时间(多久)
- def estiblish_time_score(self,value):
- try:
- self.estiblish_time_range_scale
- except:
- self.nowtime = datetime.now()
- self.estiblish_time_range_scale = [-1,0,5,10,20,30,self.pinf]
- self.estiblish_time_range_score_scale = [40,40,50,60,70,80]
- if value == -1:
- years = -1
- else:
- time = datetime.strptime(value,"%Y-%m-%d")
- time = self.nowtime - time
- years = time.days / 365
- score = self.compute_score(years,self.estiblish_time_range_scale,self.estiblish_time_range_score_scale)
- return score
- # 是否是小微企业
- def is_micro_ent_score(self,value):
- try:
- self.is_micro_ent_range_scale
- except:
- self.is_micro_ent_range_scale = [-1,0,1,self.pinf]
- self.is_micro_ent_range_score_scale = [45,40,70]
- score = self.compute_score(value,self.is_micro_ent_range_scale,self.is_micro_ent_range_score_scale)
- return score
- # 动产抵押数量
- def mortgage_score(self,value):
- try:
- self.mortgage_range_scale
- except:
- self.mortgage_range_scale = [-1,0,1,4,10,self.pinf]
- self.mortgage_range_score_scale = [75,80,60,50,40]
- score = self.compute_score(value,self.mortgage_range_scale,self.mortgage_range_score_scale)
- return score
- # 专利数量
- def patents_score(self,value):
- try:
- self.patents_range_scale
- except:
- self.patents_range_scale = [-1,0,1,10,50,100,self.pinf]
- self.patents_range_score_scale = [40,40,50,60,70,80]
- score = self.compute_score(value,self.patents_range_scale,self.patents_range_score_scale)
- return score
- # 企业人员规模
- def staff_num_range(self,value):
- try:
- self.staff_num_range_scale
- except:
- self.staff_num_range_scale = [-1,0,50,100,500,1000,5000,10000,self.pinf]
- self.staff_num_range_score_scale = [30,30,40,50,60,70,75,80]
- score = self.compute_score(value,self.staff_num_range_scale,self.staff_num_range_score_scale)
- return score
- # 总招投标数量
- def bid_num_range(self,value):
- try:
- self.bid_num_range_scale
- except:
- self.bid_num_range_scale = [-1,0,1,100,500,1000,2000,5000,self.pinf]
- self.bid_num_range_score_scale = [40,40,50,60,70,80,90,100]
- score = self.compute_score(value,self.bid_num_range_scale,self.bid_num_range_score_scale)
- return score
- # 招投标综合评分
- def zhao_tou_biao_range(self,dai_li_num,zhao_biao_num,zhao_biao_money,tou_biao_num,zhong_biao_num,zhong_biao_money):
- total_tou_biao_num = tou_biao_num + zhong_biao_num
- if dai_li_num >= zhong_biao_num:
- tou_biao_num = 0
- zhong_biao_num = 0
- total_tou_biao_num = 0
- if zhao_biao_num:
- zhao_biao_money = max(zhao_biao_money,1)
- if zhong_biao_num:
- zhong_biao_money = max(zhong_biao_money,1)
- bid_num = sum([dai_li_num,zhao_biao_num,total_tou_biao_num])
- if bid_num:
- weight = np.array([dai_li_num,zhao_biao_num,total_tou_biao_num])/bid_num
- else:
- weight = np.array([1/3,1/3,1/3])
- dai_li_score = self.daili_num_range(dai_li_num)
- zhao_biao_score = 0.5 * self.zhaobiao_num_range(zhao_biao_num) + 0.5 * self.zhaobiao_money_range(zhao_biao_money)
- tou_biao_score = 0.2 * self.zhongbiao_rate_range(tou_biao_num,zhong_biao_num) + 0.4 * self.zhongbiao_num_range(zhong_biao_num) + 0.4 * self.zhongbiao_money_range(zhong_biao_money)
- score = np.dot(weight,np.array([dai_li_score,zhao_biao_score,tou_biao_score]))
- return score
- # 上月招投标评分
- def monthly_zhao_tou_biao_range(self,dai_li_num,zhao_biao_num,tou_biao_num,zhong_biao_num):
- try:
- self.monthly_num_range_scale
- except:
- self.monthly_num_range_scale = [-1,0,1,5,10,20,50,self.pinf]
- self.monthly_num_score_range_scale = [10,10,25,40,55,70,85,100]
- total_tou_biao_num = tou_biao_num + zhong_biao_num
- if dai_li_num >= zhong_biao_num:
- tou_biao_num = 0
- zhong_biao_num = 0
- total_tou_biao_num = 0
- bid_num = sum([dai_li_num, zhao_biao_num, total_tou_biao_num])
- if bid_num:
- weight = np.array([dai_li_num, zhao_biao_num, total_tou_biao_num]) / bid_num
- else:
- weight = np.array([1 / 3, 1 / 3, 1 / 3])
- dai_li_score = self.compute_score(dai_li_num,self.monthly_num_range_scale,self.monthly_num_score_range_scale)
- zhao_biao_score = self.compute_score(zhao_biao_num,self.monthly_num_range_scale,self.monthly_num_score_range_scale)
- tou_biao_score = 0.2 * self.zhongbiao_rate_range(tou_biao_num, zhong_biao_num) \
- + 0.8 * self.compute_score(zhong_biao_num,self.monthly_num_range_scale,self.monthly_num_score_range_scale)
- score = np.dot(weight, np.array([dai_li_score, zhao_biao_score, tou_biao_score]))
- return score
- # 代理数量
- def daili_num_range(self,value):
- try:
- self.daili_num_range_scale
- except:
- self.daili_num_range_scale = [-1,0,1,50,200,500,1000,self.pinf]
- self.daili_num_range_score_scale = [40,40,55,70,85,100,115]
- score = self.compute_score(value,self.daili_num_range_scale,self.daili_num_range_score_scale)
- return score
- # 投标数量
- def toubiao_num_range(self,value):
- try:
- self.toubiao_num_range_scale
- except:
- self.toubiao_num_range_scale = [-1,0,1,50,200,500,self.pinf]
- self.toubiao_num_range_score_scale = [30,30,35,40,45,50]
- score = self.compute_score(value,self.toubiao_num_range_scale,self.toubiao_num_range_score_scale)
- return score
- # 招标数量
- def zhaobiao_num_range(self,value):
- try:
- self.zhaobiao_num_range_scale
- except:
- self.zhaobiao_num_range_scale = [-1,0,1,50,200,500,1000,self.pinf]
- self.zhaobiao_num_range_score_scale = [40,40,55,70,85,100,115]
- score = self.compute_score(value,self.zhaobiao_num_range_scale,self.zhaobiao_num_range_score_scale)
- return score
- # 中标数量
- def zhongbiao_num_range(self,value):
- try:
- self.zhongbiao_num_range_scale
- except:
- self.zhongbiao_num_range_scale = [-1,0,1,50,200,500,1000,self.pinf]
- self.zhongbiao_num_range_score_scale = [40,40,55,70,85,100,115]
- score = self.compute_score(value,self.zhongbiao_num_range_scale,self.zhongbiao_num_range_score_scale)
- return score
- # 中标率
- def zhongbiao_rate_range(self,toubiao_num,zhongbiao_num):
- try:
- self.zhongbiao_rate_range_scale
- except:
- self.zhongbiao_rate_range_scale = [-1,0,0.00000001,0.2,0.4,0.6,0.8,self.pinf]
- self.zhongbiao_rate_range_score_scale = [40,40,60,75,90,105,115]
- total_toubiao_num = toubiao_num + zhongbiao_num
- if total_toubiao_num:
- zhongbiao_rate = zhongbiao_num / total_toubiao_num
- else:
- zhongbiao_rate = 0
- score = self.compute_score(zhongbiao_rate,self.zhongbiao_rate_range_scale,self.zhongbiao_rate_range_score_scale)
- return score
- # 平均中标金额
- def zhongbiao_money_range(self,value):
- try:
- self.zhongbiao_money_range_scale
- except:
- self.zhongbiao_money_range_scale = [-1,0,1,100000,500000,1000000,5000000,10000000,50000000,100000000,self.pinf]
- self.zhongbiao_money_range_score_scale = [40,40,50,55,70,80,85,100,110,115]
- score = self.compute_score(value,self.zhongbiao_money_range_scale,self.zhongbiao_money_range_score_scale)
- return score
- # 平均招标金额
- def zhaobiao_money_range(self,value):
- try:
- self.zhaobiao_money_range_scale
- except:
- self.zhaobiao_money_range_scale = [-1,0,1,100000,500000,1000000,5000000,10000000,50000000,100000000,self.pinf]
- self.zhaobiao_money_range_score_scale = [40,40,50,55,70,80,85,100,110,115]
- score = self.compute_score(value,self.zhaobiao_money_range_scale,self.zhaobiao_money_range_score_scale)
- return score
- # 经营异常数量
- def abnormal_num_range(self, value):
- try:
- self.abnormal_num_range_scale
- except:
- self.abnormal_num_range_scale = [-1, 0, 1, 3, 5, self.pinf]
- self.abnormal_num_range_score_scale = [85, 90, 65, 55, 45]
- score = self.compute_score(value, self.abnormal_num_range_scale, self.abnormal_num_range_score_scale)
- return score
- # 行政处罚数量
- # 取credit_china_v2_number【行政处罚数量--信用中国(新版)】和punishment_info_number 【行政处罚数量(旧)】较大值
- def punishment_num_range(self, value1,value2):
- try:
- self.punishment_num_range_scale
- except:
- self.punishment_num_range_scale = [-1, 0, 1, 5, 50, 100,self.pinf]
- self.punishment_num_range_score_scale = [90, 100, 75, 60, 45, 30]
- value = max(value1,value2)
- score = self.compute_score(value, self.punishment_num_range_scale, self.punishment_num_range_score_scale)
- return score
- # 法院公告数量
- def court_announcement_num_range(self, value):
- try:
- self.court_announcement_num_range_scale
- except:
- self.court_announcement_num_range_scale = [-1, 0, 1, 10, 50,100,500, self.pinf]
- self.court_announcement_num_range_score_scale = [85, 90, 70, 60, 50, 40, 30]
- score = self.compute_score(value, self.court_announcement_num_range_scale, self.court_announcement_num_range_score_scale)
- return score
- # 守信红名单数量
- def credit_red_record_num_range(self, value):
- try:
- self.credit_red_record_num_range_scale
- except:
- self.credit_red_record_num_range_scale = [-1, 0, 1, 5, 10,self.pinf]
- self.credit_red_record_num_range_score_scale = [45, 45, 60, 75, 90]
- score = self.compute_score(value, self.credit_red_record_num_range_scale, self.credit_red_record_num_range_score_scale)
- return score
- # 失信人数量
- def dishonest_num_range(self, value):
- try:
- self.dishonest_num_range_scale
- except:
- self.dishonest_num_range_scale = [-1, 0, 1, 5, 10,self.pinf]
- self.dishonest_num_range_score_scale = [80, 90, 65, 50, 35]
- score = self.compute_score(value, self.dishonest_num_range_scale, self.dishonest_num_range_score_scale)
- return score
- # 严重违法数量
- def illegalinfo_num_range(self, value):
- try:
- self.illegalinfo_num_range_scale
- except:
- self.illegalinfo_num_range_scale = [-1, 0, 1, 2, 3,self.pinf]
- self.illegalinfo_num_range_score_scale = [100, 110, 80, 60, 40]
- score = self.compute_score(value, self.illegalinfo_num_range_scale, self.illegalinfo_num_range_score_scale)
- return score
- # 法律诉讼数量
- def law_suit_num_range(self, value):
- try:
- self.law_suit_num_range_scale
- except:
- self.law_suit_num_range_scale = [-1, 0, 1, 20, 50, 100, 200,self.pinf]
- self.law_suit_num_range_score_scale = [85, 90, 70, 60, 50,40,30]
- score = self.compute_score(value, self.law_suit_num_range_scale, self.law_suit_num_range_score_scale)
- return score
- # 欠税公告数量
- def own_tax_num_range(self, value):
- try:
- self.own_tax_num_range_scale
- except:
- self.own_tax_num_range_scale = [-1, 0, 1, 5, 20, 50, self.pinf]
- self.own_tax_num_range_score_scale = [85, 90, 70, 55, 40,25]
- score = self.compute_score(value, self.own_tax_num_range_scale, self.own_tax_num_range_score_scale)
- return score
- # 被执行人数量
- def zhixinginfo_num_range(self, value):
- try:
- self.zhixinginfo_num_range_scale
- except:
- self.zhixinginfo_num_range_scale = [-1, 0, 1, 5, 20, 50, self.pinf]
- self.zhixinginfo_num_range_score_scale = [85, 90, 70, 55, 40,25]
- score = self.compute_score(value, self.zhixinginfo_num_range_scale, self.zhixinginfo_num_range_score_scale)
- return score
- # 计算得分函数
- def compute_score(self,value,range_scale,score_scale):
- '''
- :param value: 指标值
- :param range_scale: 指标范围刻度
- :param score_scale: 指标评分刻度
- :return: score 得分
- '''
- assert len(range_scale) - 1 == len(score_scale)
- assert value >= range_scale[0]
- i = len(range_scale) - 2
- j = len(score_scale) - 1
- while i >= 0:
- if value >= range_scale[i]:
- if i == j:
- score = score_scale[i]
- elif value == range_scale[i]:
- score = score_scale[i]
- else:
- score_1 = score_scale[i]
- score_2 = score_scale[i+1]
- range_1 = range_scale[i]
- range_2 = range_scale[i+1]
- b_log = np.log2(1 + (value-range_1) / (range_2-range_1))
- score = score_1 + (score_2 - score_1) * b_log
- break
- i -= 1
- return score
- class AHP:
- def __init__(self, array):
- ## 记录矩阵相关信息
- self.array = array
- ## 记录矩阵大小
- self.n = array.shape[0]
- # 初始化RI值,用于一致性检验
- self.RI_list = [0, 0, 0.52, 0.89, 1.12, 1.26, 1.36, 1.41, 1.46, 1.49, 1.52, 1.54, 1.56, 1.58,
- 1.59]
- # 矩阵的特征值和特征向量
- self.eig_val, self.eig_vector = np.linalg.eig(self.array)
- # 矩阵的最大特征值
- self.max_eig_val = np.max(self.eig_val)
- # 矩阵最大特征值对应的特征向量
- self.max_eig_vector = self.eig_vector[:, np.argmax(self.eig_val)].real
- # 矩阵的一致性指标CI
- self.CI_val = (self.max_eig_val - self.n) / (self.n - 1)
- # 矩阵的一致性比例CR
- self.CR_val = self.CI_val / (self.RI_list[self.n - 1])
- """
- 一致性判断
- """
- def test_consist(self):
- # 打印矩阵的一致性指标CI和一致性比例CR
- print("判断矩阵的CI值为:" + str(self.CI_val))
- print("判断矩阵的CR值为:" + str(self.CR_val))
- # 进行一致性检验判断
- if self.n == 2: # 当只有两个子因素的情况
- print("仅包含两个子因素,不存在一致性问题")
- else:
- if self.CR_val < 0.1: # CR值小于0.1,可以通过一致性检验
- print("判断矩阵的CR值为" + str(self.CR_val) + ",通过一致性检验")
- return True
- else: # CR值大于0.1, 一致性检验不通过
- print("判断矩阵的CR值为" + str(self.CR_val) + "未通过一致性检验")
- return False
- """
- 算术平均法求权重
- """
- def cal_weight_by_arithmetic_method(self):
- # 求矩阵的每列的和
- col_sum = np.sum(self.array, axis=0)
- # 将判断矩阵按照列归一化
- array_normed = self.array / col_sum
- # 计算权重向量
- array_weight = np.sum(array_normed, axis=1) / self.n
- # 打印权重向量
- print("算术平均法计算得到的权重向量为:\n", array_weight)
- # 返回权重向量的值
- return array_weight
- """
- 几何平均法求权重
- """
- def cal_weight__by_geometric_method(self):
- # 求矩阵的每列的积
- col_product = np.product(self.array, axis=0)
- # 将得到的积向量的每个分量进行开n次方
- array_power = np.power(col_product, 1 / self.n)
- # 将列向量归一化
- array_weight = array_power / np.sum(array_power)
- # 打印权重向量
- print("几何平均法计算得到的权重向量为:\n", array_weight)
- # 返回权重向量的值
- return array_weight
- """
- 特征值法求权重
- """
- def cal_weight__by_eigenvalue_method(self):
- # 将矩阵最大特征值对应的特征向量进行归一化处理就得到了权重
- array_weight = self.max_eig_vector / np.sum(self.max_eig_vector)
- # 打印权重向量
- print("特征值法计算得到的权重向量为:\n", array_weight)
- # 返回权重向量的值
- return array_weight
- if __name__ == '__main__':
- credit_card = Credit_Card()
- credit_card.set_data(dict())
- print("信用得分",credit_card.get_score())
- pass
|