C:\Users\huangjiafu\.conda\envs\pytorch_gpu\lib\site-packages\sklearn\metrics\_classification.py:1344: UndefinedMetricWarning: Precision is ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result))
时间: 2023-06-30 20:18:16 浏览: 111
Miniconda3-py38_4.11.0-Linux-x86_64.zip
这个警告是由于有些类别在预测中没有被预测到,导致精度无法计算,因此被设置为了0。可以通过设置`zero_division`参数来控制这种行为。例如,如果将`average`设置为`'weighted'`,则可以将`zero_division`设置为1,这样可以确保在某些类别没有预测到的情况下,精度仍然可以计算。
以下是示例代码:
```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', zero_division=1)
confusion = confusion_matrix(y_test, y_pred)
# 输出指标
print('Recall:', recall)
print('Accuracy:', accuracy)
print('Precision:', precision)
print('Confusion Matrix:', confusion)
```
在上述代码中,我们将`precision_score`函数中的`zero_division`参数设置为了1,这样可以确保在某些类别没有预测到的情况下,精度仍然可以计算。
阅读全文