「二分类算法」提供银行精准营销解决方案

「二分类算法」提供银行精准营销解决方案

问题描述

本练习赛的数据,选自UCI机器学习库中的「银行营销数据集(Bank Marketing Data Set)」
这些数据与葡萄牙银行机构的营销活动相关。这些营销活动以电话为基础,一般,银行的客服人员需要联系客户至少一次,以此确认客户是否将认购该银行的产品(定期存款)。
因此,与该数据集对应的任务是「分类任务」,「分类目标」是预测客户是(' 1 ')或者否(' 0 ')购买该银行的产品。

分类方法常用的评估模型好坏的方法-F-score

1、导包导数据

import pandas as pd
import numpy as np
import matplotlib.pylab as plt
import seaborn as sns
from sklearn.preprocessing import StandardScaler,scale
import warnings
warnings.filterwarnings('ignore')
train_df = pd.read_csv('../../Downloads/train_set.csv')
train_df.head()
train_df.info()

RangeIndex: 25317 entries, 0 to 25316
Data columns (total 18 columns):
ID           25317 non-null int64
age          25317 non-null int64
job          25317 non-null object
marital      25317 non-null object
education    25317 non-null object
default      25317 non-null object
balance      25317 non-null int64
housing      25317 non-null object
loan         25317 non-null object
contact      25317 non-null object
day          25317 non-null int64
month        25317 non-null object
duration     25317 non-null int64
campaign     25317 non-null int64
pdays        25317 non-null int64
previous     25317 non-null int64
poutcome     25317 non-null object
y            25317 non-null int64
dtypes: int64(9), object(9)
memory usage: 3.5+ MB

train_df.describe()
test_df = pd.read_csv('../../Downloads/test_set.csv')
test_df.head()

2、数据预处理及特征抽提

logics_feature=train_df.loc[:,['job','marital','education','default','housing','loan','contact','poutcome']]
logics_features=logics_feature.apply(lambda x : x.astype('category').cat.codes)

numberic_feature=pd.DataFrame(train_df.select_dtypes(exclude='object').iloc[:,1:-1])
                              
numberic_feature.head()
std=StandardScaler()
numberic_features=std.fit_transform(numberic_feature)
df_numberic_features=pd.DataFrame(numberic_features)
df_numberic_features.columns=numberic_feature.columns
df=pd.concat([logics_features,df_numberic_features],axis=1)

3、模型构建与训练

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
X_train,X_test,y_train,y_test=train_test_split(df,train_df.iloc[:,-1],test_size=0.3)
LGmodel=LogisticRegression(fit_intercept=False)
LGmodel.fit(X_train,y_train)
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=False,
          intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,
          penalty='l2', random_state=None, solver='liblinear', tol=0.0001,
          verbose=0, warm_start=False)
LGmodel.score(X_train,y_train),LGmodel.score(X_test,y_test)
(0.8905253653857006, 0.8898104265402843)

from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import roc_auc_score,accuracy_score,fbeta_score,classification_report, r2_score, mean_absolute_error

用简单的模型观察强特征

clf = RandomForestClassifier()
clf.fit(X_train,y_train)
RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
            max_depth=None, max_features='auto', max_leaf_nodes=None,
            min_impurity_decrease=0.0, min_impurity_split=None,
            min_samples_leaf=1, min_samples_split=2,
            min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=1,
            oob_score=False, random_state=None, verbose=0,
            warm_start=False)
pred = clf.predict(X_test)
results={}
results['acc_score']=accuracy_score(y_test,pred)
results['f_score'] = fbeta_score(y_test,pred,beta=0.5)
print(results)
print('\n',classification_report(y_test,pred))
{'acc_score': 0.8967877830437072, 'f_score': 0.5059055118110236}

              precision    recall  f1-score   support

          0       0.91      0.98      0.94      6712
          1       0.62      0.29      0.40       884

avg / total       0.88      0.90      0.88      7596
# Return the feature importances (the higher, the more important the feature.
clf.feature_importances_

