用python写,找个数据集,利用SKLearn库跑一个决策树模型
时间: 2023-05-31 07:03:17 浏览: 60
以下是一个使用SKLearn库和Iris数据集的决策树模型:
```python
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# 加载Iris数据集
iris = load_iris()
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, 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)
print("Accuracy:", accuracy)
```
输出:
```
Accuracy: 0.9777777777777777
```
相关问题
用python写,找个数据集,利用SKLearn库跑一个决策树模型,然后写出数据分析和实验过程
数据集选择
我选择了Iris(鸢尾花)数据集。这是一个非常经典的数据集,具有三个类别:setosa,versicolor和virginica。每个类别有50个样本。每个样本有四个特征:花萼长度,花萼宽度,花瓣长度和花瓣宽度。
导入库
首先,我们需要导入必要的库:numpy,pandas,matplotlib和sklearn。
```python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
```
导入数据
Iris数据集是内置的,可以使用sklearn.datasets中的load_iris函数导入。
```python
iris = load_iris()
X = iris.data
y = iris.target
```
数据分析
我们可以通过使用pandas.DataFrame将数据转换为数据框,然后查看数据的详细信息。
```python
df = pd.DataFrame(X, columns=iris.feature_names)
df['target'] = y
df.head()
```
输出结果:
![iris-data](https://cdn.jsdelivr.net/gh/xiaoyu-ai/tu-chuang/img/20211008100548.png)
我们可以看到,每个样本有四个特征和一个目标值。每个目标值都对应于三个类别中的一个。
接下来,我们可以使用matplotlib.pyplot中的散点图来可视化数据。
```python
plt.scatter(X[:, 0], X[:, 1], c=y)
plt.xlabel('sepal length (cm)')
plt.ylabel('sepal width (cm)')
plt.show()
```
输出结果:
![iris-scatter](https://cdn.jsdelivr.net/gh/xiaoyu-ai/tu-chuang/img/20211008100804.png)
可以看出,setosa类别与其他两个类别有明显的分离。
实验过程
接下来,我们将数据集划分为训练集和测试集,并训练一个决策树分类器。
```python
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
clf = DecisionTreeClassifier(random_state=42)
clf.fit(X_train, y_train)
```
接下来,我们可以使用测试集来评估分类器的性能。
```python
y_pred = clf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print('Accuracy:', accuracy)
```
输出结果:
```
Accuracy: 1.0
```
可以看出,我们的分类器在测试集上表现非常好,准确度为100%。
最后,我们可以可视化决策树模型。
```python
from sklearn.tree import plot_tree
plt.figure(figsize=(15, 10))
plot_tree(clf, filled=True, feature_names=iris.feature_names, class_names=iris.target_names)
plt.show()
```
输出结果:
![iris-decision-tree](https://cdn.jsdelivr.net/gh/xiaoyu-ai/tu-chuang/img/20211008101058.png)
我们可以看到,决策树模型基于花瓣长度和花瓣宽度将数据集分为三个类别。这与我们之前看到的散点图相符。
使用python的sklearn库写一个决策树代码
以下是使用Python的sklearn库编写决策树的代码:
```python
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
# 加载数据集
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.2, random_state=42)
# 建立决策树模型
clf = DecisionTreeClassifier(random_state=42)
clf.fit(X_train, y_train)
# 测试模型精度
score = clf.score(X_test, y_test)
print(f"决策树模型的精度为{score:.2f}")
# 可视化决策树
plt.figure(figsize=(10,8))
plot_tree(clf, filled=True)
plt.show()
```
在这个例子中,我们首先加载了鸢尾花数据集,然后将数据集分为训练集和测试集。接下来,我们建立了一个决策树分类器,并使用训练数据拟合了模型。最后,我们测试了模型的精度,并使用matplotlib库的plot_tree函数可视化了决策树。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)