sklearn红酒数据集分类器的构建和评估

实验目的: 1. 掌握sklearn科学数据包中决策树和神经网络分类器的构建 2. 掌握对不同分类器进行综合评估

实验数据: 红酒数据集

红酒数据集利用红酒的化学特征来描述三种不同类型的葡萄酒。

实验内容与要求:

  1. 解压文件得到wine数据。利用pandas的read excel方法读取数据,注意保留数据的每一行数据。读取结束打印前两行
  2. 正确划分特征值矩阵X和分类目标向量y,打印数据大小(使用shape属性)
  3. 准备训练集和测试集(7:3)
  4. 利用sklearn构建神经网络分类器模型,在训练集上完成训练,观察在训练集、测试集上模型性能(如准确率、分类报告和混淆矩阵)。对参数进行调整,记录2组参数(例如不同隐藏层层数或大小设置)下的分类性能对比,讨论是否隐层数目越多越好。
  5. 准备决策树模型,在训练集上完成训练,对比决策树模型和神经网络的调试过程和训练结果,说明有何不同。

ModuleNotFoundError: No module named 'sklearn'

bash 复制代码
pip install scikit-learn

1. 读取数据

用pandas的read excel方法读取wine.xlsx的数据,注意保留数据的每一行数据,读取结束打印前两行

python 复制代码
import pandas as pd

# df = pd.read_excel('wine.xlsx', nrows=2) # 读取wine.xlsx文件的前两行
# print(df) # 打印读取到的数据(显示数据帧内容)
data = pd.read_excel('wine.xlsx') # 读取Excel文件
print(data.head(2))  # 打印前2行数据

2. 划分数据

假设数据的最后一列是目标变量(分类目标),其余列是特征。

python 复制代码
# 特征值和目标值的划分
X = data.iloc[:, :-1]  # 去掉最后一列作为特征矩阵
y = data.iloc[:, -1]   # 最后一列作为目标向量

# 打印数据的大小
print('X shape:', X.shape) # X shape: (177, 13)
print('y shape:', y.shape) # y shape: (177,)

3. 准备训练集和测试集

使用sklearn的 train_test_split 方法进行数据划分,比例为7:3。

python 复制代码
from sklearn.model_selection import train_test_split

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

print('Training set shape:', X_train.shape) # Training set shape: (123, 13)
print('Testing set shape:', X_test.shape) # Testing set shape: (54, 13)

4. 构建神经网络分类器模型

利用sklearn构建神经网络分类器模型,在训练集上完成训练,观察在训练集、测试集上模型性能(如准确率、分类报告和混淆矩阵)。对参数进行调整,记录2组参数(例如不同隐藏层层数或大小设置)下的分类性能对比,讨论是否隐层数目越多越好。

用scikit-learn的 MLPClassifier 构建神经网络分类器,并观察模型性能。

python 复制代码
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix

# 参数调整示例1
model1 = MLPClassifier(hidden_layer_sizes=(50,), max_iter=1000, random_state=42)
model1.fit(X_train, y_train)

# 在训练集上预测
y_train_pred_1 = model1.predict(X_train)
# 在测试集上预测
y_test_pred_1 = model1.predict(X_test)

# 结果评估
print('Model 1 - Training accuracy:', accuracy_score(y_train, y_train_pred_1))
print('Model 1 - Testing accuracy:', accuracy_score(y_test, y_test_pred_1))
print('Model 1 - Classification Report:\n', classification_report(y_test, y_test_pred_1))
print('Model 1 - Confusion Matrix:\n', confusion_matrix(y_test, y_test_pred_1))

# 参数调整示例2
model2 = MLPClassifier(hidden_layer_sizes=(100, 50), max_iter=1000, random_state=42)
model2.fit(X_train, y_train)

# 在训练集上预测
y_train_pred_2 = model2.predict(X_train)
# 在测试集上预测
y_test_pred_2 = model2.predict(X_test)

