[英]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 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.