department status age salary count sales senior 31...35 46K...50K 30 sales junior 26...30 26K...30K 40 sales junior 31...35 31K...35K 40 systems junior 21...35 46K...50K 20 systems junior 31...35 66K...70K 5 systems junior 26...30 46K...50K 3 systems senior 41...45 66K...70K 3 marketing senior 36...40 46K...50K 10 marketing junior 31...35 41K...45K 4 secretary senior 46...50 36K...40K 4 secretary junior 26...30 26K...30K 6 1、用python建立决策树模型并求召回率、准确率、错误率、精度
时间: 2023-07-18 22:14:36 浏览: 42
为了建立决策树模型,我们需要先准备好数据,并对数据进行预处理。以下是建立决策树模型的代码示例:
```python
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import recall_score, accuracy_score, precision_score, confusion_matrix
# 读入数据
df = pd.read_csv('data.csv')
# 特征选择
X = df.drop(['salary'], axis=1)
y = df['salary']
# 数据分割
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 建立决策树模型
clf = DecisionTreeClassifier()
clf.fit(X_train, y_train)
# 预测结果
y_pred = clf.predict(X_test)
# 计算指标
recall = recall_score(y_test, y_pred, average='weighted')
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average='weighted')
confusion = confusion_matrix(y_test, y_pred)
# 输出指标
print('Recall:', recall)
print('Accuracy:', accuracy)
print('Precision:', precision)
print('Confusion Matrix:', confusion)
```
在上述代码中,我们首先读入数据,然后进行特征选择和数据分割。接着,我们建立了一个决策树分类器,并对其进行训练。然后,我们用模型对测试集进行预测,并计算出召回率、准确率、错误率和精度等指标。最后,我们输出了这些指标。
请注意,这里的数据预处理和特征选择只是示例,实际应用中可能需要更为复杂的数据处理和特征选择方法。同时,还需要注意数据集的大小和质量,以及模型的参数选择等问题。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![](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)