Nouman Bashir’s Post

Python 𝘭𝘰𝘰𝘱𝘴 almost made me wait an hour. 𝘕𝘶𝘮𝘗𝘺 laughed and did it in 90 seconds. While working on an assignment, I built two versions of the same function. The first used a 𝘗𝘺𝘵𝘩𝘰𝘯 𝘭𝘰𝘰𝘱 to run 60 model evaluations one by one. The second used 𝘕𝘶𝘮𝘗𝘺 to compute all 3,600 pairwise distances in a single operation. Both gave the exact same accuracy of 50%, proving the winner's curse on a fully random dataset. But the loop version took nearly an hour to finish, while the NumPy version was done in 1 to 1.5 minutes. Why is 𝐍𝐮𝐦𝐏𝐲 so much faster? Python loops handle one task at a time and carry overhead with every single step. NumPy does the whole job at once using fast, low level C code that runs directly on your processor. So instead of comparing 60 samples one by one, NumPy compares all 60 against all 60 in one shot. When you run thousands of these operations inside a nested loop, the time savings are massive. 𝘈𝘭𝘸𝘢𝘺𝘴 𝘷𝘦𝘤𝘵𝘰𝘳𝘪𝘻𝘦 𝘺𝘰𝘶𝘳 𝘤𝘰𝘥𝘦 𝘸𝘩𝘦𝘳𝘦 𝘺𝘰𝘶 𝘤𝘢𝘯. Your future self will thank you. Read more: https://lnkd.in/dkxgWqvr #𝐌𝐚𝐜𝐡𝐢𝐧𝐞𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠 #𝐏𝐲𝐭𝐡𝐨𝐧 #𝐍𝐮𝐦𝐏𝐲 #𝐃𝐚𝐭𝐚𝐒𝐜𝐢𝐞𝐧𝐜𝐞 #𝐎𝐩𝐭𝐢𝐦𝐢𝐳𝐚𝐭𝐢𝐨𝐧 #𝐂𝐫𝐨𝐬𝐬𝐕𝐚𝐥𝐢𝐝𝐚𝐭𝐢𝐨𝐧

  • chart, bar chart

To view or add a comment, sign in

Explore content categories