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

Chapter 3

Uploaded by

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

Chapter 3

Uploaded by

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

Part 3 :

Object Oriented Programming (OOP), Introduction to NumPy


INF4071 MACHINE ET DEEP LEARNING
(FONDAMENTALES)
Zakaria KERKAOU
[email protected]
Object Oriented Programming
Object Oriented programming (OOP) is a programming paradigm that relies on the
concept of classes and objects.

It is used to structure a software program into


simple, reusable pieces of code blueprints
(usually called classes), which are used to
create individual instances of objects.
The structure of object-oriented
programming
• Classes are user-defined data types that act as the blueprint for individual objects,
attributes and methods.
• Objects are instances of a class created with specifically defined data. Objects can
correspond to real-world objects or an abstract entity. When class is defined
initially, the description is the only object that is defined.
• Methods are functions that are defined inside a class that describe the behaviors of
an object. Each method contained in class definitions starts with a reference to an
instance object. Additionally, the subroutines contained in an object are called
instance methods. Programmers use methods for reusability or keeping
functionality encapsulated inside one object at a time.
• Attributes are defined in the class template and represent the state of an object.
Objects will have data stored in the attributes field. Class attributes belong to the
class itself.
Main principles of OOP

• Encapsulation. This principle states that all important information is contained


inside an object and only select information is exposed.
• Abstraction. Objects only reveal internal mechanisms that are relevant for the use
of other objects, hiding any unnecessary implementation code.
• Inheritance. Classes can reuse code from other classes. Relationships and
subclasses between objects can be assigned, enabling developers to reuse common
logic while still maintaining a unique hierarchy.
• Polymorphism. Objects are designed to share behaviors and they can take on more
than one form. The program will determine which meaning or usage is necessary
for each execution of that object from a parent class,
Class

A class is user-defined data types that act a blueprint for the object.
Let’s take as example a class parrot can be :

We use the class keyword to define an empty class Parrot. From class, we
construct instances. An instance is a specific object created from a particular
class.
Object

An object (instance) is an instantiation of a class. When class is defined, only


the description for the object is defined. Therefore, no memory or storage is
allocated.

Here, obj is an object of class Parrot.


Creating Class and Object in Python.
we created a class with the name Parrot. Then, we define attributes. The attributes
are a characteristic of an object.


These attributes are defined inside the
__init__ method of the class. It is the initializer
method that is first run as soon as the object is
created.

species is what we call class attributes are the
same for all instances of a class.

We create instances of the Parrot class. Here,
blu is a reference (value) to our new objects.
Methods
Methods are functions defined inside the body of a class. They are used to define
the behaviors of an object.


In this program, we define two methods
i.e sing() and dance(). These are called
instance methods because they are
called on an instance object i.e blu.
Static Methods
Static methods in Python are extremely similar to python class level methods, the
difference being that a static method is bound to a class rather than the objects for
that class.
Using the function staticmethod():

Using the annotation @staticmethod


Encapsulation
Encapsulation in OOP is the
concept of restricting access to
methods and variables.
This prevents data from direct
modification which is called
encapsulation.
In Python, we denote private
attributes using underscore as the
prefix i.e single _ or double __.
Inheritance
Inheritance is a way of creating a new class for using details of an existing class
without modifying it. The newly formed class is a derived class (or child class).
Similarly, the existing class is a base class (or parent class).
Inheritance : Polymorphism
In this example, notice that whosiThis() method was defined in both classes,
Penguin as well as Bird. When this happens, the method in the derived class
overrides that in the base class. This is to say, whosiThis() in Penguin gets preference
over the whosiThis() in Bird.
Inheritance : Python Multiple Inheritance
 A class can be derived from more than one base class in Python, similar to C++. This is
called multiple inheritance.
 In multiple inheritance, the features of all the base classes are inherited into the
derived class.
 Every class in Python is derived from the object class. It is the most base type in
Python.
 So technically, all other classes, either built-in or user-defined, are derived classes and
all objects are instances of the object class.
Python Operator Overloading (1/2)

Python operators work for built-in classes.


But the same operator behaves differently
with different types. For example, the +
operator will perform arithmetic addition on
two numbers, merge two lists, or
concatenate two strings.

This feature in Python that allows the same


operator to have different meaning
according to the context is called operator
overloading.
Python Operator Overloading (2/2)
 Addition p1 + p2 p1.__add__(p2)
 Subtraction p1 - p2 p1.__sub__(p2)
 Multiplication p1 * p2 p1.__mul__(p2)
 Power p1 ** p2 p1.__pow__(p2)
 Division p1 / p2 p1.__truediv__(p2)
 Floor Division p1 // p2 p1.__floordiv__(p2)
 Remainder (modulo) p1 % p2 p1.__mod__(p2)
 Bitwise Left Shift p1 << p2 p1.__lshift__(p2)
 Bitwise Right Shift p1 >> p2 p1.__rshift__(p2)
 Bitwise AND p1 & p2 p1.__and__(p2)
 Bitwise OR p1 | p2 p1.__or__(p2)
 Bitwise XOR p1 ^ p2 p1.__xor__(p2)
 Bitwise NOT ~p1 p1.__invert__()
Basics
What is NumPy?

 NumPy is a Python library used for working with arrays.


 It also has functions for working in domain of linear algebra, fourier transform,
and matrices.
 NumPy was created in 2005 by Travis Oliphant. It is an open source project and
you can use it freely.
 NumPy stands for Numerical Python.
Why Use NumPy?

 NumPy arrays are stored at one continuous place in memory unlike lists, so
processes can access and manipulate them very efficiently.
 NumPy is a Python library and is written partially in Python, but most of the parts
that require fast computation are written in C or C++.
 Numpy is optimized to work with latest CPU architectures.
NumPy Creating Arrays

 NumPy is usually imported under


the np alias.

 NumPy is used to work with arrays.


The array object in NumPy is called
ndarray.

 We can create a NumPy ndarray


object by using the array() function.
Dimensions in Arrays
 0-D arrays, or Scalars, are the
elements in an array. Each value in
an array is a 0-D array..

 An array that has 0-D arrays as its


elements is called uni-dimensional
or 1-D array.

 An array that has 1-D arrays as its


elements is called a 2-D array.

 An array that has 2-D arrays NumPy Arrays provides the ndim attribute
(matrices) as its elements is called that returns an integer that tells us how
3-D array. many dimensions the array have.
Array Indexing
 Array indexing is the same as accessing an array element. You can access an array
element by referring to its index number.
Array Slicing
 Slicing in Numpy arrays works similarly to slicing in lists. Which means taking
elements from one given index to another given index. We pass slice instead of
index like this: [start:end:step].

 Example : slicing for 2-D array:


Array Reshaping
Reshaping means changing the shape of an array.
The shape of an array is the number of elements in each dimension.
By reshaping we can add or remove dimensions or change number of elements in each
dimension.

 Example:
Searching Arrays
You can search an array for a certain value, and return the indexes that get a match.
To search an array, use the where() method.
numpy.where(condition, [x, y, ]/) Return elements chosen from x or y depending on
condition.

This example will return a


tuple: (array([3, 5, 6],) Which
means that the value 4 is present
at index 3, 5, and 6.
Filter Array
Getting some elements out of an existing array and creating a new array out of them is
called filtering.
In NumPy, you filter an array using a boolean index list. If the value at an index is True
that element is contained in the filtered array, if the value at that index is False that
element is excluded from the filtered array.

This example will return [41, 43], Because


the new filter contains only the values
where the filter array had the value True,
in this case, index 0 and 2.

You might also like