From the course: Testing Python Data Science Code

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Approximate testing

Approximate testing

- [Instructor] Since floating points are not exact, we need to figure out what is close enough. Let's have a look. So here I'm importing NumPy. And then I have an array. I multiply it by 1.1 and check out that the output equals the expected output. Shift and Enter, and I'm going to see that there are False, False, and False, meaning the values do not match. However, when I look at the output, it seems like it's exactly the same thing, right? 0.11, np.nan, 01.21. But, if I'm going to look at the first element of the array, now I'm going to see the difference. So NumPy, in order to print out large arrays, is trimming down numbers, and this might confuse us from time to time. My weapon of choice when comparing arrays is numpy.allclose, which take two increments to compare, a relative tolerance and absolute tolerance. And also a flag specifying whether NaNs are equal or not. In our case, if I'm just going to use…

Contents