5.31记录:mooc深度学习第一课:银行客户流失

仅用于个人记录

完整代码在最后

深度学习及其应用_中国大学MOOC(慕课) (icourse163.org)

 

# -*- coding: utf-8 -*-
"""
Created on Mon Jun 13 19:58:45 2022

@author: 316-8
"""
# 数据准备与可视化分析
import pandas as pd
import xgboost as xgb

from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
from sklearn import metrics

from sklearn.metrics import accuracy_score
from sklearn.preprocessing import StandardScaler

from sklearn.metrics import confusion_matrix
from sklearn.externals import joblib
from sklearn.metrics import accuracy_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score
from sklearn.metrics import roc_curve, precision_recall_curve, auc, make_scorer, recall_score, accuracy_score, precision_score, confusion_matrix

import matplotlib.pyplot as plt

# load data
dataset = pd.read_csv('select-data.csv', delimiter=",")
dataset.head()
#查看数据情况
dataset.info()

#数据探索与可视化分析
import seaborn as sns

with sns.plotting_context("notebook",font_scale=1.5):
    sns.set_style("whitegrid")
    sns.distplot(dataset["Age"].dropna(),
                 bins=20,
                 kde=False,
                 color="green")
    plt.ylabel("Count")
dataset["Geography"].value_counts().plot(x=None, y=None, kind='pie') 

boxplot1=sns.boxplot(x='Geography', y='Exited', data=dataset)
boxplot1.set(xlabel='Geography')

boxplot1=sns.boxplot(x='Gender', y='EstimatedSalary', data=dataset)
boxplot1.set(xlabel='Gender')

# 数据集数值分析
dataset.describe()

#total rows count
print("total rows:",dataset.shape[0])

#Detect null values
null_columns=dataset.columns[dataset.isnull().any()]
print(dataset[dataset.isnull().any(axis=1)][null_columns].count())

#去掉无用字段
dataset.drop(dataset.columns[0], inplace=True, axis=1)

dataset.head()

#查看两类标签的分类数量
dataset.Exited.value_counts()

#构建训练集
X = dataset.iloc[:,0:len(dataset.columns.tolist())-1].values
y = dataset.iloc[:,len(dataset.columns.tolist())-1].values

# split data into train and test sets
seed = 7
test_size = 0.20
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=seed)

#标准化数据(可选)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

#3.2 XGBOOST模型训练
# fit model no training data
#训练
model = XGBClassifier()
model.fit(X_train, y_train)

# 模型测试
y_pred = model.predict(X_test)

predictions = [round(value) for value in y_pred]
# evaluate predictions
accuracy = accuracy_score(y_test, predictions)
print("Accuracy: %.2f%%" % (accuracy * 100.0))

dtest_predprob = model.predict_proba(X_test)[:,1]
#XGBOOST模型可视化
from xgboost import plot_tree

fig, ax = plt.subplots(figsize=(20, 16))
plot_tree(model, num_trees=0, rankdir='LR',ax=ax)
plt.show()
# 模型重要特征分析
from matplotlib import pyplot
# plot
pyplot.bar(range(len(model.feature_importances_)), model.feature_importances_)
pyplot.show()

#3.3 逻辑回归算法
#对比逻辑回归算法
from sklearn import model_selection
from sklearn.linear_model import LogisticRegression
model_lg = LogisticRegression()
model_lg.fit(X_train, y_train)
# 模型测试
y_pred = model_lg.predict(X_test)
print(accuracy_score(y_test, y_pred))

#3.4 SVM算法
#对比SVM算法
import sklearn.svm
import sklearn.metrics
from matplotlib import pyplot as plt

clf_svm = sklearn.svm.LinearSVC().fit(X_train, y_train)
decision_values = clf_svm.decision_function(X_train)

precision, recall, thresholds = sklearn.metrics.precision_recall_curve(y_train, decision_values)

