2020全国⼤学⽣数学建模C题中⼩微企业的信贷决策随机森林代码
2020全国⼤学⽣数学建模C题中⼩微企业的信贷决策(含随机森林代码)
某银⾏对确定要放贷企业的贷款额度为10~100万元;年利率为4%~15%;贷款期限为1年。附件1~3 分别给出了123家有信贷记录企业的相关数据、302家⽆信贷记录企业的
相关数据和贷款利率与客户流失率关系的2019年统计数据。我们团队需根据实际和附件中的数据信息,通过建⽴数学模型研究对中⼩微企业的信贷策略,主要帮助银⾏解决下列问
题:
(1) 对附件1中123家企业的信贷风险进⾏量化分析,给出该银⾏在年度信贷总额固定时对这些企业的信贷策略。
python代码(所使⽤的数据、及调⽤的⽂件均来⾃赛题)
⽂件(数据的预处理)处理后的⽂件⽤spss拟合
1 import pandas as pd
2 from pandas import DataFrame
3
4 #筛选⽆效发票
5 def ticket():
6 data = _excel('C:/Urs/yzj/Desktop/信贷风险值.xlsx', sheet_name="Sheet3")# 读取⽂件附件2:302家⽆信贷记录企业的相关数据
7 data = data[~data['发票状态'].isin(['有效发票'])] #删除作废发票
8
9 DataFrame(data).to_excel('C:/Urs/yzj/Desktop/信贷风险值.xlsx', index=Fal, header=True)
10
11 #读取⾦额信息
12 def money():
13 df=_excel("C:/Urs/yzj/Desktop/第⼆题数据/进项有效发票.xlsx") # 读取每⼀个公司信息
14 print(y('企业代号')[['⾦额','税额','价税合计']].sum())
15 d=y('企业代号')[['⾦额','税额','价税合计']].sum()
16 _csv("C:/Urs/yzj/Desktop/第⼆题数据/进项⾦额税额.csv",encoding='utf-8-sig',index=Fal, header=True)
17
18 #统计⽇期
19 def data():
20 df=_excel("C:/Urs/yzj/Desktop/第⼆题数据/进项有效发票.xlsx" ) # 读取每⼀个公司信息
21 d=y('企业代号')[['开票⽇期']].nunique()
22 print(y('企业代号')[['开票⽇期']].nunique())
23 #_csv("C:/Urs/yzj/Desktop/第⼆题数据/销项⽇期.csv",encoding='utf-8-sig',index=Fal, header=True)
24 _csv("C:/Urs/yzj/Desktop/第⼆题数据/进项⽇期.csv",encoding='utf-8-sig',index=Fal, header=True)
25
26 def obj():
27 df=_excel("C:/Urs/yzj/Desktop/第⼆题数据/销项有效发票.xlsx" ) # 读取每⼀个公司信息
28 #df=_excel("C:/Urs/yzj/Desktop/第⼆题数据/进项有效发票.xlsx" ) # 读取每⼀个公司信息
29 d=y('企业代号')[['购⽅单位代号']].nunique()
30 print(y('企业代号')[['购⽅单位代号']].nunique())
31 _csv("C:/Urs/yzj/Desktop/第⼆题数据/销项供应商.csv",encoding='utf-8-sig',index=Fal, header=True)
32
33 #统计发票状态数量
34 def ticket_state():
35 df=_excel("C:/Urs/yzj/Desktop/第⼆题数据/进项有效发票.xlsx" ) # 读取每⼀个公司信息
36 d=y('企业代号')['发票状态'].value_counts()
37 print(y('企业代号')['发票状态'].value_counts())
38
39 #统计发票总数
40 def ticket_count():
41 _option('_rows', 500)
42 _option('_columns', 100)
43 _option('', 1000)
44 df=_excel("C:/Urs/yzj/Desktop/第⼆题数据/销项有效发票.xlsx") # 读取每⼀个公司信息
45 #df=_excel("C:/Urs/yzj/Desktop/第⼆题数据/进项有效发票.xlsx") # 读取每⼀个公司信息
46 d=y('企业代号')['发票状态'].value_counts()
47 print(y('企业代号')['发票状态'].value_counts())
48 _csv("C:/Urs/yzj/Desktop/第⼆题数据/销项有效发票总数.csv",encoding='utf-8-sig',index=Fal, header=True)
⽂件(随机森林代码)选取某些参数最为评定标准,预测信誉值
1 #随机森林 预测信誉值ABCD违约⾏为
2 import pandas as pd
3 from alsimport joblib
4 from e_extractionimport DictVectorizer
5 from _lectionimport train_test_split
6 from leimport RandomForestClassifier
7 from _lectionimport GridSearchCV
8
9 def forest():
10 # 加载数据
11 firm= _excel("C:/Urs/yzj/Desktop/国赛/C/问题⼀总表")
12 # 构造特征值和⽬标值
13 feature = firm[["进项发票有效率", "销项发票有效率","总销⽅数","总售⽅数",”进项年平均价税总额”,”销项年平均价税总额”]]
14 target = firm["信誉评级"]
15 #target = titan["是否违约"]
16 # 特征预处理
17 # 查看有没有缺失值
18 print((feature).any())
19 # 划分数据集
20 x_train, x_test, y_train, y_test = train_test_split(feature, target, test_size=0.25)
21 print("训练集:", x_, y_)
22 print("测试集:", x_, y_)
23 # 建⽴模型
24 rf = RandomForestClassifier()
25 # 超参数搜索
26 param = {"n_estimators":range(20,100,10), "max_depth":range(1,50,5)}
27 model = GridSearchCV(rf, param_grid=param, cv=10)
28 # 训练
29 (x_train, y_train)
30 print("测试集准确率:", (x_test, y_test))
31 print("验证集准确率:", _score_)
32 print("最好模型参数:", _params_)
33 print("最好模型模型:", _estimator_)
34 #保存模型
35 (model, 'C:/Urs/yzj/Desktop/第⼆题数据/model_level_')
36 #(model, 'C:/Urs/yzj/Desktop/第⼆题数据/model_obey_')
37 def test():
38 #引⼊测试数据
39 df = _excel("C:/Urs/yzj/Desktop/第⼆题数据/问题⼆总表")
40 #数据预处理
41 X = (['是否违约'], axis=1)
42 #导⼊模型
43 clf = ("C:/Urs/yzj/Desktop/第⼆题数据/model_level_")
44 namelist = []
45 #得到预测⽬标值
46 tolist = t(X)
47 for iin tolist:
48 (i)
49 #对⽬标值列进⾏赋值
50 df['信誉评级'] = namelist
51 _csv("C:/Urs/yzj/Desktop/第⼆题数据/model_level_", encoding='utf-8')
52 print(tolist)
⽂件(⼀个词云图的代码)为了论⽂更直观、好看做的⼀个
1 import pandas as pd
2 # 导⼊扩展库
3 import re # 正则表达式库
4 import collections # 词频统计库
5 import wordcloud# 词云展⽰库
6 from PIL import Image # 图像处理库
7 import as plt# 图像展⽰库
8 import numpyas np
9 import jieba
10 import jieba
11 import re
12 import collections
13 from wordcloudimport WordCloud, STOPWORDS, ImageColorGenerator
14 from PIL import Image
15 import as plt
16 from pandas import DataFrame
17
18 corpus = _excel('C:/Urs/yzj/Desktop/国赛/C/附件2:302家⽆信贷记录企业的相关数据.xlsx') # 得到DataFrame
19 corpus = (corpus) # 转换为ndarray [[1], [2], [3]]
20 #print(corpus)
21 corpus = e(1, len(corpus)).tolist() # 转换成 List [[1, 2, 3]]
22 corpus = corpus[0] # 取第⼀个元素得到最终结果 [1, 2, 3]
23 print(corpus)
24 object_list = []
25 for iin range(len(corpus)):
26 text = corpus[i]
27 for chin text:
28 if ch<='9'and ch>='0':
29 text = e(ch, " ")
30 if ch<='Z'andch>='A':
31 text = e(ch, " ")
32
33 #remove_words = ['','公司','有限','有限公司','经营','个体','个体经营','有限责任',
34 # '责任','*','经营部','营部','设备','管理','技术','咨询','建筑⼯程','建设⼯程'] # ⾃定义去除词库
35 remove_words = ['', '公司', '有限', '经营', '个体',
36 '责任', '*', '经营部', '营部', '建筑⼯程', '建设⼯程'] # ⾃定义去除词库
37
38 #得到所有单词
39 words = (text,cut_all=True)
40 space_wordlist = ""
41
42 for word in words:
43 if word not in remove_words:
44 object_(word)
45 space_wordlist = word+space_wordlist
46
47 #统计词频
48 word_counts = r(object_list) # 对分词做词频统计
49 word_counts_top15 = word__common(15) # 获取前10最⾼频的词
50 print (word_counts_top15) # 输出检查
51
52 # 词频展⽰
53 #mask = (('C:/Urs/yzj/Desktop/')) # 定义词频背景
54 wc = oud(
55 font_path='C:/Windows/Fonts/', # 设置字体格式
56 #mask=mask, # 设置背景图
57 #max_words=88, # 最多显⽰词数
58 #max_font_size=100, # 字体最⼤值
59 background_color= 'white',
60 margin=2
61 )
62
63 te_from_frequencies(word_counts) # 从字典⽣成词云
64 (wc) # 显⽰词云
65 ('off') # 关闭坐标轴
66 g('C:/Urs/yzj/Desktop/')
67 () # 显⽰图像
本文发布于:2023-11-02 14:03:13,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/zhishi/a/88/27267.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:2020全国大学生数学建模C题中小微企业的信贷决策随机森林代码.doc
本文 PDF 下载地址:2020全国大学生数学建模C题中小微企业的信贷决策随机森林代码.pdf
留言与评论(共有 0 条评论) |