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

Machine_Learning_Day_5_ppt (1)

Uploaded by

photukhech
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)
26 views25 pages

Machine_Learning_Day_5_ppt (1)

Uploaded by

photukhech
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

NATIONAL INSTITUTE OF ELECTRONICS AND INFORMATION

TECHNOLOGY

Setting Up User Accounts

Machine Learning Using


Python
1
Day 5
Course: Machine Learning using Python
Day : 5
OOPs Concepts
Python is an object-oriented programming language. It allows us to
develop applications using an Object Oriented approach. In Python,
we can easily create and use classes and objects.
• Object
• Class
• Encapsulation
• Method
• Inheritance
• Polymorphism
• Data Abstraction
Course: Machine Learning using Python
Day : 5

Object
The object is an entity that has state and behavior. It may be any real-
world object like the mouse, keyboard, chair, table, pen, etc. Everything
in Python is an object, and almost everything has attributes and methods.

Class
The class can be defined as a collection of objects. It is a logical entity
that has some specific attributes and methods. For example: if you have
an employee class then it should contain an attribute and method, i.e. an
email id, name, age, salary, etc.
Course: Machine Learning using Python
Day : 5

Encapsulation

In encapsulation, code and data are wrapped together within a


single unit defined as class.

Method

The method is a function that is associated with an object.


Course: Machine Learning using Python
Day : 5
Inheritance

It specifies that the child object acquires all the properties and
behaviors of the parent object. A class may use all the
properties and behavior of another class.
• The new class is known as a derived class or child class.
• The class whose properties are acquired is known as a
base class or parent class.
• It provides re-usability of the code.
Course: Machine Learning using Python
Day : 5
Polymorphism

Polymorphism contains two words "poly" and "morphs". Poly means


many and Morphs means form, shape. By polymorphism, we understand
that one task can be performed in different ways. A class may have more
than two methods with same name with different parameters.

Data Abstraction

Abstraction is used to hide internal details and show only functionalities.


Abstracting something means to give names to things so that the name
captures the core of what a function or a whole program does.
Course: Machine Learning using Python
Day : 5
Creating classes in python

In python, a class can be created by using the keyword class


followed by the class name.
Syntax
class ClassName:
#class_members
A class contains a class_members including fields,
constructor, function, etc.
Course: Machine Learning using Python
Day : 5
Example
class data:
id=101
name=”Abhishek”

def display(s):
print(“ID=”,s.id)
print(“Name”,s.name)

The s parameter is a reference to the current instance of the


class, and is used to access variables that belong to the class.
Course: Machine Learning using Python
Day : 5
Creating objects
A class can be instantiated by calling the class using the class
name.
Syntax:
<object-name> = <class-name> (<arguments>)

Ex: m=data()
m.display() #calling display function using object of
class my.
Course: Machine Learning using Python
Day : 5
Access Specifier
Public:
By default, all the variables and member functions of a class are
public in a python program.
Ex: x #will be public member of the
class
Private:
A variable can be made private by prefixing the __(double
underscore) before the variable name.
ex: __x will be the private member of the class.
Protected:
A variable can be made public by prefixing the _ (single
underscore) before the variable name.
Course: Machine Learning using Python
Day : 5
Constructor
A constructor is a special type of method (function) which is used to initialize the
members of the class. Constructor definition is executed when we create the object of
this class.
Types.
1. Parameterized Constructor
2. Non-parameterized Constructor
Course: Machine Learning using Python
Day : 5
Creating the constructor

In python, the method __init__ is used to create the constructor of the class. This
method is called when the object of the class is instantiated.

• We can pass any number of arguments at the time of creating the class object,
depending upon __init__ definition.

• It is mostly used to initialize the class attributes. Every class must have a
constructor, even if it simply relies on the default constructor.
Course: Machine Learning using Python
class my: Day : 5
Example of Constructor
id=101
name=“Rajat"

def __init__(a): #non parameterized constructor


print("Object is created")
def display(s):
print("ID=",s.id)
print("Name=",s.name)
m=my()
m.display()
Course: Machine Learning using Python
Day : 5
Parameterized Constructor
class emp
emp_id=0
emp_name=0
emp_salary=0
def __init__(s,i,n,sal) #parameterized constructor
s.emp_id=i
s.emp_name=n
s.emp_salary=sal

def display(s)
print(“Employee ID=”,s.emp_id, “\nEmployee
Name=”,s.emp_name,”\nSalary=”,s.emp_salary)
E=emp(101, “Rahul”, 36000)
E.display()
Course: Machine Learning using Python
Day : 5

