Deep Dive in Machine Learning with Python

Part — VI: List Comprehensions and Functions in Python

Rajesh Sharma
Analytics Vidhya
9 min readDec 21, 2019

--

Welcome to the sixth blog of Deep Dive in Machine Learning with Python, in the last blog (Deep Dive in ML with Python — Part-V) we discussed the live real-time scenarios of Tuples and Dictionaries.

In today’s blog, we will walk through with List Comprehensions and Functions which are two of the most widely used python components.

Python Functions

A function is a block of reusable code that is used to perform a one or multiple actions.

Python already provides several built-in functions such as print(), type() and others, however, you can also create your own customized functions. These functions are known as user-defined functions.

User-defined functions

In Python, users can create two types of functions:

1. Functions with the return statement

These are the functions that execute some code then return the output when the function been invoked or called.

2. Functions without return statement

These are the functions that perform some operations then instead of returning the result, they print the output on the console.

Problem-1: How to create a user-defined function?

CASE-I: Function with a return statement

Solution-1.1

In the above example, we created a function ‘add_two_numbers’ that accepts two input parameters ‘num1’ and ‘num2’. And, it returns the addition of both the input parameters.

CASE-II: Function with a print statement instead of a return statement

Solution-1.2

Here, the above function is only printing the output using a print() statement instead of returning the outcome.

CASE-III: Function which overwrites the values of the arguments and returns the result

Solution-1.3

Here, in this example, although we provided the values of arguments(i.e. num1 as 9 and num2 as 3) while invoking the function but within the function definition we overwrite their values as 16 and 8 respectively.

Thus, the result printed as 2.0.

Problem-2: What are the ways to invoke/call the user-defined function?

CASE-I: Explicit function call

Solution-2.1

As shown in the above line, we invoked the used-defined function ‘add_two_numbers’ and passed the two parameters ‘num1’ as 7 and ‘num2’ as 8 by explicitly writing their names.

In the explicit function call, the position/order of its parameters doesn’t matter because we provide the parameter name along with its value.

CASE-II: Implicit function call

Solution-2.2

In the above example, we invoked the user-defined function ‘add_two_numbers’ and passed the values of parameters ‘num1’ and ‘num2’ implicitly. That means the values will be picked up based on their position in which parameters were defined in the function definition. Thus, 7 will go to num1 and 8 to num2.

Problem-3: How to write an efficient and descriptive docstring of a user-defined function?

Solution-3

In the above example, we added the docstring to the function and mentioned three major categories:

1. Description: It explains the objective of the function

2. Parameter: It tells about the parameters that the function can accept

3. Return: It mentions about the output that function will return

Problem-4: How to view the docstring of a user-defined function?

Solution-4

So, by providing the docstring while defining the function facilitates its description for future use.

Problem-5: What are the types of function parameters?

There are several kinds of function parameters:

1. Required parameters

Parameters whose values need to be provided while invoking the function are known as Required parameters.

CASE-I: Providing value for all the parameters

Solution-5.1.1

So, here in the above example, a function complete_name has been created and it accepts two parameters f_name and l_name. Both these parameters are required parameters and complete_name expects their values whenever it is been invoked.

Solution-5.1.2

Here, we provided both the arguments f_name as ‘Jetts’ and l_name as ‘Watson’. And, got the expected output ‘Jetts Watson’. Now, let’s try calling the same function with only one parameter.

CASE-II: Providing value for only one required parameter

Solution-5.2

As you can see in the above error, complete_name function expects the values of both required arguments. However, we only provided f_name as ‘Jetts’ and skipped l_name.

Thus, got the ‘missing 1 required positional argument’ error.

2. Default Parameters

These are the kind of parameters which considers a default value if a value is not provided in the function call.

CASE-I: Defining a function with default parameter

Solution-2.1

In this example, we created a function ‘batsmen_score’ with 3 arguments ‘first_name’, ‘last_name’ and ‘team’.

Out of these, ‘first_name’ and ‘last_name’ are the required parameters, however, ‘team’ is a default parameter which means if its value is not provided in function then the default value ‘India’ will be used.

CASE-II: Calling the function and passing a value to default parameter as well

Solution-2.2

Thus, the above example demonstrates the use of default parameters.

3. Variable-length parameters

These are the kind of arguments which we do not specify in the function definition like Required or Default parameters. However, the function still processes them.

Example-I

Solution-3.1