plt.plot(precision,recall)
plt.legend(loc="lower right")
plt.show()
# 模型测试
y_pred_svm = clf_svm.predict(X_test)
print(pd.crosstab(y_test, y_pred_svm, rownames=['Actual'], colnames=['Predicted']))
print(accuracy_score(y_test, y_pred_svm))
print(recall_score(y_test, y_pred_svm))

print(sklearn.metrics.roc_auc_score(y_test, y_pred_svm))
print(sklearn.metrics.f1_score(y_test, y_pred_svm))

#3.5 AdaBoost算法
#对比adaboost
from sklearn.externals.six.moves import zip
import matplotlib.pyplot as plt

from sklearn.datasets import make_gaussian_quantiles
from sklearn.ensemble import AdaBoostClassifier
from sklearn.metrics import accuracy_score
from sklearn.tree import DecisionTreeClassifier

# 模型定义
bdt_discrete = AdaBoostClassifier(
    DecisionTreeClassifier(max_depth=3),
    n_estimators=500,
    learning_rate=.5,
    algorithm="SAMME")

bdt_discrete.fit(X_train, y_train)

discrete_test_errors = []


for discrete_train_predict in bdt_discrete.staged_predict(X_test):
    discrete_test_errors.append(1. - recall_score(discrete_train_predict, y_test))

n_trees_discrete = len(bdt_discrete)

# Boosting might terminate early, but the following arrays are always
# n_estimators long. We crop them to the actual number of trees here:
discrete_estimator_errors = bdt_discrete.estimator_errors_[:n_trees_discrete]
discrete_estimator_weights = bdt_discrete.estimator_weights_[:n_trees_discrete]

plt.figure(figsize=(15, 5))

plt.subplot(131)
plt.plot(range(1, n_trees_discrete + 1),
         discrete_test_errors, c='black', label='SAMME')
plt.legend()
# plt.ylim(0.18, 0.62)
plt.ylabel('Test Error')
plt.xlabel('Number of Trees')

plt.subplot(132)
plt.plot(range(1, n_trees_discrete + 1), discrete_estimator_errors,
         "b", label='SAMME', alpha=.5)
plt.legend()
plt.ylabel('Error')
plt.xlabel('Number of Trees')
plt.ylim((.2,discrete_estimator_errors.max() * 1.2))
plt.xlim((-20, len(bdt_discrete) + 20))

plt.subplot(133)
plt.plot(range(1, n_trees_discrete + 1), discrete_estimator_weights,
         "b", label='SAMME')
plt.legend()
plt.ylabel('Weight')
plt.xlabel('Number of Trees')
plt.ylim((0, discrete_estimator_weights.max() * 1.2))
plt.xlim((-20, n_trees_discrete + 20))

# prevent overlapping y-axis labels
plt.subplots_adjust(wspace=0.25)
plt.show()

#模型测试
y_pred_adaboost = bdt_discrete.predict(X_test)
print(accuracy_score(y_test, y_pred_adaboost))
print(recall_score(y_test, y_pred_adaboost))
print(sklearn.metrics.roc_auc_score(y_test, bdt_discrete.predict_proba(X_test)[:,1]))

print(sklearn.metrics.f1_score(y_test, y_pred_adaboost))
#3.6 随机森林¶
#随机森林
from sklearn.ensemble import RandomForestClassifier

classifier_rf = RandomForestClassifier(n_estimators = 10, max_depth = 8, criterion = 'entropy',random_state = 42)
classifier_rf.fit(X_train, y_train)
# 模型测试
y_pred_rf = classifier_rf.predict(X_test)
print(pd.crosstab(y_test, y_pred_rf, rownames=['Actual Class'], colnames=['Predicted Class']))

print(accuracy_score(y_test, y_pred_rf))
print(recall_score(y_test, y_pred_rf))
print(f1_score(y_test, y_pred_rf))
print(sklearn.metrics.roc_auc_score(y_test, y_pred_rf))
#3.7 机器学习算法性能对比
from sklearn.metrics import roc_curve
from sklearn.metrics import roc_auc_score
from matplotlib import pyplot

fig, ax = plt.subplots(figsize=(10, 8))


