Data Science Economics

Introduction:

Created by author

Python Lambda functions are the anonymous (unnamed) functions that are defined using the keyword lambda. They are often used for one-off, short, operations where a full function is not needed. They have multiple arguments, but only a single expression which is automatically returned. Often used with higher orders function such as map, filter and reduce for list transformations and aggregations. used for quick, one-time operations where defining a full function would be unnecessary. These functions can have multiple arguments but contain only a single expression, which is automatically returned.

Key Characteristics:

Syntax: lambda arguments: expression

 Compact and concise.

Often used with higher-order functions like map, filter, and reduce for list transformations and aggregations.

Examples of lambda functions in Python:

1. Mathematical Operations: Solving problems with algebraic expressions, squaring and cubing.

 2. Data Transformation:

Modifying lists with map.

3. Data Filtering: Using filter to select elements from lists.

4. Aggregation: Using reduce to sum or multiply list elements

Basic Lambda Function:

x = lambda a: a * a 
X(3)
#OUTPUT= 3
def new(a):
    return a*a
new(3)
  
    #output also 3 in this case

Concept: This lambda function accepts a single argument ‘a’ and returns its square.

  • lambda functions in Python are anonymous functions (no need for def keyword or a function name) often used for simple, quick operations.

Use case :

Used for single expression operation that does not need a formal function definition like squaring or mathematic evaluation. Same as above, but cube of z is calculated. Defined using the lambda keyword. They are typically for quick, one-time operations where defining a full function would be unnecessary. These functions can have multiple arguments but contain only a single expression, which is automatically returned.

2 Lambda for Cubing a Number:

y = lambda z: z * z * z
y(2)
#Output would be 8

Concept:

  • Like the above but calculates the cube of the input z.
  • Purpose: Used when you want to do a repetitive operation (e.g. computing powers) without writing a named function

3. Lambda with filter:

we are going to apply further function on list.

my_list = [1, 2, 3, 4, 5, 6, 7, 8]

my_list = [1, 2, 3, 4, 5, 6, 7, 8]
newl = list(filter(lambda a: (a / 4 == 2), my_list))
print(new1)

Output is 8 in this case

 Concept:

  • The filter function applies the lambda to each element in the list.
  • The lambda checks if dividing the element by 4 equals 2. It returns only those elements that satisfy the condition.
  • Use Case: Getting values from a collection, based on a logical condition. Example: Valid input selection from user data or datasets. Applying lambda to every element in the list is done by map function.
  • Every element is checked to see if it doesn’t fulfill the condition a / 4 == 2. This returns a list of Boolean values (True or False).operations where defining a full function would be unnecessary.

4. Lambda with map:

my_list = [1, 2, 3, 4, 5, 6, 7, 8]
newlist = list(map(lambda a: (a / 4 != 2), my_list))
print(newlist)
#OUTPUT = true,true,true,true,true,true,true,fslse

If we change the condition:

new = list(map(lambda a: (a / 4 == 2), my_list))
print(new)
#Output would be false,false,false,false,false,false,false,true

Concept:

  • The map function applies the lambda to every element in the list. Each element is checked to see if it does not satisfy the condition a / 4 == 2. This check results in a list of Boolean values (True or False).

Use Case: These transformations can include process of converting a numerical value or applying conditions to all elements in a collection. reduce comes handy to apply a lambda function cumulatively across list elements and reduce it down to a single value. This lambda hits upon two numbers one after the other.

5. Lambda with reduce :

from functools import reduce
reduce(lambda a, b: a + b, [23, 56, 43, 98, 1, 45])
#Output 266

Concept:

  • reduce is used to apply a lambda function cumulatively across elements in the list, reducing it to a single value.
  • This lambda adds two numbers at a time, moving sequentially through the list.

Use case: Aggregating a collection into a single result, like finding sums, products, or concatenating strings.

6. Lambda for Algebraic Equations:

d = lambda x, y: 3 * x + 4 * y

Represents a linear equation 3x+4y

 d = lambda x, y: 3 * x + 4 * y
 d(99.10)
 #output  337

 Concept: Compact and dynamic representation of mathematical relationships in lambda form.

Quadratic:

v = lambda a, b : (a + b) ** 2
v(4,6)
#Output = 100
v = lambda a, b : (a + b) ** 2
v(50,90)
#Output =19600

Represents (a+b)2(, compactly solving algebraic problems. Use Case (Both): It simplifies mathematical and logical code computations for concise expressions. Elements where a / 2 == 2 is filtered out in the list. Filters keep only values for which the lambda returns True. The lambda keyword. They are used for quick, one-time operations where defining a full function would be unnecessary. These functions have multiple arguments but contain only a single expression, which is automatically returned.

7. Lambda with Filtering Elements:

#with new list.
myList = [1, 2, 3, 4, 5, 6, 7]
newlist = list(filter(lambda a: (a / 2 == 2), myList))
print(newlist)

#Output = 4

Concept:

  • Filters elements satisfying a / 2 == 2 in the list.
  • Filters work by retaining only values for which the lambda returns True.

Use Case:

1 Where we remove irrelevant or unwanted items from a dataset

. 2 Adds 3 to every element of the list by using map.d) functions defined using the lambda keyword.

  8. Lambda in Transformation:

y=tuple(map(lambda x: x+3,lst))
print(y)
#OUTPUT :    (4, 5, 6, 7, 8, 9)

Concept:

  •  Uses map to add 3 to every element of the list.
  • Purpose: Transforming or processing data collection without having to manually iterate over it.

  9. lambda functions in Python with Multiple Higher-Order Functions:

r = reduce(lambda x, y: x + y, map(lambda x: x + x, filter(lambda x: (x <= 4), [1, 2, 3, 4, 5, 6, 7, 8])))

Concepts:

  • Combines filter, map, and reduce:
  • filter picks elements satisfying x≤4x \leq 4x≤4: [1, 2, 3, 4] [2, 3, 4] [3, 4][4][1, 2, 3, 4].
  • map doubles each element: [2, 4, 6, 8] [2, 4, 6, 8] [2, 4, 6, 8].
  • These would be reduced to sums and returned in 2020.
  • Use Case: Processes and aggregates data in one operation, efficiently.

General Takeaways:

  •   Lambda Basics: lambda functions in Python used for quick, inline operations, and are great for anonymous or temporary use.
  •   Filter: Filters out values based on a condition, retaining only the relevant data.
  • required.
  •   Map: Returns a new element, which is a result of applying a function to every item of collection.
  •   Reduce: Essentially, this function combines collections into a single result by applying a function step by step.Lambda concepts play a key role here—they’re crucial for functional programming in Python. They become especially useful when you need to simplify operations on lists, tuples, and other iterables.. In other words, this process streamlines data aggregation through sequential function application.
  • To put it differently, lambdas help make functional programming in Python more efficient.
  • As a result, you can perform cleaner and more concise transformations on iterables.

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *