模型一:神经网络
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
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)
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
datafile = './data5/data/bankloan.xls'
data = pd.read_excel(datafile)
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)
from keras.models import Sequential
from keras.layers import Dense,Dropout
from keras.metrics import BinaryAccuracy
import time
start_time = time.time()
model = Sequential()
model.add(Dense(input_dim=8,units=800,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(input_dim=800,units=400,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(input_dim=400,units=1,activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam',metrics=[BinaryAccuracy()])
model.fit(x_train,y_train,epochs=100,batch_size=128)
loss,binary_accuracy = model.evaluate(x,y,batch_size=128)
end_time = time.time()
run_time = end_time-start_time
print('模型运行时间:{}'.format(run_time))
print('神经网络准确率:{}'.format(binary_accuracy))
yp = model.predict(x).reshape(len(y))
yp = np.around(yp,0).astype(int)
from cm_plot import *
cm_plot(y,yp).show()
模型二:支持向量机(SVM)
from sklearn import svm
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
import time
start_time = time.time()
data_load = "./data5/data/bankloan.xls"
data = pd.read_excel(data_load)
data.describe()
data.columns
data.index
X = np.array(data.iloc[:,0:-1])
y = np.array(data.iloc[:,-1])
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1, train_size=0.8, test_size=0.2, shuffle=True)
svm = svm.SVC()
svm.fit(X_test,y_test)
y_pred = svm.predict(X_test)
accuracy_score(y_test, y_pred)
end_time = time.time()
run_time = end_time-start_time
print('模型运行时间:{}'.format(run_time))
print("SVM支持向量机准确率:{}".format(accuracy_score(y_test, y_pred)))
cm = confusion_matrix(y_test, y_pred)
heatmap = sns.heatmap(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.title('SVM支持向量机')
plt.show()
模型三:决策树
import pandas as pd
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.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
import time
start_time = time.time()
filePath = './data5/data/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)
dtc_clf = DTC(criterion='entropy')
dtc_clf.fit(x_train,y_train)
dtc_yp = dtc_clf.predict(x)
dtc_score = accuracy_score(y, dtc_yp)
end_time = time.time()
run_time = end_time-start_time
print('模型运行时间:{}'.format(run_time))
print("决策树准确率:{}".format(dtc_score))
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
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.show()
dtc = DTC(criterion='entropy')
dtc.fit(x, y)
x = pd.DataFrame(x)
with open("./data5/data/tree.dot", 'w') as f:
export_graphviz(dtc, feature_names = x.columns, out_file = f)
f.close()
dot_data = tree.export_graphviz(dtc, out_file=None,
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('./data5/data/决策树.png')
Image(graph.create_png())
总结
通过模型的准确率可得模型效果优劣排行:决策树>神经网络>SVM。
|