# 结果评估
print('Model 2 - Training accuracy:', accuracy_score(y_train, y_train_pred_2))
print('Model 2 - Testing accuracy:', accuracy_score(y_test, y_test_pred_2))
print('Model 2 - Classification Report:\n', classification_report(y_test, y_test_pred_2))
print('Model 2 - Confusion Matrix:\n', confusion_matrix(y_test, y_test_pred_2))

屏蔽FutureWarning等

python 复制代码
import warnings
warnings.filterwarnings('ignore')

运行结果:


Model 1 - Training accuracy: 0.967479674796748
Model 1 - Testing accuracy: 0.018518518518518517
Model 1 - Classification Report:
               precision    recall  f1-score   support

         278       0.00      0.00      0.00         0
         290       0.00      0.00      0.00         0
         342       0.00      0.00      0.00         1
         372       0.00      0.00      0.00         1
         378       0.00      0.00      0.00         0
         380       0.00      0.00      0.00         1
         385       0.00      0.00      0.00         1
         406       0.00      0.00      0.00         1
         407       0.00      0.00      0.00         1
         410       0.00      0.00      0.00         0
         415       0.00      0.00      0.00         0
         434       0.00      0.00      0.00         0
         438       0.00      0.00      0.00         0
         450       0.00      0.00      0.00         2
         463       0.00      0.00      0.00         0
         470       0.00      0.00      0.00         1
         480       0.00      0.00      0.00         1
         488       0.00      0.00      0.00         0
         495       0.33      1.00      0.50         1
         500       0.00      0.00      0.00         1
         502       0.00      0.00      0.00         1
         510       0.00      0.00      0.00         1
         515       0.00      0.00      0.00         2
         520       0.00      0.00      0.00         0
         550       0.00      0.00      0.00         1
         560       0.00      0.00      0.00         0
         562       0.00      0.00      0.00         2
         564       0.00      0.00      0.00         1
         570       0.00      0.00      0.00         1
         580       0.00      0.00      0.00         0
         600       0.00      0.00      0.00         1
         607       0.00      0.00      0.00         1
         620       0.00      0.00      0.00         1
         625       0.00      0.00      0.00         1
         630       0.00      0.00      0.00         1
         640       0.00      0.00      0.00         1
         660       0.00      0.00      0.00         0
         675       0.00      0.00      0.00         1
         680       0.00      0.00      0.00         2
         685       0.00      0.00      0.00         0
         695       0.00      0.00      0.00         0
         710       0.00      0.00      0.00         0
         718       0.00      0.00      0.00         1
         720       0.00      0.00      0.00         0
         735       0.00      0.00      0.00         0
         750       0.00      0.00      0.00         2
         760       0.00      0.00      0.00         1
         780       0.00      0.00      0.00         2
         795       0.00      0.00      0.00         0
         830       0.00      0.00      0.00         2
         840       0.00      0.00      0.00         0
         845       0.00      0.00      0.00         1
         880       0.00      0.00      0.00         1
         920       0.00      0.00      0.00         0
         970       0.00      0.00      0.00         1
         985       0.00      0.00      0.00         0
         990       0.00      0.00      0.00         1
        1020       0.00      0.00      0.00         0
        1035       0.00      0.00      0.00         0
        1045       0.00      0.00      0.00         0
        1065       0.00      0.00      0.00         1
        1080       0.00      0.00      0.00         0
        1095       0.00      0.00      0.00         1
        1120       0.00      0.00      0.00         0
        1130       0.00      0.00      0.00         1
        1150       0.00      0.00      0.00         1
        1190       0.00      0.00      0.00         1
        1270       0.00      0.00      0.00         1
        1280       0.00      0.00      0.00         1
        1285       0.00      0.00      0.00         2
        1320       0.00      0.00      0.00         0
        1450       0.00      0.00      0.00         0
        1480       0.00      0.00      0.00         1
        1510       0.00      0.00      0.00         1
        1515       0.00      0.00      0.00         1

    accuracy                           0.02        54
   macro avg       0.00      0.01      0.01        54
