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

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

问题描述

本练习赛的数据,选自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()
<class 'pandas.core.frame.DataFrame'>
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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,776评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,527评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,361评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,430评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,511评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,544评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,561评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,315评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,763评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,070评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,235评论 1 343
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,911评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,554评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,173评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,424评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,106评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,103评论 2 352