0

I want to mock two methods (predict_proba and classes_) of a sklearn model. I have a function that receives a template and text, and returns a label and a score.

import numpy as np
from unittest.mock import MagicMock

def model_predict_proba(model, text):
    pred_proba_model = model.predict_proba([text])
    score = pred_proba_model.max()
    label = model.classes_[np.argmax(pred_proba_model)]
    return label, score


def test_model_predict_proba():
    mock_model = MagicMock()
    mock_model.predict_proba.return_value = np.array([0.90, 0.23])
    mock_model.classes_.return_value= np.array(['FOOD', 'DRINK'])
    text = 'Apple pie'

    expected = ("FOOD", 0.90)
    result = model_predict_proba(mock_model, text)
    
    assert result == expected

When I run this test, I get the following error message:

enter image description here

Can someone help me?

1 Answer 1

1

This should do the trick:

def model_predict_proba(model, text):
    pred_proba_model = model.predict_proba([text])
    score = pred_proba_model.max()
    label = model.classes_[np.argmax(pred_proba_model)]
    return label, score


def test_model_predict_proba():
    mock_model = MagicMock()
    mock_model.predict_proba.return_value.max.return_value = 0.90
    mock_model.classes_.__getitem__.return_value ='FOOD'
    text = 'Apple pie'

    expected = ("FOOD", 0.90)
    result = model_predict_proba(mock_model, text)

    assert result == expected

Note that since you're mocking your model, this test is not actually testing the model in any useful way -- I'm assuming you're writing this function just as an exercise to understand how MagicMock works. The purpose of mocking is usually to simulate the inputs or dependencies of the thing you're testing, rather than the thing itself.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much for the answer, it solved my problem! My goal in this test is not to test the model, but, given a model (already trained) and any text, the function has to return a label and a score to me.
Could you explain to me what these two steps mean? 1) mock_model.predict_proba.return_value.max.return_value 2) mock_model.classes_.__getitem__.return_value
It's just setting up the return values of the mocks to produce the exact things that will make your test pass. mock_model.predict_proba.return_value.max is the function that will be called in model_predict_proba to get the score (which you want to be 0.90) and mock_model.classes_.__getitem__ is called to get label. Again, none of this is testing the actual model, it's just completely fake responses to make the test pass, which is why it's not a useful test. (It's not possible to get "real" responses out of a mock -- it just returns the fake responses you give it.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.