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 - Python Tutorial
From the course: Testing Python Data Science Code
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
-
-
-
-
-
-
Using schemas2m 51s
-
(Locked)
Truth values2m 35s
-
(Locked)
Floating point wonders1m 46s
-
(Locked)
Approximate testing1m 18s
-
(Locked)
Dealing with randomness1m 45s
-
(Locked)
Comparing pandas DataFrames1m 31s
-
(Locked)
Challenge: Testing numerical code56s
-
(Locked)
Solution: Testing numerical code51s
-
-
-
-