SlideShare a Scribd company logo
1 of 38
Download to read offline
The NumPy Library
Haim Michael
August 18th
, 2020
All logos, trade marks and brand names used in this presentation belong
to the respective owners.
lifemichael
https://youtu.be/Tl5SyyBZA-4
Introduction
© 2008 Haim Michael 20150729
What is NumPy?
 NumPy is the fundamental library in use when dealing with
scientific computing.
 The NumPy library provides us with a powerful n-
dimensional array object.
 In addition, it provides us with sophisticated functions, tools
for integrating code in CC++ and in Fortran, useful linear
algebra, fourier transforms, and random numbers
generating capabilities.
© 2008 Haim Michael 20150729
Installing NumPy
 There are more than a few ways to install NumPy. The
simplest would be using the pip utility.
pip install numpy
© 2008 Haim Michael 20150729
Checking NumPy Version
 You can easily check the version of the NumPy you already
have installed using the following code.
 The expected outout should look like the following:
© 2008 Haim Michael 20150729
The SciPy Ecosystem
 SciPy (pronounced “Sigh Pie”) is a Python-based
ecosystem of open-source software for mathematics,
science, and engineering. Its core packages are the
following: NumPy, SciPy, Matplotlib, IPython, SymPy and
Pandas.
http://scipy.org
© 2008 Haim Michael 20150729
The SciPy Ecosystem
 SciPy (pronounced “Sigh Pie”) is a Python-based
ecosystem of open-source software for mathematics,
science, and engineering. Its core packages are the
following: NumPy, SciPy, Matplotlib, IPython, SymPy and
Pandas.
http://scipy.org
© 2008 Haim Michael 20150729
SciPy Documentation
 You can find detailed documentation for all SciPy's
packages (including NumPy, Pandas, and Matplotlib) at
https://www.scipy.org/docs.html
Basics
© 2008 Haim Michael 20150729
The ndarray Class
 NumPy's main purpose is to provide us with the capability to
work with an homogeneous multidimensional array, which is
a table of elements of the same type (usually numbers),
indexed by a tuple of non negative numbers.
 Objects instantiated from ndarray represent such
homogeneous multidimensional arrays. The alias for
ndarray is array.
© 2008 Haim Michael 20150729
The ndarray Class
 The number of dimensions is the rank of the array. The
following code sample creates a two dimension array (the
rank is 2).
import numpy as np
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
print(a.ndim)
© 2008 Haim Michael 20150729
The ndarray Class Attributes
 The more important attributes, objects instantiated from the
ndarray class have, are:
ndim
The number of axes (dimensions) of the array
shape
The dimensions of the array. The value of this attribute is a tuple that includes
integers. Each integer is the size of a specific axe (dimension). When having a
matrix with n rows and m columns, shape will be (n,m). The length of the
shape tuple is therefore the number of axes, ndim.
© 2008 Haim Michael 20150729
The ndarray Class Attributes
size
The total number of elements of the array.
dtype
It is an object that describes the type of the elements the array holds. It can be
one of Python standard types or it can be one of the additional types NumPy
provides us with (numpy.int32, numpy.int16, numpy.float64 etc.).
itemsize
This is the size in bytes of each element in the array (e.g. array of elements of
the type float64 has the itemsize 8).
© 2008 Haim Michael 20150729
The ndarray Class Attributes
data
This is a buffer that contains the actual elements of the array. Usually we will
access the elements directly and won't need this attribute.
© 2008 Haim Michael 20150729
Code Sample
import numpy as np
ob = np.arange(28).reshape(4,7)
print(ob)
print("ob.shape=",ob.shape)
print("ob.ndim=",ob.ndim)
print("ob.dtype.name=",ob.dtype.name)
print("ob.itemsize=",ob.itemsize)
print("ob.size=",ob.size)
print("type(ob)=",type(ob))
© 2008 Haim Michael 20150729
Code Sample
import numpy as np
ob = np.array([12,8,20,30])
print(ob)
print("ob.shape=",ob.shape)
print("ob.ndim=",ob.ndim)
print("ob.dtype.name=",ob.dtype.name)
print("ob.itemsize=",ob.itemsize)
print("ob.size=",ob.size)
print("type(ob)=",type(ob))
© 2008 Haim Michael 20150729
Creating Simple Arrays
 There are several ways for creating new objects of the type
