credit_card.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. import numpy as np
  2. import pandas as pd
  3. import json
  4. from datetime import datetime
  5. from myCreditCard.enterprise import enterprise_credit
  6. # 设置判断矩阵,获取各指标权重
  7. def get_weight():
  8. # 主准则:企业规模、招投标情况、企业信用情况
  9. print("主准则:")
  10. main_rule = np.array([
  11. [ 1, 1/2, 1/3],
  12. [ 2, 1, 1],
  13. [ 3, 1, 1]
  14. ])
  15. m = AHP(main_rule)
  16. main_weight = m.cal_weight__by_eigenvalue_method()
  17. m.test_consist()
  18. '''
  19. 1.企业规模 -->>
  20. actual_capital 实收注册资金
  21. branch_number 分支机构总数
  22. copy_reg_works_number 著作权数量
  23. estiblish_time 开业时间
  24. is_micro_ent 是否是小微企业
  25. mortgage_info_number 动产抵押数量
  26. patents_number 专利数量
  27. social_staff_num 参保人数
  28. '''
  29. print("\n企业规模--rule1:")
  30. rule1 = np.array([
  31. [ 1, 5, 3, 1/3, 3, 2, 2, 2],
  32. [1/5, 1, 1/2, 1/6, 1, 1/3, 1/3, 1/4],
  33. [1/3, 2, 1, 1/2, 2, 2, 1, 1/3],
  34. [ 3, 6, 2, 1, 3, 2, 2, 2],
  35. [1/3, 1, 1/2, 1/3, 1, 1/2, 1/3, 1/5],
  36. [1/2, 3, 1/2, 1/2, 2, 1, 1/2, 1/3],
  37. [1/2, 3, 1, 1/2, 3, 2, 1, 1/2],
  38. [1/2, 4, 3, 1/2, 5, 3, 2, 1]
  39. ])
  40. r1 = AHP(rule1)
  41. r1_weight = r1.cal_weight__by_eigenvalue_method()
  42. # r1.test_consist()
  43. '''
  44. 2、招投标情况 -->>
  45. bid_number 总的招投标数量
  46. 2.1、数量之比即为权重 [代理数量:招标数量:(投标数量+中标数量)]
  47. 2.1.1、代理情况评分
  48. dai_li_number 代理数量
  49. 2.1.2、招标情况评分
  50. 50%:zhao_biao_number 招标数量
  51. 50%:zhao_biao_money 招标金额(平均)
  52. 2.1.3、中投标情况评分
  53. 20%:中标率 :中标数量 / (中标数量 +tou_biao_number 投标数量)
  54. 40%:zhong_biao_number 中标数量
  55. 40%:zhong_biao_money :中标金额(平均)
  56. '''
  57. print("\n招投标情况--rule2:")
  58. rule2 = np.array([
  59. [ 1, 1/5],
  60. [ 5, 1]
  61. ])
  62. # rule2 = np.array([
  63. # [ 1, 2, 2, 2, 1/2, 1/2],
  64. # [1/2, 1, 1, 1, 1/4, 1/3],
  65. # [1/2, 1, 1, 1, 1/4, 1/3],
  66. # [1/2, 1, 1, 1, 1/4, 1/3],
  67. # [ 2, 4, 4, 4, 1, 1/2],
  68. # [ 2, 3, 3, 3, 2, 1]
  69. # ])
  70. r2 = AHP(rule2)
  71. r2_weight = r2.cal_weight__by_eigenvalue_method()
  72. # r2.test_consist()
  73. '''
  74. 3.企业信用情况 -->>
  75. abnormal_number 经营异常数量
  76. admin_penalties_number 行政处罚数量 xx
  77. bad_credit_behaviors_number 不良行为数量 xx
  78. court_announcement_number 法院公告数量
  79. credit_black_records_number 黑名单记录数量 xx
  80. credit_china_v2_number 行政处罚数量--信用中国 (新版) 1
  81. punishment_info_number 行政处罚数量(旧)x 2
  82. credit_red_record_number 守信红名单数量
  83. dishonest_number 失信人数量
  84. good_credit_behaviors_number 良好行为数量 xx
  85. illegalinfo_number 严重违法数量
  86. law_suit_number 法律诉讼数量
  87. own_tax_number 欠税公告数量
  88. zhixinginfo_number 被执行人数量
  89. '''
  90. print("\n企业信用情况--rule3:")
  91. rule3 = np.array([
  92. [ 1, 1, 1/3, 1/3, 1/2, 1/5, 1, 1/2, 1/2],
  93. [ 1, 1, 1/3, 1/2, 1, 1/4, 1, 1/2, 1/2],
  94. [ 3, 3, 1, 2, 2, 1/2, 2, 2, 2],
  95. [ 3, 2, 1/2, 1, 1, 1/3, 1, 1, 1],
  96. [ 2, 1, 1/2, 1, 1, 1/4, 1, 1, 1],
  97. [ 5, 4, 2, 3, 4, 1, 3, 2, 3],
  98. [ 1, 1, 1/2, 1, 1, 1/3, 1, 2, 1],
  99. [ 2, 2, 1/2, 1, 1, 1/2, 1/2, 1, 1/2],
  100. [ 2, 2, 1/2, 1, 1, 1/3, 1, 2, 1]
  101. ])
  102. r3 = AHP(rule3)
  103. r3_weight = r3.cal_weight__by_eigenvalue_method()
  104. # r3.test_consist()
  105. return main_weight,r1_weight,r2_weight,r3_weight
  106. # 信用评分卡
  107. class Credit_Card():
  108. def __init__(self):
  109. self.main_weight,self.r1_weight,self.r2_weight,self.r3_weight = get_weight()
  110. self.r1_weight = self.main_weight[0] * self.r1_weight
  111. self.r2_weight = self.main_weight[1] * self.r2_weight
  112. self.r3_weight = self.main_weight[2] * self.r3_weight
  113. self.pinf = float('inf') # 正无穷大
  114. def set_data(self,data):
  115. # data : 传入的数据字典
  116. data_dict = enterprise_credit().toDict()
  117. data_dict.update(data)
  118. self.data_dict = data_dict
  119. def scoring_scale(self):
  120. data_dict = self.data_dict
  121. # rule1
  122. capital_score = self.capital_score(data_dict.get('actual_capital'))
  123. branch_score = self.branch_score(data_dict.get('branch_number'))
  124. copy_reg_works_score = self.copy_reg_works_score(data_dict.get('copy_reg_works_number'))
  125. estiblish_time_score = self.estiblish_time_score(data_dict.get('estiblish_time'))
  126. is_micro_ent_score = self.is_micro_ent_score(data_dict.get('is_micro_ent'))
  127. mortgage_score = self.mortgage_score(data_dict.get('mortgage_info_number'))
  128. patents_score = self.patents_score(data_dict.get('patents_number'))
  129. staff_num_range = self.staff_num_range(data_dict.get('social_staff_num'))
  130. 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]
  131. # rule2
  132. bid_num_range = self.bid_num_range(data_dict.get('bid_number'))
  133. # daili_num_range = self.daili_num_range(data_dict.get('dai_li_number'))
  134. # toubiao_num_range = self.toubiao_num_range(data_dict.get('tou_biao_number'))
  135. # zhaobiao_num_range = self.zhaobiao_num_range(data_dict.get('zhao_biao_number'))
  136. # zhongbiao_num_range = self.zhongbiao_num_range(data_dict.get('zhong_biao_number'))
  137. # zhongbiao_money_range = self.zhongbiao_money_range(data_dict.get('zhong_biao_money'))
  138. 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'),
  139. data_dict.get('tou_biao_number'),data_dict.get('zhong_biao_number'),data_dict.get('zhong_biao_money'))
  140. # rule2_score_list = [bid_num_range,daili_num_range,toubiao_num_range,zhaobiao_num_range,zhongbiao_num_range,zhongbiao_money_range]
  141. rule2_score_list = [bid_num_range,zhao_tou_biao_score]
  142. # rule3
  143. abnormal_num_range = self.abnormal_num_range(data_dict.get('abnormal_number'))
  144. court_announcement_num_range = self.court_announcement_num_range(data_dict.get('court_announcement_number'))
  145. punishment_num_range = self.punishment_num_range(data_dict.get('credit_china_v2_number'),data_dict.get('punishment_info_number'))
  146. credit_red_record_num_range = self.credit_red_record_num_range(data_dict.get('credit_red_record_number'))
  147. dishonest_num_range = self.dishonest_num_range(data_dict.get('dishonest_number'))
  148. illegalinfo_num_range = self.illegalinfo_num_range(data_dict.get('illegalinfo_number'))
  149. law_suit_num_range = self.law_suit_num_range(data_dict.get('law_suit_number'))
  150. own_tax_num_range = self.own_tax_num_range(data_dict.get('own_tax_number'))
  151. zhixinginfo_num_range = self.zhixinginfo_num_range(data_dict.get('zhixinginfo_number'))
  152. rule3_score_list = [abnormal_num_range,court_announcement_num_range,punishment_num_range,credit_red_record_num_range,
  153. dishonest_num_range,illegalinfo_num_range,law_suit_num_range,own_tax_num_range,zhixinginfo_num_range]
  154. return rule1_score_list,rule2_score_list,rule3_score_list
  155. def get_score(self,get_each_score=False):
  156. rule1_score_list,rule2_score_list,rule3_score_list = self.scoring_scale()
  157. main_score = sum([np.dot(self.r1_weight,np.array(rule1_score_list)),
  158. np.dot(self.r2_weight,np.array(rule2_score_list)),
  159. np.dot(self.r3_weight,np.array(rule3_score_list))])
  160. monthly_score = self.monthly_zhao_tou_biao_range(self.data_dict.get('monthly_dai_li_number'),self.data_dict.get('monthly_zhao_biao_number'),
  161. self.data_dict.get('monthly_tou_biao_number'),self.data_dict.get('monthly_zhong_biao_number'))
  162. credit_score = 0.9 * main_score + 0.1 * monthly_score
  163. credit_score = int(round(credit_score * 10))
  164. if get_each_score:
  165. r1_score = np.array(rule1_score_list)*self.r1_weight
  166. r2_score = np.array(rule2_score_list)*self.r2_weight
  167. r3_score = np.array(rule3_score_list)*self.r3_weight
  168. return r1_score,r2_score,r3_score,credit_score
  169. else:
  170. return credit_score
  171. # 注册资金
  172. def capital_score(self,value):
  173. try:
  174. self.capital_range_scale
  175. except:
  176. self.capital_range_scale = [-1,0,50,100,500,1000,5000,10000,self.pinf]
  177. self.capital_range_score_scale = [30,30,40,50,60,70,75,80]
  178. score = self.compute_score(value,self.capital_range_scale,self.capital_range_score_scale)
  179. return score
  180. # 分支机构/公司
  181. def branch_score(self,value):
  182. try:
  183. self.branch_range_scale
  184. except:
  185. self.branch_range_scale = [-1,0,1,10,50,100,self.pinf]
  186. self.branch_range_score_scale = [40,40,50,60,70,80]
  187. score = self.compute_score(value,self.branch_range_scale,self.branch_range_score_scale)
  188. return score
  189. # 著作权数量
  190. def copy_reg_works_score(self,value):
  191. try:
  192. self.copy_reg_works_range_scale
  193. except:
  194. self.copy_reg_works_range_scale = [-1,0,1,10,50,self.pinf]
  195. self.copy_reg_works_range_score_scale = [40,40,50,60,70]
  196. score = self.compute_score(value,self.copy_reg_works_range_scale,self.copy_reg_works_range_score_scale)
  197. return score
  198. # 注册时间(多久)
  199. def estiblish_time_score(self,value):
  200. try:
  201. self.estiblish_time_range_scale
  202. except:
  203. self.nowtime = datetime.now()
  204. self.estiblish_time_range_scale = [-1,0,5,10,20,30,self.pinf]
  205. self.estiblish_time_range_score_scale = [40,40,50,60,70,80]
  206. if value == -1:
  207. years = -1
  208. else:
  209. time = datetime.strptime(value,"%Y-%m-%d")
  210. time = self.nowtime - time
  211. years = time.days / 365
  212. score = self.compute_score(years,self.estiblish_time_range_scale,self.estiblish_time_range_score_scale)
  213. return score
  214. # 是否是小微企业
  215. def is_micro_ent_score(self,value):
  216. try:
  217. self.is_micro_ent_range_scale
  218. except:
  219. self.is_micro_ent_range_scale = [-1,0,1,self.pinf]
  220. self.is_micro_ent_range_score_scale = [45,40,70]
  221. score = self.compute_score(value,self.is_micro_ent_range_scale,self.is_micro_ent_range_score_scale)
  222. return score
  223. # 动产抵押数量
  224. def mortgage_score(self,value):
  225. try:
  226. self.mortgage_range_scale
  227. except:
  228. self.mortgage_range_scale = [-1,0,1,4,10,self.pinf]
  229. self.mortgage_range_score_scale = [75,80,60,50,40]
  230. score = self.compute_score(value,self.mortgage_range_scale,self.mortgage_range_score_scale)
  231. return score
  232. # 专利数量
  233. def patents_score(self,value):
  234. try:
  235. self.patents_range_scale
  236. except:
  237. self.patents_range_scale = [-1,0,1,10,50,100,self.pinf]
  238. self.patents_range_score_scale = [40,40,50,60,70,80]
  239. score = self.compute_score(value,self.patents_range_scale,self.patents_range_score_scale)
  240. return score
  241. # 企业人员规模
  242. def staff_num_range(self,value):
  243. try:
  244. self.staff_num_range_scale
  245. except:
  246. self.staff_num_range_scale = [-1,0,50,100,500,1000,5000,10000,self.pinf]
  247. self.staff_num_range_score_scale = [30,30,40,50,60,70,75,80]
  248. score = self.compute_score(value,self.staff_num_range_scale,self.staff_num_range_score_scale)
  249. return score
  250. # 总招投标数量
  251. def bid_num_range(self,value):
  252. try:
  253. self.bid_num_range_scale
  254. except:
  255. self.bid_num_range_scale = [-1,0,1,100,500,1000,2000,5000,self.pinf]
  256. self.bid_num_range_score_scale = [40,40,50,60,70,80,90,100]
  257. score = self.compute_score(value,self.bid_num_range_scale,self.bid_num_range_score_scale)
  258. return score
  259. # 招投标综合评分
  260. def zhao_tou_biao_range(self,dai_li_num,zhao_biao_num,zhao_biao_money,tou_biao_num,zhong_biao_num,zhong_biao_money):
  261. total_tou_biao_num = tou_biao_num + zhong_biao_num
  262. if dai_li_num >= zhong_biao_num:
  263. tou_biao_num = 0
  264. zhong_biao_num = 0
  265. total_tou_biao_num = 0
  266. if zhao_biao_num:
  267. zhao_biao_money = max(zhao_biao_money,1)
  268. if zhong_biao_num:
  269. zhong_biao_money = max(zhong_biao_money,1)
  270. bid_num = sum([dai_li_num,zhao_biao_num,total_tou_biao_num])
  271. if bid_num:
  272. weight = np.array([dai_li_num,zhao_biao_num,total_tou_biao_num])/bid_num
  273. else:
  274. weight = np.array([1/3,1/3,1/3])
  275. dai_li_score = self.daili_num_range(dai_li_num)
  276. zhao_biao_score = 0.5 * self.zhaobiao_num_range(zhao_biao_num) + 0.5 * self.zhaobiao_money_range(zhao_biao_money)
  277. 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)
  278. score = np.dot(weight,np.array([dai_li_score,zhao_biao_score,tou_biao_score]))
  279. return score
  280. # 上月招投标评分
  281. def monthly_zhao_tou_biao_range(self,dai_li_num,zhao_biao_num,tou_biao_num,zhong_biao_num):
  282. try:
  283. self.monthly_num_range_scale
  284. except:
  285. self.monthly_num_range_scale = [-1,0,1,5,10,20,50,self.pinf]
  286. self.monthly_num_score_range_scale = [10,10,25,40,55,70,85,100]
  287. total_tou_biao_num = tou_biao_num + zhong_biao_num
  288. if dai_li_num >= zhong_biao_num:
  289. tou_biao_num = 0
  290. zhong_biao_num = 0
  291. total_tou_biao_num = 0
  292. bid_num = sum([dai_li_num, zhao_biao_num, total_tou_biao_num])
  293. if bid_num:
  294. weight = np.array([dai_li_num, zhao_biao_num, total_tou_biao_num]) / bid_num
  295. else:
  296. weight = np.array([1 / 3, 1 / 3, 1 / 3])
  297. dai_li_score = self.compute_score(dai_li_num,self.monthly_num_range_scale,self.monthly_num_score_range_scale)
  298. zhao_biao_score = self.compute_score(zhao_biao_num,self.monthly_num_range_scale,self.monthly_num_score_range_scale)
  299. tou_biao_score = 0.2 * self.zhongbiao_rate_range(tou_biao_num, zhong_biao_num) \
  300. + 0.8 * self.compute_score(zhong_biao_num,self.monthly_num_range_scale,self.monthly_num_score_range_scale)
  301. score = np.dot(weight, np.array([dai_li_score, zhao_biao_score, tou_biao_score]))
  302. return score
  303. # 代理数量
  304. def daili_num_range(self,value):
  305. try:
  306. self.daili_num_range_scale
  307. except:
  308. self.daili_num_range_scale = [-1,0,1,50,200,500,1000,self.pinf]
  309. self.daili_num_range_score_scale = [40,40,55,70,85,100,115]
  310. score = self.compute_score(value,self.daili_num_range_scale,self.daili_num_range_score_scale)
  311. return score
  312. # 投标数量
  313. def toubiao_num_range(self,value):
  314. try:
  315. self.toubiao_num_range_scale
  316. except:
  317. self.toubiao_num_range_scale = [-1,0,1,50,200,500,self.pinf]
  318. self.toubiao_num_range_score_scale = [30,30,35,40,45,50]
  319. score = self.compute_score(value,self.toubiao_num_range_scale,self.toubiao_num_range_score_scale)
  320. return score
  321. # 招标数量
  322. def zhaobiao_num_range(self,value):
  323. try:
  324. self.zhaobiao_num_range_scale
  325. except:
  326. self.zhaobiao_num_range_scale = [-1,0,1,50,200,500,1000,self.pinf]
  327. self.zhaobiao_num_range_score_scale = [40,40,55,70,85,100,115]
  328. score = self.compute_score(value,self.zhaobiao_num_range_scale,self.zhaobiao_num_range_score_scale)
  329. return score
  330. # 中标数量
  331. def zhongbiao_num_range(self,value):
  332. try:
  333. self.zhongbiao_num_range_scale
  334. except:
  335. self.zhongbiao_num_range_scale = [-1,0,1,50,200,500,1000,self.pinf]
  336. self.zhongbiao_num_range_score_scale = [40,40,55,70,85,100,115]
  337. score = self.compute_score(value,self.zhongbiao_num_range_scale,self.zhongbiao_num_range_score_scale)
  338. return score
  339. # 中标率
  340. def zhongbiao_rate_range(self,toubiao_num,zhongbiao_num):
  341. try:
  342. self.zhongbiao_rate_range_scale
  343. except:
  344. self.zhongbiao_rate_range_scale = [-1,0,0.00000001,0.2,0.4,0.6,0.8,self.pinf]
  345. self.zhongbiao_rate_range_score_scale = [40,40,60,75,90,105,115]
  346. total_toubiao_num = toubiao_num + zhongbiao_num
  347. if total_toubiao_num:
  348. zhongbiao_rate = zhongbiao_num / total_toubiao_num
  349. else:
  350. zhongbiao_rate = 0
  351. score = self.compute_score(zhongbiao_rate,self.zhongbiao_rate_range_scale,self.zhongbiao_rate_range_score_scale)
  352. return score
  353. # 平均中标金额
  354. def zhongbiao_money_range(self,value):
  355. try:
  356. self.zhongbiao_money_range_scale
  357. except:
  358. self.zhongbiao_money_range_scale = [-1,0,1,100000,500000,1000000,5000000,10000000,50000000,100000000,self.pinf]
  359. self.zhongbiao_money_range_score_scale = [40,40,50,55,70,80,85,100,110,115]
  360. score = self.compute_score(value,self.zhongbiao_money_range_scale,self.zhongbiao_money_range_score_scale)
  361. return score
  362. # 平均招标金额
  363. def zhaobiao_money_range(self,value):
  364. try:
  365. self.zhaobiao_money_range_scale
  366. except:
  367. self.zhaobiao_money_range_scale = [-1,0,1,100000,500000,1000000,5000000,10000000,50000000,100000000,self.pinf]
  368. self.zhaobiao_money_range_score_scale = [40,40,50,55,70,80,85,100,110,115]
  369. score = self.compute_score(value,self.zhaobiao_money_range_scale,self.zhaobiao_money_range_score_scale)
  370. return score
  371. # 经营异常数量
  372. def abnormal_num_range(self, value):
  373. try:
  374. self.abnormal_num_range_scale
  375. except:
  376. self.abnormal_num_range_scale = [-1, 0, 1, 3, 5, self.pinf]
  377. self.abnormal_num_range_score_scale = [85, 90, 65, 55, 45]
  378. score = self.compute_score(value, self.abnormal_num_range_scale, self.abnormal_num_range_score_scale)
  379. return score
  380. # 行政处罚数量
  381. # 取credit_china_v2_number【行政处罚数量--信用中国(新版)】和punishment_info_number 【行政处罚数量(旧)】较大值
  382. def punishment_num_range(self, value1,value2):
  383. try:
  384. self.punishment_num_range_scale
  385. except:
  386. self.punishment_num_range_scale = [-1, 0, 1, 5, 50, 100,self.pinf]
  387. self.punishment_num_range_score_scale = [90, 100, 75, 60, 45, 30]
  388. value = max(value1,value2)
  389. score = self.compute_score(value, self.punishment_num_range_scale, self.punishment_num_range_score_scale)
  390. return score
  391. # 法院公告数量
  392. def court_announcement_num_range(self, value):
  393. try:
  394. self.court_announcement_num_range_scale
  395. except:
  396. self.court_announcement_num_range_scale = [-1, 0, 1, 10, 50,100,500, self.pinf]
  397. self.court_announcement_num_range_score_scale = [85, 90, 70, 60, 50, 40, 30]
  398. score = self.compute_score(value, self.court_announcement_num_range_scale, self.court_announcement_num_range_score_scale)
  399. return score
  400. # 守信红名单数量
  401. def credit_red_record_num_range(self, value):
  402. try:
  403. self.credit_red_record_num_range_scale
  404. except:
  405. self.credit_red_record_num_range_scale = [-1, 0, 1, 5, 10,self.pinf]
  406. self.credit_red_record_num_range_score_scale = [45, 45, 60, 75, 90]
  407. score = self.compute_score(value, self.credit_red_record_num_range_scale, self.credit_red_record_num_range_score_scale)
  408. return score
  409. # 失信人数量
  410. def dishonest_num_range(self, value):
  411. try:
  412. self.dishonest_num_range_scale
  413. except:
  414. self.dishonest_num_range_scale = [-1, 0, 1, 5, 10,self.pinf]
  415. self.dishonest_num_range_score_scale = [80, 90, 65, 50, 35]
  416. score = self.compute_score(value, self.dishonest_num_range_scale, self.dishonest_num_range_score_scale)
  417. return score
  418. # 严重违法数量
  419. def illegalinfo_num_range(self, value):
  420. try:
  421. self.illegalinfo_num_range_scale
  422. except:
  423. self.illegalinfo_num_range_scale = [-1, 0, 1, 2, 3,self.pinf]
  424. self.illegalinfo_num_range_score_scale = [100, 110, 80, 60, 40]
  425. score = self.compute_score(value, self.illegalinfo_num_range_scale, self.illegalinfo_num_range_score_scale)
  426. return score
  427. # 法律诉讼数量
  428. def law_suit_num_range(self, value):
  429. try:
  430. self.law_suit_num_range_scale
  431. except:
  432. self.law_suit_num_range_scale = [-1, 0, 1, 20, 50, 100, 200,self.pinf]
  433. self.law_suit_num_range_score_scale = [85, 90, 70, 60, 50,40,30]
  434. score = self.compute_score(value, self.law_suit_num_range_scale, self.law_suit_num_range_score_scale)
  435. return score
  436. # 欠税公告数量
  437. def own_tax_num_range(self, value):
  438. try:
  439. self.own_tax_num_range_scale
  440. except:
  441. self.own_tax_num_range_scale = [-1, 0, 1, 5, 20, 50, self.pinf]
  442. self.own_tax_num_range_score_scale = [85, 90, 70, 55, 40,25]
  443. score = self.compute_score(value, self.own_tax_num_range_scale, self.own_tax_num_range_score_scale)
  444. return score
  445. # 被执行人数量
  446. def zhixinginfo_num_range(self, value):
  447. try:
  448. self.zhixinginfo_num_range_scale
  449. except:
  450. self.zhixinginfo_num_range_scale = [-1, 0, 1, 5, 20, 50, self.pinf]
  451. self.zhixinginfo_num_range_score_scale = [85, 90, 70, 55, 40,25]
  452. score = self.compute_score(value, self.zhixinginfo_num_range_scale, self.zhixinginfo_num_range_score_scale)
  453. return score
  454. # 计算得分函数
  455. def compute_score(self,value,range_scale,score_scale):
  456. '''
  457. :param value: 指标值
  458. :param range_scale: 指标范围刻度
  459. :param score_scale: 指标评分刻度
  460. :return: score 得分
  461. '''
  462. assert len(range_scale) - 1 == len(score_scale)
  463. assert value >= range_scale[0]
  464. i = len(range_scale) - 2
  465. j = len(score_scale) - 1
  466. while i >= 0:
  467. if value >= range_scale[i]:
  468. if i == j:
  469. score = score_scale[i]
  470. elif value == range_scale[i]:
  471. score = score_scale[i]
  472. else:
  473. score_1 = score_scale[i]
  474. score_2 = score_scale[i+1]
  475. range_1 = range_scale[i]
  476. range_2 = range_scale[i+1]
  477. b_log = np.log2(1 + (value-range_1) / (range_2-range_1))
  478. score = score_1 + (score_2 - score_1) * b_log
  479. break
  480. i -= 1
  481. return score
  482. class AHP:
  483. def __init__(self, array):
  484. ## 记录矩阵相关信息
  485. self.array = array
  486. ## 记录矩阵大小
  487. self.n = array.shape[0]
  488. # 初始化RI值,用于一致性检验
  489. 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,
  490. 1.59]
  491. # 矩阵的特征值和特征向量
  492. self.eig_val, self.eig_vector = np.linalg.eig(self.array)
  493. # 矩阵的最大特征值
  494. self.max_eig_val = np.max(self.eig_val)
  495. # 矩阵最大特征值对应的特征向量
  496. self.max_eig_vector = self.eig_vector[:, np.argmax(self.eig_val)].real
  497. # 矩阵的一致性指标CI
  498. self.CI_val = (self.max_eig_val - self.n) / (self.n - 1)
  499. # 矩阵的一致性比例CR
  500. self.CR_val = self.CI_val / (self.RI_list[self.n - 1])
  501. """
  502. 一致性判断
  503. """
  504. def test_consist(self):
  505. # 打印矩阵的一致性指标CI和一致性比例CR
  506. print("判断矩阵的CI值为:" + str(self.CI_val))
  507. print("判断矩阵的CR值为:" + str(self.CR_val))
  508. # 进行一致性检验判断
  509. if self.n == 2: # 当只有两个子因素的情况
  510. print("仅包含两个子因素,不存在一致性问题")
  511. else:
  512. if self.CR_val < 0.1: # CR值小于0.1,可以通过一致性检验
  513. print("判断矩阵的CR值为" + str(self.CR_val) + ",通过一致性检验")
  514. return True
  515. else: # CR值大于0.1, 一致性检验不通过
  516. print("判断矩阵的CR值为" + str(self.CR_val) + "未通过一致性检验")
  517. return False
  518. """
  519. 算术平均法求权重
  520. """
  521. def cal_weight_by_arithmetic_method(self):
  522. # 求矩阵的每列的和
  523. col_sum = np.sum(self.array, axis=0)
  524. # 将判断矩阵按照列归一化
  525. array_normed = self.array / col_sum
  526. # 计算权重向量
  527. array_weight = np.sum(array_normed, axis=1) / self.n
  528. # 打印权重向量
  529. print("算术平均法计算得到的权重向量为:\n", array_weight)
  530. # 返回权重向量的值
  531. return array_weight
  532. """
  533. 几何平均法求权重
  534. """
  535. def cal_weight__by_geometric_method(self):
  536. # 求矩阵的每列的积
  537. col_product = np.product(self.array, axis=0)
  538. # 将得到的积向量的每个分量进行开n次方
  539. array_power = np.power(col_product, 1 / self.n)
  540. # 将列向量归一化
  541. array_weight = array_power / np.sum(array_power)
  542. # 打印权重向量
  543. print("几何平均法计算得到的权重向量为:\n", array_weight)
  544. # 返回权重向量的值
  545. return array_weight
  546. """
  547. 特征值法求权重
  548. """
  549. def cal_weight__by_eigenvalue_method(self):
  550. # 将矩阵最大特征值对应的特征向量进行归一化处理就得到了权重
  551. array_weight = self.max_eig_vector / np.sum(self.max_eig_vector)
  552. # 打印权重向量
  553. print("特征值法计算得到的权重向量为:\n", array_weight)
  554. # 返回权重向量的值
  555. return array_weight
  556. if __name__ == '__main__':
  557. credit_card = Credit_Card()
  558. credit_card.set_data(dict())
  559. print("信用得分",credit_card.get_score())
  560. pass