Part1_Cours_Python

Download as pdf or txt
Download as pdf or txt
You are on page 1of 62

Programmation Avancée en Python

Pr. Mohamed NAJI


Why Programing ?

We know the We DON’T know


program the program

Programing vs. Machine Learning Programing


Machine Learning
Supervised ML

Teach the
machine
some
features
Supervised ML

Teach the
machine
Thesome
machine can
learn by itself and
features
predicts a
solution/result
Outline

• Basics – Fundamentals of Python

• Programming with Python

• Working with datasets

• Machine Learning programing


Program Computer
qSequence of instructions stored inside computer
• Built from predefined set of primitive instructions
1. arithmetic and logic
2. simple test
3. moving data
qSpecial program (interpréter) executes each instruction in order
• Use tests to change flow of control through sequence
• Stop when done
Computers are Machines
• Fixed program : calculator
• Stored program : machines store and execute

Recipe and Algorithm


1. Sequence of simple steps
2. Flow of control process that specifies when each
step is executed
3. A means of determining when to stop
IPython: Beyond Normal Python
IPython + text editor è Python environment
Launch IPython Shell
IPython notebook : Jupyter notebook
Help and Documentation in IPython
• How do I call this function? What arguments and options does
it have?
• What does the source code of this Python object look like?
• What is in this package I imported? What attributes or methods
does this object have?
help() built-in function in python
Ipython : Jupyter notebook – help
IPython introduces the ? character as a shorthand for accessing this
documentation and other relevant information:

This notation works for just about anything, including object methods:
In [3]: L = [1, 2, 3]
In [4]: L.insert?
Type: builtin_function_or_method
String form: <built-in method insert of list object at 0x1024b8ea8>
Docstring: L.insert(index, object) -- insert object before index

or even objects themselves, with the documentation from their type:


In [5]: L?
Type: list
String form: [1, 2, 3]
Length: 3 Docstring: list() -> new empty list
list(iterable) -> new list initialized from iterable's items
Ipython : Jupyter notebook – help
Importantly, this will even work for functions or other objects you create yourself! Here
we'll define a small function with a docstring:
In [6]: def square(a):
....: """Return the square of a."""
....: return a ** 2

Now we'll use the ? mark to find this doc string:


In [7]: square?
Type: function String form: <function square at 0x103713cb0>
Definition: square(a)
Docstring: Return the square of a.

Accessing source code with ??


In [8]: square??
Type: function String form: <function square at 0x103713cb0>
Definition: square(a)
Source: def square(a):
"Return the square of a"
return a ** 2
Exploring Modules with Tab-Completion
Tab-completion of object contents
In [10]: L.<TAB>
Tab completion when importing
L.append L.copy L.extend L.insert Tab completion is also useful when importing objects
L.remove L.sort L.clear L.count L.index from packages. Here we'll use it to find all possible
L.pop L.reverse imports in the itertools package that start with co:

To narrow-down the list, you can type the first character


or several characters of the name, and the Tab key will
find the matching attributes and methods
IPython Magic Commands
Running External Code: %run

For example, imagine you've created a myscript.py file with the following contents:
#----------- # file: myscript.py
def square(x):
"""square a number"""
return x ** 2
1 squared is 1
for N in range(1, 4):
print(N, "squared is", square(N))
2 squared is 4
3 squared is 9
You can execute this from your IPython session as follows:
In [6]: %run myscript.py
Timing Code Execution: %timeit
In [9]: %%timeit
...: L = []
...: for n in range(1000):
...: L.append(n ** 2)
...:
1000 loops, best of 3: 373 µs per loop

In [10]: %timeit comprehensive list


L = [n ** 2 for n in range(1000)] 10% faster than the
equivalent for-loop
1000 loops, best of 3: 325 µs per loop
construction

Other magic commands In [11]: %magic


See help In [12]: %lsmagic
In and Out Objects Related Magic Commands
In [1]: import math For accessing a batch of previous inputs at
In [2]: math.sin(2) once, the %history magic command is
Out[2]: 0.9092974268256817 very helpful.
In [3]: math.cos(2)
Out[3]: -0.4161468365471424 Here is how you can print the first four
inputs:
In [4]: print(In)
['', 'import math',
'math.sin(2)’, 'math.cos(2)’, In [16]: %history -n 1-4
'print(In)'] 1: import math
2: math.sin(2)
3: math.cos(2)
In [5]: Out
4: print(In)
Out[5]: {2: 0.9092974268256817,
3: -0.416146836547142}

In [6]: print(In[1])
Data types: Python Integer
x = 1
• Python is coded in C
struct _longobject {
long ob_refcnt;
PyTypeObject *ob_type;
size_t ob_size;
long ob_digit[1];
};
A single integer in Python 3.4 actually contains four pieces:
•ob_refcnt, a reference count that helps Python silently handle memory allocation
and deallocation
•ob_type, which encodes the type of the variable
•ob_size, which specifies the size of the following data members
•ob_digit, which contains the actual integer value that we expect the Python variable
to represent.
Data types: Python List

