SlideShare a Scribd company logo
NumPy- Numerical
Python
DAP
Lecture
Installation of
Numpy
Step1. In pycharm,go to
terminal
Step 2.Write following
command
Or
Step 1. Goto project settings in
Pycharm
Step2. Select Python interpreter
Step3. Search package to install and
add
Arrays in
python
import array
# initializing array with array values
# initializes array with signed integers
arr = array.array('i', [1, 2, 3])
# printing original array
print ("The new created array is :
",end="")
for i in range (0,3):
print (arr[i], end="
")
Array module can be imported to create arrays
in python but operations on such arrays was
very slow Due to its slow response,it lead to use
of Numpy
Datatype of Arrays in
python
TYPE CODE C TYPE
PYTHON MINIMUM
SIZE
TYPE IN BYTES
‘b’ signed char int 1
‘B’ unsigned char int 1
‘i’ signed int int 2
‘I’ unsigned int int 2
‘f’ float float 4
‘d’ double float 8
These are the datatypes which can be used while creating array using array
module
Array creation using
numpy
We can use arange function from NumPy that creates a NumPy
array for us with integers 0 to n.
import numpy
arr=numpy.arange(5)
for i in range(0,
len(arr)):
print(arr[i])
Output
Difference between code with numpy and without
numpy
Took 3000
microseconds
Took 2000
microseconds
for i in range(0, len(arr1)):
arr3.append(arr1[i]
+arr2[i]) print(arr3[i],
end=" ")
Without numpy
import datetime
def printelements(n):
arr1=range(n)
arr2=range(n)
arr3=[]
start = datetime.datetime.now()
printelements( 300)
diff = datetime.datetime.now() - start
print("time taken",
diff.microseconds)
import numpy as n
import datetime
def printelements(num):
arr1=n.arange(num)
arr2=n.arange(num)
arr3=arr1+arr2
for i in range(0, len(arr1)):
print(arr3[i], end=" ")
start = datetime.datetime.now()
printelements( 300)
diff = datetime.datetime.now() - start
print("time taken",
diff.microseconds)
Adding two arrays
With
numpy
Datatypes in
Numpy
Python has an integer type, a float type, and a complex type, however, this is not
enough for scientific computing and, for this reason, NumPy has a lot more data
types.Datatypes are associated with different precision and different memory size.
We can view the datatype of array created
import numpy as n
arr1=n.arange( 12
)
print(arr1.dtype)
The data type of array a is int32 (at least on my machine), but you may get int64 as
output if you are using 64-bit Python.
Numeric Datatypes in
Numpy
Examples of creating arrays with
dtype
arr1=numpy.arange(7,dtype="float16")
print(arr1)
arr1=numpy.arange(7,dtype="int16")
print(arr1)
arr1=numpy.arange(7,dtype=“complex")
print(arr1)
Output:
[0. 1. 2. 3. 4. 5. 6.]
[0 1 2 3 4 5 6]
[0.+0.j 1.+0.j 2.+0.j 3.+0.j 4.+0.j
5.+0.j 6.+0.j]
Character codes associated with
datatypes
Character codes are included for backward compatibility with Numeric. Numeric
is the predecessor of NumPy. Their use is not recommended, but the codes are
provided here because they pop up in several places. You should instead use
dtype objects.
Example:
Output
:
Datatype(dtype) Objects &
Constructors
Data type objects are instances of the numpy.dtype class. Once again, arrays have a data
type. Every element in a NumPy array has the same data type. Each dtype object has
itemsize attribute which tell you the size of the data in bytes.
dtype constructors
We have a variety of ways to create data types. We can use either numeric
datatypes or character codes to be passed to constructor to create object as
shown below:
D1=dtype(“float1
6) D2=dtype(“f”)
Create the record:
To create heterogenous datatype which contain multiple
datatype D3 = dtype([('name', str, 40), ('numitems', int),
('price',float)])
Dtype constructor
examples
▪ We can use numeric
datatypes: dtype(float32)
▪ We can specify a single precision float with a
character code: dtype('f')
▪ We can use a double precision float character
code: dtype('d')
▪ We can use a complex character code: dtype(‘D')
▪ We can give the data type constructor a two-
character code. The first character signifies the
type; the second character is a number specifying
the number of bytes in the type: dtype('f8')
Example
:
Outpu
t:
Dtype
Attributes
Attribute Description
dtype.char Gives a unique character code for each of the
21 different built-in types. E.g ‘D’
dtype.type corresponds to the type of object of the
array elements e.g float32
dtype.str Gives string representation of the data type.
It starts with a character representing
endianness(<or>), then a character code,
followed by a number corresponding to the
number of bytes that each array item requires.
Eg. ‘<f8’ <- Endianness, f-float,8-8 bytes
dtype.byteorder
(Endiannness)
Endianness, here, means the way bytes are ordered
within a 32 or 64-bit word. In big-endian order, the most
significant byte is stored first. In little-endian order, the
least significant byte is stored first.
dtype.itemsize Gives size of data in bytes example 64
ENDIANNESS
0x00040000 0x00040001 0x00040002 0x00040003
0x00040000 0x00040001 0x00040002 0x00040003
Consider number 0x12674592 in 32-bit representation can be stored
as −
MSB LSB
Examples of dtype
attributes
Creating Multidimensional
Array
Use array() function to create 2-D array
from numpy import *
matrix1= array([[0,2],[3,4]])
Or
row1=array([1,2])
row2=array([3,4],’I’)
matrix1 = array([row1,row2])
print(matrix1)
□[0,2]
[3,4]
Creating Multidimensional
Array
Program to create a matrix and
display it
from numpy import *
matrix1 = array([[0,2],[3,4]])
for i in range(0,len(matrix1[0])):
for jin range(0, len(matrix1)):
print(matrix1[i][j],end="
")
print("n")
#len(matrix1[0]) give number of columns in first row
#len(matrix1) gives number of rows
Outpu
t
To add two matrices simply create two matrices with data
and third zero matrix
matrix1 = array([[0,2],[3,4]])
matrix2 = array([[0,2],[3,4]])
matrix3 = array([[0,0],[0,0]])
Then Simply add in loop
matrix3[i][j]= matrix1[i][j]+matrix2[i][j]=

More Related Content

Similar to Numpy _ (20)

PPTX
Numpy_Pandas_for beginners_________.pptx
Abhi Marvel
 
KEY
Numpy Talk at SIAM
Enthought, Inc.
 
PPTX
Unit 1 array based implementation
LavanyaJ28
 
PPTX
object oriented programing in python and pip
LakshmiMarineni
 
PDF
Standardizing arrays -- Microsoft Presentation
Travis Oliphant
 
PPT
CAP776Numpy (2).ppt
ChhaviCoachingCenter
 
PPT
CAP776Numpy.ppt
kdr52121
 
PDF
Numpy ndarrays.pdf
SudhanshiBakre1
 
PDF
Crafting Your Own Numpy: Do More in C++ and Make It Python @ PyCon JP 2024
Anchi Liu
 
PDF
Python for Computer Vision - Revision
Ahmed Gad
 
PDF
Python Data Science Cheat Sheet NumPy Basics 2 .pdf
1stepgrow
 
PDF
Session2
daviessegera
 
PPTX
Introduction-to-NumPy-in-Python (1).pptx
disserdekabrcha
 
PDF
Migrating from matlab to python
ActiveState
 
PPTX
DATA ANALYSIS AND VISUALISATION using python
ChiragNahata2
 
PDF
CDAT - cdms numpy arrays - Introduction
Arulalan T
 
PPTX
NumPy
AbhijeetAnand88
 
PPTX
lec08-numpy.pptx
lekha572836
 
PDF
Python programming : Arrays
Emertxe Information Technologies Pvt Ltd
 
Numpy_Pandas_for beginners_________.pptx
Abhi Marvel
 
Numpy Talk at SIAM
Enthought, Inc.
 
Unit 1 array based implementation
LavanyaJ28
 
object oriented programing in python and pip
LakshmiMarineni
 
Standardizing arrays -- Microsoft Presentation
Travis Oliphant
 
CAP776Numpy (2).ppt
ChhaviCoachingCenter
 
CAP776Numpy.ppt
kdr52121
 
Numpy ndarrays.pdf
SudhanshiBakre1
 
Crafting Your Own Numpy: Do More in C++ and Make It Python @ PyCon JP 2024
Anchi Liu
 
Python for Computer Vision - Revision
Ahmed Gad
 
Python Data Science Cheat Sheet NumPy Basics 2 .pdf
1stepgrow
 
Session2
daviessegera
 
Introduction-to-NumPy-in-Python (1).pptx
disserdekabrcha
 
Migrating from matlab to python
ActiveState
 
DATA ANALYSIS AND VISUALISATION using python
ChiragNahata2
 
CDAT - cdms numpy arrays - Introduction
Arulalan T
 
lec08-numpy.pptx
lekha572836
 
Python programming : Arrays
Emertxe Information Technologies Pvt Ltd
 

More from SwatiHans10 (20)

PPT
ip addressing _
SwatiHans10
 
PPT
stop and wait _
SwatiHans10
 
PPT
TCP _
SwatiHans10
 
PPT
subnetting _
SwatiHans10
 
PPT
PipelineHazards _
SwatiHans10
 
PPT
Pipelining _
SwatiHans10
 
PPTX
Mobile Customer Experience Management.pptx
SwatiHans10
 
PPTX
Social Web multimedia analytics goals _
SwatiHans10
 
PPTX
Hardwires and Microprogrammed Control ,
SwatiHans10
 
PPTX
Restoring Algorithm _
SwatiHans10
 
PPTX
Non -Restoring Algorithm _
SwatiHans10
 
PPTX
loops _
SwatiHans10
 
PPT
functions _
SwatiHans10
 
PDF
Instruction execution cycle _
SwatiHans10
 
PPT
RTL,Instruction set _
SwatiHans10
 
PPT
Data representation _
SwatiHans10
 
PPTX
CAO PPT-Lect 1 _
SwatiHans10
 
PPTX
CIRCULAR LINKED LIST _
SwatiHans10
 
PPT
Transmission control protocol _
SwatiHans10
 
PPTX
Lecture 1 .
SwatiHans10
 
ip addressing _
SwatiHans10
 
stop and wait _
SwatiHans10
 
subnetting _
SwatiHans10
 
PipelineHazards _
SwatiHans10
 
Pipelining _
SwatiHans10
 
Mobile Customer Experience Management.pptx
SwatiHans10
 
Social Web multimedia analytics goals _
SwatiHans10
 
Hardwires and Microprogrammed Control ,
SwatiHans10
 
Restoring Algorithm _
SwatiHans10
 
Non -Restoring Algorithm _
SwatiHans10
 
loops _
SwatiHans10
 
functions _
SwatiHans10
 
Instruction execution cycle _
SwatiHans10
 
RTL,Instruction set _
SwatiHans10
 
Data representation _
SwatiHans10
 
CAO PPT-Lect 1 _
SwatiHans10
 
CIRCULAR LINKED LIST _
SwatiHans10
 
Transmission control protocol _
SwatiHans10
 
Lecture 1 .
SwatiHans10
 
Ad

Recently uploaded (20)

PDF
Artificial Neural Network-Types,Perceptron,Problems
Sharmila Chidaravalli
 
PDF
Module - 5 Machine Learning-22ISE62.pdf
Dr. Shivashankar
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PPTX
darshai cross section and river section analysis
muk7971
 
PPT
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
PDF
A Brief Introduction About Robert Paul Hardee
Robert Paul Hardee
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PDF
Module - 4 Machine Learning -22ISE62.pdf
Dr. Shivashankar
 
PPTX
UNIT 1 - INTRODUCTION TO AI and AI tools and basic concept
gokuld13012005
 
PPTX
Alan Turing - life and importance for all of us now
Pedro Concejero
 
PDF
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
PPTX
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
PPTX
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PPTX
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
PPT
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
PDF
William Stallings - Foundations of Modern Networking_ SDN, NFV, QoE, IoT, and...
lavanya896395
 
PPTX
Final Major project a b c d e f g h i j k l m
bharathpsnab
 
PPTX
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
Artificial Neural Network-Types,Perceptron,Problems
Sharmila Chidaravalli
 
Module - 5 Machine Learning-22ISE62.pdf
Dr. Shivashankar
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
darshai cross section and river section analysis
muk7971
 
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
A Brief Introduction About Robert Paul Hardee
Robert Paul Hardee
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
Module - 4 Machine Learning -22ISE62.pdf
Dr. Shivashankar
 
UNIT 1 - INTRODUCTION TO AI and AI tools and basic concept
gokuld13012005
 
Alan Turing - life and importance for all of us now
Pedro Concejero
 
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
William Stallings - Foundations of Modern Networking_ SDN, NFV, QoE, IoT, and...
lavanya896395
 
Final Major project a b c d e f g h i j k l m
bharathpsnab
 
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
Ad

Numpy _

  • 2. Installation of Numpy Step1. In pycharm,go to terminal Step 2.Write following command Or Step 1. Goto project settings in Pycharm Step2. Select Python interpreter Step3. Search package to install and add
  • 3. Arrays in python import array # initializing array with array values # initializes array with signed integers arr = array.array('i', [1, 2, 3]) # printing original array print ("The new created array is : ",end="") for i in range (0,3): print (arr[i], end=" ") Array module can be imported to create arrays in python but operations on such arrays was very slow Due to its slow response,it lead to use of Numpy
  • 4. Datatype of Arrays in python TYPE CODE C TYPE PYTHON MINIMUM SIZE TYPE IN BYTES ‘b’ signed char int 1 ‘B’ unsigned char int 1 ‘i’ signed int int 2 ‘I’ unsigned int int 2 ‘f’ float float 4 ‘d’ double float 8 These are the datatypes which can be used while creating array using array module
  • 5. Array creation using numpy We can use arange function from NumPy that creates a NumPy array for us with integers 0 to n. import numpy arr=numpy.arange(5) for i in range(0, len(arr)): print(arr[i]) Output
  • 6. Difference between code with numpy and without numpy Took 3000 microseconds Took 2000 microseconds for i in range(0, len(arr1)): arr3.append(arr1[i] +arr2[i]) print(arr3[i], end=" ") Without numpy import datetime def printelements(n): arr1=range(n) arr2=range(n) arr3=[] start = datetime.datetime.now() printelements( 300) diff = datetime.datetime.now() - start print("time taken", diff.microseconds) import numpy as n import datetime def printelements(num): arr1=n.arange(num) arr2=n.arange(num) arr3=arr1+arr2 for i in range(0, len(arr1)): print(arr3[i], end=" ") start = datetime.datetime.now() printelements( 300) diff = datetime.datetime.now() - start print("time taken", diff.microseconds) Adding two arrays With numpy
  • 7. Datatypes in Numpy Python has an integer type, a float type, and a complex type, however, this is not enough for scientific computing and, for this reason, NumPy has a lot more data types.Datatypes are associated with different precision and different memory size. We can view the datatype of array created import numpy as n arr1=n.arange( 12 ) print(arr1.dtype) The data type of array a is int32 (at least on my machine), but you may get int64 as output if you are using 64-bit Python.
  • 9. Examples of creating arrays with dtype arr1=numpy.arange(7,dtype="float16") print(arr1) arr1=numpy.arange(7,dtype="int16") print(arr1) arr1=numpy.arange(7,dtype=“complex") print(arr1) Output: [0. 1. 2. 3. 4. 5. 6.] [0 1 2 3 4 5 6] [0.+0.j 1.+0.j 2.+0.j 3.+0.j 4.+0.j 5.+0.j 6.+0.j]
  • 10. Character codes associated with datatypes Character codes are included for backward compatibility with Numeric. Numeric is the predecessor of NumPy. Their use is not recommended, but the codes are provided here because they pop up in several places. You should instead use dtype objects. Example: Output :
  • 11. Datatype(dtype) Objects & Constructors Data type objects are instances of the numpy.dtype class. Once again, arrays have a data type. Every element in a NumPy array has the same data type. Each dtype object has itemsize attribute which tell you the size of the data in bytes. dtype constructors We have a variety of ways to create data types. We can use either numeric datatypes or character codes to be passed to constructor to create object as shown below: D1=dtype(“float1 6) D2=dtype(“f”) Create the record: To create heterogenous datatype which contain multiple datatype D3 = dtype([('name', str, 40), ('numitems', int), ('price',float)])
  • 12. Dtype constructor examples ▪ We can use numeric datatypes: dtype(float32) ▪ We can specify a single precision float with a character code: dtype('f') ▪ We can use a double precision float character code: dtype('d') ▪ We can use a complex character code: dtype(‘D') ▪ We can give the data type constructor a two- character code. The first character signifies the type; the second character is a number specifying the number of bytes in the type: dtype('f8') Example : Outpu t:
  • 13. Dtype Attributes Attribute Description dtype.char Gives a unique character code for each of the 21 different built-in types. E.g ‘D’ dtype.type corresponds to the type of object of the array elements e.g float32 dtype.str Gives string representation of the data type. It starts with a character representing endianness(<or>), then a character code, followed by a number corresponding to the number of bytes that each array item requires. Eg. ‘<f8’ <- Endianness, f-float,8-8 bytes dtype.byteorder (Endiannness) Endianness, here, means the way bytes are ordered within a 32 or 64-bit word. In big-endian order, the most significant byte is stored first. In little-endian order, the least significant byte is stored first. dtype.itemsize Gives size of data in bytes example 64
  • 14. ENDIANNESS 0x00040000 0x00040001 0x00040002 0x00040003 0x00040000 0x00040001 0x00040002 0x00040003 Consider number 0x12674592 in 32-bit representation can be stored as − MSB LSB
  • 16. Creating Multidimensional Array Use array() function to create 2-D array from numpy import * matrix1= array([[0,2],[3,4]]) Or row1=array([1,2]) row2=array([3,4],’I’) matrix1 = array([row1,row2]) print(matrix1) □[0,2] [3,4]
  • 17. Creating Multidimensional Array Program to create a matrix and display it from numpy import * matrix1 = array([[0,2],[3,4]]) for i in range(0,len(matrix1[0])): for jin range(0, len(matrix1)): print(matrix1[i][j],end=" ") print("n") #len(matrix1[0]) give number of columns in first row #len(matrix1) gives number of rows Outpu t To add two matrices simply create two matrices with data and third zero matrix matrix1 = array([[0,2],[3,4]]) matrix2 = array([[0,2],[3,4]]) matrix3 = array([[0,0],[0,0]]) Then Simply add in loop matrix3[i][j]= matrix1[i][j]+matrix2[i][j]=