probs_lg = model_lg.predict_proba(X_test)[:,1]
auc_lg = roc_auc_score(y_test, probs_lg)
print('Logististics AUC: %.3f' % auc_lg)
fpr_lg, tpr_lg, thresholds_lg = roc_curve(y_test, probs_lg)


probs_svm = y_pred_svm
auc_svm = roc_auc_score(y_test, probs_svm)
print('SVM AUC: %.3f' % auc_svm)
fpr_svm, tpr_svm, thresholds_svm = roc_curve(y_test, probs_svm)

probs_rf = y_pred_rf
auc_rf = roc_auc_score(y_test, probs_rf)
print('Random Forest AUC: %.3f' % auc_rf)
fpr_rf, tpr_rf, thresholds_rf = roc_curve(y_test, probs_rf)

probs_xgb = dtest_predprob
# calculate AUC
auc_xgb = roc_auc_score(y_test, probs_xgb)
print('XGBoost AUC: %.3f' % auc_xgb)
# calculate roc curve
fpr_xgb, tpr_xgb, thresholds = roc_curve(y_test, probs_xgb)

pyplot.plot([0, 1], [0, 1], linestyle='--')

# plot the roc curve for models
pyplot.plot(fpr_xgb, tpr_xgb, marker='.')
pyplot.plot(fpr_lg, tpr_lg, marker='*')
pyplot.plot(fpr_svm, tpr_svm, marker='o')
pyplot.plot(fpr_rf, tpr_rf, marker='^')
pyplot.legend(loc="best")

pyplot.show()

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
#import tensorflow as tf

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

import pandas as pd
import numpy as np
from sklearn.utils import shuffle
from sklearn.preprocessing import OneHotEncoder
import matplotlib.pyplot  as plt
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['STZhongsong']    # 指定默认字体:解决plot不能显示中文问题


'''
客户流失预测模型
owNumber:行号 ×
CustomerID:用户编号 ×
Surname:用户姓名 ×
CreditScore:信用分数
Geography:用户所在国家/地区 ×
Gender:用户性别
Age:年龄
Tenure:当了本银行多少年用户
Balance:存贷款情况
NumOfProducts:使用产品数量
HasCrCard:是否有本行信用卡
IsActiveMember:是否活跃用户
EstimatedSalary:估计收入
Exited:是否已流失,这将作为我们的标签数据
'''


df = pd.read_csv("./data/select-data.csv")
df_test = pd.read_csv("./data/scalar-test.csv")
print("构建向量.........")
# 构建向量
train = []
target = []
for i in range(0, len(df["EstimatedSalary"])):
    mid = []
    mid.append(df["Geography"][i])
    mid.append(df["Gender"][i])
    mid.append(df["EB"][i])
    mid.append(df["Age"][i])
    mid.append(df["EstimatedSalary"][i])
    mid.append(df["NumOfProducts"][i])
    mid.append(df["CreditScore"][i])
    mid.append(df["Tenure"][i])
    mid.append(df["HasCrCard"][i])
    mid.append(df["IsActiveMember"][i])
    target.append(df["Exited"][i])
    train.append(mid)
train = np.array(train)
target = np.array(target)

test = []
test_target = []

for i in range(0, len(df_test["EstimatedSalary"])):
    mid = []
    mid.append(df_test["Geography"][i])
    mid.append(df_test["Gender"][i])
    mid.append(df_test["EB"][i])
    mid.append(df_test["Age"][i])
    mid.append(df_test["EstimatedSalary"][i])
    mid.append(df_test["NumOfProducts"][i])
    mid.append(df_test["CreditScore"][i])
    mid.append(df_test["Tenure"][i])
    mid.append(df_test["HasCrCard"][i])
    mid.append(df_test["IsActiveMember"][i])
    test_target.append(df_test["Exited"][i])
    test.append(mid)
test = np.array(test)
test_target = np.array(test_target)
# train = np.trunc(train * 100)

# 随机打乱训练集与标签
train, target = shuffle(train, target)
target = target.reshape(-1, 1)
test_target = test_target.reshape(-1, 1)