weighted avg       0.01      0.02      0.01        54

Model 1 - Confusion Matrix:
 [[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 ...
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]]
Model 2 - Training accuracy: 1.0
Model 2 - Testing accuracy: 0.05555555555555555
Model 2 - Classification Report:
               precision    recall  f1-score   support

         290       0.00      0.00      0.00         0
         325       0.00      0.00      0.00         0
         342       0.00      0.00      0.00         1
         345       0.00      0.00      0.00         0
         372       0.00      0.00      0.00         1
         378       0.00      0.00      0.00         0
         380       1.00      1.00      1.00         1
         385       0.00      0.00      0.00         1
         406       0.00      0.00      0.00         1
         407       0.00      0.00      0.00         1
         420       0.00      0.00      0.00         0
         428       0.00      0.00      0.00         0
         438       0.00      0.00      0.00         0
         450       0.00      0.00      0.00         2
         463       0.00      0.00      0.00         0
         470       0.00      0.00      0.00         1
         480       0.00      0.00      0.00         1
         495       0.50      1.00      0.67         1
         500       0.00      0.00      0.00         1
         502       0.00      0.00      0.00         1
         510       0.00      0.00      0.00         1
         515       0.00      0.00      0.00         2
         520       0.00      0.00      0.00         0
         550       0.00      0.00      0.00         1
         560       0.00      0.00      0.00         0
         562       0.00      0.00      0.00         2
         564       0.00      0.00      0.00         1
         570       0.00      0.00      0.00         1
         580       0.00      0.00      0.00         0
         600       0.00      0.00      0.00         1
         607       0.00      0.00      0.00         1
         620       0.00      0.00      0.00         1
         625       0.00      0.00      0.00         1
         630       0.00      0.00      0.00         1
         640       0.00      0.00      0.00         1
         660       0.00      0.00      0.00         0
         675       0.00      0.00      0.00         1
         680       0.00      0.00      0.00         2
         685       0.00      0.00      0.00         0
         695       0.00      0.00      0.00         0
         718       0.00      0.00      0.00         1
         720       0.00      0.00      0.00         0
         750       0.00      0.00      0.00         2
         760       0.00      0.00      0.00         1
         770       0.00      0.00      0.00         0
         780       0.00      0.00      0.00         2
         795       0.00      0.00      0.00         0
         830       0.00      0.00      0.00         2
         840       0.00      0.00      0.00         0
         845       0.00      0.00      0.00         1
         880       0.00      0.00      0.00         1
         970       0.00      0.00      0.00         1
         985       0.00      0.00      0.00         0
         990       0.00      0.00      0.00         1
        1035       0.00      0.00      0.00         0
        1050       0.00      0.00      0.00         0
        1060       0.00      0.00      0.00         0
        1065       0.00      0.00      0.00         1
        1080       0.00      0.00      0.00         0
        1095       0.00      0.00      0.00         1
        1120       0.00      0.00      0.00         0
        1130       0.00      0.00      0.00         1
        1150       1.00      1.00      1.00         1
        1190       0.00      0.00      0.00         1
        1270       0.00      0.00      0.00         1
        1280       0.00      0.00      0.00         1
        1285       0.00      0.00      0.00         2
        1295       0.00      0.00      0.00         0
        1375       0.00      0.00      0.00         0
        1450       0.00      0.00      0.00         0
        1480       0.00      0.00      0.00         1
        1510       0.00      0.00      0.00         1
        1515       0.00      0.00      0.00         1
        1547       0.00      0.00      0.00         0

    accuracy                           0.06        54
   macro avg       0.03      0.04      0.04        54
weighted avg       0.05      0.06      0.05        54

Model 2 - Confusion Matrix:
 [[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 ...
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]]

