Search code examples
python-3.xmachine-learningscikit-learnmodel

Retrieve metrics from a saved model machine learning


I have a question, is it possible to recover the metrics of a saved model like f1 score, confusion matrix, recall, ... without going through the train and the test?

I use pickle to save my model

with open('SVM_Model.pkl', 'wb') as f:
    pickle.dump(fitted_model, f)

                                    
with open('SVM_Model.pkl', 'rb') as f:
    joblib_LR_model = pickle.load(f)

Solution

  • There are two approaches.

    First one is to calculate metrics of some dataset and save them, for example in json file.

    from sklearn.metrics import f1_score
    import json
    
    f1_value = f1_score(y_true, y_pred, average='macro')
    
    f1_save = {'f1': f1_value}
    
    with open('f1_save.json', 'wb') as f:
        json.dump(f1_save, f)
    

    Another approach is to calculate the metrics on the new data after loading the model

    
    with open('SVM_Model.pkl', 'rb') as f:
        joblib_LR_model = pickle.load(f)
    
    f1_value = f1_score(y_test, model.predict(x_test), average='macro')