簡體   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