ndarray (AKA array):
ob = np.array([3,4,5,6])
ob = np.array([[2,4,6],[8,54,2],[4,3,2]])
ob = np.array([[2,4],[8,2],[4,3]])
© 2008 Haim Michael 20150729
Creating Array of Zeros
 Using the zeros function we can easily create an array of
zeros.
ob = np.zeros(9)
© 2008 Haim Michael 20150729
Creating Array of Ones
 Using the ones function we can easily create an array full of
ones.
ob = np.ones(9)
© 2008 Haim Michael 20150729
Creating Array of Random Numbers
 Using the empty function we can easily create an array full
of random numbers.
ob = np.empty(9)
© 2008 Haim Michael 20150729
Creating Array of Sequence of Numbers
 Using the arange function we can easily create an array
that holds a sequence of numbers.The arange function
signature is similar to the signature of range.
© 2008 Haim Michael 20150729
Creating Array of Sequence of Numbers
import numpy as np
ob = np.arange(10,80,7)
print(ob)
© 2008 Haim Michael 20150729
Printing Array
 When trying to print an array we get a representation similar
to the representation of nested lists. One dimensional arrays
are printed as rows. Bidimensional arrays are printed as
matrices. Tridimensionals are printed as lists of matrices.
© 2008 Haim Michael 20150729
Printing Array
import numpy as np
ob = np.arange(24).reshape(4,3,2)
print(ob)
© 2008 Haim Michael 20150729
Basic Operations
 When performing arithmetic operators on arrays, a new
array is created and filled with the result.
import numpy as np
a = np.array([100, 200, 300, 400])
b = np.arange(1, 5)
c = a + b
print(c)
c = a - b
print(c)
c = a * b
print(c)
c = a / b
print(c)
© 2008 Haim Michael 20150729
Basic Operations
 The +=, -=, *=, and /= operators works as well. Please note
that although the documentation says these operators
modify rather than create a new one, the truth is different.
These operators are just a shorthand.
© 2008 Haim Michael 20150729
Basic Operations
 The way the * operator works is elementwise. In order to
calculate the product of two arrays we should use the @
operator or the dot method.
https://courses.lumenlearning.com/ivytech-collegealgebra/chapter/finding-the-product-of-two-matrices/
© 2008 Haim Michael 20150729
Basic Operations
import numpy as np
a = np.array( [ [2,3,4], [6,2,2] ] )
b = np.array( [ [5,4,3], [2,3,2], [1,1,1] ] )
c = a @ b
print("a:")
print(a)
print("b:")
print(b)
print("c:")
print(c)
© 2008 Haim Michael 20150729
Useful Methods
 Some operations are available through methods we can
invoke on the array object.
© 2008 Haim Michael 20150729
The sum Function
 This function returns the sum of all numbers the array holds,
or the sum of each column or row in accordance with the
value we pass over to the axis parameter.
import numpy as np
a = np.array( [ [2,3,4], [6,2,2] ] )
print("a.sum()=",a.sum())
print("a.sum(axis=0)",a.sum(axis=0)) #sum of columns
print("a.sum(axis=1)",a.sum(axis=1)) #sum of rows
© 2008 Haim Michael 20150729
The sum Function
import numpy as np
a = np.array( [ [2,3,4], [6,2,2] ] )
print("a.sum()=",a.sum())
print("a.sum(axis=0)",a.sum(axis=0)) #sum of columns
print("a.sum(axis=1)",a.sum(axis=1)) #sum of rows
© 2008 Haim Michael 20150729
The min Function
 This function returns the smallest number of all numbers the
