使用分类算法对银行电话营销的结果进行预测
时间: 2023-11-24 22:05:28 浏览: 94
银行电话营销的结果预测是一个经典的分类问题,可以使用各种分类算法来解决。在这里,我将介绍两种常用的分类算法:逻辑回归和随机森林。
逻辑回归模型:
```python
# 导入需要的库
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# 读取数据
data = pd.read_csv('bank.csv', sep=';')
# 特征工程
data['education'] = data['education'].replace(['basic.4y', 'basic.6y', 'basic.9y'], 'basic')
data['education'] = data['education'].replace(['high.school', 'university.degree'], ['high', 'university'])
# 特征选择
X = data[['age', 'job', 'education', 'default', 'housing', 'loan', 'contact', 'month', 'day_of_week', 'duration', 'campaign', 'pdays', 'previous', 'poutcome']]
y = data['y']
# 将分类变量转换为数值变量
X = pd.get_dummies(X)
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
# 建立逻辑回归模型
clf = LogisticRegression()
clf.fit(X_train, y_train)
# 预测测试集
y_pred = clf.predict(X_test)
# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
print('逻辑回归模型准确率:', accuracy)
```
随机森林模型:
```python
# 导入需要的库
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
# 读取数据
data = pd.read_csv('bank.csv', sep=';')
# 特征工程
data['education'] = data['education'].replace(['basic.4y', 'basic.6y', 'basic.9y'], 'basic')
data['education'] = data['education'].replace(['high.school', 'university.degree'], ['high', 'university'])
# 特征选择
X = data[['age', 'job', 'education', 'default', 'housing', 'loan', 'contact', 'month', 'day_of_week', 'duration', 'campaign', 'pdays', 'previous', 'poutcome']]
y = data['y']
# 将分类变量转换为数值变量
X = pd.get_dummies(X)
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
# 建立随机森林模型
clf = RandomForestClassifier(n_estimators=100, random_state=0)
clf.fit(X_train, y_train)
# 预测测试集
y_pred = clf.predict(X_test)
# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
print('随机森林模型准确率:', accuracy)
```
以上两个模型都使用了相同的数据预处理和特征选择方法,但是使用了不同的分类算法。逻辑回归模型是一种简单有效的分类算法,而随机森林模型则是基于决策树的集成学习算法,可以处理高维度、复杂的数据集。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)