- Hi, this is Jeff Heaton.
Welcome to Applications of deep neutral networks
with Washington University.
In this video, we're going to look at how to use
functions and even lambdas which allows you
to pass a function to a function.
This is a very powerful feature
of the Python programming language that allows us
to do very interesting things with our data processing,
particularly in the next module
when we start working with pandas.
We'll see how (mumbles) use things
like map, reduce, filter and these basic operations
that you do on collections making use of lambda functions
that you can pass in.
For the latest on my AI course and projects,
click subscribe and the bell next to it
to be notified of every new video.
Functions are very common
in many different programming languages.
They allow you to take code and package it up as a function
and not have to just duplicate it
all over the place in your program source code.
Here, I am creating a function called "say_hello."
This is how you define them in Python def, "say_hello."
You don't need anything in the function definition
to say if you're going to return something or not
that, Python knows that it's a function
by the fact that you do return something
or that it's a procedure by the fact
that you do not return something.
Then a procedure is commonly referred to as a function
that doesn't return something.
This is a procedure, so it does not return anything.
It takes a couple of values, arguments,
speaker, person to greet and the greeting.
Now, notice the equal "Hello" up here.
That means that it has a default value for greeting.
So, if you don't give it a greeting,
then it's just going to default that "Hello"
and we can run this, it calls it three times.
So, the first time it calls it
with a speaker and a person to greet.
Second time it calls it with a speaker and a person to greet
and it changes the greeting so that it's "Goodbye."
You can also use named arguments
and this is very useful if you have a lot of arguments
so that it doesn't get confusing.
You can say, okay, the speaker is Jeff,
the person to greet is John and greeting
and you can mix that order up.
It does not particularly matter
and if we run this, you can see it says,
"Hello John, this is Jeff."
"Goodbye John, this is Jeff."
and same thing for the last one.
Now, function, a true function that returns something.
Here, you'll see that I basically pass in a string
and what this does is, it strips off.
So, strip removes white space from the beginning
and the end of the string.
A string is basically an erase,
so string zero is going to be
the first character of the string.
I convert that to uppercase and then I say,
hey, I want the rest of the string from one to the end
because I don't say anything if I did one to two they would,
it would get just a portion of the string.
So, if we do that, it defines the function
and you can now call it quite easily.
I pass in "hello" with this white space
and it gives me back "Hello" with the H capitalized.
Python's map function is very useful.
This comes from functional programming map,
MapReduce of Hadoop fame.
Now this is not dealing with big data or anything like that,
it's just simply the map function.
A map takes a list of values
and applies a function to each of them.
So here, I'm going to run it.
The list just converts the output for map into a list,
so that it can be displayed
and we give it the, the process string that's the function
that we want applied across everything, across this list.
So, it is going to give me a list back,
a map always returns a list
and it is all of the values that came back
from whatever function that you had called it against.
The map is very similar to the comprehension
that we had in Python.
So, here, we are going to look at this
as a comprehension, doing basically the same thing.
So, here we're calling processing string for x in l.
Now, comprehension is a Python specific sort of thing
where as a map is common in a lot of programming languages.
I tend to use comprehensions, but others will use maps
just because they can write their codes
so that it can be understood by more
than just the Python programmers.
Because this, what this comprehension here is doing
is not necessarily clear unless you've worked with Python.
Filter is another functional sort of operator
that you, that you can deal with.
A filter takes a list and filters it down
to potentially a smaller list,
could be the same size list as what came in,
but this is defining a function that says,
is it greater than five?
and then we're taking this list
where some of the values are not greater than five
and we process that list.
So, only ten and twenty were greater than five.
The rest all got filtered out.
So, filter can be a very handy way
to basically apply a function to check, to see
if items should be included in the list or not.
Lambda is very handy.
It's a type of function, it's an anonymous function.
Usually, lambdas, you're just one line,
but they can, they can certainly be longer.
A lambda basically makes a function a expression of sorts
that you can assign right into variables.
So here, I am going to run this.
Instead of writing this big long function,
it's not that long, it's two lines, but still,
(mumbles) we write it simply as a lambda,
so that lambda replaces the greater than five function.
It's nameless, it has no name,
it is literally just passed right into the filter function.
So, if you're going to write very short functions
to be passed into things like maps and reduces and filters,
this can be a very good way to write those and then reduce.
So, (mumbles) these other things like filters and maps,
you had a list in, a list out.
This is a list in, a single value out.
So, it is reducing the list into a single value.
This is showing how to do a simple summation.
So, this is the way the reduce works.
You have two values in the lambda.
The first one is the accumulator that is coming through
and then y is the value
that you're adding to that accumulator.
Thank you for watching this module
on the Python programming language.
In the next module which is a set of parts,
we will see how to start to use the pandas library
that comes with Python that allows you
to prepare data for tabular neural networks.
This content changes often, so subscribe to the channel
to stay up to date on this course and other topics
and artificial intelligence.