“RuntimeWarning: invalid value encountered in true_divide” in tests

The following tests throw a RuntimeWarning

RuntimeWarning: invalid value encountered in true_divide

in the current development version

  • test_sanity_check_pls_regression_constant_column_Y in sklearn/cross_decomposition/tests/test_pls.py
  • test_iforest_with_uniform_data in sklearn/ensemble/tests/test_iforest.py
  • test_kernel_gradient in sklearn/gaussian_process/tests/test_kernels.py
  • test_radius_neighbors_classifier_zero_distance in sklearn/neighbors/tests/test_neighbors.py
  • test_calibration_multiclass[0-True-isotonic] in sklearn/tests/test_calibration.py
  • test_estimators[LinearDiscriminantAnalysis()-check_classifiers_one_label] in sklearn/tests/test_common.py
  • test_random_choice_csc in sklearn/utils/tests/test_random.py

Those warnings are caused by matricial divisions having some element equal to zero in the denominator, either in the test itself or in the function/class called by the test.

The failure can be reproduced locally using

pytest -Werror::RuntimeWarning <test_file.py> -k <test_name>

cc @ogrisel, @reshamas

1 possible answer(s) on ““RuntimeWarning: invalid value encountered in true_divide” in tests