As we saw in the above cell, the last 3 values(‘ICC World Cup 2007’, ‘ICC Champions Trophy 2012’, ‘VB Series 2013’) in the function call were treated as Variable-length parameters and not named in the function definition.

An asterisk (*) is placed before the variable name that holds the values of all the non-keyword variable parameters.

Example-II

Solution-3.2

In this example, only the required parameters were passed to the function. As mentioned above, variable-length parameters are the nonrequired parameters. So, even if their values are not provided in the function call, you won’t receive any error.

Here, along with the required parameters we also facilitated the values of variable-length parameters in the function call. Thus, the same got displayed in the output.

Anonymous Function

A function is called the Anonymous when it is not declared in the standard fashion by using the ‘def’ keyword. We use ‘lambda’ keyword to create an anonymous function. I prefer using ‘lambda’ anonymous functions because they are simple, easy to use and most importantly powerful enough to perform any operation.

Let’s try to solve some problems using ‘lambda’ functions:

Problem-1: How to create a lambda function?

Solution-1

As shown in the above cell, we create an Anonymous function by using ‘lambda’ keyword. In this example, the function accepts 2 parameters x and y. And returns their sum that means x + y.

Problem-2: How to add two numbers by using lambda?

Solution-2

Here as you can see just a simple way to add two numbers via ‘lambda’.

Problem-3: How to add a number to every value of a list using lambda?

Solution-3

So, in this example, we added 10 to every element of a list.

Problem-4: How to assign a lambda function to a python variable object?

Solution-4

In this example, we defined the lambda function and assigned it to a python object ‘cal_power_of_number’, then passed the values of the parameters to this object which in the backend runs the lambda function.

Problem-5: How to define a lambda function inside a standard function?

Solution-5

Here, the ‘numbers_multlipication’ function invokes the lambda function and passes the parameters to it.

MAP Function

It is the function that returns a list of the results after applying a function to each element of an iterable (list, tuple, etc.)

MAP, REDUCE and FILTER Functions

Problem-1: How to use MAP function to apply over an iterable?

Solution-1

This is the way to map the function on any iterable(e.g. list, we also used student_scores in the above example)

Problem-2: How to get the resultant values after applying the MAP function to an iterable?

Solution-2

That’s how you can obtain the resultant values after applying the MAP function to an iterable.

Problem-3: How to use MAP function to apply the LAMBDA function on an iterable?

Solution-3

In the above example, we map the lambda function on an iterable(i.e. batsmen_last5_runs)

REDUCE Function

The reduce function applies a particular function passed in its argument to all the list elements. It is defined in the ‘functools’ module.

Problem-1: How to import REDUCE function?

Solution-1

Problem-2: How to apply the REDUCE operation by using lambda function on an iterable?

Solution-2

In this example, we applied the reduce function on ‘batsmen_scores_in_a_series’ to get the total runs scored by a batsman in a series.

Problem-3: How to apply the REDUCE operation on an iterable inside the standard function?

Solution-3

Here, we used the variable-length arguments and applied the REDUCE function on the ‘fruits’ list to obtain the count of fruits.

FILTER Function

The filter() method filters the data by using a function that validates whether each element satisfies the particular condition or not.

Problem-1: How to apply the filter method on the data inside a standard function?

Solution-1

In the above example, we simply applied the ‘filter_int_values’ function on the iterable [20,40,‘Mathew’]. Thus, the non-integer values of the iterable got printed.

Problem-2: How to apply filter method by using the LAMBDA function?

Solution-2

In the above example, we just dropped the records which were not divisible of 2.

LIST Comprehension

It is a way to define and create a list in python. When we can create lists based on some statements/conditions in one line only is known as List Comprehensions.

Problem-1: How to create a list by a range of numbers?

Solution-1

In the above example, we created the list of numbers from 1 to 39 based on a number greater than 10 and divisible of 2.

Problem-2: How to create a list of numbers by using LAMBDA and REDUCE function?

Solution-2

In the above example, we created the list of numbers that are greater than 10 but smaller than 30 and divisible of 5.

Congratulations, we come to the end of this blog, to summarize, we worked with various python functions and list comprehensions. Next blog onwards we will start with Data Wrangling LibraryPandas.

If you want to download the Jupyter Notebook of this blog, then kindly access below GitHub repository:

https://github.com/Rajesh-ML-Engg/Deep_Dive_in_ML_Python

Thank you and happy learning!!!!

Blog-7: All about Pandas

--

--

Rajesh Sharma
Analytics Vidhya

It can be messy, it can be unstructured but it always speaks, we only need to understand its language!!