【问题标题】:Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior精度和 F 分数定义不明确,在没有预测样本的标签中设置为 0.0。使用 `zero_division` 参数来控制这种行为
【发布时间】:2022-01-09 14:42:35
【问题描述】:

我正在运行逻辑回归,但我获得的 f1 分数为 0.0。我认为这与零除错误有关,但我无法修复它

data4=data[['Age','BusinessTravel_Travel_Frequently','DistanceFromHome','Education','EnvironmentSatisfaction','Gender_Male','JobInvolvement','YearsWithCurrManager','MaritalStatus_Married','JobSatisfaction','NumCompaniesWorked','TotalWorkingYears','TrainingTimesLastYear','YearsAtCompany','Performance_dummy']]

X1=data4[['Age','BusinessTravel_Travel_Frequently','DistanceFromHome','Education','EnvironmentSatisfaction','Gender_Male','JobInvolvement','YearsWithCurrManager','MaritalStatus_Married','JobSatisfaction','NumCompaniesWorked','TotalWorkingYears','TrainingTimesLastYear','YearsAtCompany']]

y1=data4.Performance_dummy
# split X and y into training and testing sets
from sklearn.model_selection import train_test_split
X_train1,X_test1,y_train1,y_test1=train_test_split(X1,y1,test_size=0.5,random_state=0,stratify=y1)

# import the class
from sklearn.linear_model import LogisticRegression

# instantiate the model (using the default parameters)
logreg1 = LogisticRegression(max_iter=1000)

# fit the model with data
logreg1.fit(X_train1,y_train1)

#
y_pred1=logreg1.predict(X_test1)
print('Accuracy of logistic regression classifier on test set: {:.2f}'.format(logreg1.score(X_test1, y_test1)))

我得到以下输出

Accuracy of logistic regression classifier on test set: 0.85

我运行了如下所示的混淆矩阵代码

from sklearn.metrics import confusion_matrix
confusion_matrix = confusion_matrix(y_test1, y_pred1)
print("Confusion Matrix:\n",confusion_matrix)
from sklearn.metrics import classification_report
print("Classification Report:\n",classification_report(y_test1, y_pred1,zero_division=1))

以上代码的输出

Confusion Matrix:
 [[622   0]
 [113   0]]
Classification Report:
               precision    recall  f1-score   support

           0       0.85      1.00      0.92       622
           1       1.00      0.00      0.00       113

    accuracy                           0.85       735
   macro avg       0.92      0.50      0.46       735
weighted avg       0.87      0.85      0.78       735

我还运行了这段代码来了解我的测试数据中的结果比率,并得到了以下输出,但我不知道如何解决这个零除错误

from collections import Counter
print(Counter(y_train1))
print(Counter(y_test1))

输出

Counter({0: 622, 1: 113})
Counter({0: 622, 1: 113})

【问题讨论】:

    标签: python scikit-learn logistic-regression


    【解决方案1】:

    您的 f1-score 定义不明确,因为您的模型仅预测一个类别 (0)。

    您可以在您的LogisticRegression 上使用class_weight="balanced" 来惩罚代表不足的样本。

    如果这不起作用,增加训练集大小或使用更高级的模型可能是明智之举。

    【讨论】:

      猜你喜欢
      • 2021-10-02
      • 2019-09-22
      • 2018-09-25
      • 2017-08-27
      • 2016-05-15
      • 2018-09-29
      • 2018-08-05
      • 2019-09-30
      • 2020-07-27
      相关资源
      最近更新 更多