Data & ML Engineer | Building scalable AI systems, lakehouse pipelines, LLM workflows & vector search | AWS + Databricks | Sharing projects in AI engineering
🐍 Use list comprehension instead of loops
Programmers often use lists in combination with loops to store results. However, this approach is not efficient in terms of run time. For this reason, it is better to use list comprehension, which has better performance.
The figure shows the difference in performance.
A list comprehension begins with an opening bracket [. The calculation of the for loop follows. Then comes the loop header with three elements (keyword for, run variable, length of the loop). The list comprehension is closed with a closing bracket ]. Once you understand the syntax, you can write for-loops much more compactly.
List comprehension is somewhat controversial in programming. Some programmers find the syntax hard to read, as one line of code expresses all statements.
In our opinion, the syntax is clear and concise. It is a matter of taste, but the performance is better with a list comprehension.
#Python #programming #Tips
If you enjoyed this post:
👉🏽 Follow us @tinztwins for more Tech and AI content.
👉🏽 Like, Comment and Share the post to support our content 🤝🏽.
Thanks. Have a great day! 😉😉
Data Classes in Python can save you hours of writing boilerplate code! 🔥
Today, I'll clearly explain them using illustrative code examples!
Let's go! 🚀
Central tendency is one aspect of summarizing a dataset.
But if we want to know if the data is clustered or spread out we need another dimension.
Let's discuss variability 👇
We usually measure variability by comparing real data points to the mean or median.
If we have two data points: {2,6,1} then the mean is 3 the deviation for the first data point is 3-2=1. But for the third value, it is -2.
Mean absolute deviation
Since minus and plus values can offset each other, we usually consider absolute values.
MAD calculates the average of the absolute values of the deviations from the mean.
Variance & Standard deviation
Another way to achieve positive values is by squaring numbers.
Variance does that. It is an average of the squared deviations.
While the standard deviation (the most known estimate of variability) is the square root of the variance.
SD is a preferred measure since it is on the same scale as the original data.
Are they robust?
None of the above-mentioned measures are robust since they are based on the mean.
Variance and SD are extra sensitive because they use squared values in the calculation.
If we need a robust estimate we can use one that is based on the median.
Median absolute deviation (MedAD) is the median of the absolute deviations from the median of the data.
An interesting fact
99.99% of the time SD>MAD>MedAD.
Why is this the case?
- When you square numbers in SD, they become larger than the absolute deviations in MAD and MedAD.
- SD is heavily affected by outliers because it squares the deviations. MAD is also affected but to a lesser extent, while MedAD is robust to outliers. So the effect of outliers is also visible in this relationship.
___
I hope you've found this Tweet helpful.
In this series, I share my notes from the book Practical Statistics for Data Scientists by Peter Bruce, Andrew Bruce, and Peter Gedeck.
Like/Retweet for support and follow @levikul09 for more Data Science content.
Thanks 😉
𝗪𝗵𝗮𝘁 𝗶𝘀 𝘁𝗵𝗲 𝗗𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝗰𝗲 𝗕𝗲𝘁𝘄𝗲𝗲𝗻 𝗜𝗻𝗻𝗲𝗿, 𝗟𝗲𝗳𝘁, 𝗥𝗶𝗴𝗵𝘁, 𝗮𝗻𝗱 𝗙𝘂𝗹𝗹 𝗝𝗼𝗶𝗻?
🔹 (INNER) JOIN - return all rows with matching values in both tables.
🔹 LEFT (OUTER) JOIN - return all rows from the left table and those that meet the condition from the right table.
🔹 RIGHT (OUTER) JOIN - return all rows from the right table and those that meet the condition from the left table.
🔹 FULL (OUTER) JOIN - return all rows with a match in either table.
Image credits: Andreas Martinson.
#technology #softwareengineering #data #techworldwithmilan #sql