# -*- coding: utf-8 -*-
"""
Created on Sun Mar 27 19:33:58 2022
@author: 86183
"""
import pandas as pd
import time
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier as DTC
from sklearn.ensemble import RandomForestClassifier as RFC
from sklearn import svm
from sklearn import tree
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.metrics import roc_curve, auc
from sklearn.neighbors import KNeighborsClassifier as KNN
#导入plot_roc_curve,roc_curve和roc_auc_score模块
from sklearn.metrics import plot_roc_curve,roc_curve,auc,roc_auc_score
filePath = 'E:/桌面/作业\py/bankloan.xls'
data = pd.read_excel(filePath)
x = data.iloc[:,:8]
y = data.iloc[:,8]
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=100)
#模型
svm_clf = svm.SVC()#支持向量机
dtc_clf = DTC(criterion='entropy')#决策树
rfc_clf = RFC(n_estimators=10)#随机森林
knn_clf = KNN()#K邻近
#训练
knn_clf.fit(x_train,y_train)
rfc_clf.fit(x_train,y_train)
dtc_clf.fit(x_train,y_train)
svm_clf.fit(x_train, y_train)
#ROC曲线比较
fig,ax = plt.subplots(figsize=(12,10))
rfc_roc = plot_roc_curve(estimator=rfc_clf, X=x,
y=y, ax=ax, linewidth=1)
svm_roc = plot_roc_curve(estimator=svm_clf, X=x,
y=y, ax=ax, linewidth=1)
dtc_roc = plot_roc_curve(estimator=dtc_clf, X=x,
y=y, ax=ax, linewidth=1)
knn_roc = plot_roc_curve(estimator=knn_clf, X=x,
y=y, ax=ax, linewidth=1)
ax.legend(fontsize=12)
plt.show()
#模型评价
rfc_yp = rfc_clf.predict(x)
rfc_score = accuracy_score(y, rfc_yp)
svm_yp = svm_clf.predict(x)
svm_score = accuracy_score(y, svm_yp)
dtc_yp = dtc_clf.predict(x)
dtc_score = accuracy_score(y, dtc_yp)
knn_yp = knn_clf.predict(x)
knn_score = accuracy_score(y, knn_yp)
score = {"随机森林得分":rfc_score,"支持向量机得分":svm_score,"决策树得分":dtc_score,"K邻近得分":knn_score}
score = sorted(score.items(),key = lambda score:score[0],reverse=True)
print(pd.DataFrame(score))
#中文标签、负号正常显示
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
#绘制混淆矩阵
figure = plt.subplots(figsize=(12,10))
plt.subplot(2,2,1)
plt.title('随机森林')
rfc_cm = confusion_matrix(y, rfc_yp)
heatmap = sns.heatmap(rfc_cm, annot=True, fmt='d')
heatmap.yaxis.set_ticklabels(heatmap.yaxis.get_ticklabels(), rotation=0, ha='right')
heatmap.xaxis.set_ticklabels(heatmap.xaxis.get_ticklabels(), rotation=45, ha='right')
plt.ylabel("true label")
plt.xlabel("predict label")
plt.subplot(2,2,2)
plt.title('支持向量机')
svm_cm = confusion_matrix(y, svm_yp)
heatmap = sns.heatmap(svm_cm, annot=True, fmt='d')
heatmap.yaxis.set_ticklabels(heatmap.yaxis.get_ticklabels(), rotation=0, ha='right')
heatmap.xaxis.set_ticklabels(heatmap.xaxis.get_ticklabels(), rotation=45, ha='right')
plt.ylabel("true label")
plt.xlabel("predict label")
plt.subplot(2,2,3)
plt.title('决策树')
dtc_cm = confusion_matrix(y, dtc_yp)
heatmap = sns.heatmap(dtc_cm, annot=True, fmt='d')
heatmap.yaxis.set_ticklabels(heatmap.yaxis.get_ticklabels(), rotation=0, ha='right')
heatmap.xaxis.set_ticklabels(heatmap.xaxis.get_ticklabels(), rotation=45, ha='right')
plt.ylabel("true label")
plt.xlabel("predict label")
plt.subplot(2,2,4)
plt.title('K邻近')
knn_cm = confusion_matrix(y, knn_yp)
heatmap = sns.heatmap(knn_cm, annot=True, fmt='d')
heatmap.yaxis.set_ticklabels(heatmap.yaxis.get_ticklabels(), rotation=0, ha='right')
heatmap.xaxis.set_ticklabels(heatmap.xaxis.get_ticklabels(), rotation=45, ha='right')
plt.ylabel("true label")
plt.xlabel("predict label")
plt.show()