讨论:

记录了两组不同的参数下的分类性能。

  • 模型1:使用一个隐藏层,包含50个神经元。
  • 模型2:使用两个隐藏层,分别包含100和50个神经元。

通过对比可以发现:通常,更复杂的模型(更多的隐层和神经元)能够捕捉数据中的更多特征,但也可能导致过拟合。因此,在选用模型时需要进行适当的验证,观察在测试集上的表现,并合理选择模型的复杂度。

5. 构建决策树模型

准备决策树模型,在训练集上完成训练,对比决策树模型和神经网络的调试过程和训练结果,说明有何不同。

接下来,使用决策树进行分类,并对比其与神经网络的性能。

python 复制代码
from sklearn.tree import DecisionTreeClassifier

# 决策树模型
tree_model = DecisionTreeClassifier(random_state=42)
tree_model.fit(X_train, y_train)

# 在训练集上预测
y_train_pred_tree = tree_model.predict(X_train)
# 在测试集上预测
y_test_pred_tree = tree_model.predict(X_test)

# 结果评估
print('Decision Tree - Training accuracy:', accuracy_score(y_train, y_train_pred_tree))
print('Decision Tree - Testing accuracy:', accuracy_score(y_test, y_test_pred_tree))
print('Decision Tree - Classification Report:\n', classification_report(y_test, y_test_pred_tree))
print('Decision Tree - Confusion Matrix:\n', confusion_matrix(y_test, y_test_pred_tree))

运行结果:

Decision Tree - Training accuracy: 1.0
Decision Tree - Testing accuracy: 0.0
Decision Tree - Classification Report:
               precision    recall  f1-score   support

         290       0.00      0.00      0.00       0.0
         342       0.00      0.00      0.00       1.0
         352       0.00      0.00      0.00       0.0
         372       0.00      0.00      0.00       1.0
         378       0.00      0.00      0.00       0.0
         380       0.00      0.00      0.00       1.0
         385       0.00      0.00      0.00       1.0
         406       0.00      0.00      0.00       1.0
         407       0.00      0.00      0.00       1.0
         415       0.00      0.00      0.00       0.0
         428       0.00      0.00      0.00       0.0
         438       0.00      0.00      0.00       0.0
         450       0.00      0.00      0.00       2.0
         470       0.00      0.00      0.00       1.0
         480       0.00      0.00      0.00       1.0
         488       0.00      0.00      0.00       0.0
         495       0.00      0.00      0.00       1.0
         500       0.00      0.00      0.00       1.0
         502       0.00      0.00      0.00       1.0
         510       0.00      0.00      0.00       1.0
         515       0.00      0.00      0.00       2.0
         520       0.00      0.00      0.00       0.0
         550       0.00      0.00      0.00       1.0
         560       0.00      0.00      0.00       0.0
         562       0.00      0.00      0.00       2.0
         564       0.00      0.00      0.00       1.0
         570       0.00      0.00      0.00       1.0
         590       0.00      0.00      0.00       0.0
         600       0.00      0.00      0.00       1.0
         607       0.00      0.00      0.00       1.0
         615       0.00      0.00      0.00       0.0
         620       0.00      0.00      0.00       1.0
         625       0.00      0.00      0.00       1.0
         630       0.00      0.00      0.00       1.0
         640       0.00      0.00      0.00       1.0
         650       0.00      0.00      0.00       0.0
         675       0.00      0.00      0.00       1.0
         680       0.00      0.00      0.00       2.0
         695       0.00      0.00      0.00       0.0
         718       0.00      0.00      0.00       1.0
         720       0.00      0.00      0.00       0.0
         725       0.00      0.00      0.00       0.0
         740       0.00      0.00      0.00       0.0
         750       0.00      0.00      0.00       2.0
         760       0.00      0.00      0.00       1.0
         770       0.00      0.00      0.00       0.0
         780       0.00      0.00      0.00       2.0
         795       0.00      0.00      0.00       0.0
         830       0.00      0.00      0.00       2.0
         845       0.00      0.00      0.00       1.0
         870       0.00      0.00      0.00       0.0
         880       0.00      0.00      0.00       1.0
         886       0.00      0.00      0.00       0.0
         970       0.00      0.00      0.00       1.0
         985       0.00      0.00      0.00       0.0
         990       0.00      0.00      0.00       1.0
        1035       0.00      0.00      0.00       0.0
        1045       0.00      0.00      0.00       0.0
        1065       0.00      0.00      0.00       1.0
        1080       0.00      0.00      0.00       0.0
        1095       0.00      0.00      0.00       1.0
        1105       0.00      0.00      0.00       0.0
        1130       0.00      0.00      0.00       1.0
        1150       0.00      0.00      0.00       1.0
        1185       0.00      0.00      0.00       0.0
        1190       0.00      0.00      0.00       1.0
        1260       0.00      0.00      0.00       0.0
        1265       0.00      0.00      0.00       0.0
        1270       0.00      0.00      0.00       1.0
        1280       0.00      0.00      0.00       1.0
        1285       0.00      0.00      0.00       2.0
        1310       0.00      0.00      0.00       0.0
        1480       0.00      0.00      0.00       1.0
        1510       0.00      0.00      0.00       1.0
        1515       0.00      0.00      0.00       1.0
        1547       0.00      0.00      0.00       0.0

    accuracy                           0.00      54.0
   macro avg       0.00      0.00      0.00      54.0
