python决策树预测模型
时间: 2023-09-07 10:14:55 浏览: 45
Python中有多种决策树预测模型,其中比较常用的是基于scikit-learn库的决策树模型。
以下是基于scikit-learn库的决策树预测模型的实现步骤:
1. 导入需要的库和数据集:
```python
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
iris = load_iris()
X = iris.data
y = iris.target
```
2. 将数据集分为训练集和测试集:
```python
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
```
3. 创建决策树模型:
```python
clf = DecisionTreeClassifier(random_state=42)
```
4. 训练模型:
```python
clf.fit(X_train, y_train)
```
5. 预测测试集结果:
```python
y_pred = clf.predict(X_test)
```
6. 计算模型的准确率:
```python
from sklearn.metrics import accuracy_score
accuracy_score(y_test, y_pred)
```
以上就是基于scikit-learn库的决策树预测模型的实现步骤。