array([0.05565159, 0.02233854, 0.03332797, 0.00180719, 0.02731953,
       0.01078673, 0.02080074, 0.04506919, 0.11666299, 0.12840883,
       0.10369968, 0.29536108, 0.04487155, 0.06885117, 0.0250432 ])

随机森林调参

def plot_est_score(Range):
    score_list = pd.DataFrame({}, index=np.arange(
        Range.shape[0]+1), columns=[["train_score", "test_score"]])
    for i in Range:
        rfc = RandomForestClassifier(n_estimators=i,
                                     n_jobs=-1,
                                     max_depth=14,
                                     random_state=90)
        pred = rfc.fit(X_train, y_train).predict(X_test)
        ascore = accuracy_score(y_test, pred)
        fscore = fbeta_score(y_test, pred, 0.5)
        score_list.loc[i-1, "train_score"] = ascore
        score_list.loc[i-1, "test_score"] = fscore
    score_list.dropna(inplace=True)
    score_max = score_list.max()
    score_max_index = score_list[score_list == score_list.max()].dropna().index[0]
    print(
        "n_estimators={}\nmax =\n{}".format(
            score_max_index,
            score_max))
    score_list.plot(figsize=(16, 4))
plot_est_score(np.arange(50,70))
n_estimators=63
max =
train_score    0.906003
test_score     0.575734
dtype: float64
def plot_depth_score(Range):
    score_list = pd.DataFrame({}, columns=["train_score", "test_score"])
    for i in Range:
        rfc = RandomForestClassifier(n_estimators=64,
                                     n_jobs=-1,
                                     max_depth=i,
                                     random_state=90)
        pred = rfc.fit(X_train, y_train).predict(X_test)
        ascore = accuracy_score(y_test, pred)
        fscore = fbeta_score(y_test, pred, 0.5)
        score_list.loc[i-1, "train_score"] = ascore
        score_list.loc[i-1, "test_score"] = fscore
    score_list.dropna(inplace=True)
    score_max = score_list.max()
    score_max_index = score_list[score_list==score_list.max()]
    print(
        "n_estimators={}\nmax =\n{}".format(
            score_max_index,
            score_max))
    score_list.plot(figsize=(16, 4))

plot_depth_score(np.arange(10, 24))

n_estimators=   train_score test_score
9          NaN        NaN
10         NaN        NaN
11         NaN        NaN
12         NaN        NaN
13    0.906003        NaN
14         NaN        NaN
15         NaN        NaN
16         NaN   0.577762
17         NaN        NaN
18         NaN        NaN
19         NaN        NaN
20         NaN        NaN
21         NaN        NaN
22         NaN        NaN
max =
train_score    0.906003
test_score     0.577762
dtype: float64
def plot_rs_score(Range):
    score_list = pd.DataFrame({}, index=np.arange(
        Range.shape[0]+1), columns=[["train_score", "test_score"]])
    for i in Range:
        rfc = RandomForestClassifier(n_estimators=64,
                                     n_jobs=-1,
                                     max_depth=17,
                                     random_state=i)
        pred = rfc.fit(X_train, y_train).predict(X_test)
        ascore = accuracy_score(y_test, pred)
        fscore = fbeta_score(y_test, pred, 0.5)
        score_list.loc[i-1, "train_score"] = ascore
        score_list.loc[i-1, "test_score"] = fscore
    score_list.dropna(inplace=True)
    score_max = score_list.max()
    score_max_index = score_list[score_list == score_list.max()].dropna().index[0]
    print(
        "n_random_state={}\nmax =\n{}".format(
            score_max_index,
            score_max))
    score_list.plot(figsize=(16, 4))

plot_rs_score(np.arange(70, 90, 1))

n_random_state=77
max =
train_score    0.906793
test_score     0.582707
dtype: float64

