建立银行分控模型
作者:互联网
cm_plot 函数代码
def cm_plot(y, yp): from sklearn.metrics import confusion_matrix #µ¼Èë»ìÏý¾ØÕóº¯Êý cm = confusion_matrix(y, yp) #»ìÏý¾ØÕó import matplotlib.pyplot as plt #µ¼Èë×÷ͼ¿â plt.matshow(cm, cmap=plt.cm.Greens) #»»ìÏý¾ØÕóͼ£¬ÅäÉ«·ç¸ñʹÓÃcm.Greens£¬¸ü¶à·ç¸ñÇë²Î¿¼¹ÙÍø¡£ plt.colorbar() #ÑÕÉ«±êÇ© for x in range(len(cm)): #Êý¾Ý±êÇ© for y in range(len(cm)): plt.annotate(cm[x,y], xy=(x, y), horizontalalignment='center', verticalalignment='center') plt.ylabel('True label') #×ø±êÖá±êÇ© plt.xlabel('Predicted label') #×ø±êÖá±êÇ© return plt
基于神经网络算法进行分析
#导入库 import pandas as pd from keras.models import Sequential from keras.layers.core import Dense, Activation from sklearn.linear_model import LogisticRegression as LR import numpy as np #读取数据 data = pd.read_excel("D:\lysim\大学\数据挖掘\data\bankloan.xls") x = data.iloc[:,:8].values y = data.iloc[:,8].values #建立模型 model = Sequential() model.add(Dense(input_dim = 8, units = 16)) model.add(Activation('relu')) #用relu函数作为激活函数,能够大幅提供准确度 model.add(Dense(input_dim = 16, units = 1)) model.add(Activation('sigmoid')) #由于是0-1输出,用sigmoid函数作为激活函数 model.compile(loss = 'mean_squared_error', optimizer = 'adam') #编译模型。由于我们做的是二元分类,所以我们指定损失函数为binary_crossentropy,以及模式为binary #另外常见的损失函数还有mean_squared_error、categorical_crossentropy等,请阅读帮助文件。 #求解方法我们指定用adam,还有sgd、rmsprop等可选 model.fit(x, y, epochs = 100, batch_size = 10) #训练模型 #损失值 score = model.evaluate(x,y,batch_size=128) #模型评估 print("分类预测损失值:",score) lr = LR() # 建立逻辑回归模型 lr.fit(x, y) # 用筛选后的特征数据来训练模型 print('模型的平均准确度为:%s' % lr.score(x, y)) #分类 yp = model.predict(x).reshape(len(y)) # 分类预测 yp = yp.astype(np.int32) #画出混淆矩阵图 print("混淆矩阵图:\n") from cm_plot import * # 导入自行编写的混淆矩阵可视化函数 cm_plot(y,yp).show() # 显示混淆矩阵可视化结果
ID3决策树算法分析
import pandas as pd # 参数初始化 filename = "D:\lysim\大学\数据挖掘\data\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) 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) graph = pydotplus.graph_from_dot_data(dot_data) graph.write_png('example.png') #保存图像 Image(graph.create_png()) import matplotlib.pyplot as plt img = plt.imread('example.png') fig = plt.figure('show picture') plt.imshow(img)
标签:plt,cm,模型,yp,银行,分控,import,model,data 来源: https://www.cnblogs.com/Lysim/p/16065527.html