In relation to this, IMHO the best way to learn the foundations of mathematics is to teach yourself either:
* Linear algebra and real analysis (if your end goal is ML or statistics)
* Logic and type theory (if your end goal is formal methods or PLT)
For the first option, a typical sequence (like the one taught at Harvard Math 55) is Halmos (or Axler) plus Rudin (and a good companion like Gelbaum & Olmsted). A more practical bottom-up approach is to use a good matrix analysis book such as Meyer, plus a more practical calculus (vs analysis) textbook like Hubbard & Hubbard.
For the second option, a good theoretical logic book (Ben Ari and Schoening are my favorites) paired along with Girard. A more practical option would use Huth & Ryan as a logic volume along with Pierce's TAPL.