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

More Related Content

PPTX
1.2 matlab numerical data
PDF
Two dimensional array
PPTX
2D Array
PPTX
Chapter 7.3
PPTX
Lines and planes in space
PPT
basic concepts
PPTX
Two dimensional arrays
PDF
Multi dimensional array
1.2 matlab numerical data
Two dimensional array
2D Array
Chapter 7.3
Lines and planes in space
basic concepts
Two dimensional arrays
Multi dimensional array

What's hot (20)

PPTX
Arrays, Structures And Enums
PPT
PPT
Sparse Matrix and Polynomial
PDF
PPT
Arrays in C++
PPTX
Learn How to create pyramids in c
PDF
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
PPT
07 Arrays
PDF
Matlab numbers
PPT
Vector3
PDF
Matlab Graphics Tutorial
PDF
Extensible Operators and Literals for JavaScript
PPTX
2 dimension array in programms
PDF
JS Responsibilities
PPTX
Vector class in C++
PDF
The Ring programming language version 1.8 book - Part 23 of 202
DOC
Matlabtut1
PDF
Options and trade offs for parallelism and concurrency in Modern C++
PPTX
Sorting and Searching - Data Structure - Notes
PPTX
arrays-120712074248-phpapp01
Arrays, Structures And Enums
Sparse Matrix and Polynomial
Arrays in C++
Learn How to create pyramids in c
Value Objects, Full Throttle (to be updated for spring TC39 meetings)
07 Arrays
Matlab numbers
Vector3
Matlab Graphics Tutorial
Extensible Operators and Literals for JavaScript
2 dimension array in programms
JS Responsibilities
Vector class in C++
The Ring programming language version 1.8 book - Part 23 of 202
Matlabtut1
Options and trade offs for parallelism and concurrency in Modern C++
Sorting and Searching - Data Structure - Notes
arrays-120712074248-phpapp01
Ad

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

PPTX
Arrays with Numpy, Computer Graphics
PDF
CE344L-200365-Lab2.pdf
PPT
CAP776Numpy.ppt
PPT
CAP776Numpy (2).ppt
PPTX
NumPy.pptx
PDF
DAA Notes.pdf
PPTX
Fosdem2017 Scientific computing on Jruby
PPTX
VCE Unit 01 (2).pptx
PPTX
lec08-numpy.pptx
PPTX
Chapter 5-Numpy-Pandas.pptx python programming
PDF
C Programming - Refresher - Part III
PPT
Analysis.ppt
ODP
Talk on Standard Template Library
PPTX
THE NUMPY LIBRARY of python with slides.pptx
PDF
Arrays and library functions
PPTX
NUMPY-2.pptx
PDF
‏‏Lecture 2.pdf
PPTX
python for data anal gh i o fytysis creation.pptx
PPTX
Lecture 1 Pandas Basics.pptx machine learning
PPTX
Numpy_Pandas_for beginners_________.pptx
Arrays with Numpy, Computer Graphics
CE344L-200365-Lab2.pdf
CAP776Numpy.ppt
CAP776Numpy (2).ppt
NumPy.pptx
DAA Notes.pdf
Fosdem2017 Scientific computing on Jruby
VCE Unit 01 (2).pptx
lec08-numpy.pptx
Chapter 5-Numpy-Pandas.pptx python programming
C Programming - Refresher - Part III
Analysis.ppt
Talk on Standard Template Library
THE NUMPY LIBRARY of python with slides.pptx
Arrays and library functions
NUMPY-2.pptx
‏‏Lecture 2.pdf
python for data anal gh i o fytysis creation.pptx
Lecture 1 Pandas Basics.pptx machine learning
Numpy_Pandas_for beginners_________.pptx
Ad

More from AminaRepo (20)

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

Recently uploaded (20)

PDF
IFIT3 RNA-binding activity primores influenza A viruz infection and translati...
PDF
CAPERS-LRD-z9:AGas-enshroudedLittleRedDotHostingaBroad-lineActive GalacticNuc...
PDF
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
PDF
Phytochemical Investigation of Miliusa longipes.pdf
PPTX
Cell Membrane: Structure, Composition & Functions
PPTX
TOTAL hIP ARTHROPLASTY Presentation.pptx
PPTX
7. General Toxicologyfor clinical phrmacy.pptx
PPTX
Derivatives of integument scales, beaks, horns,.pptx
PPTX
EPIDURAL ANESTHESIA ANATOMY AND PHYSIOLOGY.pptx
PPTX
G5Q1W8 PPT SCIENCE.pptx 2025-2026 GRADE 5
PPTX
BIOMOLECULES PPT........................
PDF
. Radiology Case Scenariosssssssssssssss
PPT
POSITIONING IN OPERATION THEATRE ROOM.ppt
PPTX
2Systematics of Living Organisms t-.pptx
PDF
An interstellar mission to test astrophysical black holes
PDF
bbec55_b34400a7914c42429908233dbd381773.pdf
PDF
Mastering Bioreactors and Media Sterilization: A Complete Guide to Sterile Fe...
PPTX
GEN. BIO 1 - CELL TYPES & CELL MODIFICATIONS
PPT
protein biochemistry.ppt for university classes
PPTX
ECG_Course_Presentation د.محمد صقران ppt
IFIT3 RNA-binding activity primores influenza A viruz infection and translati...
CAPERS-LRD-z9:AGas-enshroudedLittleRedDotHostingaBroad-lineActive GalacticNuc...
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
Phytochemical Investigation of Miliusa longipes.pdf
Cell Membrane: Structure, Composition & Functions
TOTAL hIP ARTHROPLASTY Presentation.pptx
7. General Toxicologyfor clinical phrmacy.pptx
Derivatives of integument scales, beaks, horns,.pptx
EPIDURAL ANESTHESIA ANATOMY AND PHYSIOLOGY.pptx
G5Q1W8 PPT SCIENCE.pptx 2025-2026 GRADE 5
BIOMOLECULES PPT........................
. Radiology Case Scenariosssssssssssssss
POSITIONING IN OPERATION THEATRE ROOM.ppt
2Systematics of Living Organisms t-.pptx
An interstellar mission to test astrophysical black holes
bbec55_b34400a7914c42429908233dbd381773.pdf
Mastering Bioreactors and Media Sterilization: A Complete Guide to Sterile Fe...
GEN. BIO 1 - CELL TYPES & CELL MODIFICATIONS
protein biochemistry.ppt for university classes
ECG_Course_Presentation د.محمد صقران ppt

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.