array holds.
import numpy as np
a = np.array( [ [2,3,4], [6,2,-2] ] )
print("a.min()=",a.min())
© 2008 Haim Michael 20150729
Indexing, Slicing & Iterating
 When dealing with one dimensional arrays we can index,
slice and iterate, as if we were working with tuples or lists.
 When trying to index two dimensional arrays we should
specify two numbers. The index number of rows and the
index number of cols.
© 2008 Haim Michael 20150729
Indexing, Slicing & Iterating
import numpy as np
a = np.array( [ [2,3,4], [6,2,-2] ] )
print(a)
print(a[1,2])
© 2008 Haim Michael 20150729
Indexing, Slicing & Iterating
 When trying to slice a two dimensional array. We can
specify a column... a row... or a range of columns... or a
range of rows.
import numpy as np
a = np.array( [ [2,3,4], [6,2,-2] ] )
print(a)
print(a[0:2,0]) #all rows in column 0
print(a[0:2,1]) #all rows in column 1
print(a[0:2,0:2]) #all rows in column 0 and 1
© 2008 Haim Michael 20150729
Indexing, Slicing & Iterating
[[ 2 3 4]
[ 6 2 -2]]
[2 6]
[3 2]
[[2 3]
[6 2]]
import numpy as np
a = np.array( [ [2,3,4], [6,2,-2] ] )
print(a)
print()
print(a[0:2,0]) #all rows in column 0
print()
print(a[0:2,1]) #all rows in column 1
print()
print(a[0:2,0:2]) #all rows in column 0 and 1
© 2008 Haim Michael 20150729
Indexing, Slicing & Iterating
 When dealing with two dimensional array we can either
simply iterating the rows or we can iterate all elements by
using the flat attribute.
import numpy as np
a = np.array( [ [2,3,4], [6,2,-2] ] )
for num in a.flat:
print(num)
© 2008 Haim Michael 20150729
The fromfunction Function
 We can easily create a new array using a function we
specify.
import numpy as np
def f(x,y):
return 100*x+y
b = np.fromfunction(f,(2,3),dtype=int)

More Related Content

What's hot

What's hot (20)

Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTURE
 
Pandas Cheat Sheet
Pandas Cheat SheetPandas Cheat Sheet
Pandas Cheat Sheet
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Python Scipy Numpy
Python Scipy NumpyPython Scipy Numpy
Python Scipy Numpy
 
Array
ArrayArray
Array
 
NUMPY
NUMPY NUMPY
NUMPY
 
Numpy
NumpyNumpy
Numpy
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Approximation Data Structures for Streaming Applications
Approximation Data Structures for Streaming ApplicationsApproximation Data Structures for Streaming Applications
Approximation Data Structures for Streaming Applications
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
L5 array
L5 arrayL5 array
L5 array
 
Arrays basics
Arrays basicsArrays basics
Arrays basics
 
Python Pandas for Data Science cheatsheet
Python Pandas for Data Science cheatsheet Python Pandas for Data Science cheatsheet
Python Pandas for Data Science cheatsheet
 
Multi dimensional array
Multi dimensional arrayMulti dimensional array
Multi dimensional array
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning Libraries
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
 
Pandas,scipy,numpy cheatsheet
Pandas,scipy,numpy cheatsheetPandas,scipy,numpy cheatsheet
Pandas,scipy,numpy cheatsheet
 
Statistical inference for (Python) Data Analysis. An introduction.
Statistical inference for (Python) Data Analysis. An introduction.Statistical inference for (Python) Data Analysis. An introduction.
Statistical inference for (Python) Data Analysis. An introduction.
 
02 arrays
02 arrays02 arrays
02 arrays
 

Similar to The num py_library_20200818

CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfUmarMustafa13
 
Numpy in Python.docx
Numpy in Python.docxNumpy in Python.docx
Numpy in Python.docxmanohar25689
 
NUMPY [Autosaved] .pptx
NUMPY [Autosaved]                    .pptxNUMPY [Autosaved]                    .pptx
NUMPY [Autosaved] .pptxcoolmanbalu123
 
Essential numpy before you start your Machine Learning journey in python.pdf
Essential numpy before you start your Machine Learning journey in python.pdfEssential numpy before you start your Machine Learning journey in python.pdf
Essential numpy before you start your Machine Learning journey in python.pdfSmrati Kumar Katiyar
 
CAP776Numpy.ppt
CAP776Numpy.pptCAP776Numpy.ppt
CAP776Numpy.pptkdr52121
 
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...HendraPurnama31
 
python-numpyandpandas-170922144956 (1).pptx
python-numpyandpandas-170922144956 (1).pptxpython-numpyandpandas-170922144956 (1).pptx
python-numpyandpandas-170922144956 (1).pptxAkashgupta517936
 
Effective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyEffective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyKimikazu Kato
 
Introduction to NumPy
Introduction to NumPyIntroduction to NumPy
Introduction to NumPyHuy Nguyen
 
Pandas meetup 20200908
Pandas meetup 20200908Pandas meetup 20200908
Pandas meetup 20200908Haim Michael
 
Mastering Coding Assignments: Expert Help for Your Coding Assignment Needs
Mastering Coding Assignments: Expert Help for Your Coding Assignment NeedsMastering Coding Assignments: Expert Help for Your Coding Assignment Needs
Mastering Coding Assignments: Expert Help for Your Coding Assignment NeedsCoding Assignment Help
 
Informatics Practices (new) solution CBSE 2021, Compartment, improvement ex...
Informatics Practices (new) solution CBSE  2021, Compartment,  improvement ex...Informatics Practices (new) solution CBSE  2021, Compartment,  improvement ex...
Informatics Practices (new) solution CBSE 2021, Compartment, improvement ex...FarhanAhmade
 

Similar to The num py_library_20200818 (20)

CE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdfCE344L-200365-Lab2.pdf
CE344L-200365-Lab2.pdf
 
NumPy.pptx
NumPy.pptxNumPy.pptx
NumPy.pptx
 
NUMPY-2.pptx
NUMPY-2.pptxNUMPY-2.pptx
NUMPY-2.pptx
 
Numpy in Python.docx
Numpy in Python.docxNumpy in Python.docx
Numpy in Python.docx
 
NUMPY [Autosaved] .pptx
NUMPY [Autosaved]                    .pptxNUMPY [Autosaved]                    .pptx
NUMPY [Autosaved] .pptx
 
Essential numpy before you start your Machine Learning journey in python.pdf
Essential numpy before you start your Machine Learning journey in python.pdfEssential numpy before you start your Machine Learning journey in python.pdf
Essential numpy before you start your Machine Learning journey in python.pdf
 
CAP776Numpy (2).ppt
CAP776Numpy (2).pptCAP776Numpy (2).ppt
CAP776Numpy (2).ppt
 
CAP776Numpy.ppt
CAP776Numpy.pptCAP776Numpy.ppt
CAP776Numpy.ppt
 
Numpy - Array.pdf
Numpy - Array.pdfNumpy - Array.pdf
Numpy - Array.pdf
 
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
 
python-numpyandpandas-170922144956 (1).pptx
python-numpyandpandas-170922144956 (1).pptxpython-numpyandpandas-170922144956 (1).pptx
python-numpyandpandas-170922144956 (1).pptx
 
Effective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyEffective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPy
 
Introduction to NumPy
Introduction to NumPyIntroduction to NumPy
Introduction to NumPy
 
Pandas meetup 20200908
Pandas meetup 20200908Pandas meetup 20200908
Pandas meetup 20200908
 
C sharp chap6
C sharp chap6C sharp chap6
C sharp chap6
 
NumPy.pptx
NumPy.pptxNumPy.pptx
NumPy.pptx
 
C# p9
C# p9C# p9
C# p9
 
