Python Lambda Expressions in Data Science
Upgrade your python coding standards to upgrade your research
Coding efficiently is one of the key premises to the use case of Python and Lambda expressions are no different. Python lambda’s are anonymous
functions which involve small and concise syntax, whereas at times, regular functions can be too descriptive and quite long.
Python is one of a few languages which had lambda functions added to their syntax whereas other languages, like Haskell, uses lambda expressions as a core concept.
Whatever your use-case of a Lambda function, it’s really good to know what they’re about and how to use them.
Why Use Lambda Functions?
The true power of a lambda function can be shown when used inside another function but let’s start on the easy step.
Say you have a function definition that takes one argument, and that argument will be added to an unknown number:
def identity(x):
... return x + 10
However this can be compressed into a simple one-liner as follows:
identity = lambda a : a + 10
This function can then be used as follows:
identity(10)
which will give the answer 20
.
Now with this simple concept, we can also extend this to have more than one input as follows:
myfunc = lambda a, b, c : a + b + c
So the following:
myfunc(2,3,4)
Will give the function 9
. It’s really that simple!
Now a really cool use case of Lambda expressions occurs when you use lambda functions within functions. Take the following example:
def myfunc(n):
return lambda a : a * n
Here, the function myfunc
returns a lambda function which multiplies the input a
by a pre-defined integer, n
. This allows the user to create functions on the fly:
mydoubler = myfunc(2)
mytripler = myfunc(3)
As can be seen, the function mydoubler
is a function that simply defines an input by the number 2, whereas mytripler
multiplies an input by 3. Test it out!
print(mydoubler(11))
print(mytripler(11))
This brings about the answers 22
and 33
.
Are Lambdas Pythonic or Not?
According to the style-guide of Python (PEP 8
), it describes the following which actually recommends users TO NOT use Lambda expressions:
Always use a def statement instead of an assignment statement that binds a lambda expression directly to an identifier.
Yes:
def f(x):
return 2*x
No:
f = lambda x: 2*x
The logic around this is probably more to do with readability than any personal vendetta against lambda expressions. Agreeably, they can make it a bit more difficult to understand the use case but as a coder who prefers efficiency and simplicity in code, I do feel that there’s a place for them.
However, readable code has to be the most important feature of any code — debatably more important than efficiently run code.
Example Math Formulas
Mean:
mu = lambda x: sum(x) / len(x)
Variance:
variance = lambda x: sum((x - mu(x))**2) / (len(x) - 1)
Thanks for reading! If you have any messages, please let me know!
Keep up to date with my latest articles here!
'Data Analytics(en)' 카테고리의 다른 글
Data Visualisation using Pandas and Plotly (0) | 2020.09.30 |
---|---|
Bye-bye Python. Hello Julia! (0) | 2020.09.29 |
Launch of the New Jupyter Book (0) | 2020.09.28 |
Bringing the best out of Jupyter Notebooks for Data Science (0) | 2020.09.28 |
Please Stop Doing These 5 Things in Pandas (0) | 2020.09.27 |