You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Note, how the `test_linear_model.py` contains 9 functions with names starting with `test...`. Those contain the actual test code. It also has a couple of data constructor functions whose names do not start with `test...` and they are ignored by Pytest.
49
+
- Note, how the `test_linear_model.py` contains 9 functions with names starting with `test...`. Those contain the actual test code.
50
+
- It also has a couple of data constructor functions (`random_data_constructor` and `fixed_data_constructor`) whose names do not start with `test...` and they are ignored by Pytest. The `random_data_constructor` even takes a `noise_mag` argument which is used to control the magnitude of noise to test the expected behavior of a linear regression algorithm. Refer to the `test_noise_impact` function for this.
50
51
51
52
- Note that we need to import a variety of libraries to test all kind of things e.g. we imported libraries like `joblib`, `os`, `sklearn`, `numpy`, and of course, the `train_linear_model` function from the `linear_model` module.
52
53
- Note the **clear and distinctive names** for the testing functions e.g. `test_model_return_object()` which only checks the returned object from the `train_linear_model` function, or the `test_model_save_load()` which checks whether the saved model can be loaded properly (but does not try to make predictions or anything). **Always write short and crisp test functions with a singular focus**.
53
-
- For checking the predictions i.e. whether the trained model really works or not, we have the `test_loaded_model_works()` function which uses a fixed data generator with no noise (as compared to other cases, where we can use a random data generator with random noise). It passes on the fixed `X` and `y` data, loads the trained model, checks if the R^2 ([regression coefficient](https://www.geeksforgeeks.org/python-coefficient-of-determination-r2-score/)) scores are perfectly equal to 1.0 (true for a fixed dataset with no noise) and then compare the model predictions with the original ground truth `y` vector. Note, how it uses a **special Numpy testing function `np.testing.assert_allclose` instead of the regular `assert` statement**. This is to avoid any potential numerical precision issues associated with the model data i.e. Numpy arrays and the prediction algorithm involving linear algebra operations.
54
-
- Take a look at the `random_data_constructor` and `fixed_data_constructor` functions too to see how they are designed and used in the test code. The `random_data_constructor` even takes a `noise_mag` argument which is used to control the magnitude of noise to test the expected behavior of a linear regression algorithm. Refer to the `test_noise_impact` function for this.
55
-
- Finally, take a look at the `test_wrong_input_raises_assertion` function which tests **if the original modeling function raises the correct Exception and messages** when fed with various types of wrong input arguments. Always remember that **Pytest (or any testing module for that matter) is not to be used for error-handling** (e.g. a wrong input type). The developer (ML sofware engineer in this case) must write the necessary error-checking and handling code. In this example, we write a bunch of `assert` statements in the original `train_linear_model` function and wrap them around a whole `try...except` block. The test code only checks if we are returning the `AssertionType` error for those wrong input cases and if the correct error message is being printed for the end user.
56
-
- The `test_raised_exception` function tests the **rise of other exception types (i.e. different from the `AssertionError` that could be raised by the `assert` statements in the function module)** during runtime using a special Pytest feature - [the `pytest.raises` context manager](https://docs.pytest.org/en/reorganize-docs/new-docs/user/pytest_raises.html).
54
+
- For checking the predictions i.e. whether the trained model really works or not, we have the `test_loaded_model_works()` function which uses a fixed data generator with no noise (as compared to other cases, where we can use a random data generator with random noise). It passes on the fixed `X` and `y` data, loads the trained model, checks if the R^2 ([regression coefficient](https://www.geeksforgeeks.org/python-coefficient-of-determination-r2-score/)) scores are perfectly equal to 1.0 (true for a fixed dataset with no noise) and then compare the model predictions with the original ground truth `y` vector.
55
+
- Note, how the aforementioned function uses a **special Numpy testing function `np.testing.assert_allclose` instead of the regular `assert` statement**. This is to avoid any potential numerical precision issues associated with the model data i.e. Numpy arrays and the prediction algorithm involving linear algebra operations.
56
+
- The `test_model_works_data_range_sign_change` function tests the expected behavior of a linear regression estimator - that the regression scores will still be 1.0 no matter the range of the data (scaling the data by 10e-9 or 10e+9). It also changes the expected behavior if the data flips sign somehow.
57
+
- The `test_additive_invariance` function tests the additive invariance property of the linear regression estimator. **Similarly, you should think about the special properties of your ML estimator and write customized tests for them**.
58
+
- Take a look at the `test_wrong_input_raises_assertion` function which tests **if the original modeling function raises the correct Exception and messages** when fed with various types of wrong input arguments. Always remember that **Pytest (or any testing module for that matter) is not to be used for error-handling** (e.g. a wrong input type). The developer (ML sofware engineer in this case) must write the necessary error-checking and handling code. In this example, we write a bunch of `assert` statements in the original `train_linear_model` function and wrap them around a whole `try...except` block. The test code only checks if we are returning the `AssertionType` error for those wrong input cases and if the correct error message is being printed for the end user.
59
+
- Finally, the `test_raised_exception` function tests the **rise of other exception types (i.e. different from the `AssertionError` that could be raised by the `assert` statements in the function module)** during runtime using a special Pytest feature - [the `pytest.raises` context manager](https://docs.pytest.org/en/reorganize-docs/new-docs/user/pytest_raises.html).
0 commit comments