Let's explore the "any" and "all" built-in functions in Python.
A π§΅
First of all, I'll teach you how to fish in Python before giving you the fish. ππ£
The built-in "help" function will give you documentation in Python. Make liberal use of it and reach out to it before ceding control to a search engine.
These are "aggregation" or "reducing" functions. They take a sequence and collapse it to a single value.
"any" returns if any value was truthy.
"all" returns if all values were truthy.
Most examples look like this:
Generally, I don't have lists of true, false values. Rather I tend to create them using "predicate functions" and comprehensions. A predicate returns a true/false value:
You can also replace "and" and "or" with "all" and "any". Here is code that checks if a letter is alphanumeric, period, or underscore:
Here is another pattern. If I wanted to determine if all characters in a filename were "valid", I would probably use a for loop and check each character. However, such for loops can be replaced with "all":
Likewise, we can replace searching for loops with "any". Here I search if there is a negative number and show the equivalent "any" version:
I hope you enjoyed learning about patterns for "any" and "all".
Feel free to share to your network and follow me for more Python insights. π
β’ β’ β’
Missing some Tweet in this thread? You can try to
force a refresh
XGBoost is a popular machine learning algorithm that provides excellent performance out of the box for various types of machine learning problems, including regression and classification.
However, one common issue with XGBoost is overfitting, leading to poor generalization and inaccurate predictions on new data.
Overfitting occurs when the model learns the training data too well, including the noise and randomness, and cannot generalize to new data.
You can use various techniques to determine whether an XGBoost model is overfitting, including cross-validation, regularization, and monitoring the training and validation errors.
Many know how to use them, but few can write them.
These are tricky because nested functions make our brains hurt.
Here are some hints for grokking them.
1/
In short, decorators allow you to inject orthogonal behavior before or after a function is executed.
But my favorite decorator definition is related to the construction and will help you easily create them: A callable that takes a callable and returns a callable.
2/
What do I mean by "orthogonal"?
A function should do one thing. If you want to add caching or logging, it really isn't related to the function (and could be applied to multiple functions). It is "orthogonal" behavior.
3/
Many know how to use them, but few can write them.
These are tricky because nested functions make our brains hurt.
Here are some hints for grokking them.
1/
In short, decorators allow you to inject orthogonal behavior before or after a function is executed.
But my favorite decorator definition is related to the construction and will help you easily create them: A callable that takes a callable and returns a callable.
2/
What do I mean by "orthogonal"?
A function should do one thing. If you want to add caching or logging, it really isn't related to the function (and could be applied to multiple functions). It is "orthogonal" behavior.
3/
Many know how to use them, but few can write them.
These are tricky because nested functions make our brains hurt.
Here are some hints for grokking them.
1/
In short, decorators allow you to inject orthogonal behavior before or after a function is executed.
But my favorite decorator definition is related to the construction and will help you easily create them: A callable that takes a callable and returns a callable.
2/
What do I mean by "orthogonal"?
A function should do one thing. If you want to add caching or logging, it really isn't related to the function (and could be applied to multiple functions). It is "orthogonal" behavior.
3/