SlideShare a Scribd company logo
Data manipulation:
Numpy
AAA-Python Edition
Plan
●
1- Numpy: ndarray
●
2- indexing
●
3- Operations with ndarray
●
4- File saving and loading
●
5- Structures with dtype
3
1-Numpy:ndarray
[By Amina Delali]
Numpy
●
Numpy for Numerical Python, a library for numerical computing
in Python.
●
It defnes:
●
ndarray : multidimentioanl array
●
Fast Mathematical functions and operations with ndarray
including reading and writing array data from/to disk
●
Linear algebra, random number generation,
and Fourrier transform capabilities
●
A C API for connecting NumPy with libraries written in C, C++,
or FORTRAN.
●
We will focus int this course on the 3 frst points.
4
1-Numpy:ndarray
[By Amina Delali]
ndarray
●
ndarray is a multidimensional array object = a generic
multidimensional container for data of the same type.
a is a list
b is an ndarray
Import Numpy to use “array” function
array function used to transform
a list to an ndarray
5
1-Numpy:ndarray
[By Amina Delali]
ndarray
●
ndarray is characterized by its shape and dimension
Number of external
Brackets = dimension
(=3)
The ndarray d is a 3 dimensional array, composed of: 2 elements of dimesion 2.
Each dimension 2 element is composed of: 3 elements of dimension 1
Each dimension 1 element is composed of: 3 elements of dimension 0
==> the shape of d = 2 x 3 x 3
2 elements of dimension 2
(a 2 dimensionl element has2 external brackets)
3 elements of
dimension 1
3 elements
of dimension 0 =scalars
6
1-Numpy:ndarray
[By Amina Delali]
Creating ndarray
●
like array function, other functions exist to create an ndarray
Since b is an ndarray, it will not be copied.
g and b will refer to the same element
Modifying the first element of g, will
modify also the first element of b
Using array function, the array b
will be copied in a new element h
Modifying the second element of h
will not modify the second element of b
7
1-Numpy:ndarray
[By Amina Delali]
Creating ndarray
Identity matrix (2 dimensions:
rows and columns)
1. 0. 0.
0. 1. 0.
0. 0. 1.
1 in diagonal,
0 elsewhereRange from 1 to (9-1)
with step 3
Axis 0 == lines
Axis 1 == columns
id1
0 1 2
0
1
2
8
1-Numpy:ndarray
[By Amina Delali]
Creating ndarray
●
Each of the following functions has two versions: function-name
and function-nam_like
We can specify in
these functions the
dtype argument
(the values type)
9
1-Numpy:ndarray
[By Amina Delali]
dtype
●
The ndarray can be created specifying a type "dtype"
●
The types can be:
●
int : signed (i1, i2, i4 or i8) and unsigned (u1, u2, u4 or u8)
●
foat: f2, f4 or f, f8 or d, f16 or g
●
complex: c8, c16, c32
●
boolean: ?
●
object: O
●
String: S . Fixed length ASCII String type, (S"number" for a
stirng of "number" byte size)
●
Unicode: U . Fixed length Unicode type, (U"number" for unicode
of "number" of certain_byte size )
These codes can be used
as arguments: dtype=”i8”
The float fill
value(3.2) is
converted
to int
10
2-Indexing
[By Amina Delali]
indexes
●
ndarray can be indexed by: integers, arrays, slices,and Boolean
c[0]==[1,2,3]
1 2 3
4 5 6
0
1
0 1 2
1 2 3
4 5 6
0
1
0 1 2
1 2 3
4 5 6
0
1
0 1 2
Ind == [1, 1]
c[ind]==[[4,5,6],[4,5,6]]
1 2 3
4 5 6
0
1
0 1 2
C[ : ,0:2]==[[1,2],[4,5]]
“ : “
0:2
c
11
2-Indexing
[By Amina Delali]
indexes
●
ndarray can be indexed by: integers, arrays, slices,and Boolean
1 2 3
4 5 6
0
1
0 1 2
1 2 3
4 5 6
0
1
0 1 2
False False False
False False False
0
1
0 1 2
c
ind2
Assigning one value(True)
to the entire line (0)
ind2
ind2
Assigning one value
(True) to one cell (1,0)
True True True
False False False
0
1
0 1 2
True True True
True False False
0
1
0 1 2
Selecting values from c
corresponding to True in
ind2
12
2-Indexing
[By Amina Delali]
Slices and copies
●
Using slices to create arrays from other ndarrays doesn't create
copies.To have distinct arrays, we have to use the method copy
1 2 3
4 5 6
0
1
0 1 2
c
part
1 2
4 5
0
1
0 1
1 1000
4 5
0
1
0 1
1 1000 3
4 5 6
0
1
0 1 2c
13
2-Indexing
[By Amina Delali]
Slices and copies
●
Using slices to create arrays from other ndarrays doesn't create
copies.To have distinct arrays, we have to use the method copy
1 2 3
4 5 6
0
1
0 1 2
c
part2
1 2
4 5
0
1
0 1
1 1000
4 5
0
1
0 1
1 2 3
4 5 6
0
1
0 1 2c
14
3-Operationswith
Ndarrays
[By Amina Delali]
Arithmetic operations & Linear Algebra
●
1 2 3
4 5 6
0
1
0 1 2 *
@
1 0 0
0 1 0
0
1
0 1 2
1 0 0
0 1 0
0 0 1
0
1
0 1 2
1 0 0
0 5 0
0
1
0 1 2
0 1 2
0
1
1 2 3
4 5 6
c @ indentity== c
15
3-Operationswith
Ndarrays
[By Amina Delali]
Element wise multiplication vs Matrix multiplication
1 2 3
4 5 6
0
1
0 1 2
*
1 -1 0
-5 1 2
0
1
0 1 2
1 -2 0
-20 5 12
0
1
0 1 2
1 * 1 == 1 6 * 2 == 12
Element wise multiplication
Matrix multiplication
@
1 -1
0 -5
1 2
0
1
2
0 1
4 -5
10 -17
0
1
0 1
Line 0 * Column 0 == Cell ( 0 , 0 ):
1 * 1 + 2 * 0 + 3 * 1 == 4
1 2 3
4 5 6
0
1
0 1 2
Line 1 * Column 1 == Cell ( 1 , 1 )
4 * -1 + 5 * -5 + 6 * 2 = -17
16
3-Operationswith
Ndarrays
[By Amina Delali]
Arithmetic and Logical operations
Same operation can be done
With: - , / , *
17
3-Operationswith
Ndarrays
[By Amina Delali]
Arithmetic and Logical operations
The result is an ndarray ( each value
Greater than 3 will produce a True value)
●
We can use logical operations to select certain elements of an array
18
3-Operationswith
Ndarrays
[By Amina Delali]
Linear Algebra
●
We already seen the matrix multiplication using dot method
or np.dot function or the operator @
●
There are other functions related to linear Algebra as: diag,trace,
inv, solve, … etc.
●
as for sacalrs, matrices have inverse regarding the matrix
multiplication operation:
is the Identity matrix
●
A system of linear equations can be represented by matrices:
for example:
And the solution will be :
mat∗mat
−1
=I
I
mat∗x= y
19
3-Operationswith
Ndarrays
[By Amina Delali]
Linear Algebra
A square matrix means:
number of rows ==
number of columns
The solution must be equal to
the inverse matrix of mat
20
3-Operationswith
Ndarrays
[By Amina Delali]
Some functions and methods
● Numpy defnes a list of element wise functions applicable to:
●
One ndarray, as: sqrt, exp, modf, log, sign, ceil and floor,
cos, logical_not, … etc
●
Two ndarray as: add, mod, maximum... etc
Access to the
first ndarray
1 for positive elements, -1 for negative
elements and 0 for 0 values
21
3-Operationswith
Ndarrays
[By Amina Delali]
Some functions and methods
● There is a list of functions that permit the generation of ndarrays
with certain values. For example: randn, meshgrid, and where
If a value from c is greater
than 3 it will return “G”
else it will return “L”
Each value from the generated range
Can be associated with all values
22
3-Operationswith
Ndarrays
[By Amina Delali]
Some functions and methods
● With the function append we can create a new ndarray by
appending new values
The given values must have
the same dimension as
the first argument”
c didn’t change
23
3-Operationswith
Ndarrays
[By Amina Delali]
Some functions and methods
●
ndarray objects defne a list of useful methods like: mean, sum,
cumsum, max, sort,T, ...etc
Lines 0,1 become
columns 0,1.
And columns 0,1,2
become lines 0,1,2
24
4-Arraysaving
andloading
[By Amina Delali]
Save and Load
● It is possible to save and load ndarrays into binray format
If the extensions “npy”
or “npz” are not specified
they will be added.
The extension has to be
specified in loading data
Access to the arrays
with the names
used in the saving
25
5-Structureswith
dtype
[By Amina Delali]
Some functions and methods
● dtype constructor can be used to create structured type.
name and type
Each myType element is defined by
two values: “code” and “Value”
Initialized by tuples of two values
corresponding to myType definition
References
●
Wes McKinney. Python for data analysis: Data wrangling
with Pandas, NumPy, and IPython. O’Reilly Media, Inc, 2018.
●
SciPy.org. Data type objects. On-line at
https://docs.scipy.org/doc/numpy-
1.13.0/reference/arrays.dtypes.html. Accessed on 05-10-
2018.
Thank
you!
FOR ALL YOUR TIME
Ad