Mastering Coding Assignments: Expert Help for Your Coding Assignment Needs
Mastering Coding Assignments: Expert Help for Your Coding Assignment NeedsMastering Coding Assignments: Expert Help for Your Coding Assignment Needs
Mastering Coding Assignments: Expert Help for Your Coding Assignment Needs
 
Informatics Practices (new) solution CBSE 2021, Compartment, improvement ex...
Informatics Practices (new) solution CBSE  2021, Compartment,  improvement ex...Informatics Practices (new) solution CBSE  2021, Compartment,  improvement ex...
Informatics Practices (new) solution CBSE 2021, Compartment, improvement ex...
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 

More from Haim Michael

Virtual Threads in Java
Virtual Threads in JavaVirtual Threads in Java
Virtual Threads in JavaHaim Michael
 
MongoDB Design Patterns
MongoDB Design PatternsMongoDB Design Patterns
MongoDB Design PatternsHaim Michael
 
Introduction to SQL Injections
Introduction to SQL InjectionsIntroduction to SQL Injections
Introduction to SQL InjectionsHaim Michael
 
Record Classes in Java
Record Classes in JavaRecord Classes in Java
Record Classes in JavaHaim Michael
 
Microservices Design Patterns
Microservices Design PatternsMicroservices Design Patterns
Microservices Design PatternsHaim Michael
 
Structural Pattern Matching in Python
Structural Pattern Matching in PythonStructural Pattern Matching in Python
Structural Pattern Matching in PythonHaim Michael
 
Unit Testing in Python
Unit Testing in PythonUnit Testing in Python
Unit Testing in PythonHaim Michael
 
OOP Best Practices in JavaScript
OOP Best Practices in JavaScriptOOP Best Practices in JavaScript
OOP Best Practices in JavaScriptHaim Michael
 
JavaScript Jump Start 20220214
JavaScript Jump Start 20220214JavaScript Jump Start 20220214
JavaScript Jump Start 20220214Haim Michael
 
Bootstrap Jump Start
Bootstrap Jump StartBootstrap Jump Start
Bootstrap Jump StartHaim Michael
 
What is new in PHP
What is new in PHPWhat is new in PHP
What is new in PHPHaim Michael
 
What is new in Python 3.9
What is new in Python 3.9What is new in Python 3.9
What is new in Python 3.9Haim Michael
 
Programming in Python on Steroid
Programming in Python on SteroidProgramming in Python on Steroid
Programming in Python on SteroidHaim Michael
 
The matplotlib Library
The matplotlib LibraryThe matplotlib Library
The matplotlib LibraryHaim Michael
 
Jupyter notebook 20200728
Jupyter notebook 20200728Jupyter notebook 20200728
Jupyter notebook 20200728Haim Michael
 
Node.js Crash Course (Jump Start)
Node.js Crash Course (Jump Start) Node.js Crash Course (Jump Start)
Node.js Crash Course (Jump Start) Haim Michael
 
The Power of Decorators in Python [Meetup]
The Power of Decorators in Python [Meetup]The Power of Decorators in Python [Meetup]
The Power of Decorators in Python [Meetup]Haim Michael
 
Asynchronous JavaScript Programming
Asynchronous JavaScript ProgrammingAsynchronous JavaScript Programming
Asynchronous JavaScript ProgrammingHaim Michael
 

More from Haim Michael (20)

Anti Patterns
Anti PatternsAnti Patterns
Anti Patterns
 
Virtual Threads in Java
Virtual Threads in JavaVirtual Threads in Java
Virtual Threads in Java
 
MongoDB Design Patterns
MongoDB Design PatternsMongoDB Design Patterns
MongoDB Design Patterns
 
Introduction to SQL Injections
Introduction to SQL InjectionsIntroduction to SQL Injections
Introduction to SQL Injections
 
Record Classes in Java
Record Classes in JavaRecord Classes in Java
Record Classes in Java
 
Microservices Design Patterns
Microservices Design PatternsMicroservices Design Patterns
Microservices Design Patterns
 
