0% found this document useful (0 votes)
13 views25 pages

Day 5 Machine Learning

The document discusses machine learning and provides information about Python sets, functions, arguments, lambda functions, and map functions. Sets are described including how to create, add, remove, and perform union, intersection, and difference operations on sets. Functions are defined including parameters, calling, returning values, and different argument types. Lambda functions and how to use map functions are also covered.

Uploaded by

nisha gautam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views25 pages

Day 5 Machine Learning

The document discusses machine learning and provides information about Python sets, functions, arguments, lambda functions, and map functions. Sets are described including how to create, add, remove, and perform union, intersection, and difference operations on sets. Functions are defined including parameters, calling, returning values, and different argument types. Lambda functions and how to use map functions are also covered.

Uploaded by

nisha gautam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Machine Learning

Day 5
Python - Sets
Mathematically a set is a collection of items not in any
particular order. A Python set is similar to this mathematical
definition with below additional conditions.

• The elements in the set cannot be duplicates.


• The elements in the set are immutable(cannot be
modified) but the set as a whole is mutable.
• There is no index attached to any element in a python set.
So they do not support any indexing or slicing operation.
Creating a set
A set is created by using the set() function or placing all the
elements within a pair of curly braces.

Days=set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
Months={"Jan","Feb","Mar"}
Dates={21,22,17}
print(Days)
print(Months)
print(Dates)
Accessing Values in a Set
We cannot access individual values in a set. We can only
access all the elements together as shown above. But we can
also get a list of individual elements by looping through the
set.

Days=set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
for d in Days:
print(d)
Adding Items to a Set
We can add elements to a set by using add() method. Again
as discussed there is no specific index attached to the newly
added element.

Days=set(["Mon","Tue","Wed","Thu","Fri","Sat"])
Days.add("Sun")
print(Days)
Removing Item from a Set

Remove Item

We can remove elements from a set by using discard()


method.
thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
print(thisset)

Note: If the item to remove does not exist, remove() will raise an error.
Removing Item from a Set

Remove the item by using the discard() Method

Days=set(["Mon","Tue","Wed","Thu","Fri","Sat"])
Days.discard("Sun")
print(Days)
Note: If the item to remove does not exist, discard() will
NOT raise an error.
Union of Sets
The union operation on two sets produces a new set
containing all the distinct elements from both the sets. In the
below example the element “Wed” is present in both the
sets.

DaysA = set(["Mon","Tue","Wed"])
DaysB = set(["Wed","Thu","Fri","Sat","Sun"])
AllDays = DaysA|DaysB
print(AllDays)
Intersection of Sets
The intersection operation on two sets produces a new set
containing only the common elements from both the sets. In
the below example the element “Wed” is present in both the
sets.

DaysA = set(["Mon","Tue","Wed"])
DaysB = set(["Wed","Thu","Fri","Sat","Sun"])
AllDays = DaysA & DaysB
print(AllDays)
Difference of Sets
The difference operation on two sets produces a new set
containing only the elements from the first set and none
from the second set. In the below example the element
“Wed” is present in both the sets so it will not be found in
the result set.

DaysA = set(["Mon","Tue","Wed"])
DaysB = set(["Wed","Thu","Fri","Sat","Sun"])
AllDays = DaysA - DaysB
print(AllDays)
Functions
A function can be defined as the organized block of reusable
code which can be called whenever required. A function is a
block of code which only runs when it is called.

• Python allows us to divide a large program into the


basic building blocks known as function.
• A function can be called multiple times to provide
reusability and modularity to the python program.
Function definition
In python, we can use def keyword to define the function.
Syntax:
def function_name(parameter_list):
function-definition
return <expression>

• The function block is started with the colon (:)


• All the same level block statements remain at the same
indentation.
• A function can accept any number of parameters that
Example:
def hello_world(): #function declaration
print("hello world") #function definition
hello_world() #function calling

Function parameters
The information into the functions can be passed as the
parameters. The parameters are specified in the parentheses.
• A function may have any number of parameters.
• Multiple parameters are separated with a comma.
Function calling
In python, a function must be defined before the function
calling otherwise the python interpreter gives an error. Once
the function is defined, we can call it. To call the function, use
the function name followed by the parentheses.
Example #defining the function
def sum(a,b):
c=a+b
print("Sum=",c)
#calling the function
sum(13,34)
Returning a value
def si_intst(p,r,t):
si=(p*r*t)/100
return si #calling the function
s=si_intst(20000,8.5,3)
print("Simple Interest=",s)
print("Simple Interest=",si_intst(50000,7.5,5))
Types of arguments
There may be several types of arguments which can be passed at the time
of function calling.

1. Required arguments
2. Keyword arguments
3. Default arguments
4. Variable-length arguments
Required Arguments
The required arguments are required to be passed at the time of function
calling with the exact match of their positions in the function call and
function definition. If either of the arguments is not provided in the
function call, or the position of the arguments is changed, then the
python interpreter will show the error.

Example
def calculate(a,b):
return a+b
calculate(10)
# this causes an error as we are missing a required arguments b.
Keyword arguments
Python allows us to call the function with the keyword
arguments. This kind of function call will enable us to pass the
arguments in the random order.

Example #The function simple_interest(p, t, r) is called with


the keyword arguments the order of arguments doesn't matter
in this case
def simple_interest(p,t,r):
return (p*t*r)/100
print("Simple Interest: ",simple_interest(t=10,r=10,p=1900))
• If we provide the different name of arguments at the
time of function call, an error will be thrown.
simple_interest(20000,rate=7.5, time=6) #error

• The python allows us to provide the mix of the


required arguments and keyword arguments at the time of
function call.
simple_interest(20000,t=5,r=6.5)

• The required argument must not be given after the


keyword argument.
simple_interest(20000,r=7.5,6) #error
Default Arguments
Python allows us to initialize the arguments at the function
definition. If the value of any of the argument is not provided
at the time of function call, then the default value for the
argument will be used.
def printme(name,age=22):
print("Name:",name,"\nAge:",age)
printme("Ravi") # name=Ravi age=22 (default value)
printme(“Sachin”, 33) #name =Sachin age=33
Variable length Arguments
Variable length argument is a feature that allows a function to receive
any number of arguments. However, at the function definition, we have
to define the variable with * (star) as *<variable - name >.
Example
def printme(*names):
print("printing the passed arguments...")
for name in names:
print(name)
printme('Prashant', 'Vimal', 'Gaurav', 'Jatin')
Example 2:
def adder(*num):
sum = 0
for n in num:
sum = sum + n
print("Sum:",sum)

adder(3,5)
adder(4,5,6,7)
adder(1,2,3,5,6)
Lambda function
• lambda operator or lambda function is used for creating
small, one-time and anonymous function objects.
• A lambda function can take any number of arguments, but
can only have one expression.
• lambda keyword is used to define a function.
Syntax:
lambda arguments : expression
Example:
add=lambda x,y: x+y
print(“Addition:”,add(5,6))
Map function
• Used to apply a function on all the elements of specified
iterable item.
• map() function returns a list of the results after applying the
given function to each item of a given iterable item (list,
tuple etc.)
Syntax :
map(function, iterable_item)
Ex: def multiply2(x):
return x * 2
list=[1,2,3,4]
m=[*map(multiply2, list)] # Output [2, 4, 6, 8]
print((m))
Lambda and map
Square1 = [2,5,6,7,4]
Square2= [*map(lambda x:x **2, Square1)]
print([(Square2)])

You might also like