Recommended

PPTX
1.2 matlab numerical data
TANVIRAHMED611926
 
PDF
Two dimensional array
Rajendran
 
PPTX
2D Array
Ehatsham Riaz
 
PPTX
Chapter 7.3
sotlsoc
 
PPTX
Lines and planes in space
Faizan Shabbir
 
PPT
basic concepts
Jetti Chowdary
 
PPTX
Two dimensional arrays
Neeru Mittal
 
PDF
Multi dimensional array
Rajendran
 
PPTX
Arrays, Structures And Enums
Bhushan Mulmule
 
PPT
Arrays
archikabhatia
 
PPT
Sparse Matrix and Polynomial
Aroosa Rajput
 
PDF
2-D array
Swarup Kumar Boro
 
PPT
Arrays in C++
Janpreet Singh
 
PPTX
Learn How to create pyramids in c
Harish Gyanani
 
PDF
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Brendan Eich
 
PPT
07 Arrays
maznabili
 
PDF
Matlab numbers
pramodkumar1804
 
PPT
Vector3
Rajendran
 
PDF
Matlab Graphics Tutorial
Cheng-An Yang
 
PDF
Extensible Operators and Literals for JavaScript
Brendan Eich
 
PPTX
2 dimension array in programms
Anil Pokhrel
 
