简体   繁体   English

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

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

i need my precision,recall and f1 score results to be like the output below我需要我的精确度、召回率和 f1 分数结果像下面的 output

precision  0.98
recall     0.98 
f1 score   0.93

the numbers are just an example数字只是一个例子

here is my data head这是我的数据头在此处输入图像描述

here is my code这是我的代码

    #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))

here is my results of the code这是我的代码结果

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

i had two numbers for the F1 score i wanted to be just one number and i do not know how and i tried to find a code to find out the precision or the recall and i could not find any我有两个 F1 分数的数字,我只想成为一个数字,但我不知道该怎么做,我试图找到一个代码来找出精确度或召回率,但我找不到任何

please help me at least with the F1 score output Thank you请至少帮助我获得 F1 分数 output 谢谢

From sklearn import the metrics从 sklearn 导入指标

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

Split data and train the model拆分数据并训练 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)

Print the metrics, here for the average parameter you can change it check sklearn for details打印指标,此处为平均参数,您可以更改它检查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.

相关问题 Tensorflow:计算精度、召回率、F1 分数 - Tensorflow: Compute Precision, Recall, F1 Score 使用 precision_recall_curve 计算最大 f1 分数? - compute maximum f1 score using precision_recall_curve? 计算多 label 分类 keras 的召回精度和 F1 分数 - compute the recall precision and F1 score for a multi label classification keras 如何计算 K 折交叉验证的不平衡数据集的精度、召回率和 f1 分数? - How to compute precision,recall and f1 score of an imbalanced dataset for K fold cross validation? 如何在迁移学习 vgg16 模型中获得准确率、召回率、f1 分数? - How to get precision, recall, f1 score in transfer learning vgg16 model? 如何找到我的 word2vec model 的准确度、精确度、召回率和 f1 分数? - How to find accuracy, precision, recall, f1 score for my word2vec model? 如何在训练 SSD 后预测 Precision、Recall 和 F1 分数 - How to predict Precision, Recall and F1 score after training SSD 精度,召回率,F1得分与sklearn相等 - Precision, recall, F1 score equal with sklearn 自定义 Keras 指标函数(召回率、精度、F1 分数)无法加载 H5 model - Custom Keras metric functions (Recall, Precision, F1 Score) isn't enabling the Loading of H5 model 如何在 python 中计算一类 SVM 的准确度、F1 分数、召回率、精度和 EER? - How to compute accuracy, F1-score, recall, precision and EER for one-class SVM in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM