python语言决策树算法对数据集分类,代码说明
时间: 2023-07-05 20:18:33 浏览: 93
python利用c4.5决策树对鸢尾花卉数据集进行分类(iris)
好的,以下是 Python 决策树算法对数据集分类的代码说明:
首先需要导入相关的库和数据:
```python
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
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)
```
接下来创建决策树分类器,并将训练数据集传入 fit 方法进行训练:
```python
clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)
```
最后使用测试数据集来评估模型的准确度:
```python
accuracy = clf.score(X_test, y_test)
print("Accuracy:", accuracy)
```
完整的代码如下:
```python
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
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)
accuracy = clf.score(X_test, y_test)
print("Accuracy:", accuracy)
```
希望这个代码示例能够帮助您理解 Python 决策树算法对数据集分类的实现。
阅读全文