行销(Marketing)里用决策树来做转换率 (Conversion Rate)预测
我们在01_行销(Marketing)里的有用的KPI-转换率 (Conversion Rate) 文章中介绍了什么是转换率。在这篇里我还是用银行的数据来演示怎么用决策树来做转换率预测。从而可以帮我们很好地看看到底哪些因素导致了顾客的转换。逻辑回归模型通过找到最佳估计事件发生对数几率的特征变量的线性组合来从数据中学习。顾名思义,决策树是通过生长一棵树来从数据中学习的。在下一节中,我们将讨论决策树模型如何增长以及如何构建树,但是逻辑回归和决策树模型之间的主要区别在于,逻辑回归算法会在决策树中搜索单个最佳线性边界。特征集,而决策树算法则对数据进行分区,以查找发生事件的可能性很高的数据子组。
对于决策树,我们一般有两个指标来做树的分枝。基尼杂质 (Gini impurity)和熵信息增益 (Entropy information gain)。简而言之,Gini杂质测量的是分区的不纯,熵信息增益的测量是通过使用测试的标准将数据分割得到的信息量。
在这篇里,我会继续用Kaggle的数据来演示怎么用决策树做预测分类模型。数据来源于 bank-full.csv。
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory
import os
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All"
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
/kaggle/input/bank-marketing-dataset/bank-full.csv
Load the packages
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import tree
import graphviz
%matplotlib inline
Load the data
df = pd.read_csv('../input/bank-marketing-dataset/bank-full.csv', sep=",")
df.head(3)
age | job | marital | education | default | balance | housing | loan | contact | day | month | duration | campaign | pdays | previous | poutcome | y | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 58 | management | married | tertiary | no | 2143 | yes | no | unknown | 5 | may | 261 | 1 | -1 | 0 | unknown | no |
1 | 44 | technician | single | secondary | no | 29 | yes | no | unknown | 5 | may | 151 | 1 | -1 | 0 | unknown | no |
2 | 33 | entrepreneur | married | secondary | no | 2 | yes | yes | unknown | 5 | may | 76 | 1 | -1 | 0 | unknown | no |
df['conversion'] = df['y'].apply(lambda x: 0 if x == 'no' else 1)
Data Analysis
Conversion Rate
conversion_rate_df = pd.DataFrame(
df.groupby('conversion').count()['y'] / df.shape[0] * 100.0
)
conversion_rate_df.T
conversion | 0 | 1 |
---|---|---|
y | 88.30152 | 11.69848 |
Conversion Rates by Marital Status
conversion_rate_by_marital = df.groupby(
by='marital'
)['conversion'].sum() / df.groupby(
by='marital'
)['conversion'].count() * 100.0