def plot_leaf_score(Range):
    score_list = pd.DataFrame({}, index=np.arange(
        Range.shape[0]+1), columns=[["train_score", "test_score"]])
    for i in Range:
        rfc = RandomForestClassifier(n_estimators=64,
                                     n_jobs=-1,
                                     max_depth=17,
                                     random_state=78,
                                    min_samples_leaf=i)
        pred = rfc.fit(X_train, y_train).predict(X_test)
        ascore = accuracy_score(y_test, pred)
        fscore = fbeta_score(y_test, pred, 0.5)
        score_list.loc[i-1, "train_score"] = ascore
        score_list.loc[i-1, "test_score"] = fscore
    score_list.dropna(inplace=True)
    score_max = score_list.max()
    score_max_index = score_list[score_list == score_list.max()].dropna().index[0]
    print(
        "n_leaf={}\nmax =\n{}".format(
            score_max_index,
            score_max))
    score_list.plot(figsize=(16, 4))
plot_leaf_score(np.arange(1,200,10))
n_leaf=0
max =
train_score    0.906793
test_score     0.582707
dtype: float64
make_scorer

Make a scorer from a performance metric or loss function.

This factory function wraps scoring functions for use in GridSearchCV
and cross_val_score. It takes a score function, such as accuracy_score,
mean_squared_error, adjusted_rand_index or average_precision
and returns a callable that scores an estimator's output.
从性能指标或损失函数中进行记分。
此工厂函数包装用于GridSearchCV的评分函数
并交叉得分。它有一个记分函数,比如“精确记分”,
均方误差````调整后的兰德指数``或``平均精度``
并返回一个可调用的值,该值对估计器的输出进行评分。

from sklearn.metrics import make_scorer
from sklearn.model_selection import GridSearchCV
clf = RandomForestClassifier(min_samples_leaf=1, random_state=77)
parameters = {'max_depth':np.arange(16, 18), 'n_estimators':np.arange(62, 66)}
scorer = make_scorer(fbeta_score, beta=0.5)
grid_obj = GridSearchCV(clf, parameters, scoring=scorer)

# 训练数据拟合网格搜索对象并找到最佳参数
grid_obj = grid_obj.fit(X_train, y_train)
# 得到estimator
best_clf =grid_obj.best_estimator_

# 使用没有调优的模型做预测
predictions = (clf.fit(X_train, y_train)).predict(X_test)
best_predictions = best_clf.predict(X_test)

# 汇报调优后的模型
print ("best_clf\n------")
print (best_clf)

# 汇报调参前和调参后的分数
print ("\nUnoptimized model\n------")
print ("Accuracy score on validation data: {:.4f}".format(accuracy_score(y_test, predictions)))
print ("F-score on validation data: {:.4f}".format(fbeta_score(y_test, predictions, beta = 0.5)))
print ("\nOptimized Model\n------")
print ("Final accuracy score on the validation data: {:.4f}".format(accuracy_score(y_test, best_predictions)))
print ("Final F-score on the validation data: {:.4f}".format(fbeta_score(y_test, best_predictions, beta = 0.5)))
best_clf
------
RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
            max_depth=17, max_features='auto', max_leaf_nodes=None,
            min_impurity_decrease=0.0, min_impurity_split=None,
            min_samples_leaf=1, min_samples_split=2,
            min_weight_fraction_leaf=0.0, n_estimators=64, n_jobs=1,
            oob_score=False, random_state=77, verbose=0, warm_start=False)

Unoptimized model
------
Accuracy score on validation data: 0.8968
F-score on validation data: 0.5044

Optimized Model
------
Final accuracy score on the validation data: 0.9059
Final F-score on the validation data: 0.5752
best_clf.score(X_train, y_train), best_clf.score(X_test, y_test)
train: 0.9848766999604989
test: 0.9058715113217483

结论

  • 1、最后一次联系的交流时长,每年账户的平均余额,客户年龄,最后一次联系的时间特征是客户是否会订购定期存款业务的最重要因素。
  • 2、本次建模分别尝试并进行了逻辑回归和优化随机森林,来预测客户是否订购业务,最终模型训练集准确率可以达到0.98,但测试集的准确率最高徘徊在 0.91左右。综合来看,再特征工程上还有提升空间。
  • 3、数据的建模在于对业务的理解,挖掘数据中的特征,这样才能让模型更加健壮。
    本文方法主要参照和鲸 nmg的方法:https://www.kesci.com/home/project/5d0d81fe38dc33002bbb84b6

你可能感兴趣的:(「二分类算法」提供银行精准营销解决方案)