使用 Scikit-learn 库中自带的鸢尾花数据集,实现一个基于决策树分类模型的分类任 务,输出分类结果及在测试集上的准确率、精确率、召回率并绘制混淆矩阵。
时间: 2023-06-13 11:05:13 浏览: 210
好的,下面是实现过程:
首先,我们需要导入需要的库和鸢尾花数据集:
```python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score, confusion_matrix
iris = load_iris()
X = iris.data
y = iris.target
```
接着,我们将数据集分为训练集和测试集,并创建决策树分类模型:
```python
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)
```
然后,我们使用训练好的模型对测试集进行预测,并计算准确率、精确率、召回率:
```python
y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average='weighted')
recall = recall_score(y_test, y_pred, average='weighted')
```
最后,我们绘制混淆矩阵:
```python
confusion_mat = confusion_matrix(y_test, y_pred)
print('Confusion matrix:\n', confusion_mat)
```
完整代码如下:
```python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score, confusion_matrix
iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average='weighted')
recall = recall_score(y_test, y_pred, average='weighted')
confusion_mat = confusion_matrix(y_test, y_pred)
print('Accuracy: {:.2f}'.format(accuracy))
print('Precision: {:.2f}'.format(precision))
print('Recall: {:.2f}'.format(recall))
print('Confusion matrix:\n', confusion_mat)
```
输出结果为:
```
Accuracy: 1.00
Precision: 1.00
Recall: 1.00
Confusion matrix:
[[19 0 0]
[ 0 13 0]
[ 0 0 13]]
```
可以看到,在测试集上,我们的模型准确率、精确率、召回率均为 1.00,说明分类效果非常好。混淆矩阵也显示了模型的分类结果。
阅读全文