Here in this post, we will see how to define a function in python along with the importance of Functions in Python for Machine Learning and Data Analysis. We’ll start with the definition of function and then see some basic syntax with examples and in last we’ll discuss some use cases in the real world.
What is a function?
Function save our time and complexity in our code. Basically, functions are used so that we can reuse our subset of code in our program whenever we need it.
Basic Function Syntax in Python
Here shown below is a basic syntax and example to show you, how you can define a function:
def function_name(): code
Basically, a function has the following things:
- def keyword – This makes python knows that we are defining a user-defined function.
- function name – the name by which we can easily call our function whenever we need it later
- Code – Code that we want our function to execute whenever we call it.
And when we want to use it we can call a function, use the function name followed by parenthesis:
function_name()
For example, let’s say we want our function to print “Hello” with the name of the user, we can write a function as follows:
def func(name): print("Hello" +" " + name)
And when you want to call it, pass the name to the function as a parameter in string format and it will return the desired output:
func("Rahul") Hello Rahul
Here below shown are other ways we can define our function with one, two or n number of parameters:
- When we want our function to return a value –
def square(value): new_value = value ** 2 return new_value Output- number = square(4) print(number) 16
- When we want our function to accept multiple parameters and returns and return value :
def square(value1, value2): new_value = value1 ** value2 return new_value Output- number = square(4,4) print(number) 256
- When we want our function to return multiple values:
def square(value1, value2): new_value1 = value1 ** value2 new_value1 = value2** value1 #To return multiple values we have to use tuples. new_value = (new_value1, new_value2) return new_value Output- number = square(2,3) print(number) (8,9)
- Default and Flexible arguments:
When you want to define default arguments, means that if someone doesn’t pass any value for that argument it will automatically take the default value which you have defined in your function. For example,
def power(num pow=2): new_value = num ** pow return new_value output- power(9,1) 9 power(9) 81 power(3,4) 81
- Flexible arguments : *args
Below is a function that will take any number of values and provides a sum of them.
def add_all(*args): #Initialize sum_all sum_all = 0 #Accumulate the sum for num in args: sum_all += num return sum_all Output- add_all(1) 1 add_all(1,2) 3 add_all(5,10,15) 30
Why use functions in Python for Machine Learning and Data Analysis?
So now the question came that why we need to use functions for Machine Learning and Data Analysis. Let’s try to understand the importance of functions in Python for Machine Learning and Data Analysis with a few examples that we came across in a real-world environment :
When you are working on a problem where you have to check different machine learning models to get better metrics like accuracy and then plotting your results using data visualization packages like Matplotlib or seaborne. When in such situations you have to two options, one you can write code again and again for the same task making it a time-consuming thing or simply define a function which takes parameters for each model and plot it accordingly. So which one you prefer, I guess the latter one as it saves your time and reduces the complexity of your code.
Also, we know that when the number of features increases in a dataset, the number of plots we can plot will also increase. So, after performing feature engineering, we can define a function that can take a different combination of features and creates plots automatically.
The above examples are only a few out of n number of uses depends on your use case and dataset.
Conclusion
In a nutshell, we can say that by defining a function we reduce the complexity of our code and the time it takes to run. Also, it will help us automate the time-consuming process.
Leave a Reply