PDF
JS Responsibilities
Brendan Eich
 
PPTX
Vector class in C++
Jawad Khan
 
PDF
The Ring programming language version 1.8 book - Part 23 of 202
Mahmoud Samir Fayed
 
DOC
Matlabtut1
Vinnu Vinay
 
PDF
Options and trade offs for parallelism and concurrency in Modern C++
Satalia
 
PPTX
Sorting and Searching - Data Structure - Notes
Omprakash Chauhan
 
PPTX
arrays-120712074248-phpapp01
Abdul Samee
 
PDF
numpy.pdf
ssuser457188
 
PPTX
UNIT-03_Numpy (1) python yeksodbbsisbsjsjsh
tony8553004135
 

More Related Content

What's hot (20)

PPTX
Arrays, Structures And Enums
Bhushan Mulmule
 
PPT
Arrays
archikabhatia
 
PPT
Sparse Matrix and Polynomial
Aroosa Rajput
 
PDF
2-D array
Swarup Kumar Boro
 
PPT
Arrays in C++
Janpreet Singh
 
PPTX
Learn How to create pyramids in c
Harish Gyanani
 
PDF
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Brendan Eich
 
PPT
07 Arrays
maznabili
 
PDF
Matlab numbers
pramodkumar1804
 
PPT
Vector3
Rajendran
 
PDF
Matlab Graphics Tutorial
Cheng-An Yang
 
PDF
Extensible Operators and Literals for JavaScript
Brendan Eich
 
PPTX
2 dimension array in programms
Anil Pokhrel
 
PDF
JS Responsibilities
Brendan Eich
 
PPTX
Vector class in C++
Jawad Khan
 
PDF
The Ring programming language version 1.8 book - Part 23 of 202
Mahmoud Samir Fayed
 
DOC
Matlabtut1
Vinnu Vinay
 
PDF
Options and trade offs for parallelism and concurrency in Modern C++
Satalia
 
PPTX
Sorting and Searching - Data Structure - Notes
Omprakash Chauhan
 
PPTX
arrays-120712074248-phpapp01
Abdul Samee
 
Arrays, Structures And Enums
Bhushan Mulmule
 
Sparse Matrix and Polynomial
Aroosa Rajput
 
Arrays in C++
Janpreet Singh
 
Learn How to create pyramids in c
Harish Gyanani
 
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
Brendan Eich
 
07 Arrays
maznabili
 
Matlab numbers
pramodkumar1804
 
Vector3
Rajendran
 
Matlab Graphics Tutorial
Cheng-An Yang
 
Extensible Operators and Literals for JavaScript
Brendan Eich
 
2 dimension array in programms
Anil Pokhrel
 
JS Responsibilities
Brendan Eich
 
Vector class in C++
Jawad Khan
 
The Ring programming language version 1.8 book - Part 23 of 202
Mahmoud Samir Fayed
 
Matlabtut1
Vinnu Vinay
 
Options and trade offs for parallelism and concurrency in Modern C++
Satalia
 
Sorting and Searching - Data Structure - Notes
Omprakash Chauhan
 
arrays-120712074248-phpapp01
Abdul Samee
 

Similar to Aaa ped-4- Data manipulation: Numpy (20)

PDF
numpy.pdf
ssuser457188
 
PPTX
UNIT-03_Numpy (1) python yeksodbbsisbsjsjsh
tony8553004135
 
PDF
numpy.pdf
DrSudheerHanumanthak
 
PPT
CAP776Numpy (2).ppt
ChhaviCoachingCenter
 
PPT
CAP776Numpy.ppt
kdr52121
 
PPTX
Lecture 2 _Foundions foundions NumPyI.pptx
disserdekabrcha
 
PPTX
Chapter 4 NumPy Basics Arrays and Vectorized Computation (Part I).pptx
SovannDoeur
 
PPTX
NumPy.pptx
EN1036VivekSingh
 
PPTX
numpy code and examples with attributes.pptx
swathis752031
 
PDF
Numpy ndarrays.pdf
SudhanshiBakre1
 
PDF
‏‏Lecture 2.pdf
AhmedAbdalla903058
 
PPT
Introduction to Numpy Foundation Study GuideStudyGuide
elharriettm
 
PDF
Introduction to NumPy (PyData SV 2013)
PyData
 
PDF
Introduction to NumPy
Huy Nguyen
 
PDF
The num py_library_20200818
Haim Michael
 
PPTX
Chapter 5-Numpy-Pandas.pptx python programming
ssuser77162c
 
PPTX
NUMPY LIBRARY study materials PPT 2.pptx
CHETHANKUMAR274045
 
PPTX
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
tahirnaquash2
 
PPTX
NumPy.pptx
DrJasmineBeulahG
 
numpy.pdf
ssuser457188
 
UNIT-03_Numpy (1) python yeksodbbsisbsjsjsh
tony8553004135
 
CAP776Numpy (2).ppt
ChhaviCoachingCenter
 
CAP776Numpy.ppt
kdr52121
 
Lecture 2 _Foundions foundions NumPyI.pptx
disserdekabrcha
 
Chapter 4 NumPy Basics Arrays and Vectorized Computation (Part I).pptx
SovannDoeur
 
NumPy.pptx
EN1036VivekSingh
 
numpy code and examples with attributes.pptx
swathis752031
 
Numpy ndarrays.pdf
SudhanshiBakre1
 
‏‏Lecture 2.pdf
AhmedAbdalla903058
 
Introduction to Numpy Foundation Study GuideStudyGuide
elharriettm
 
Introduction to NumPy (PyData SV 2013)
PyData
 
Introduction to NumPy
Huy Nguyen
 
The num py_library_20200818
Haim Michael
 
Chapter 5-Numpy-Pandas.pptx python programming
ssuser77162c
 
NUMPY LIBRARY study materials PPT 2.pptx
CHETHANKUMAR274045
 
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
tahirnaquash2
 
NumPy.pptx
DrJasmineBeulahG
 
Ad

More from AminaRepo (20)

PDF
Aaa ped-23-Artificial Neural Network: Keras and Tensorfow
AminaRepo
 
PDF
Aaa ped-22-Artificial Neural Network: Introduction to ANN
AminaRepo
 
