用python写,找个数据集,利用SKLearn库跑一个决策树模型,然后写出数据分析和实验过程
时间: 2023-05-31 22:03:28 浏览: 151
数据集选择
我选择了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)
我们可以看到,决策树模型基于花瓣长度和花瓣宽度将数据集分为三个类别。这与我们之前看到的散点图相符。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.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)