weighted avg       0.00      0.00      0.00      54.0

Decision Tree - Confusion Matrix:
 [[0 0 0 ... 0 0 0]
 [1 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 ...
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]]

比较神经网络和决策树

  1. 训练过程:神经网络通常需要更多的训练时间和调整超参数(如学习率、隐藏层大小),而决策树参数较少,训练速度快。
  2. 性能:根据输出的准确率、分类报告和混淆矩阵,比较两个模型的表现。神经网络可能在某些复杂模式中表现更好,而决策树在处理缺失值和解释性方面更具优势。
  3. 复杂性与可解释性:神经网络更复杂,难以解释,而决策树的结果易于解释。

6. 总结

通过以上步骤,我们成功地读取了酒的数据集,训练了神经网络和决策树分类模型,并对比了它们的性能。在参数调整的过程中,我们讨论了隐藏层数量与模型性能之间的关系,并观察了不同模型在处理相同数据时的表现差异。

相关推荐
紫雾凌寒41 分钟前
计算机视觉 |解锁视频理解三剑客——TimeSformer
python·深度学习·神经网络·计算机视觉·transformer·timesformer
程序员杰哥1 小时前
测试用例详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
go54631584657 小时前
本地部署 GitHub 上的 Python 人脸识别项目
开发语言·python·github
FreakStudio8 小时前
手把手教你用 MicroPython 玩转幻尔串口舵机,代码+教程全公开
python·嵌入式·大学生·面向对象·技术栈·电子diy·电子计算机
tekin8 小时前
基于 Python 开发在线多人游戏服务器案例解析
服务器·python·游戏·在线多人游戏服务器
亲持红叶9 小时前
sklearn中的决策树-分类树:泰坦尼克号生存预测
决策树·分类·sklearn
让学习成为一种生活方式10 小时前
libGL.so.1: cannot open shared object file: No such file or directory-linux022
linux·开发语言·python
java1234_小锋10 小时前
一周学会Flask3 Python Web开发-Jinja2模板继承和include标签使用
python·flask·flask3
图书馆钉子户11 小时前
from flask_session import Session 为什么是Session(app)这么用?
python·flask·mybatis
一只蜗牛儿11 小时前
Sherpa-ONNX:说话人识别与语音识别自动开启(VAD)+ Python API 完整指南
人工智能·python·语音识别