PDF
Aaa ped-21-Recommender Systems: Content-based Filtering
AminaRepo
 
PDF
Aaa ped-20-Recommender Systems: Model-based collaborative filtering
AminaRepo
 
PDF
Aaa ped-19-Recommender Systems: Neighborhood-based Filtering
AminaRepo
 
PDF
Aaa ped-18-Unsupervised Learning: Association Rule Learning
AminaRepo
 
PDF
Aaa ped-17-Unsupervised Learning: Dimensionality reduction
AminaRepo
 
PDF
Aaa ped-16-Unsupervised Learning: clustering
AminaRepo
 
PDF
Aaa ped-15-Ensemble Learning: Random Forests
AminaRepo
 
PDF
Aaa ped-14-Ensemble Learning: About Ensemble Learning
AminaRepo
 
PDF
Aaa ped-12-Supervised Learning: Support Vector Machines & Naive Bayes Classifer
AminaRepo
 
PDF
Aaa ped-11-Supervised Learning: Multivariable Regressor & Classifers
AminaRepo
 
PDF
Aaa ped-10-Supervised Learning: Introduction to Supervised Learning
AminaRepo
 
PDF
Aaa ped-9-Data manipulation: Time Series & Geographical visualization
AminaRepo
 
PDF
Aaa ped-Data-8- manipulation: Plotting and Visualization
AminaRepo
 
PDF
Aaa ped-8- Data manipulation: Data wrangling, aggregation, and group operations
AminaRepo
 
PDF
Aaa ped-6-Data manipulation: Data Files, and Data Cleaning & Preparation
AminaRepo
 
PDF
Aaa ped-5-Data manipulation: Pandas
AminaRepo
 
PDF
Aaa ped-3. Pythond: advanced concepts
AminaRepo
 
PDF
Aaa ped-2- Python: Basics
AminaRepo
 
Aaa ped-23-Artificial Neural Network: Keras and Tensorfow
AminaRepo
 
Aaa ped-22-Artificial Neural Network: Introduction to ANN
AminaRepo
 
Aaa ped-21-Recommender Systems: Content-based Filtering
AminaRepo
 
Aaa ped-20-Recommender Systems: Model-based collaborative filtering
AminaRepo
 
Aaa ped-19-Recommender Systems: Neighborhood-based Filtering
AminaRepo
 
Aaa ped-18-Unsupervised Learning: Association Rule Learning
AminaRepo
 
Aaa ped-17-Unsupervised Learning: Dimensionality reduction
AminaRepo
 
Aaa ped-16-Unsupervised Learning: clustering
AminaRepo
 
Aaa ped-15-Ensemble Learning: Random Forests
AminaRepo
 
Aaa ped-14-Ensemble Learning: About Ensemble Learning
AminaRepo
 
Aaa ped-12-Supervised Learning: Support Vector Machines & Naive Bayes Classifer
AminaRepo
 
Aaa ped-11-Supervised Learning: Multivariable Regressor & Classifers
AminaRepo
 
Aaa ped-10-Supervised Learning: Introduction to Supervised Learning
AminaRepo
 
Aaa ped-9-Data manipulation: Time Series & Geographical visualization
AminaRepo
 
Aaa ped-Data-8- manipulation: Plotting and Visualization
AminaRepo
 
Aaa ped-8- Data manipulation: Data wrangling, aggregation, and group operations
AminaRepo
 
Aaa ped-6-Data manipulation: Data Files, and Data Cleaning & Preparation
AminaRepo
 
Aaa ped-5-Data manipulation: Pandas
AminaRepo
 
Aaa ped-3. Pythond: advanced concepts
AminaRepo
 
Aaa ped-2- Python: Basics
AminaRepo
 
Ad

Recently uploaded (20)

PPTX
How Psychology Can Power Product Decisions: A Human-Centered Blueprint- Shray...
ShrayasiRoy2
 
PPTX
GBSN_Unit 3 - Medical and surgical Asepsis
Areesha Ahmad
 
PPTX
transitioning to quality approach. pptx
091HarshikaModi
 
PDF
We are Living in a Dangerous Multilingual World!
Editions La Dondaine
 
PPTX
Fake Science: Where it comes from and how to avoid beign part of it
Leonid Schneider
 
DOCX
Science 8 Quarter 4 first quiz digestive system.docx
junefermunez
 
