SlideShare a Scribd company logo
1 of 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]])

More Related Content

What's hot

Artificial Neural Network | Deep Neural Network Explained | Artificial Neural...
Artificial Neural Network | Deep Neural Network Explained | Artificial Neural...Artificial Neural Network | Deep Neural Network Explained | Artificial Neural...
Artificial Neural Network | Deep Neural Network Explained | Artificial Neural...
Simplilearn
ย 

What's hot (20)

Pandas
PandasPandas
Pandas
ย 
Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandas
ย 
Introduction to NumPy
Introduction to NumPyIntroduction to NumPy
Introduction to NumPy
ย 
Introduction to Python Pandas for Data Analytics
Introduction to Python Pandas for Data AnalyticsIntroduction to Python Pandas for Data Analytics
Introduction to Python Pandas for Data Analytics
ย 
Python Collections
Python CollectionsPython Collections
Python Collections
ย 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
ย 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
ย 
NumPy.pptx
NumPy.pptxNumPy.pptx
NumPy.pptx
ย 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
ย 
Plotting data with python and pylab
Plotting data with python and pylabPlotting data with python and pylab
Plotting data with python and pylab
ย 
NumPy
NumPyNumPy
NumPy
ย 
Python Scipy Numpy
Python Scipy NumpyPython Scipy Numpy
Python Scipy Numpy
ย 
8 python data structure-1
8 python data structure-18 python data structure-1
8 python data structure-1
ย 
Dictionaries and Sets in Python
Dictionaries and Sets in PythonDictionaries and Sets in Python
Dictionaries and Sets in Python
ย 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
ย 
Pandas
PandasPandas
Pandas
ย 
NumPy.pptx
NumPy.pptxNumPy.pptx
NumPy.pptx
ย 
NUMPY
NUMPY NUMPY
NUMPY
ย 
Artificial Neural Network | Deep Neural Network Explained | Artificial Neural...
Artificial Neural Network | Deep Neural Network Explained | Artificial Neural...Artificial Neural Network | Deep Neural Network Explained | Artificial Neural...
Artificial Neural Network | Deep Neural Network Explained | Artificial Neural...
ย 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
ย 

Similar to NumPy.pptx

NUMPY [Autosaved] .pptx
NUMPY [Autosaved]                    .pptxNUMPY [Autosaved]                    .pptx
NUMPY [Autosaved] .pptx
coolmanbalu123
ย 
NumPy.pptx Bachelor of Computer Application
NumPy.pptx Bachelor of Computer ApplicationNumPy.pptx Bachelor of Computer Application
NumPy.pptx Bachelor of Computer Application
sharmavishal49202
ย 
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
ย 
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
PremaGanesh1
ย 

Similar to NumPy.pptx (20)

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
ย 
Introduction to Numpy, NumPy Arrays.pdf
Introduction to Numpy, NumPy  Arrays.pdfIntroduction to Numpy, NumPy  Arrays.pdf
Introduction to Numpy, NumPy Arrays.pdf
ย 
Scientific Computing with Python - NumPy | WeiYuan
Scientific Computing with Python - NumPy | WeiYuanScientific Computing with Python - NumPy | WeiYuan
Scientific Computing with Python - NumPy | WeiYuan
ย 
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
ย 

More from 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
ย 

Recently uploaded

Diamond Harbour \ Russian Call Girls Kolkata | Book 8005736733 Extreme Naught...
Diamond Harbour \ Russian Call Girls Kolkata | Book 8005736733 Extreme Naught...Diamond Harbour \ Russian Call Girls Kolkata | Book 8005736733 Extreme Naught...
Diamond Harbour \ Russian Call Girls Kolkata | Book 8005736733 Extreme Naught...
HyderabadDolls
ย 
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
gajnagarg
ย 
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
nirzagarg
ย 
Abortion pills in Doha {{ QATAR }} +966572737505) Get Cytotec
Abortion pills in Doha {{ QATAR }} +966572737505) Get CytotecAbortion pills in Doha {{ QATAR }} +966572737505) Get Cytotec
Abortion pills in Doha {{ QATAR }} +966572737505) Get Cytotec
Abortion pills in Riyadh +966572737505 get cytotec
ย 
Lake Town / Independent Kolkata Call Girls Phone No 8005736733 Elite Escort S...
Lake Town / Independent Kolkata Call Girls Phone No 8005736733 Elite Escort S...Lake Town / Independent Kolkata Call Girls Phone No 8005736733 Elite Escort S...
Lake Town / Independent Kolkata Call Girls Phone No 8005736733 Elite Escort S...
HyderabadDolls
ย 
Top Call Girls in Balaghat 9332606886Call Girls Advance Cash On Delivery Ser...
Top Call Girls in Balaghat  9332606886Call Girls Advance Cash On Delivery Ser...Top Call Girls in Balaghat  9332606886Call Girls Advance Cash On Delivery Ser...
Top Call Girls in Balaghat 9332606886Call Girls Advance Cash On Delivery Ser...
kumargunjan9515
ย 
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
nirzagarg
ย 
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Klinik kandungan
ย 
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
gajnagarg
ย 
Top profile Call Girls In Nandurbar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Nandurbar [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Nandurbar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Nandurbar [ 7014168258 ] Call Me For Genuine Models...
gajnagarg
ย 
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
nirzagarg
ย 

Recently uploaded (20)

๐Ÿ’ž Safe And Secure Call Girls Agra Call Girls Service Just Call ๐Ÿ‘๐Ÿ‘„6378878445 ๐Ÿ‘...
๐Ÿ’ž Safe And Secure Call Girls Agra Call Girls Service Just Call ๐Ÿ‘๐Ÿ‘„6378878445 ๐Ÿ‘...๐Ÿ’ž Safe And Secure Call Girls Agra Call Girls Service Just Call ๐Ÿ‘๐Ÿ‘„6378878445 ๐Ÿ‘...
๐Ÿ’ž Safe And Secure Call Girls Agra Call Girls Service Just Call ๐Ÿ‘๐Ÿ‘„6378878445 ๐Ÿ‘...
ย 
Diamond Harbour \ Russian Call Girls Kolkata | Book 8005736733 Extreme Naught...
Diamond Harbour \ Russian Call Girls Kolkata | Book 8005736733 Extreme Naught...Diamond Harbour \ Russian Call Girls Kolkata | Book 8005736733 Extreme Naught...
Diamond Harbour \ Russian Call Girls Kolkata | Book 8005736733 Extreme Naught...
ย 
Case Study 4 Where the cry of rebellion happen?
Case Study 4 Where the cry of rebellion happen?Case Study 4 Where the cry of rebellion happen?
Case Study 4 Where the cry of rebellion happen?
ย 
Ranking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRanking and Scoring Exercises for Research
Ranking and Scoring Exercises for Research
ย 
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
ย 
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
ย 
Abortion pills in Doha {{ QATAR }} +966572737505) Get Cytotec
Abortion pills in Doha {{ QATAR }} +966572737505) Get CytotecAbortion pills in Doha {{ QATAR }} +966572737505) Get Cytotec
Abortion pills in Doha {{ QATAR }} +966572737505) Get Cytotec
ย 
Lake Town / Independent Kolkata Call Girls Phone No 8005736733 Elite Escort S...
Lake Town / Independent Kolkata Call Girls Phone No 8005736733 Elite Escort S...Lake Town / Independent Kolkata Call Girls Phone No 8005736733 Elite Escort S...
Lake Town / Independent Kolkata Call Girls Phone No 8005736733 Elite Escort S...
ย 
Top Call Girls in Balaghat 9332606886Call Girls Advance Cash On Delivery Ser...
Top Call Girls in Balaghat  9332606886Call Girls Advance Cash On Delivery Ser...Top Call Girls in Balaghat  9332606886Call Girls Advance Cash On Delivery Ser...
Top Call Girls in Balaghat 9332606886Call Girls Advance Cash On Delivery Ser...
ย 
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptxRESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
ย 
Charbagh + Female Escorts Service in Lucknow | Starting โ‚น,5K To @25k with A/C...
Charbagh + Female Escorts Service in Lucknow | Starting โ‚น,5K To @25k with A/C...Charbagh + Female Escorts Service in Lucknow | Starting โ‚น,5K To @25k with A/C...
Charbagh + Female Escorts Service in Lucknow | Starting โ‚น,5K To @25k with A/C...
ย 
Digital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham WareDigital Transformation Playbook by Graham Ware
Digital Transformation Playbook by Graham Ware
ย 
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
ย 
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
ย 
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
ย 
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
ย 
Top profile Call Girls In Nandurbar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Nandurbar [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Nandurbar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Nandurbar [ 7014168258 ] Call Me For Genuine Models...
ย 
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
ย 
๐Ÿ‘‰ Bhilai Call Girls Service Just Call ๐Ÿ‘๐Ÿ‘„6378878445 ๐Ÿ‘๐Ÿ‘„ Top Class Call Girl Ser...
๐Ÿ‘‰ Bhilai Call Girls Service Just Call ๐Ÿ‘๐Ÿ‘„6378878445 ๐Ÿ‘๐Ÿ‘„ Top Class Call Girl Ser...๐Ÿ‘‰ Bhilai Call Girls Service Just Call ๐Ÿ‘๐Ÿ‘„6378878445 ๐Ÿ‘๐Ÿ‘„ Top Class Call Girl Ser...
๐Ÿ‘‰ Bhilai Call Girls Service Just Call ๐Ÿ‘๐Ÿ‘„6378878445 ๐Ÿ‘๐Ÿ‘„ Top Class Call Girl Ser...
ย 
็คพๅ†…ๅ‹‰ๅผทไผš่ณ‡ๆ–™_Object Recognition as Next Token Prediction
็คพๅ†…ๅ‹‰ๅผทไผš่ณ‡ๆ–™_Object Recognition as Next Token Prediction็คพๅ†…ๅ‹‰ๅผทไผš่ณ‡ๆ–™_Object Recognition as Next Token Prediction
็คพๅ†…ๅ‹‰ๅผทไผš่ณ‡ๆ–™_Object Recognition as Next Token Prediction
ย 

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]])