Day 5 Machine Learning
Day 5 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.
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
Note: If the item to remove does not exist, remove() will raise an error.
Removing Item from a Set
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.
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.
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)])