PPTX
It's about the habitat of organisms. Where they live and what they need to su...
KwabenaAbrokwah1
 
PPTX
How Psychology Can Power Product Decisions: A Human-Centered Blueprint- Shray...
ShrayasiRoy
 
PPTX
Single-Cell Multi-Omics in Neurodegeneration p1.pptx
KanakChaudhary10
 
PPTX
Science 10 1.3 Mountain Belts in the Philippines.pptx
ClaireMangundayao1
 
PDF
Enzyme Kinetics_Lecture 8.5.2025 Enzymology.pdf
ayeshaalibukhari125
 
PPTX
An Analysis Of The Pearl Short Story By John Steinbeck
BillyDarmawan3
 
PPTX
HERBAL INGREDIENTS USED IN ORAL CARE.pptx
Vidhi889356
 
PPTX
Instrumentation of IR and Raman Spectrophotometers.pptx
sngth2h2acc
 
PDF
Operationalising OGC Processes with Application Packages in ILIAD: A Service ...
Marco Amaro Oliveira
 
PDF
The scientific heritage No 162 (162) (2025)
The scientific heritage
 
PPTX
What is Skeleton system.pptx by aahil sir
bhatbashir421
 
PPTX
THE CIRCULATORY SYSTEM GRADE 9 SCIENCE.pptx
roselyncatacutan
 
PDF
Sujay Rao Mandavilli public profile June 2025.pdf
Sujay Rao Mandavilli
 
PDF
SULFUR PEARL OF NAMIBIA - Thiomargarita namibiensis
aparnamp966
 
How Psychology Can Power Product Decisions: A Human-Centered Blueprint- Shray...
ShrayasiRoy2
 
GBSN_Unit 3 - Medical and surgical Asepsis
Areesha Ahmad
 
transitioning to quality approach. pptx
091HarshikaModi
 
We are Living in a Dangerous Multilingual World!
Editions La Dondaine
 
Fake Science: Where it comes from and how to avoid beign part of it
Leonid Schneider
 
Science 8 Quarter 4 first quiz digestive system.docx
junefermunez
 
It's about the habitat of organisms. Where they live and what they need to su...
KwabenaAbrokwah1
 
How Psychology Can Power Product Decisions: A Human-Centered Blueprint- Shray...
ShrayasiRoy
 
Single-Cell Multi-Omics in Neurodegeneration p1.pptx
KanakChaudhary10
 
Science 10 1.3 Mountain Belts in the Philippines.pptx
ClaireMangundayao1
 
Enzyme Kinetics_Lecture 8.5.2025 Enzymology.pdf
ayeshaalibukhari125
 
An Analysis Of The Pearl Short Story By John Steinbeck
BillyDarmawan3
 
HERBAL INGREDIENTS USED IN ORAL CARE.pptx
Vidhi889356
 
Instrumentation of IR and Raman Spectrophotometers.pptx
sngth2h2acc
 
Operationalising OGC Processes with Application Packages in ILIAD: A Service ...
Marco Amaro Oliveira
 
The scientific heritage No 162 (162) (2025)
The scientific heritage
 
What is Skeleton system.pptx by aahil sir
bhatbashir421
 
THE CIRCULATORY SYSTEM GRADE 9 SCIENCE.pptx
roselyncatacutan
 
Sujay Rao Mandavilli public profile June 2025.pdf
Sujay Rao Mandavilli
 
SULFUR PEARL OF NAMIBIA - Thiomargarita namibiensis
aparnamp966
 