Structural Pattern Matching in Python
Structural Pattern Matching in PythonStructural Pattern Matching in Python
Structural Pattern Matching in Python
 
Unit Testing in Python
Unit Testing in PythonUnit Testing in Python
Unit Testing in Python
 
OOP Best Practices in JavaScript
OOP Best Practices in JavaScriptOOP Best Practices in JavaScript
OOP Best Practices in JavaScript
 
Java Jump Start
Java Jump StartJava Jump Start
Java Jump Start
 
JavaScript Jump Start 20220214
JavaScript Jump Start 20220214JavaScript Jump Start 20220214
JavaScript Jump Start 20220214
 
Bootstrap Jump Start
Bootstrap Jump StartBootstrap Jump Start
Bootstrap Jump Start
 
What is new in PHP
What is new in PHPWhat is new in PHP
What is new in PHP
 
What is new in Python 3.9
What is new in Python 3.9What is new in Python 3.9
What is new in Python 3.9
 
Programming in Python on Steroid
Programming in Python on SteroidProgramming in Python on Steroid
Programming in Python on Steroid
 
The matplotlib Library
The matplotlib LibraryThe matplotlib Library
The matplotlib Library
 
Jupyter notebook 20200728
Jupyter notebook 20200728Jupyter notebook 20200728
Jupyter notebook 20200728
 
Node.js Crash Course (Jump Start)
Node.js Crash Course (Jump Start) Node.js Crash Course (Jump Start)
Node.js Crash Course (Jump Start)
 
The Power of Decorators in Python [Meetup]
The Power of Decorators in Python [Meetup]The Power of Decorators in Python [Meetup]
The Power of Decorators in Python [Meetup]
 
Asynchronous JavaScript Programming
Asynchronous JavaScript ProgrammingAsynchronous JavaScript Programming
Asynchronous JavaScript Programming
 

Recently uploaded

How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 

Recently uploaded (20)

How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 

The num py_library_20200818

  • 1. The NumPy Library Haim Michael August 18th , 2020 All logos, trade marks and brand names used in this presentation belong to the respective owners. lifemichael https://youtu.be/Tl5SyyBZA-4
  • 3. © 2008 Haim Michael 20150729 What is NumPy?  NumPy is the fundamental library in use when dealing with scientific computing.  The NumPy library provides us with a powerful n- dimensional array object.  In addition, it provides us with sophisticated functions, tools for integrating code in CC++ and in Fortran, useful linear algebra, fourier transforms, and random numbers generating capabilities.
  • 4. © 2008 Haim Michael 20150729 Installing NumPy  There are more than a few ways to install NumPy. The simplest would be using the pip utility. pip install numpy
  • 5. © 2008 Haim Michael 20150729 Checking NumPy Version  You can easily check the version of the NumPy you already have installed using the following code.  The expected outout should look like the following:
  • 6. © 2008 Haim Michael 20150729 The SciPy Ecosystem  SciPy (pronounced “Sigh Pie”) is a Python-based ecosystem of open-source software for mathematics, science, and engineering. Its core packages are the following: NumPy, SciPy, Matplotlib, IPython, SymPy and Pandas. http://scipy.org
  • 7. © 2008 Haim Michael 20150729 The SciPy Ecosystem  SciPy (pronounced “Sigh Pie”) is a Python-based ecosystem of open-source software for mathematics, science, and engineering. Its core packages are the following: NumPy, SciPy, Matplotlib, IPython, SymPy and Pandas. http://scipy.org
  • 8. © 2008 Haim Michael 20150729 SciPy Documentation  You can find detailed documentation for all SciPy's packages (including NumPy, Pandas, and Matplotlib) at https://www.scipy.org/docs.html
  • 10. © 2008 Haim Michael 20150729 The ndarray Class  NumPy's main purpose is to provide us with the capability to work with an homogeneous multidimensional array, which is a table of elements of the same type (usually numbers), indexed by a tuple of non negative numbers.  Objects instantiated from ndarray represent such homogeneous multidimensional arrays. The alias for ndarray is array.
  • 11. © 2008 Haim Michael 20150729 The ndarray Class  The number of dimensions is the rank of the array. The following code sample creates a two dimension array (the rank is 2). import numpy as np a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]]) print(a.ndim)
  • 12. © 2008 Haim Michael 20150729 The ndarray Class Attributes  The more important attributes, objects instantiated from the ndarray class have, are: ndim The number of axes (dimensions) of the array shape The dimensions of the array. The value of this attribute is a tuple that includes integers. Each integer is the size of a specific axe (dimension). When having a matrix with n rows and m columns, shape will be (n,m). The length of the shape tuple is therefore the number of axes, ndim.
  • 13. © 2008 Haim Michael 20150729 The ndarray Class Attributes size The total number of elements of the array. dtype It is an object that describes the type of the elements the array holds. It can be one of Python standard types or it can be one of the additional types NumPy provides us with (numpy.int32, numpy.int16, numpy.float64 etc.). itemsize This is the size in bytes of each element in the array (e.g. array of elements of the type float64 has the itemsize 8).
  • 14. © 2008 Haim Michael 20150729 The ndarray Class Attributes data This is a buffer that contains the actual elements of the array. Usually we will access the elements directly and won't need this attribute.
  • 15. © 2008 Haim Michael 20150729 Code Sample import numpy as np ob = np.arange(28).reshape(4,7) print(ob) print("ob.shape=",ob.shape) print("ob.ndim=",ob.ndim) print("ob.dtype.name=",ob.dtype.name) print("ob.itemsize=",ob.itemsize) print("ob.size=",ob.size) print("type(ob)=",type(ob))
  • 16. © 2008 Haim Michael 20150729 Code Sample import numpy as np ob = np.array([12,8,20,30]) print(ob) print("ob.shape=",ob.shape) print("ob.ndim=",ob.ndim) print("ob.dtype.name=",ob.dtype.name) print("ob.itemsize=",ob.itemsize) print("ob.size=",ob.size) print("type(ob)=",type(ob))
  • 17. © 2008 Haim Michael 20150729 Creating Simple Arrays  There are several ways for creating new objects of the type ndarray (AKA array): ob = np.array([3,4,5,6]) ob = np.array([[2,4,6],[8,54,2],[4,3,2]]) ob = np.array([[2,4],[8,2],[4,3]])
  • 18. © 2008 Haim Michael 20150729 Creating Array of Zeros  Using the zeros function we can easily create an array of zeros. ob = np.zeros(9)
  • 19. © 2008 Haim Michael 20150729 Creating Array of Ones  Using the ones function we can easily create an array full of ones. ob = np.ones(9)
  • 20. © 2008 Haim Michael 20150729 Creating Array of Random Numbers  Using the empty function we can easily create an array full of random numbers. ob = np.empty(9)
  • 21. © 2008 Haim Michael 20150729 Creating Array of Sequence of Numbers  Using the arange function we can easily create an array that holds a sequence of numbers.The arange function signature is similar to the signature of range.
  • 22. © 2008 Haim Michael 20150729 Creating Array of Sequence of Numbers import numpy as np ob = np.arange(10,80,7) print(ob)
  • 23. © 2008 Haim Michael 20150729 Printing Array  When trying to print an array we get a representation similar to the representation of nested lists. One dimensional arrays are printed as rows. Bidimensional arrays are printed as matrices. Tridimensionals are printed as lists of matrices.
  • 24. © 2008 Haim Michael 20150729 Printing Array import numpy as np ob = np.arange(24).reshape(4,3,2) print(ob)
  • 25. © 2008 Haim Michael 20150729 Basic Operations  When performing arithmetic operators on arrays, a new array is created and filled with the result. import numpy as np a = np.array([100, 200, 300, 400]) b = np.arange(1, 5) c = a + b print(c) c = a - b print(c) c = a * b print(c) c = a / b print(c)
  • 26. © 2008 Haim Michael 20150729 Basic Operations  The +=, -=, *=, and /= operators works as well. Please note that although the documentation says these operators modify rather than create a new one, the truth is different. These operators are just a shorthand.
  • 27. © 2008 Haim Michael 20150729 Basic Operations  The way the * operator works is elementwise. In order to calculate the product of two arrays we should use the @ operator or the dot method. https://courses.lumenlearning.com/ivytech-collegealgebra/chapter/finding-the-product-of-two-matrices/
  • 28. © 2008 Haim Michael 20150729 Basic Operations import numpy as np a = np.array( [ [2,3,4], [6,2,2] ] ) b = np.array( [ [5,4,3], [2,3,2], [1,1,1] ] ) c = a @ b print("a:") print(a) print("b:") print(b) print("c:") print(c)
  • 29. © 2008 Haim Michael 20150729 Useful Methods  Some operations are available through methods we can invoke on the array object.
  • 30. © 2008 Haim Michael 20150729 The sum Function  This function returns the sum of all numbers the array holds, or the sum of each column or row in accordance with the value we pass over to the axis parameter. import numpy as np a = np.array( [ [2,3,4], [6,2,2] ] ) print("a.sum()=",a.sum()) print("a.sum(axis=0)",a.sum(axis=0)) #sum of columns print("a.sum(axis=1)",a.sum(axis=1)) #sum of rows
  • 31. © 2008 Haim Michael 20150729 The sum Function import numpy as np a = np.array( [ [2,3,4], [6,2,2] ] ) print("a.sum()=",a.sum()) print("a.sum(axis=0)",a.sum(axis=0)) #sum of columns print("a.sum(axis=1)",a.sum(axis=1)) #sum of rows
  • 32. © 2008 Haim Michael 20150729 The min Function  This function returns the smallest number of all numbers the array holds. import numpy as np a = np.array( [ [2,3,4], [6,2,-2] ] ) print("a.min()=",a.min())
  • 33. © 2008 Haim Michael 20150729 Indexing, Slicing & Iterating  When dealing with one dimensional arrays we can index, slice and iterate, as if we were working with tuples or lists.  When trying to index two dimensional arrays we should specify two numbers. The index number of rows and the index number of cols.
  • 34. © 2008 Haim Michael 20150729 Indexing, Slicing & Iterating import numpy as np a = np.array( [ [2,3,4], [6,2,-2] ] ) print(a) print(a[1,2])
  • 35. © 2008 Haim Michael 20150729 Indexing, Slicing & Iterating  When trying to slice a two dimensional array. We can specify a column... a row... or a range of columns... or a range of rows. import numpy as np a = np.array( [ [2,3,4], [6,2,-2] ] ) print(a) print(a[0:2,0]) #all rows in column 0 print(a[0:2,1]) #all rows in column 1 print(a[0:2,0:2]) #all rows in column 0 and 1
  • 36. © 2008 Haim Michael 20150729 Indexing, Slicing & Iterating [[ 2 3 4] [ 6 2 -2]] [2 6] [3 2] [[2 3] [6 2]] import numpy as np a = np.array( [ [2,3,4], [6,2,-2] ] ) print(a) print() print(a[0:2,0]) #all rows in column 0 print() print(a[0:2,1]) #all rows in column 1 print() print(a[0:2,0:2]) #all rows in column 0 and 1
  • 37. © 2008 Haim Michael 20150729 Indexing, Slicing & Iterating  When dealing with two dimensional array we can either simply iterating the rows or we can iterate all elements by using the flat attribute. import numpy as np a = np.array( [ [2,3,4], [6,2,-2] ] ) for num in a.flat: print(num)
  • 38. © 2008 Haim Michael 20150729 The fromfunction Function  We can easily create a new array using a function we specify. import numpy as np def f(x,y): return 100*x+y b = np.fromfunction(f,(2,3),dtype=int)