繁体   English   中英

如何计算 python 中平衡逻辑回归 model 的精度、召回率和 f1 分数

[英]How to compute precision,recall and f1 score of an balanced logistic regression model in python

我需要我的精确度、召回率和 f1 分数结果像下面的 output

precision  0.98
recall     0.98 
f1 score   0.93

数字只是一个例子

这是我的数据头在此处输入图像描述

这是我的代码

    #training and test sample :
x1_training_data, x1_test_data, y1_training_data, y1_test_data = train_test_split(x1_data, y1_data, test_size = 0.3)

    # Estimation result:
logit_model=sm.Logit(y1_training_data,x1_training_data)
result1=logit_model.fit()
print(result1.summary2())

    # Model Evaluation:
logreg=LogisticRegression()
logreg.fit(x1_training_data,y1_training_data)
y1_pred=logreg.predict(x1_test_data)
print('Logistic regression model accuracy:{:.2f}'.format(logreg.score(x1_test_data,y1_test_data)))
print("Logistic Regression F1 Score :",f1_score(y1_test_data,logreg.predict(x1_test_data),average=None))

这是我的代码结果

logistic Regression Accuracy after undersampling : 0.902297169964584
Logistic Regression F1 Score after undersampling : [0.90023556 0.9042753 ]

我有两个 F1 分数的数字,我只想成为一个数字,但我不知道该怎么做,我试图找到一个代码来找出精确度或召回率,但我找不到任何

请至少帮助我获得 F1 分数 output 谢谢

从 sklearn 导入指标

from sklearn.metrics import f1_score
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score

拆分数据并训练 model

#training and test sample :
x1_training_data, x1_test_data, y1_training_data, y1_test_data = train_test_split(x1_data, y1_data, test_size = 0.3)

# Estimation result:
logit_model=sm.Logit(y1_training_data,x1_training_data)
result1=logit_model.fit()
print(result1.summary2())

# Model Evaluation:
logreg=LogisticRegression()
logreg.fit(x1_training_data,y1_training_data)
y1_pred=logreg.predict(x1_test_data)

打印指标,此处为平均参数,您可以更改它检查sklearn以获取详细信息

print('precision: %.2f' % precision_score(y1_data, y1_pred,average='weighted'))
print('recall: %.2f' % recall_score(y1_data, y1_pred,average='weighted'))
print('f1_score: %.2f' % f1_score(y1_data, y1_pred,average='weighted'))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM