SlideShare ist ein Scribd-Unternehmen logo
1 von 25
NumPy in Python
Dr.G.Jasmine Beulah
Assistant Professor, Dept Computer Science,
Kristu Jayanti College, Bengaluru.
• NumPy (Numerical Python) is an open source Python library that’s
used in almost every field of science and engineering.
• It’s the universal standard for working with numerical data in Python,
and it’s at the core of the scientific Python and PyData ecosystems.
• The NumPy library contains multidimensional array and matrix data
structures.
• It provides ndarray, a homogeneous n-dimensional array object, with
methods to efficiently operate on it. NumPy can be used to perform a
wide variety of mathematical operations on arrays.
• It adds powerful data structures to Python that guarantee efficient
calculations with arrays and matrices and it supplies an enormous
library of high-level mathematical functions that operate on these
arrays and matrices.
How to import NumPy
• To access NumPy and its functions import it in your Python code like
this:
• import numpy as np
What’s the difference between a Python list and a
NumPy array?
• NumPy gives you an enormous range of fast and efficient ways of
creating arrays and manipulating numerical data inside them.
• While a Python list can contain different data types within a single list,
all of the elements in a NumPy array should be homogeneous.
• The mathematical operations that are meant to be performed on
arrays would be extremely inefficient if the arrays weren’t
homogeneous.
Why use NumPy?
• NumPy arrays are faster and more compact than Python lists.
• An array consumes less memory and is convenient to use.
• NumPy uses much less memory to store data and it provides a
mechanism of specifying the data types.
• This allows the code to be optimized even further.
What is an array?
• An array is a central data structure of the NumPy library.
• An array is a grid of values and it contains information about the raw
data, how to locate an element, and how to interpret an element.
• It has a grid of elements that can be indexed in various ways.
• The elements are all of the same type, referred to as the array dtype.
• An array can be indexed by a tuple of nonnegative integers, by booleans, by another array, or by integers.
• The rank of the array is the number of dimensions.
• The shape of the array is a tuple of integers giving the size of the array along each dimension.
• One way we can initialize NumPy arrays is from Python lists, using nested lists for two- or higher-dimensional
data.
For example:
a = np.array([1, 2, 3, 4, 5, 6])
or:
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
• We can access the elements in the array using square brackets. When
you’re accessing elements, remember that indexing in NumPy starts
at 0. That means that if you want to access the first element in your
array, you’ll be accessing element “0”.
print(a[0])
[1 2 3 4]
How to create a basic array
• To create a NumPy array, you can use the function np.array().
import numpy as np
a = np.array([1, 2, 3])
create an array filled with 0’s:
np.zeros(2)
array([0., 0.])
An array filled with 1’s:
np.ones(2)
array([1., 1.])
Create a NumPy ndarray Object
• NumPy is used to work with arrays. The array object in NumPy is called
ndarray.
• We can create a NumPy ndarray object by using the array() function.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
print(type(arr))
o/p:arr is numpy.ndarray type
Use a tuple to create a NumPy array:
• To create an ndarray, we can pass a list, tuple or any array-like object
into the array() method, and it will be converted into an ndarray:
import numpy as np
arr = np.array((1, 2, 3, 4, 5))
print(arr)
o/p: [1 2 3 4 5]
Dimensions in Arrays
• A dimension in arrays is one level of array depth (nested arrays).
• nested array: are arrays that have arrays as their elements.
0-D Arrays
0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array.
Example
Create a 0-D array with value 42
import numpy as np
arr = np.array(42)
print(arr)
1-D Arrays
• An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array.
• These are the most common and basic arrays.
Example
Create a 1-D array containing the values 1,2,3,4,5:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
2-D Arrays
• An array that has 1-D arrays as its elements is called a 2-D array.
• These are often used to represent matrix or 2nd order tensors.
• NumPy has a whole sub module dedicated towards matrix operations called numpy.mat
Create a 2-D array containing two arrays with the values 1,2,3 and 4,5,6:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)
3-D arrays
• n array that has 2-D arrays (matrices) as its elements is called 3-D array.
• These are often used to represent a 3rd order tensor.
• Create a 3-D array with two 2-D arrays, both containing two arrays with the
values 1,2,3 and 4,5,6:
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(arr)
o/p: [[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]]
Check Number of Dimensions?
NumPy Arrays provides the ndim attribute that returns an integer that tells us how many
dimensions the array have.
Example
import numpy as np
a = np.array(42)
b = np.array([1, 2, 3, 4, 5])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)
• Creating an array from a sequence of elements, you can easily create an array
filled with 0’s:
np.zeros(2)
An array filled with 1’s:
np.ones(2)
# Create an empty array with 2 elements
np.empty(2)
#create an array with a range of elements:
np.arange(4)
#an array that contains a range of evenly spaced intervals. To do this, you will
specify the first number, last number, and the step size.
np.arange(2, 9, 2)
Use the Numpy Linspace Function
• The NumPy linspace function (sometimes called np.linspace) is a tool
in Python for creating numeric sequences.
np.linspace(start = 0, stop = 100, num = 5)
Syntax:
np.linspace(0, 10, num=5)
Specifying your data type
While the default data type is floating point (np.float64), you
can explicitly specify which data type you want using the dtype
keyword.
Adding, removing, and sorting elements
arr = np.array([2, 1, 5, 3, 7, 4, 6, 8])
np.sort(arr)
a = np.array([1, 2, 3, 4])
b = np.array([5, 6, 7, 8])
#concatenate with np.concatenate()
np.concatenate((a, b))
o/p:
array([1, 2, 3, 4, 5, 6, 7, 8])
#start with these arrays:
x = np.array([[1, 2], [3, 4]])
y = np.array([[5, 6]])
np.concatenate((x, y), axis=0)
o/p:
array([[1, 2],
[3, 4],
[5, 6]])

Weitere ähnliche Inhalte

Was ist angesagt?

Introduction to numpy Session 1
Introduction to numpy Session 1Introduction to numpy Session 1
Introduction to numpy Session 1Jatin Miglani
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in pythonhydpy
 
Learn Python Programming | Python Programming - Step by Step | Python for Beg...
Learn Python Programming | Python Programming - Step by Step | Python for Beg...Learn Python Programming | Python Programming - Step by Step | Python for Beg...
Learn Python Programming | Python Programming - Step by Step | Python for Beg...Edureka!
 
Python NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | EdurekaPython NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | EdurekaEdureka!
 
Scientific Computing with Python - NumPy | WeiYuan
Scientific Computing with Python - NumPy | WeiYuanScientific Computing with Python - NumPy | WeiYuan
Scientific Computing with Python - NumPy | WeiYuanWei-Yuan Chang
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionarynitamhaske
 
Introduction to NumPy
Introduction to NumPyIntroduction to NumPy
Introduction to NumPyHuy Nguyen
 
Strings in Python
Strings in PythonStrings in Python
Strings in Pythonnitamhaske
 

Was ist angesagt? (20)

Python pandas Library
Python pandas LibraryPython pandas Library
Python pandas Library
 
Pandas
PandasPandas
Pandas
 
Introduction to numpy Session 1
Introduction to numpy Session 1Introduction to numpy Session 1
Introduction to numpy Session 1
 
Python Data-Types
Python Data-TypesPython Data-Types
Python Data-Types
 
Introduction to numpy
Introduction to numpyIntroduction to numpy
Introduction to numpy
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
NumPy.pptx
NumPy.pptxNumPy.pptx
NumPy.pptx
 
Python programming
Python  programmingPython  programming
Python programming
 
Learn Python Programming | Python Programming - Step by Step | Python for Beg...
Learn Python Programming | Python Programming - Step by Step | Python for Beg...Learn Python Programming | Python Programming - Step by Step | Python for Beg...
Learn Python Programming | Python Programming - Step by Step | Python for Beg...
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Python NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | EdurekaPython NumPy Tutorial | NumPy Array | Edureka
Python NumPy Tutorial | NumPy Array | Edureka
 
Python basics
Python basicsPython basics
Python basics
 
Scientific Computing with Python - NumPy | WeiYuan
Scientific Computing with Python - NumPy | WeiYuanScientific Computing with Python - NumPy | WeiYuan
Scientific Computing with Python - NumPy | WeiYuan
 
List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
 
Pandas
PandasPandas
Pandas
 
Essential NumPy
Essential NumPyEssential NumPy
Essential NumPy
 
NumPy.pptx
NumPy.pptxNumPy.pptx
NumPy.pptx
 
Introduction to NumPy
Introduction to NumPyIntroduction to NumPy
Introduction to NumPy
 
NumPy
NumPyNumPy
NumPy
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
 

Ähnlich wie NumPy.pptx

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
 
Introduction to numpy.pptx
Introduction to numpy.pptxIntroduction to numpy.pptx
Introduction to numpy.pptxssuser0e701a
 
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
 
NumPy.pptx Bachelor of Computer Application
NumPy.pptx Bachelor of Computer ApplicationNumPy.pptx Bachelor of Computer Application
NumPy.pptx Bachelor of Computer Applicationsharmavishal49202
 
python-numpyandpandas-170922144956 (1).pptx
python-numpyandpandas-170922144956 (1).pptxpython-numpyandpandas-170922144956 (1).pptx
python-numpyandpandas-170922144956 (1).pptxAkashgupta517936
 
getting started with numpy and pandas.pptx
getting started with numpy and pandas.pptxgetting started with numpy and pandas.pptx
getting started with numpy and pandas.pptxworkvishalkumarmahat
 
ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...
ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...
ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...DineshThallapelly
 
Basic Introduction to Python Programming
Basic Introduction to Python ProgrammingBasic Introduction to Python Programming
Basic Introduction to Python ProgrammingSubashiniRathinavel
 
Week 11.pptx
Week 11.pptxWeek 11.pptx
Week 11.pptxCruiseCH
 
Numpy in Python.docx
Numpy in Python.docxNumpy in Python.docx
Numpy in Python.docxmanohar25689
 
arraycreation.pptx
arraycreation.pptxarraycreation.pptx
arraycreation.pptxsathya930629
 
Comparing EDA with classical and Bayesian analysis.pptx
Comparing EDA with classical and Bayesian analysis.pptxComparing EDA with classical and Bayesian analysis.pptx
Comparing EDA with classical and Bayesian analysis.pptxPremaGanesh1
 
Python 8416516 16 196 46 5163 51 63 51 6.pptx
Python 8416516 16 196 46 5163 51 63 51 6.pptxPython 8416516 16 196 46 5163 51 63 51 6.pptx
Python 8416516 16 196 46 5163 51 63 51 6.pptxChetanRaut43
 

Ähnlich wie NumPy.pptx (20)

NUMPY-2.pptx
NUMPY-2.pptxNUMPY-2.pptx
NUMPY-2.pptx
 
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
 
Introduction to numpy.pptx
Introduction to numpy.pptxIntroduction to numpy.pptx
Introduction to numpy.pptx
 
Numpy.pdf
Numpy.pdfNumpy.pdf
Numpy.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...
 
NumPy.pptx Bachelor of Computer Application
NumPy.pptx Bachelor of Computer ApplicationNumPy.pptx Bachelor of Computer Application
NumPy.pptx Bachelor of Computer Application
 
python-numpyandpandas-170922144956 (1).pptx
python-numpyandpandas-170922144956 (1).pptxpython-numpyandpandas-170922144956 (1).pptx
python-numpyandpandas-170922144956 (1).pptx
 
getting started with numpy and pandas.pptx
getting started with numpy and pandas.pptxgetting started with numpy and pandas.pptx
getting started with numpy and pandas.pptx
 
numpy.pdf
numpy.pdfnumpy.pdf
numpy.pdf
 
ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...
ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...
ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...
 
Basic Introduction to Python Programming
Basic Introduction to Python ProgrammingBasic Introduction to Python Programming
Basic Introduction to Python Programming
 
Week 11.pptx
Week 11.pptxWeek 11.pptx
Week 11.pptx
 
Numpy in Python.docx
Numpy in Python.docxNumpy in Python.docx
Numpy in Python.docx
 
arraycreation.pptx
arraycreation.pptxarraycreation.pptx
arraycreation.pptx
 
NumPy.pptx
NumPy.pptxNumPy.pptx
NumPy.pptx
 
Comparing EDA with classical and Bayesian analysis.pptx
Comparing EDA with classical and Bayesian analysis.pptxComparing EDA with classical and Bayesian analysis.pptx
Comparing EDA with classical and Bayesian analysis.pptx
 
Python 8416516 16 196 46 5163 51 63 51 6.pptx
Python 8416516 16 196 46 5163 51 63 51 6.pptxPython 8416516 16 196 46 5163 51 63 51 6.pptx
Python 8416516 16 196 46 5163 51 63 51 6.pptx
 

Mehr von DrJasmineBeulahG

Mehr von DrJasmineBeulahG (9)

File Handling in C.pptx
File Handling in C.pptxFile Handling in C.pptx
File Handling in C.pptx
 
Constants and Unformatted Input Output Functions.pptx
Constants and Unformatted Input Output Functions.pptxConstants and Unformatted Input Output Functions.pptx
Constants and Unformatted Input Output Functions.pptx
 
Software Testing.pptx
Software Testing.pptxSoftware Testing.pptx
Software Testing.pptx
 
Software Process Model.ppt
Software Process Model.pptSoftware Process Model.ppt
Software Process Model.ppt
 
Exception Handling in Python
Exception Handling in PythonException Handling in Python
Exception Handling in Python
 
STUDENT DETAILS DATABASE.pptx
STUDENT DETAILS DATABASE.pptxSTUDENT DETAILS DATABASE.pptx
STUDENT DETAILS DATABASE.pptx
 
Selection Sort.pptx
Selection Sort.pptxSelection Sort.pptx
Selection Sort.pptx
 
Structures
StructuresStructures
Structures
 
Arrays
ArraysArrays
Arrays
 

Kürzlich hochgeladen

Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxJohnnyPlasten
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...amitlee9823
 
Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...shambhavirathore45
 
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...shivangimorya083
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxolyaivanovalion
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxolyaivanovalion
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFxolyaivanovalion
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfMarinCaroMartnezBerg
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...amitlee9823
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfRachmat Ramadhan H
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Valters Lauzums
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Delhi Call girls
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxolyaivanovalion
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...amitlee9823
 
Zuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptxZuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptxolyaivanovalion
 
Smarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxSmarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxolyaivanovalion
 

Kürzlich hochgeladen (20)

Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptx
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
 
Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...Determinants of health, dimensions of health, positive health and spectrum of...
Determinants of health, dimensions of health, positive health and spectrum of...
 
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFx
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptx
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFx
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
 
Sampling (random) method and Non random.ppt
Sampling (random) method and Non random.pptSampling (random) method and Non random.ppt
Sampling (random) method and Non random.ppt
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptx
 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
Zuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptxZuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptx
 
Smarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptxSmarteg dropshipping via API with DroFx.pptx
Smarteg dropshipping via API with DroFx.pptx
 

NumPy.pptx

  • 1. NumPy in Python Dr.G.Jasmine Beulah Assistant Professor, Dept Computer Science, Kristu Jayanti College, Bengaluru.
  • 2. • NumPy (Numerical Python) is an open source Python library that’s used in almost every field of science and engineering. • It’s the universal standard for working with numerical data in Python, and it’s at the core of the scientific Python and PyData ecosystems.
  • 3. • The NumPy library contains multidimensional array and matrix data structures. • It provides ndarray, a homogeneous n-dimensional array object, with methods to efficiently operate on it. NumPy can be used to perform a wide variety of mathematical operations on arrays. • It adds powerful data structures to Python that guarantee efficient calculations with arrays and matrices and it supplies an enormous library of high-level mathematical functions that operate on these arrays and matrices.
  • 4. How to import NumPy • To access NumPy and its functions import it in your Python code like this: • import numpy as np
  • 5. What’s the difference between a Python list and a NumPy array? • NumPy gives you an enormous range of fast and efficient ways of creating arrays and manipulating numerical data inside them. • While a Python list can contain different data types within a single list, all of the elements in a NumPy array should be homogeneous. • The mathematical operations that are meant to be performed on arrays would be extremely inefficient if the arrays weren’t homogeneous.
  • 6. Why use NumPy? • NumPy arrays are faster and more compact than Python lists. • An array consumes less memory and is convenient to use. • NumPy uses much less memory to store data and it provides a mechanism of specifying the data types. • This allows the code to be optimized even further.
  • 7. What is an array? • An array is a central data structure of the NumPy library. • An array is a grid of values and it contains information about the raw data, how to locate an element, and how to interpret an element. • It has a grid of elements that can be indexed in various ways. • The elements are all of the same type, referred to as the array dtype.
  • 8. • An array can be indexed by a tuple of nonnegative integers, by booleans, by another array, or by integers. • The rank of the array is the number of dimensions. • The shape of the array is a tuple of integers giving the size of the array along each dimension. • One way we can initialize NumPy arrays is from Python lists, using nested lists for two- or higher-dimensional data. For example: a = np.array([1, 2, 3, 4, 5, 6]) or: a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
  • 9. • We can access the elements in the array using square brackets. When you’re accessing elements, remember that indexing in NumPy starts at 0. That means that if you want to access the first element in your array, you’ll be accessing element “0”. print(a[0]) [1 2 3 4]
  • 10. How to create a basic array • To create a NumPy array, you can use the function np.array(). import numpy as np a = np.array([1, 2, 3])
  • 11. create an array filled with 0’s: np.zeros(2) array([0., 0.]) An array filled with 1’s: np.ones(2) array([1., 1.])
  • 12. Create a NumPy ndarray Object • NumPy is used to work with arrays. The array object in NumPy is called ndarray. • We can create a NumPy ndarray object by using the array() function. import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(arr) print(type(arr)) o/p:arr is numpy.ndarray type
  • 13. Use a tuple to create a NumPy array: • To create an ndarray, we can pass a list, tuple or any array-like object into the array() method, and it will be converted into an ndarray: import numpy as np arr = np.array((1, 2, 3, 4, 5)) print(arr) o/p: [1 2 3 4 5]
  • 14. Dimensions in Arrays • A dimension in arrays is one level of array depth (nested arrays). • nested array: are arrays that have arrays as their elements. 0-D Arrays 0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array. Example Create a 0-D array with value 42 import numpy as np arr = np.array(42) print(arr)
  • 15. 1-D Arrays • An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array. • These are the most common and basic arrays. Example Create a 1-D array containing the values 1,2,3,4,5: import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(arr)
  • 16. 2-D Arrays • An array that has 1-D arrays as its elements is called a 2-D array. • These are often used to represent matrix or 2nd order tensors. • NumPy has a whole sub module dedicated towards matrix operations called numpy.mat Create a 2-D array containing two arrays with the values 1,2,3 and 4,5,6: import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr)
  • 17. 3-D arrays • n array that has 2-D arrays (matrices) as its elements is called 3-D array. • These are often used to represent a 3rd order tensor. • Create a 3-D array with two 2-D arrays, both containing two arrays with the values 1,2,3 and 4,5,6: import numpy as np arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]]) print(arr) o/p: [[[1 2 3] [4 5 6]] [[1 2 3] [4 5 6]]]
  • 18. Check Number of Dimensions? NumPy Arrays provides the ndim attribute that returns an integer that tells us how many dimensions the array have. Example import numpy as np a = np.array(42) b = np.array([1, 2, 3, 4, 5]) c = np.array([[1, 2, 3], [4, 5, 6]]) d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]]) print(a.ndim) print(b.ndim) print(c.ndim) print(d.ndim)
  • 19. • Creating an array from a sequence of elements, you can easily create an array filled with 0’s: np.zeros(2) An array filled with 1’s: np.ones(2) # Create an empty array with 2 elements np.empty(2) #create an array with a range of elements: np.arange(4) #an array that contains a range of evenly spaced intervals. To do this, you will specify the first number, last number, and the step size. np.arange(2, 9, 2)
  • 20. Use the Numpy Linspace Function • The NumPy linspace function (sometimes called np.linspace) is a tool in Python for creating numeric sequences. np.linspace(start = 0, stop = 100, num = 5)
  • 21.
  • 23.
  • 24. Specifying your data type While the default data type is floating point (np.float64), you can explicitly specify which data type you want using the dtype keyword.
  • 25. Adding, removing, and sorting elements arr = np.array([2, 1, 5, 3, 7, 4, 6, 8]) np.sort(arr) a = np.array([1, 2, 3, 4]) b = np.array([5, 6, 7, 8]) #concatenate with np.concatenate() np.concatenate((a, b)) o/p: array([1, 2, 3, 4, 5, 6, 7, 8]) #start with these arrays: x = np.array([[1, 2], [3, 4]]) y = np.array([[5, 6]]) np.concatenate((x, y), axis=0) o/p: array([[1, 2], [3, 4], [5, 6]])