根据https://archive.ics.uci.edu/ml/datasets/Adult所给Adult数据集构建朴素贝叶斯分类器,并测试分类器的性能,输出性能指标并画出ROC曲线 python代码有注释
时间: 2023-07-19 17:16:40 浏览: 94
下面是根据Adult数据集构建朴素贝叶斯分类器,并测试分类器性能,输出性能指标并画出ROC曲线的Python代码:
```python
import pandas as pd
import numpy as np
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score, confusion_matrix, roc_curve, auc
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
# 读取数据集
data = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data', header=None, na_values='?')
# 给数据集添加列名
data.columns = ['age', 'workclass', 'fnlwgt', 'education', 'education-num', 'marital-status', 'occupation', 'relationship', 'race', 'sex', 'capital-gain', 'capital-loss', 'hours-per-week', 'native-country', 'income']
# 删除含有缺失值的行
data = data.dropna()
# 将分类变量进行编码
data['income'] = np.where(data['income'] == '>50K', 1, 0)
data['sex'] = np.where(data['sex'] == 'Male', 1, 0)
# 将连续变量进行标准化
data['age'] = (data['age'] - data['age'].mean()) / data['age'].std()
data['fnlwgt'] = (data['fnlwgt'] - data['fnlwgt'].mean()) / data['fnlwgt'].std()
data['education-num'] = (data['education-num'] - data['education-num'].mean()) / data['education-num'].std()
data['capital-gain'] = (data['capital-gain'] - data['capital-gain'].mean()) / data['capital-gain'].std()
data['capital-loss'] = (data['capital-loss'] - data['capital-loss'].mean()) / data['capital-loss'].std()
data['hours-per-week'] = (data['hours-per-week'] - data['hours-per-week'].mean()) / data['hours-per-week'].std()
# 将分类变量进行one-hot编码
data = pd.get_dummies(data, columns=['workclass', 'education', 'marital-status', 'occupation', 'relationship', 'race', 'native-country'])
# 将数据集划分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(data.drop(['income'], axis=1), data['income'], test_size=0.3, random_state=123)
# 构建朴素贝叶斯分类器
nb = GaussianNB()
nb.fit(X_train, y_train)
# 在测试集上进行预测
y_pred = nb.predict(X_test)
# 计算分类器的性能指标
accuracy = accuracy_score(y_test, y_pred)
confusion = confusion_matrix(y_test, y_pred)
fpr, tpr, thresholds = roc_curve(y_test, y_pred)
roc_auc = auc(fpr, tpr)
# 输出性能指标
print("Accuracy:", accuracy)
print("Confusion matrix:\n", confusion)
print("AUC of ROC curve:", roc_auc)
# 画出ROC曲线
plt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic')
plt.legend(loc="lower right")
plt.show()
```
代码解释:
1. 首先读取数据集,并对数据集进行预处理,包括删除含有缺失值的行、对分类变量进行编码、对连续变量进行标准化、对分类变量进行one-hot编码等。
2. 然后将数据集划分为训练集和测试集。
3. 接着构建朴素贝叶斯分类器,并在测试集上进行预测。
4. 最后计算分类器的性能指标,包括准确率、混淆矩阵、ROC曲线和AUC。
5. 画出ROC曲线,用于评估分类器的性能。
注意:本代码中使用的是高斯朴素贝叶斯分类器,适用于处理连续变量。如果数据集中有离散变量,可以考虑使用多项式朴素贝叶斯分类器。
阅读全文