shankarpandala / lazypredict

Lazy Predict help build a lot of basic models without much code and helps understand which models works better without any parameter tuning
MIT License
2.87k stars 333 forks source link

Add a custom metric example for the classifier or regressor #350

Open KangboLu opened 3 years ago

KangboLu commented 3 years ago

Is your feature request related to a problem? Please describe. I noticed you used the weighted F1 score as the default metric for F1. Sometimes, you don't want to just use the weighted version. There is no example of how should the users add their customized scorer function.

Describe the solution you'd like Can you provide an example of how to add a custom scorer to the classifier or the regressor for using lazypredict?

fabio-reale commented 3 years ago

I just took a look at the code. Never used it, never installed it even. So, with that caveat... it looks like if you pass to the custom_metric parameter a callabe with the same signature as .score() from sklearn it has to work.

mp675 commented 6 months ago

you can add any metrics available on sklearn.metrics or even add a real custom one in example:

`import math from sklearn.metrics import matthews_corrcoef, recall_score, precision_score, f1_score, accuracy_score, roc_auc_score

def custom_metrics(y_true, y_pred, model_name): mcc = matthews_corrcoef(y_true, y_pred) recall = recall_score(y_true, y_pred) precision = precision_score(y_true, y_pred) f1 = f1_score(y_true, y_pred) accuracy = accuracy_score(y_true, y_pred) roc_auc = roc_auc_score(y_true, y_pred)

# Calculate confusion matrix values
TN = ((y_true == 0) & (y_pred == 0)).sum()
FP = ((y_true == 0) & (y_pred == 1)).sum()
FN = ((y_true == 1) & (y_pred == 0)).sum()
TP = ((y_true == 1) & (y_pred == 1)).sum()

sensitivity = recall
specificity = TN / (TN + FP) if (TN + FP) != 0 else 0
npv = TN / (TN + FN) if (TN + FN) != 0 else 0
TPR = recall
FPR = FP / (FP + TN)
FNR = FN / (TP + FN)
TNR = specificity
ACC = accuracy
F1S = f1
MCC = mcc

print("{} - Sensitivity = {:.2f}%".format(model_name, sensitivity * 100))
print("{} - Specificity = {:.2f}%".format(model_name, specificity * 100))
print("{} - Precision   = {:.2f}%".format(model_name, precision * 100))
print("{} - NPV         = {:.2f}%".format(model_name, npv * 100))
print("{} - TPR         = {:.2f}%".format(model_name, TPR * 100))
print("{} - FPR         = {:.2f}%".format(model_name, FPR * 100))
print("{} - FNR         = {:.2f}%".format(model_name, FNR * 100))
print("{} - TNR         = {:.2f}%".format(model_name, TNR * 100))
print("{} - ACC         = {:.2f}".format(model_name, ACC))
print("{} - F1 Score    = {:.2f}%".format(model_name, F1S * 100))
print("{} - MCC         = {:.2f}".format(model_name, MCC))
print("{} - ROC AUC     = {:.2f}".format(model_name, roc_auc))

return {'Model': model_name, 'Sensitivity': sensitivity, 'Specificity': specificity, 
        'Precision': precision, 'NPV': npv, 'TPR': TPR, 'FPR': FPR, 'FNR': FNR, 
        'TNR': TNR, 'ACC': ACC, 'F1 Score': F1S, 'MCC': MCC, 'ROC AUC': roc_auc}

Initialize LazyClassifier for training

clf = LazyClassifier(custom_metric=custom_metrics, ignore_warnings=True, verbose=False) models, predictions = clf.fit(X_train, X_test, y_train, y_test) `