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. 总结

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

相关推荐
郝学胜-神的一滴1 分钟前
Effective Python 条款35:用类封装替代生成器异常注入
开发语言·人工智能·python·金融·pandas·scikit-learn·matplotlib
LK_0734 分钟前
【力扣】142. 环形链表 II |Python
python·leetcode·链表
wuyk55535 分钟前
Python办公自动化-第02讲-Excel文件认知与四大库选型
开发语言·python·excel
用户54817845909502 小时前
Java 与 Python 在大模型与智能体领域的选型
java·python
yiangL_2 小时前
1、Python 输入输出实验|print 与 input 人机交互实践
开发语言·python·人机交互
高洁013 小时前
世界模型的因果内核:从预测表象到理解机制
人工智能·python·深度学习·transformer·迁移学习
数字融合3 小时前
透明化视频动态三维重建空间数据零核心技术
人工智能·python·virtualenv
AI创界者3 小时前
Python 进阶实战(五十四章):大模型推理网关终极架构——分布式全局前缀 Trie 缓存同步与多网关高可用协同调度
人工智能·python·aigc
萧子琦4 小时前
零第三方依赖能做到什么程度?开源两个纯标准库安全小工具:StarShield Forensics 与澄镜 ClearLens
python