利用朴素贝叶斯对UCI 的 mushroom 数据集进行分类

朴素贝叶斯(Naive Bayes)是一种基于贝叶斯定理的简单而有效的分类算法,特别适合处理文本分类和多类别分类问题。UCI的Mushroom数据集是一个经典的分类数据集,包含蘑菇的特征和类别(可食用或有毒)。

1. 数据集介绍

Mushroom数据集包含22个特征,每个特征都是分类变量(非数值型)。目标是根据这些特征预测蘑菇是否可食用。

2. 数据预处理

由于朴素贝叶斯算法在sklearn中默认处理数值型数据,我们需要将分类变量转换为数值型。这可以通过LabelEncoder实现。

3. 实现步骤

  1. 加载数据集。
  2. 数据预处理(将分类变量转换为数值型)。
  3. 划分训练集和测试集。
  4. 使用朴素贝叶斯分类器进行训练。
  5. 评估模型性能。

Python代码实现:

python 复制代码
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix

# 1. 加载数据集
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/mushroom/agaricus-lepiota.data"
column_names = ["class", "cap-shape", "cap-surface", "cap-color", "bruises", "odor", "gill-attachment",
                "gill-spacing", "gill-size", "gill-color", "stalk-shape", "stalk-root", "stalk-surface-above-ring",
                "stalk-surface-below-ring", "stalk-color-above-ring", "stalk-color-below-ring", "veil-type",
                "veil-color", "ring-number", "ring-type", "spore-print-color", "population", "habitat"]
data = pd.read_csv(url, header=None, names=column_names)

# 2. 数据预处理
# 将分类变量转换为数值型
label_encoders = {}
for column in data.columns:
    le = LabelEncoder()
    data[column] = le.fit_transform(data[column])
    label_encoders[column] = le

# 3. 划分训练集和测试集
X = data.drop(columns=["class"])
y = data["class"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# 4. 使用朴素贝叶斯分类器进行训练
model = GaussianNB()
model.fit(X_train, y_train)

# 5. 评估模型性能
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}")
print("Classification Report:")
print(classification_report(y_test, y_pred))
print("Confusion Matrix:")
print(confusion_matrix(y_test, y_pred))

代码解释

  1. 加载数据集:从UCI机器学习库中加载Mushroom数据集。
  2. 数据预处理 :使用LabelEncoder将所有分类变量转换为数值型。
  3. 划分训练集和测试集 :使用train_test_split将数据集划分为训练集和测试集。
  4. 训练模型 :使用GaussianNB(高斯朴素贝叶斯)进行训练。
  5. 评估模型:计算准确率、分类报告和混淆矩阵。

输出示例

运行上述代码后,你将看到类似以下的输出:

复制代码
Accuracy: 0.99
Classification Report:
              precision    recall  f1-score   support

           0       1.00      1.00      1.00      1274
           1       1.00      1.00      1.00       702

    accuracy                           1.00      1976
   macro avg       1.00      1.00      1.00      1976
weighted avg       1.00      1.00      1.00      1976

Confusion Matrix:
[[1274    0]
 [   0  702]]

matlab 利用朴素贝叶斯对UCI 的 mushroom 数据集进行分类

注意事项

  1. 数据预处理:确保所有分类变量都被正确转换为数值型。
  2. 模型选择 :虽然这里使用了GaussianNB,但也可以尝试其他朴素贝叶斯变体,如MultinomialNB
  3. 特征选择:可以进一步分析哪些特征对分类最有帮助,可能需要进行特征选择或降维。

通过上述步骤,你可以使用朴素贝叶斯对Mushroom数据集进行分类,并评估模型的性能。

相关推荐
(・Д・)ノ4 分钟前
python打卡day31
开发语言·人工智能·python
AndrewHZ14 分钟前
【ISP算法精粹】什么是global tone mapping和local tone mapping?
人工智能·深度学习·算法·计算机视觉·视觉算法·isp算法·色调映射
pen-ai17 分钟前
【NLP】37. NLP中的众包
人工智能·自然语言处理
闭月之泪舞28 分钟前
OpenCv高阶(8.0)——答题卡识别自动判分
人工智能·opencv·计算机视觉
乐言36133 分钟前
目前主流的AI测试工具推荐
人工智能·测试工具
yingxiao8881 小时前
OpenAI与微软洽谈新融资及IPO,Instagram因TikTok流失四成用户
人工智能·microsoft
熊猫在哪1 小时前
野火鲁班猫(arrch64架构debian)从零实现用MobileFaceNet算法进行实时人脸识别(三)用yolov5-face算法实现人脸检测
人工智能·python·嵌入式硬件·神经网络·yolo·目标检测·机器学习
十步杀一人_千里不留行1 小时前
AI无法解决的Bug系列(一)跨时区日期过滤问题
人工智能·bug
2401_878624791 小时前
机器学习 KNN算法
人工智能·算法·机器学习
小oo呆2 小时前
【自然语言处理与大模型】向量数据库:Milvus使用指南
人工智能·自然语言处理·milvus