Sklearn-使用SVC对iris数据集进行分类

Sklearn-使用SVC对iris数据集进行分类

使用SVC对iris数据集进行分类预测

涉及内容包含:

  • 数据集的加载,训练集和测试集的划分
  • 训练svc模型,对测试集的预测
  • 输出混淆矩阵和分类报告
  • 使用Pipeline执行操作

iris数据集的加载

加载数据集

用DataFrame展示数据

划分训练集和测试集合

python 复制代码
from sklearn.datasets import load_iris
python 复制代码
iris = load_iris()
python 复制代码
iris.keys()
复制代码
dict_keys(['data', 'target', 'frame', 'target_names', 'DESCR', 'feature_names', 'filename'])
python 复制代码
data = iris['data']
target = iris['target']

# 以DataFrame显示所有的数据
import pandas as pd
df = pd.DataFrame(data,columns=iris['feature_names']) 
df['target'] = target # 添加target列

| | sepal length (cm) | sepal width (cm) | petal length (cm) | petal width (cm) | target |
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | 0 |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | 0 |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | 0 |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | 0 |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | 0 |
| ... | ... | ... | ... | ... | ... |
| 145 | 6.7 | 3.0 | 5.2 | 2.3 | 2 |
| 146 | 6.3 | 2.5 | 5.0 | 1.9 | 2 |
| 147 | 6.5 | 3.0 | 5.2 | 2.0 | 2 |
| 148 | 6.2 | 3.4 | 5.4 | 2.3 | 2 |

149 5.9 3.0 5.1 1.8 2

150 rows × 5 columns

python 复制代码
# 划分数据集:训练集和测试集
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(data,target,test_size=0.3) # 测试集占30%。训练集70%

训练svc模型

  • 导入库文件
  • 初始化svc
  • 训练svc
python 复制代码
from sklearn.svm import SVC
# 初始化SVC
svc = SVC()
# 训练
svc.fit(x_train,y_train)
# 查看训练效果
print("训练集的精度",svc.score(x_train,y_train))
# 对测试集预测的精度
print("对测试集的预测效果:",svc.score(x_test,y_test))

# 对测试集进行预测
y_pre = svc.predict(x_test)
# 表格对比预测与实际结果
df2 = pd.DataFrame(data = {
    'predict':y_pre,
    'true':y_test
})
复制代码
训练集的精度 0.9714285714285714
对测试集的预测效果: 0.9555555555555556

输出混淆矩阵和分类报告

  • 输出混淆矩阵:查看每个类预测的成功与失败的情况
  • 输出分类报告:查看分类的性能
python 复制代码
from sklearn.metrics import confusion_matrix

# 输出混淆矩阵
con_matrix = confusion_matrix(y_test,y_pre)
print(con_matrix)
复制代码
[[12  0  0]
 [ 0 15  1]
 [ 0  1 16]]
python 复制代码
from sklearn.metrics import classification_report
# 输出分类报告
report = classification_report(y_test,y_pre,
                            target_names=iris['target_names'])
print(report)
复制代码
              precision    recall  f1-score   support

      setosa       1.00      1.00      1.00        12
  versicolor       0.94      0.94      0.94        16
   virginica       0.94      0.94      0.94        17

    accuracy                           0.96        45
   macro avg       0.96      0.96      0.96        45
weighted avg       0.96      0.96      0.96        45

相关推荐
aigcapi7 小时前
2026 GPT/Gemini API接入优选指南+平台榜单:破解“GPT API哪个平台好”核心难题
人工智能·gpt·api
百胜软件@百胜软件7 小时前
喜讯|百胜软件荣膺“2025年度零售科技最佳服务商”
大数据·人工智能
张祥6422889047 小时前
误差理论与测量平差基础四
人工智能·机器学习·概率论
雨大王5127 小时前
智能仓储系统在汽车零部件管理中的应用
人工智能·汽车·制造
神气龙7 小时前
Dify试用
人工智能
WLJT1231231237 小时前
品质配件与专业维保筑牢安全发展根基
大数据·人工智能·科技·安全·生活
深圳南柯电子7 小时前
深圳南柯电子|EMC电磁兼容测试系统:5G时代应对频段的干扰挑战
网络·人工智能·互联网·实验室·emc
小郭团队7 小时前
教育公平的探索
大数据·人工智能·嵌入式硬件·算法·硬件架构
驭白.7 小时前
从订单到行驶:构建新能源汽车产品全生命周期数据链
人工智能·汽车·制造·数字化·制造业·新能源汽车
Watermelo6177 小时前
探究TOON的价值边界:比JSON更优的大模型友好数据格式?
数据结构·人工智能·语言模型·自然语言处理·数据挖掘·数据分析·json