Aaa ped-4- Data manipulation: Numpy

  • 2. Plan ● 1- Numpy: ndarray ● 2- indexing ● 3- Operations with ndarray ● 4- File saving and loading ● 5- Structures with dtype
  • 3. 3 1-Numpy:ndarray [By Amina Delali] Numpy ● Numpy for Numerical Python, a library for numerical computing in Python. ● It defnes: ● ndarray : multidimentioanl array ● Fast Mathematical functions and operations with ndarray including reading and writing array data from/to disk ● Linear algebra, random number generation, and Fourrier transform capabilities ● A C API for connecting NumPy with libraries written in C, C++, or FORTRAN. ● We will focus int this course on the 3 frst points.
  • 4. 4 1-Numpy:ndarray [By Amina Delali] ndarray ● ndarray is a multidimensional array object = a generic multidimensional container for data of the same type. a is a list b is an ndarray Import Numpy to use “array” function array function used to transform a list to an ndarray
  • 5. 5 1-Numpy:ndarray [By Amina Delali] ndarray ● ndarray is characterized by its shape and dimension Number of external Brackets = dimension (=3) The ndarray d is a 3 dimensional array, composed of: 2 elements of dimesion 2. Each dimension 2 element is composed of: 3 elements of dimension 1 Each dimension 1 element is composed of: 3 elements of dimension 0 ==> the shape of d = 2 x 3 x 3 2 elements of dimension 2 (a 2 dimensionl element has2 external brackets) 3 elements of dimension 1 3 elements of dimension 0 =scalars
  • 6. 6 1-Numpy:ndarray [By Amina Delali] Creating ndarray ● like array function, other functions exist to create an ndarray Since b is an ndarray, it will not be copied. g and b will refer to the same element Modifying the first element of g, will modify also the first element of b Using array function, the array b will be copied in a new element h Modifying the second element of h will not modify the second element of b
  • 7. 7 1-Numpy:ndarray [By Amina Delali] Creating ndarray Identity matrix (2 dimensions: rows and columns) 1. 0. 0. 0. 1. 0. 0. 0. 1. 1 in diagonal, 0 elsewhereRange from 1 to (9-1) with step 3 Axis 0 == lines Axis 1 == columns id1 0 1 2 0 1 2
  • 8. 8 1-Numpy:ndarray [By Amina Delali] Creating ndarray ● Each of the following functions has two versions: function-name and function-nam_like We can specify in these functions the dtype argument (the values type)
  • 9. 9 1-Numpy:ndarray [By Amina Delali] dtype ● The ndarray can be created specifying a type "dtype" ● The types can be: ● int : signed (i1, i2, i4 or i8) and unsigned (u1, u2, u4 or u8) ● foat: f2, f4 or f, f8 or d, f16 or g ● complex: c8, c16, c32 ● boolean: ? ● object: O ● String: S . Fixed length ASCII String type, (S"number" for a stirng of "number" byte size) ● Unicode: U . Fixed length Unicode type, (U"number" for unicode of "number" of certain_byte size ) These codes can be used as arguments: dtype=”i8” The float fill value(3.2) is converted to int
  • 10. 10 2-Indexing [By Amina Delali] indexes ● ndarray can be indexed by: integers, arrays, slices,and Boolean c[0]==[1,2,3] 1 2 3 4 5 6 0 1 0 1 2 1 2 3 4 5 6 0 1 0 1 2 1 2 3 4 5 6 0 1 0 1 2 Ind == [1, 1] c[ind]==[[4,5,6],[4,5,6]] 1 2 3 4 5 6 0 1 0 1 2 C[ : ,0:2]==[[1,2],[4,5]] “ : “ 0:2 c
  • 11. 11 2-Indexing [By Amina Delali] indexes ● ndarray can be indexed by: integers, arrays, slices,and Boolean 1 2 3 4 5 6 0 1 0 1 2 1 2 3 4 5 6 0 1 0 1 2 False False False False False False 0 1 0 1 2 c ind2 Assigning one value(True) to the entire line (0) ind2 ind2 Assigning one value (True) to one cell (1,0) True True True False False False 0 1 0 1 2 True True True True False False 0 1 0 1 2 Selecting values from c corresponding to True in ind2
  • 12. 12 2-Indexing [By Amina Delali] Slices and copies ● Using slices to create arrays from other ndarrays doesn't create copies.To have distinct arrays, we have to use the method copy 1 2 3 4 5 6 0 1 0 1 2 c part 1 2 4 5 0 1 0 1 1 1000 4 5 0 1 0 1 1 1000 3 4 5 6 0 1 0 1 2c
  • 13. 13 2-Indexing [By Amina Delali] Slices and copies ● Using slices to create arrays from other ndarrays doesn't create copies.To have distinct arrays, we have to use the method copy 1 2 3 4 5 6 0 1 0 1 2 c part2 1 2 4 5 0 1 0 1 1 1000 4 5 0 1 0 1 1 2 3 4 5 6 0 1 0 1 2c
  • 14. 14 3-Operationswith Ndarrays [By Amina Delali] Arithmetic operations & Linear Algebra ● 1 2 3 4 5 6 0 1 0 1 2 * @ 1 0 0 0 1 0 0 1 0 1 2 1 0 0 0 1 0 0 0 1 0 1 0 1 2 1 0 0 0 5 0 0 1 0 1 2 0 1 2 0 1 1 2 3 4 5 6 c @ indentity== c
  • 15. 15 3-Operationswith Ndarrays [By Amina Delali] Element wise multiplication vs Matrix multiplication 1 2 3 4 5 6 0 1 0 1 2 * 1 -1 0 -5 1 2 0 1 0 1 2 1 -2 0 -20 5 12 0 1 0 1 2 1 * 1 == 1 6 * 2 == 12 Element wise multiplication Matrix multiplication @ 1 -1 0 -5 1 2 0 1 2 0 1 4 -5 10 -17 0 1 0 1 Line 0 * Column 0 == Cell ( 0 , 0 ): 1 * 1 + 2 * 0 + 3 * 1 == 4 1 2 3 4 5 6 0 1 0 1 2 Line 1 * Column 1 == Cell ( 1 , 1 ) 4 * -1 + 5 * -5 + 6 * 2 = -17
  • 16. 16 3-Operationswith Ndarrays [By Amina Delali] Arithmetic and Logical operations Same operation can be done With: - , / , *
  • 17. 17 3-Operationswith Ndarrays [By Amina Delali] Arithmetic and Logical operations The result is an ndarray ( each value Greater than 3 will produce a True value) ● We can use logical operations to select certain elements of an array
  • 18. 18 3-Operationswith Ndarrays [By Amina Delali] Linear Algebra ● We already seen the matrix multiplication using dot method or np.dot function or the operator @ ● There are other functions related to linear Algebra as: diag,trace, inv, solve, … etc. ● as for sacalrs, matrices have inverse regarding the matrix multiplication operation: is the Identity matrix ● A system of linear equations can be represented by matrices: for example: And the solution will be : mat∗mat −1 =I I mat∗x= y
  • 19. 19 3-Operationswith Ndarrays [By Amina Delali] Linear Algebra A square matrix means: number of rows == number of columns The solution must be equal to the inverse matrix of mat
  • 20. 20 3-Operationswith Ndarrays [By Amina Delali] Some functions and methods ● Numpy defnes a list of element wise functions applicable to: ● One ndarray, as: sqrt, exp, modf, log, sign, ceil and floor, cos, logical_not, … etc ● Two ndarray as: add, mod, maximum... etc Access to the first ndarray 1 for positive elements, -1 for negative elements and 0 for 0 values
  • 21. 21 3-Operationswith Ndarrays [By Amina Delali] Some functions and methods ● There is a list of functions that permit the generation of ndarrays with certain values. For example: randn, meshgrid, and where If a value from c is greater than 3 it will return “G” else it will return “L” Each value from the generated range Can be associated with all values
  • 22. 22 3-Operationswith Ndarrays [By Amina Delali] Some functions and methods ● With the function append we can create a new ndarray by appending new values The given values must have the same dimension as the first argument” c didn’t change
  • 23. 23 3-Operationswith Ndarrays [By Amina Delali] Some functions and methods ● ndarray objects defne a list of useful methods like: mean, sum, cumsum, max, sort,T, ...etc Lines 0,1 become columns 0,1. And columns 0,1,2 become lines 0,1,2
  • 24. 24 4-Arraysaving andloading [By Amina Delali] Save and Load ● It is possible to save and load ndarrays into binray format If the extensions “npy” or “npz” are not specified they will be added. The extension has to be specified in loading data Access to the arrays with the names used in the saving
  • 25. 25 5-Structureswith dtype [By Amina Delali] Some functions and methods ● dtype constructor can be used to create structured type. name and type Each myType element is defined by two values: “code” and “Value” Initialized by tuples of two values corresponding to myType definition
  • 26. References ● Wes McKinney. Python for data analysis: Data wrangling with Pandas, NumPy, and IPython. O’Reilly Media, Inc, 2018. ● SciPy.org. Data type objects. On-line at https://docs.scipy.org/doc/numpy- 1.13.0/reference/arrays.dtypes.html. Accessed on 05-10- 2018.