银行风控模型
作者:互联网
一、决策树
代码如下:
# -*- coding: utf-8 -*- """ Created on Sun Mar 27 00:01:20 2022 @author: dd """ import pandas as pd # 参数初始化 filename ='D:/ISS/anaconda/bankloan.xls' data = pd.read_excel(filename) # 导入数据 x = data.iloc[:,:8].astype(int) y = data.iloc[:,8].astype(int) from sklearn.tree import DecisionTreeClassifier as DTC dtc = DTC(criterion='entropy') # 建立决策树模型,基于信息熵 dtc.fit(x, y) # 训练模型 # 导入相关函数,可视化决策树。 # 导出的结果是一个dot文件,需要安装Graphviz才能将它转换为pdf或png等格式。 from sklearn.tree import export_graphviz x = pd.DataFrame(x) """ string1 = ''' edge [fontname="NSimSun"]; node [ fontname="NSimSun" size="15,15"]; { ''' string2 = '}' """ with open("tree.dot", 'w') as f: export_graphviz(dtc, feature_names = x.columns, out_file = f) f.close() from IPython.display import Image from sklearn import tree import pydotplus dot_data = tree.export_graphviz(dtc, out_file=None, #regr_1 是对应分类器 feature_names=data.columns[:8], #对应特征的名字 class_names=data.columns[8], #对应类别的名字 filled=True, rounded=True, special_characters=True) dot_data = dot_data.replace('helvetica 14', 'MicrosoftYaHei 14') #修改字体 graph = pydotplus.graph_from_dot_data(dot_data) graph.write_png('D:/ISS/anaconda/tmp/banktree.png') #保存图像 Image(graph.create_png()) import matplotlib.pyplot as plt img = plt.imread('D:/ISS/anaconda/tmp/banktree.png') fig = plt.figure('show picture') plt.imshow(img)
结果如下:
二、神经网络
代码如下:
结果如下:
标签:tree,模型,银行,风控,png,graph,import,data,dot 来源: https://www.cnblogs.com/deng11/p/16062653.html