Heterogenous list
List of strings
The Python list, contains a pointer to a
block of pointers, each of which in turn
points to a full Python object like the
Python integer we saw earlier
The array essentially contains a single
pointer to one contiguous block of data
Arrays from scratch
Numpy data types
The Basics of NumPy Arrays¶
• NumPy Array Attributes
Array indexing
Array Slicing: Accessing Subarrays
Just as we can use square brackets to
access individual array elements, we can
also use them to access subarrays with
the slice notation, marked by the colon (:)
character. The NumPy slicing syntax
follows that of the standard Python list; to
access a slice of an array x, use this:
x[start:stop:step]
If any of these are unspecified, they
default to the values start=0, stop=size of
dimension, step=1. We'll take a look at
accessing sub-arrays in one dimension and
in multiple dimensions.

x[5::-2]
Array Slicing: Accessing Subarrays
• Array slices return views rather
than copies of the array data. This is one
area in which NumPy array slicing differs
from Python list slicing: in lists, slices will be
copies.
Reshaping of Arrays Concatenate
The most flexible way of doing this is with
the reshape method. For example, if you want to put
the numbers 1 through 9 in a 3×3 grid, you can do the
following

You can also concatenate more than two


arrays at once

• Reshape 2D into 1D row or vector


Computation on NumPy
Arrays: Universal
Functions

• Ufuncts using vectorized functions are


more efficient than looping through all
the elements of the array
Ufuns

Operator Equivalent ufunc Description


+ np.add Addition (e.g., 1 + 1 = 2)
- np.subtract Subtraction (e.g., 3 - 2 = 1)
- np.negative Unary negation (e.g., -2)
* np.multiply Multiplication (e.g., 2 * 3 = 6)
/ np.divide Division (e.g., 3 / 2 = 1.5)
// np.floor_divide Floor division (e.g., 3 // 2 = 1)
** np.power Exponentiation (e.g., 2 ** 3 = 8)
% np.mod Modulus/remainder (e.g., 9 % 4 = 1)
• Absolute value

• Trigonometric operations
• Aggregate

• Min/Max of each column/row


Introducing
Broadcasting
•Rule 1: If the two arrays differ in
their number of dimensions, the
shape of the one with fewer
dimensions is padded with ones on
its leading (left) side.
•Rule 2: If the shape of the two
arrays does not match in any
dimension, the array with shape
equal to 1 in that dimension is
stretched to match the other shape.
•Rule 3: If in any dimension the sizes
disagree and neither is equal to 1, an
error is raised.
Examples
Pandas
Pandas data structures: the Series, DataFrame, and Index

The Pandas Series Object


A Pandas Series is a one-dimensional array of indexed data. It can be created from a list or array as follows:

Series = pd.Series([list1, list2, …]) Series = pd.Series(np.array([1, 2, 3]))


Pandas Series indexing
Series as specialized dictionary
Dict = { ‘key1’ : value1 ,
‘key1’ : value1 ,
‘key1’ : value1 ,
‘key1’ : value1 ,
‘key1’ : value1 ,
}
Dictionnary = pd.Series(Dict)
The Pandas DataFrame Object
• If a Series is an analog of a one-dimensional array with flexible
indices, a DataFrame is an analog of a two-dimensional array with
both flexible row indices and flexible column names.
Constructing DataFrame objects
The Pandas Index Object
• This Index object is an interesting structure, and it can be thought of
either as an immutable array or as an ordered set (technically a
multi-set, as Index objects may contain repeated values).
Indexing and immutable index
Operations
• Unions, intersections, differences, and other combinations can
be computed

The caret (^) operator is used for


the symmetric difference of two
sets.
Data Indexing and Selection
• Indexers: loc, iloc, and ix
Explicit Implicit indexer
DataFrame as 2D array
Implicit indexing Explicit indexing

ix indexer
Masking and fancy indexing Ufuncs
Time taken for operation as an object NaN - data virus
NaN in Pandas
Typeclass Conversion When Storing NAs NA Sentinel Value
floating No change np.nan
object No change None or np.nan
integer Cast to float64 np.nan
boolean Cast to object None or np.nan

Example
Operating on NaN values

•isnull(): Generate a boolean mask indicating missing values


•notnull(): Opposite of isnull()
•dropna(): Return a filtered version of the data
•fillna(): Return a copy of the data with missing values filled or imputed

Drop all columns axis =


1 having NA values
MatPlotlib

• show() or plt.show()

It needs to be done only once per kernel/session è Embeded PNG plot per Kernel
Plot and save to file MATLAB-style Interface
Object-oriented interface
The object-oriented interface is available for these more complicated situations, and for when you want
more control over your figure. Rather than depending on some notion of an "active" figure or axes, in the
object-oriented interface the plotting functions are methods of explicit Figure and Axes objects.

The choice of which style to use is largely a matter of preference, but the object-
oriented approach can become a necessity as plots become more complicated.
Set Figure
• fig, ax = plt.subplots(3,2)
• ax[0,1].plot(x, y1, color = 'r', linestyle = '--')
• ax[1,1].plot(x, y2, color = 'b', linestyle = '-.')
• ax[0,1].plot(x, y1, color = 'r', linestyle = '--')
• ax[1,1].plot(x, y2, color = 'b', linestyle = '-.')
Line Colors and Style
Set ax limits, labels
plt.plot() → ax.plot(),
plt.legend() → ax.legend()

•plt.xlabel() → ax.set_xlabel()
•plt.ylabel() → ax.set_ylabel()
•plt.xlim() → ax.set_xlim()
•plt.ylim() → ax.set_ylim()
•plt.title() → ax.set_title()
Plot Scatter
Contour Plot
Histograms

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