1.1选择鸢尾花数据集任意两个特征,建立决策树模型; 1.2输出组合特征的名称,训练精度,测试精度; 1.3将最终不同组合下的分类结果输出可视化图;
时间: 2024-11-24 19:41:28 浏览: 32
1. 首先,让我们从Python的scikit-learn库中导入鸢尾花数据集(Iris dataset),它包含四个特征(sepal length, sepal width, petal length, petal width),我们将选取其中的两个作为我们的输入特征。假设我们选择了"sepal length"和"petal length"这两个特征。
```python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
# 加载数据
iris = load_iris()
X = iris.data[:, [0, 2]] # 选择sepal length和petal length作为特征
y = iris.target
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
```
2. 接下来,我们构建决策树模型并训练它,同时计算训练集和测试集的精度:
```python
model = DecisionTreeClassifier()
model.fit(X_train, y_train)
# 计算并输出训练精度和测试精度
train_accuracy = model.score(X_train, y_train)
test_accuracy = model.score(X_test, y_test)
print(f"组合特征: {['Sepal Length', 'Petal Length']}")
print(f"训练精度: {train_accuracy:.2f}")
print(f"测试精度: {test_accuracy:.2f}")
```
3. 最后,我们可以使用matplotlib库绘制混淆矩阵(confusion matrix)来可视化分类结果,展示每个类别预测的准确性和错误情况:
```python
from sklearn.metrics import plot_confusion_matrix
import seaborn as sns
# 绘制混淆矩阵
disp = plot_confusion_matrix(model, X_test, y_test, cmap=plt.cm.Blues, xticks_rotation='vertical')
disp.ax_.set_title("Decision Tree Confusion Matrix")
plt.show()
```
阅读全文