# One-Hot编码
enc = OneHotEncoder()
enc.fit(test_target)
test_target = enc.transform(test_target).toarray()
enc.fit(target)
target = enc.transform(target).toarray()
enc.fit(test_target)


# 定义输入占位符
x = tf.placeholder(tf.float32, shape=(None, 10))
# # 二分类问题 [0,1]
y = tf.placeholder(tf.float32, shape=(None, 2))
keep = tf.placeholder(tf.float32)

# 定义网络结构
# layer1
var1 = tf.Variable(tf.truncated_normal([10, 256], stddev=0.1))
bias1 = tf.Variable(tf.zeros([256]))
hc1 = tf.add(tf.matmul(x, var1), bias1)
h1 = tf.sigmoid(hc1)
h1 = tf.nn.dropout(h1, keep_prob=keep)

# layer2
var2 = tf.Variable(tf.truncated_normal([256, 256], stddev=0.1))
bias2 = tf.Variable(tf.zeros([256]))
hc2 = tf.add(tf.matmul(h1, var2), bias2)
h2 = tf.sigmoid(hc2)
h2 = tf.nn.dropout(h2, keep_prob=keep)

# layer3
var3 = tf.Variable(tf.truncated_normal([256, 2], stddev=0.1))
bias3 = tf.Variable(tf.zeros([2]))
hc3 = tf.add(tf.matmul(h2, var3), bias3)
h3 = tf.nn.softmax(hc3)


# 定义损失
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=h3, labels=y))
tf.summary.scalar('loss', loss)

# 定义正确率
ac = tf.cast(tf.equal(tf.argmax(h3, 1), tf.argmax(y, 1)), tf.float32)
acc = tf.reduce_mean(ac)
tf.summary.scalar('accuracy', acc)

# 定义优化器
optimzer = tf.train.AdamOptimizer(1e-3).minimize(loss)

merge_summary = tf.summary.merge_all()

isTrain = 1

# 定义训练
print("正在训练.....")
saver = tf.train.Saver(max_to_keep=1)

with tf.Session() as sess:
    if isTrain:
        init_op = tf.global_variables_initializer()
        sess.run(init_op)
        summary_writer = tf.summary.FileWriter('./logs/', sess.graph)
        nepoch=[]
        trainacc=[]
        testacc=[]
        loss1=[]
        for i in range(0, 10001):
             
            sess.run(optimzer, feed_dict={x: train, y: target, keep: 0.5})
            train_summary = sess.run(merge_summary, feed_dict={x: train, y: target, keep: 1})
            summary_writer.add_summary(train_summary, i)
           
            if i % 50 == 0:
                accu = sess.run(acc, feed_dict={x: train, y: target, keep: 1})
                accuT = sess.run(acc, feed_dict={x: test, y: test_target, keep: 1})
                losss = sess.run(loss, feed_dict={x: train, y: target, keep: 1})
               
                print("epoch:" + str(i) + "   train_acc:" + str(accu) + "   test_acc:" + str(accuT) + "   loss:" + str(
                    losss))
      
                nepoch.append(i)
                trainacc.append(accu)
                testacc.append(accuT)
                loss1.append(losss)
           
        plt.title("BP神经网络训练性能曲线")
        plt.xlabel('训练次数')
        plt.ylabel('训练样本和检验样本的准确度')
        plt.plot(nepoch,trainacc,nepoch,testacc)
        plt.plot(nepoch,trainacc,nepoch,loss1)
        plt.show()

saver.save(sess, './model/bank.ckpt', global_step=i)
'''
    else:
        f = open("./result/NN-target.txt", "w")
        model_file = tf.train.latest_checkpoint('./NN-model/')
        saver.restore(sess, model_file)
        tar = sess.run(h3, feed_dict={x: test, y: test_target, keep: 1})
        tar = sess.run(tf.argmax(tar, 1))
        for i in range(0, len(tar)):
            f.write(str(tar[i]) + " ")
        f.close()
        print(tar)'''

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值