决策树python代码wine
时间: 2024-07-21 11:01:02 浏览: 64
决策树是一种常用的数据挖掘算法,用于分类和回归问题。在Python中,我们可以使用sklearn库中的`DecisionTreeClassifier`或`DecisionTreeRegressor`来构建决策树模型,这里以葡萄酒数据集(Wine Dataset)为例,来进行分类任务。
首先,你需要安装必要的库:
```bash
pip install pandas sklearn numpy
```
然后,导入所需的模块并加载数据:
```python
import pandas as pd
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
# 加载葡萄酒数据
wine_data = load_wine()
X = wine_data.data
y = wine_data.target
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
```
接下来,创建并训练决策树模型:
```python
# 创建决策树分类器
clf = DecisionTreeClassifier()
# 训练模型
clf.fit(X_train, y_train)
# 预测
y_pred = clf.predict(X_test)
# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy}")
```
最后,你可以可视化决策树,例如使用`graphviz`库:
```python
from sklearn.tree import export_graphviz
import graphviz
dot_data = export_graphviz(clf, out_file=None, filled=True, rounded=True, special_characters=True)
graph = graphviz.Source(dot_data)
graph.render("wine_tree")
```
阅读全文