Modify Object Properties


The previously initialized variable can be modified as
e.salary=45000

Delete Object Properties


• The del keyword is used to delete object properties.
del e.emp_id #deletes the value of emp_id
• The objects can also be deleted by using del keyword.
del e #deletes the object e
Course: Machine Learning using Python
Day : 5
Python Inheritance

Inheritance allows us to define a class that inherits all the methods and
properties from another class.

• Parent class is the class being inherited from, also called base
class.

• Child class is the class that inherits from another class, also
called derived class. The child class acquires the properties and can
access all the data members and functions defined in the parent class.
Course: Machine Learning using Python
Day : 5

Syntax:
class class_name(base_class_name):
class definition

• A class can inherit multiple classes by mentioning all


of them inside the bracket.
class class_name(baseclass1, baseclass2,…..):
class definition
Course: Machine Learning using Python
Day : 5
Types of Inheritance in Python
Python - Multiple Inheritance
Multiple Inheritance means that you're inheriting the property of
multiple classes into one. In case you have two classes, say A
and B, and you want to create a new class which inherits the
properties of both A and B, then:

class A:
# variable of class A
# functions of class A

class B:
# variable of class A
# functions of class A

class C(A, B):


# class C inheriting property of both class A and B
# add more properties to class C
Course: Machine Learning using Python
Day : 5
Multiple inheritance
Python provides us the flexibility to inherit multiple base classes in the child class.

class Calculation1:
def Summation(self,a,b):
return a+b;
class Calculation2:
def Multiplication(self,a,b):
return a*b;
class Derived(Calculation1,Calculation2):
def Divide(self,a,b):
return a/b;
d=Derived()
print(d.Summation(10,20))
print(d.Multiplication(10,20))
print(d.Divide(10,20))
Course: Machine Learning using Python
Day : 5
Types of Inheritance in Python
Python - Multilevel Inheritance
In multilevel inheritance, we inherit the classes at multiple
separate levels. We have three classes A, B and C, where A is
the super class, B is its sub(child) class and C is the sub class of
B.

Here is a simple example, its just to explain you how this looks
in code:

class A:
# properties of class A
class B(A):
# class B inheriting property of class A # more
properties of class B
class C(B):
# class C inheriting property of class B
Course: Machine Learning using Python
Day : 5
Multi-Level Inheritance

class Animal:
def speak(self):
print("Animal Speaking")
class Dog(Animal):
def bark(self):
print("dog barking")
class DogChild(Dog):
def eat(self):
print("Eating bread...")
d = DogChild()
d.bark()
d.speak()
d.eat()
Course: Machine Learning using Python
Day : 5
Method Overloading
In Python you can define a method in such a way that there are
multiple ways to call it. Depending on the function definition, it can be
called with zero, one, two or more parameters. This is known as
method overloading.
In the given code, there is a class with one method sayHello(). We
rewrite as shown below. The first parameter of this method is set to
None, this gives us the option to call it with or without a parameter.
An object is created based on the class, and we call its method using
zero and one parameter. To implement method overloading, we call the
method sayHello() in two ways: 1. obj.sayHello() and
2.obj.sayHello('Rambo')
class Human:
def sayHello(self, name=None):
if name is not None:
print 'Hello ' + name
else:
print 'Hello '
obj = Human()
Course: Machine Learning using Python
Day : 5
Method Overriding
We can provide some specific implementation of the parent class
method in our child class. When the parent class method is defined in
the child class with some specific implementation, then the concept is
called method overriding. We may need to perform method overriding
in the scenario where the different definition of a parent class method
is needed in the child class.

Consider the following example to perform method overriding in


python.

class Animal:
def speak(self):
print("speaking")
class Dog(Animal):
def speak(self):
print("Barking")
d=Dog()
d.speak()
Course: Machine Learning using Python
Day : 5
Example of method overriding
class Bank:
def getroi(self):
return 10;
class SBI(Bank):
def getroi(self):
return 7;
class ICICI(Bank):
def getroi(self):
return 8;
b1=Bank()
b2=SBI()
b3=ICICI()
print("Bank Rate of interest:",b1.getroi());
print("SBI Rate of interest:",b2.getroi());
print("ICICI Rate of interest:",b3.getroi());
Course: Machine Learning using Python
Day : 5
Tomorrow Agenda

Python : Module and


Numpy

THANKS

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy