SlideShare a Scribd company logo
1 | P a g e
############################Python Numpy Source Codes######################
Note: Loops are slower than numpy arrays!
###############
import numpy as np
l=[1,2]
nplist = np.array(l)
print(nplist)
RESULT:
[1 2]
##################
import numpy as np
L=[1,2,3]
NA = np.array(L)
for i in L:
print(i)
RESULT:
1
2
3
####################
import numpy as np
L=[1,2,3]
NA = np.array(L)
for i in NA:
print(i)
RESULT:
1
2
3
########################
import numpy as np
L=[1,2,3]
L1=L+[4] #append
print(L1)
RESULT:
[1, 2, 3, 4]
2 | P a g e
#########################
import numpy as np
import numpy as np
L=[1,2,3,4]
L.append(5)
print(L)
RESULT:
[1, 2, 3, 4, 5]
#########################
import numpy as np
L=[1,2,3]
NA = np.array(L)
print(NA) #NA=[1 2 3]
NA1 = NA + [4] #vector addition, 4 is added to each element
print(NA1) #NA1=[5 6 7]
##########################
import numpy as np
L=[1,2,3]
NA = np.array(L)
print(NA) #NA=[1 2 3]
NA.append(8)
RESULT:
AttributeError: 'numpy.ndarray' object has no attribute 'append'
#############################
import numpy as np
L=[1,2,3]
NA = np.array(L)#[1 2 3]
NA2 = NA + NA
print(NA2) #Vector addition:[1 2 3]+[1 2 3]=[2 4 6]
L2 = L + L
print(L2) #List addition is simply concatenation [1, 2, 3, 1, 2, 3]
##########vector addition in list#########
L= [1,2,3]
L3 = []
for i in L:
L3.append(i+i)
print(L3) #[2,4,6]
3 | P a g e
#########vector multipication######
import numpy as np
L=[1,2,3]
NA = np.array(L)#[1 2 3]
print(2*NA) # vector multiplication [2 4 6]
print(2*L) #list multiplication :[1, 2, 3, 1, 2, 3]
#############Square operation########
import numpy as np
L=[1,2,3]
NA = np.array(L)#[1 2 3]
print(NA**2) # Square operation: [1 4 9]
print(L**2) #TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
############List Square operation#######
L= [1,2,3]
L3 = []
for i in L:
L3=i*i
print(L3) #[1,4,9]
############Square root operation######
import numpy as np
L=[1,2,3]
NA = np.array(L)#[1 2 3]
print(np.sqrt(NA)) # Square operation: [1. 1.41421356 1.73205081]
############log operation############
import numpy as np
L=[1,2,3]
NA = np.array(L)#[1 2 3]
print(np.log(NA))# log operation:[0. 0.69314718 1.09861229]
###########exponential operation######
import numpy as np
L=[1,2,3]
NA = np.array(L)#[1 2 3]
print(np.exp(NA))# exponential operation:[ 2.71828183 7.3890561 20.08553692]
#############################
import numpy as np
a=np.array([1,2,3])
b=np.array([[1,2], [3,4], [5,6]])
print(a[0]) #a[0]=1
print(b[0]) #b[0]=[1 2]
4 | P a g e
print(b[0][0])# b[0][0]= 1
print(b[0][1])# b[0][1]= 2
M=np.matrix([[1,2], [3,4], [5,6]])
print(M) #Matrix form
print(M[0][0]) #M[0][0]=[[1 2]]
print(M[0,0]) #M[0,0]=1
############################
import numpy as np
a=np.array([1,2,3])
b=np.array([[1,2], [3,4], [5,6]])
print(b)
RESULT:
[[1 2]
[3 4]
[5 6]]
#####################Transpose operation##############
import numpy as np
a=np.array([1,2,3])
b=np.array([[1,2], [3,4], [5,6]])
print(b.T) # Transpose operation
RESULT:
[[1 3 5]
[2 4 6]]
#############No. of rows and cols ##############
import numpy as np
b=np.array([[1,2], [3,4], [5,6]])
print(b.shape) # rows x cols= (3, 2)
c=b.T
print(c.shape) # rows x cols=(2, 3)
###############To check dimension of array#############
import numpy as np
a=np.array([1,2,3])
print(a.ndim) #a is 1 dimensional array
b=np.array([[1,2], [3,4], [5,6]])
print(b.ndim) #b is 2 dimensional array
c=b.T
print(c.ndim) #c is 2 dimensional array
5 | P a g e
##############To check total no. of elements in an array###########
import numpy as np
a=np.array([1,2,3])
print(a.size) #a has 3 elements
b=np.array([[1,2], [3,4], [5,6]])
print(b.size) #b has 6 elements
c=b.T
print(c.size) #c has 6 elements
##############To check data type of an array###########
import numpy as np
a=np.array([1,2,3])
print(a.dtype) #int32
b=np.array([[1,2], [3,4], [5,6]])
print(b.dtype) #int32
c=b.T
print(c.dtype) #int32
##########data type conversion########
import numpy as np
a=np.array([1,2,3])
b=np.array([1,2,3], dtype=np.float32)
print(b) #[1. 2. 3.]
##########Check each elemenet size#########
import numpy as np
a=np.array([1,2,3])
print(a.itemsize) #4 bytes
b=np.array([1,2,3], dtype=np.float64)
print(b.itemsize) #8 bytes
##########Check minimum and maximum elemenet #######
import numpy as np
a=np.array([1,2,3])
print(a.min()) #minimum element is 1
print(a.max()) #maximum element is 3
##########Check sum of elemenets#######
import numpy as np
a=np.array([1,2,3])
print(a.sum()) #sum of all elements is 6
6 | P a g e
##########axis sum##########
import numpy as np
b=np.array([[1,2], [3,4], [5,6]])
print(b.sum(axis=0)) #1+3+5=9, 2+4+6=12,
print(b.sum(axis=1)) #1+2=3, 3+4=7, 5+6=11
###########
import numpy as np
a=np.zeros((2,3)) #2 rows and 3 cols
print(a)
RESULT:
[[0. 0. 0.]
[0. 0. 0.]]
###############
import numpy as np
b=np.ones((3,2))
print(b)
RESULT:
[[1. 1.]
[1. 1.]
[1. 1.]]
###########
import numpy as np
b=np.ones((3,2), dtype=np.int16)
print(b)
RESULT:
[[1 1]
[1 1]
[1 1]]
##########Crete random data###########
import numpy as np
print(np.empty((3,3)))
RESULT:
[[0.00000000e+000 0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 1.91697471e-321]
[1.93101617e-312 1.93101617e-312 0.00000000e+000]]
7 | P a g e
#######################
import numpy as np
print(np.empty([3,3]))
RESULT:
[[6.23042070e-307 3.56043053e-307 1.60219306e-306]
[7.56571288e-307 1.89146896e-307 1.37961302e-306]
[1.05699242e-307 8.01097889e-307 0.00000000e+000]]
########################
import numpy as np
print(np.empty([3,3], dtype=np.int16))
RESULT:
After first execution:
[[4 0 0]
[0 4 0]
[0 0 0]]
After second execution:
[[0 0 0]
[0 0 0]
[0 0 0]]
#################
import numpy as np
print(np.arange(0,5)) #[0 1 2 3 4]
print(np.arange(0,5, 0.5)) #[0. 0.5 1. 1.5 2. 2.5 3. 3.5 4. 4.5]
##################
import numpy as np
print(np.linspace(0,5)) #Linearly space the value in range 0 to 5
#By default it takes 50 values
RESULT:
[0. 0.10204082 0.20408163 0.30612245 0.40816327 0.51020408
0.6122449 0.71428571 0.81632653 0.91836735 1.02040816 1.12244898
1.2244898 1.32653061 1.42857143 1.53061224 1.63265306 1.73469388
1.83673469 1.93877551 2.04081633 2.14285714 2.24489796 2.34693878
2.44897959 2.55102041 2.65306122 2.75510204 2.85714286 2.95918367
3.06122449 3.16326531 3.26530612 3.36734694 3.46938776 3.57142857
3.67346939 3.7755102 3.87755102 3.97959184 4.08163265 4.18367347
4.28571429 4.3877551 4.48979592 4.59183673 4.69387755 4.79591837
4.89795918 5.]
8 | P a g e
##############
import numpy as np
print(np.linspace(1,5,10))
RESULT:
[1. 1.44444444 1.88888889 2.33333333 2.77777778 3.22222222
3.66666667 4.11111111 4.55555556 5. ]
############Create random numbers#############
import numpy as np
print(np.random.random((2,3))) #dimension is 2 rows X 3 cols
RESULT:
[[0.13945931 0.16273058 0.56452845]
[0.61644482 0.18447141 0.17318377]]
############Reshaping array dimension#############
import numpy as np
c=np.zeros((2,3))
print(c)
print(c.reshape(6,1))
print(c.reshape(3,2))
############Reshaping array dimension#############
import numpy as np
c=np.zeros((2,5))
print(c)
print(c.reshape(5,-1)) # here 5 rows are created while cols created automatically by using '-1'
#############Stack vertically####################
import numpy as np
c=np.zeros((2,5))
d=np.ones((1,5))
e=np.vstack((c,d))
print(e)
RESULT;
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[1. 1. 1. 1. 1.]]
#############Stack vertically####################
9 | P a g e
import numpy as np
c=np.zeros((2,5))
d=np.ones((1,5))
e=np.vstack((d,c))
print(e)
RESULT:
[[1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
#############Stack horizontally####################
import numpy as np
c=np.zeros((1,5))
d=np.ones((1,5))
e=np.hstack((d,c))
print(e)
RESULT:
[[1. 1. 1. 1. 1. 0. 0. 0. 0. 0.]]
#########Vertical array Split###########
import numpy as np
b=np.array([[1,2], [3,4], [5,6]])
print(b)
e=np.vsplit(b , 3) #vertical split in 3 parts
print(e)
RESULT:
[[1 2]
[3 4]
[5 6]]
[array([[1, 2]]), array([[3, 4]]), array([[5, 6]])]
#########Horizontal array Split###########
import numpy as np
b=np.array([[1,2], [3,4], [5,6]])
print(b)
e=np.hsplit(b , 2)
print(e)
10 | P a g e
RESULT:
[[1 2]
[3 4]
[5 6]]
[array([[1],
[3],
[5]]), array([[2],
[4],
[6]])]

More Related Content

What's hot (20)

Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
Maher Alshammari
 
Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals review
ElifTech
 
Sets and disjoint sets union123
Sets and disjoint sets union123Sets and disjoint sets union123
Sets and disjoint sets union123
Ankita Goyal
 
Master method theorem
Master method theoremMaster method theorem
Master method theorem
Rajendran
 
Pumping lemma Theory Of Automata
Pumping lemma Theory Of AutomataPumping lemma Theory Of Automata
Pumping lemma Theory Of Automata
hafizhamza0322
 
Dempster shafer theory
Dempster shafer theoryDempster shafer theory
Dempster shafer theory
Dr. C.V. Suresh Babu
 
Pda
PdaPda
Pda
rsreddyphd
 
Longest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisLongest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm Analysis
Rajendran
 
Amortized Analysis
Amortized Analysis Amortized Analysis
Amortized Analysis
sathish sak
 
Simplification of cfg ppt
Simplification of cfg pptSimplification of cfg ppt
Simplification of cfg ppt
Shiela Rani
 
NI221 - Fundamentals of Computer
NI221 - Fundamentals of ComputerNI221 - Fundamentals of Computer
NI221 - Fundamentals of Computer
paulcaspe
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysis
Dr. Rajdeep Chatterjee
 
Lecture: Automata
Lecture: AutomataLecture: Automata
Lecture: Automata
Marina Santini
 
Time and Space Complexity
Time and Space ComplexityTime and Space Complexity
Time and Space Complexity
Ashutosh Satapathy
 
NFA & DFA
NFA & DFANFA & DFA
NFA & DFA
Akhil Kaushik
 
Binary Search
Binary SearchBinary Search
Binary Search
kunj desai
 
Merge sort
Merge sortMerge sort
Merge sort
lakshitha perera
 
Minimizing DFA
Minimizing DFAMinimizing DFA
Minimizing DFA
Animesh Chaturvedi
 
History of c++
History of c++History of c++
History of c++
Ihsan Ali
 
Java I/O
Java I/OJava I/O
Java I/O
Jussi Pohjolainen
 
Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals review
ElifTech
 
Sets and disjoint sets union123
Sets and disjoint sets union123Sets and disjoint sets union123
Sets and disjoint sets union123
Ankita Goyal
 
Master method theorem
Master method theoremMaster method theorem
Master method theorem
Rajendran
 
Pumping lemma Theory Of Automata
Pumping lemma Theory Of AutomataPumping lemma Theory Of Automata
Pumping lemma Theory Of Automata
hafizhamza0322
 
Longest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisLongest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm Analysis
Rajendran
 
Amortized Analysis
Amortized Analysis Amortized Analysis
Amortized Analysis
sathish sak
 
Simplification of cfg ppt
Simplification of cfg pptSimplification of cfg ppt
Simplification of cfg ppt
Shiela Rani
 
NI221 - Fundamentals of Computer
NI221 - Fundamentals of ComputerNI221 - Fundamentals of Computer
NI221 - Fundamentals of Computer
paulcaspe
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysis
Dr. Rajdeep Chatterjee
 
History of c++
History of c++History of c++
History of c++
Ihsan Ali
 

Similar to Python Numpy Source Codes (20)

NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
tahirnaquash2
 
Concept of Data science and Numpy concept
Concept of Data science and Numpy conceptConcept of Data science and Numpy concept
Concept of Data science and Numpy concept
Deena38
 
Data Preprocessing Introduction for Machine Learning
Data Preprocessing Introduction for Machine LearningData Preprocessing Introduction for Machine Learning
Data Preprocessing Introduction for Machine Learning
sonali sonavane
 
Numpy - Array.pdf
Numpy - Array.pdfNumpy - Array.pdf
Numpy - Array.pdf
AnkitaArjunDevkate
 
Demystifying Software Interviews
Demystifying Software InterviewsDemystifying Software Interviews
Demystifying Software Interviews
Michael Viveros
 
Numpy questions with answers and practice
Numpy questions with answers and practiceNumpy questions with answers and practice
Numpy questions with answers and practice
basicinfohub67
 
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
Smrati Kumar Katiyar
 
CS3401- Algorithmto use for data structure.docx
CS3401- Algorithmto use for data structure.docxCS3401- Algorithmto use for data structure.docx
CS3401- Algorithmto use for data structure.docx
ywar08112
 
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
Coding Assignment Help
 
03 standard Data Types
03 standard Data Types 03 standard Data Types
03 standard Data Types
Ebad Qureshi
 
PYTHON.pptx
PYTHON.pptxPYTHON.pptx
PYTHON.pptx
rohithprakash16
 
Python collections
Python collectionsPython collections
Python collections
Manusha Dilan
 
第二讲 预备-Python基礎
第二讲 预备-Python基礎第二讲 预备-Python基礎
第二讲 预备-Python基礎
anzhong70
 
第二讲 Python基礎
第二讲 Python基礎第二讲 Python基礎
第二讲 Python基礎
juzihua1102
 
ECE-PYTHON.docx
ECE-PYTHON.docxECE-PYTHON.docx
ECE-PYTHON.docx
Chaithanya89350
 
NUMPY
NUMPY NUMPY
NUMPY
Global Academy of Technology
 
good_2023_0208.pptx
good_2023_0208.pptxgood_2023_0208.pptx
good_2023_0208.pptx
GavinFETHsieh
 
Course notes on Astronomical data analysis by python pdf
Course notes on Astronomical data analysis by python pdfCourse notes on Astronomical data analysis by python pdf
Course notes on Astronomical data analysis by python pdf
ZainRahim3
 
Python for High School Programmers
Python for High School ProgrammersPython for High School Programmers
Python for High School Programmers
Siva Arunachalam
 
NUMPY [Autosaved] .pptx
NUMPY [Autosaved]                    .pptxNUMPY [Autosaved]                    .pptx
NUMPY [Autosaved] .pptx
coolmanbalu123
 
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
tahirnaquash2
 
Concept of Data science and Numpy concept
Concept of Data science and Numpy conceptConcept of Data science and Numpy concept
Concept of Data science and Numpy concept
Deena38
 
Data Preprocessing Introduction for Machine Learning
Data Preprocessing Introduction for Machine LearningData Preprocessing Introduction for Machine Learning
Data Preprocessing Introduction for Machine Learning
sonali sonavane
 
Demystifying Software Interviews
Demystifying Software InterviewsDemystifying Software Interviews
Demystifying Software Interviews
Michael Viveros
 
Numpy questions with answers and practice
Numpy questions with answers and practiceNumpy questions with answers and practice
Numpy questions with answers and practice
basicinfohub67
 
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
Smrati Kumar Katiyar
 
CS3401- Algorithmto use for data structure.docx
CS3401- Algorithmto use for data structure.docxCS3401- Algorithmto use for data structure.docx
CS3401- Algorithmto use for data structure.docx
ywar08112
 
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
Coding Assignment Help
 
03 standard Data Types
03 standard Data Types 03 standard Data Types
03 standard Data Types
Ebad Qureshi
 
第二讲 预备-Python基礎
第二讲 预备-Python基礎第二讲 预备-Python基礎
第二讲 预备-Python基礎
anzhong70
 
第二讲 Python基礎
第二讲 Python基礎第二讲 Python基礎
第二讲 Python基礎
juzihua1102
 
Course notes on Astronomical data analysis by python pdf
Course notes on Astronomical data analysis by python pdfCourse notes on Astronomical data analysis by python pdf
Course notes on Astronomical data analysis by python pdf
ZainRahim3
 
Python for High School Programmers
Python for High School ProgrammersPython for High School Programmers
Python for High School Programmers
Siva Arunachalam
 
NUMPY [Autosaved] .pptx
NUMPY [Autosaved]                    .pptxNUMPY [Autosaved]                    .pptx
NUMPY [Autosaved] .pptx
coolmanbalu123
 
Ad

More from Amarjeetsingh Thakur (20)

“Introduction to MATLAB & SIMULINK”
“Introduction to MATLAB  & SIMULINK”“Introduction to MATLAB  & SIMULINK”
“Introduction to MATLAB & SIMULINK”
Amarjeetsingh Thakur
 
Python code for servo control using Raspberry Pi
Python code for servo control using Raspberry PiPython code for servo control using Raspberry Pi
Python code for servo control using Raspberry Pi
Amarjeetsingh Thakur
 
Python code for Push button using Raspberry Pi
Python code for Push button using Raspberry PiPython code for Push button using Raspberry Pi
Python code for Push button using Raspberry Pi
Amarjeetsingh Thakur
 
Python code for Buzzer Control using Raspberry Pi
Python code for Buzzer Control using Raspberry PiPython code for Buzzer Control using Raspberry Pi
Python code for Buzzer Control using Raspberry Pi
Amarjeetsingh Thakur
 
Arduino programming part 2
Arduino programming part 2Arduino programming part 2
Arduino programming part 2
Amarjeetsingh Thakur
 
Arduino programming part1
Arduino programming part1Arduino programming part1
Arduino programming part1
Amarjeetsingh Thakur
 
Python openCV codes
Python openCV codesPython openCV codes
Python openCV codes
Amarjeetsingh Thakur
 
Steemit html blog
Steemit html blogSteemit html blog
Steemit html blog
Amarjeetsingh Thakur
 
Python OpenCV Real Time projects
Python OpenCV Real Time projectsPython OpenCV Real Time projects
Python OpenCV Real Time projects
Amarjeetsingh Thakur
 
Adafruit_IoT_Platform
Adafruit_IoT_PlatformAdafruit_IoT_Platform
Adafruit_IoT_Platform
Amarjeetsingh Thakur
 
Core python programming tutorial
Core python programming tutorialCore python programming tutorial
Core python programming tutorial
Amarjeetsingh Thakur
 
Python openpyxl
Python openpyxlPython openpyxl
Python openpyxl
Amarjeetsingh Thakur
 
Introduction to Internet of Things (IoT)
Introduction to Internet of Things (IoT)Introduction to Internet of Things (IoT)
Introduction to Internet of Things (IoT)
Amarjeetsingh Thakur
 
Introduction to Node MCU
Introduction to Node MCUIntroduction to Node MCU
Introduction to Node MCU
Amarjeetsingh Thakur
 
Introduction to Things board (An Open Source IoT Cloud Platform)
Introduction to Things board (An Open Source IoT Cloud Platform)Introduction to Things board (An Open Source IoT Cloud Platform)
Introduction to Things board (An Open Source IoT Cloud Platform)
Amarjeetsingh Thakur
 
Introduction to MQ Telemetry Transport (MQTT)
Introduction to MQ Telemetry Transport (MQTT)Introduction to MQ Telemetry Transport (MQTT)
Introduction to MQ Telemetry Transport (MQTT)
Amarjeetsingh Thakur
 
Arduino Interfacing with different sensors and motor
Arduino Interfacing with different sensors and motorArduino Interfacing with different sensors and motor
Arduino Interfacing with different sensors and motor
Amarjeetsingh Thakur
 
Image processing in MATLAB
Image processing in MATLABImage processing in MATLAB
Image processing in MATLAB
Amarjeetsingh Thakur
 
Introduction to Arduino
Introduction to ArduinoIntroduction to Arduino
Introduction to Arduino
Amarjeetsingh Thakur
 
Introduction to Arduino
Introduction to ArduinoIntroduction to Arduino
Introduction to Arduino
Amarjeetsingh Thakur
 
“Introduction to MATLAB & SIMULINK”
“Introduction to MATLAB  & SIMULINK”“Introduction to MATLAB  & SIMULINK”
“Introduction to MATLAB & SIMULINK”
Amarjeetsingh Thakur
 
Python code for servo control using Raspberry Pi
Python code for servo control using Raspberry PiPython code for servo control using Raspberry Pi
Python code for servo control using Raspberry Pi
Amarjeetsingh Thakur
 
Python code for Push button using Raspberry Pi
Python code for Push button using Raspberry PiPython code for Push button using Raspberry Pi
Python code for Push button using Raspberry Pi
Amarjeetsingh Thakur
 
Python code for Buzzer Control using Raspberry Pi
Python code for Buzzer Control using Raspberry PiPython code for Buzzer Control using Raspberry Pi
Python code for Buzzer Control using Raspberry Pi
Amarjeetsingh Thakur
 
Introduction to Internet of Things (IoT)
Introduction to Internet of Things (IoT)Introduction to Internet of Things (IoT)
Introduction to Internet of Things (IoT)
Amarjeetsingh Thakur
 
Introduction to Things board (An Open Source IoT Cloud Platform)
Introduction to Things board (An Open Source IoT Cloud Platform)Introduction to Things board (An Open Source IoT Cloud Platform)
Introduction to Things board (An Open Source IoT Cloud Platform)
Amarjeetsingh Thakur
 
Introduction to MQ Telemetry Transport (MQTT)
Introduction to MQ Telemetry Transport (MQTT)Introduction to MQ Telemetry Transport (MQTT)
Introduction to MQ Telemetry Transport (MQTT)
Amarjeetsingh Thakur
 
Arduino Interfacing with different sensors and motor
Arduino Interfacing with different sensors and motorArduino Interfacing with different sensors and motor
Arduino Interfacing with different sensors and motor
Amarjeetsingh Thakur
 
Ad

Recently uploaded (20)

9aeb2aae-3b85-47a5-9776-154883bbae57.pdf
9aeb2aae-3b85-47a5-9776-154883bbae57.pdf9aeb2aae-3b85-47a5-9776-154883bbae57.pdf
9aeb2aae-3b85-47a5-9776-154883bbae57.pdf
RishabhGupta578788
 
Android basics – Key Codes – ADB – Rooting Android – Boot Process – File Syst...
Android basics – Key Codes – ADB – Rooting Android – Boot Process – File Syst...Android basics – Key Codes – ADB – Rooting Android – Boot Process – File Syst...
Android basics – Key Codes – ADB – Rooting Android – Boot Process – File Syst...
ManiMaran230751
 
Software_Engineering_in_6_Hours_lyst1728638742594.pdf
Software_Engineering_in_6_Hours_lyst1728638742594.pdfSoftware_Engineering_in_6_Hours_lyst1728638742594.pdf
Software_Engineering_in_6_Hours_lyst1728638742594.pdf
VanshMunjal7
 
"The Enigmas of the Riemann Hypothesis" by Julio Chai
"The Enigmas of the Riemann Hypothesis" by Julio Chai"The Enigmas of the Riemann Hypothesis" by Julio Chai
"The Enigmas of the Riemann Hypothesis" by Julio Chai
Julio Chai
 
ISO 4548-9 Oil Filter Anti Drain Catalogue.pdf
ISO 4548-9 Oil Filter Anti Drain Catalogue.pdfISO 4548-9 Oil Filter Anti Drain Catalogue.pdf
ISO 4548-9 Oil Filter Anti Drain Catalogue.pdf
FILTRATION ENGINEERING & CUNSULTANT
 
ISO 4548-7 Filter Vibration Fatigue Test Rig Catalogue.pdf
ISO 4548-7 Filter Vibration Fatigue Test Rig Catalogue.pdfISO 4548-7 Filter Vibration Fatigue Test Rig Catalogue.pdf
ISO 4548-7 Filter Vibration Fatigue Test Rig Catalogue.pdf
FILTRATION ENGINEERING & CUNSULTANT
 
ENERGY STORING DEVICES-Primary Battery.pdf
ENERGY STORING DEVICES-Primary Battery.pdfENERGY STORING DEVICES-Primary Battery.pdf
ENERGY STORING DEVICES-Primary Battery.pdf
TAMILISAI R
 
하이플럭스 락피팅 카달로그 2025 (Lok Fitting Catalog 2025)
하이플럭스 락피팅 카달로그 2025 (Lok Fitting Catalog 2025)하이플럭스 락피팅 카달로그 2025 (Lok Fitting Catalog 2025)
하이플럭스 락피팅 카달로그 2025 (Lok Fitting Catalog 2025)
하이플럭스 / HIFLUX Co., Ltd.
 
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
sebastianku31
 
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
gerogepatton
 
Tesia Dobrydnia - A Leader In Her Industry
Tesia Dobrydnia - A Leader In Her IndustryTesia Dobrydnia - A Leader In Her Industry
Tesia Dobrydnia - A Leader In Her Industry
Tesia Dobrydnia
 
ISO 4020-6.1- Filter Cleanliness Test Rig Catalogue.pdf
ISO 4020-6.1- Filter Cleanliness Test Rig Catalogue.pdfISO 4020-6.1- Filter Cleanliness Test Rig Catalogue.pdf
ISO 4020-6.1- Filter Cleanliness Test Rig Catalogue.pdf
FILTRATION ENGINEERING & CUNSULTANT
 
Software Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha TasnuvaSoftware Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha Tasnuva
tanishatasnuva76
 
Air Filter Flat Sheet Media-Catalouge-Final.pdf
Air Filter Flat Sheet Media-Catalouge-Final.pdfAir Filter Flat Sheet Media-Catalouge-Final.pdf
Air Filter Flat Sheet Media-Catalouge-Final.pdf
FILTRATION ENGINEERING & CUNSULTANT
 
Software Developer Portfolio: Backend Architecture & Performance Optimization
Software Developer Portfolio: Backend Architecture & Performance OptimizationSoftware Developer Portfolio: Backend Architecture & Performance Optimization
Software Developer Portfolio: Backend Architecture & Performance Optimization
kiwoong (daniel) kim
 
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning ModelEnhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
IRJET Journal
 
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra OpticaPruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
OmarAlfredoDelCastil
 
Highway Engineering - Pavement materials
Highway Engineering - Pavement materialsHighway Engineering - Pavement materials
Highway Engineering - Pavement materials
AmrutaBhosale9
 
Influence line diagram for truss in a robust
Influence line diagram for truss in a robustInfluence line diagram for truss in a robust
Influence line diagram for truss in a robust
ParthaSengupta26
 
9aeb2aae-3b85-47a5-9776-154883bbae57.pdf
9aeb2aae-3b85-47a5-9776-154883bbae57.pdf9aeb2aae-3b85-47a5-9776-154883bbae57.pdf
9aeb2aae-3b85-47a5-9776-154883bbae57.pdf
RishabhGupta578788
 
Android basics – Key Codes – ADB – Rooting Android – Boot Process – File Syst...
Android basics – Key Codes – ADB – Rooting Android – Boot Process – File Syst...Android basics – Key Codes – ADB – Rooting Android – Boot Process – File Syst...
Android basics – Key Codes – ADB – Rooting Android – Boot Process – File Syst...
ManiMaran230751
 
Software_Engineering_in_6_Hours_lyst1728638742594.pdf
Software_Engineering_in_6_Hours_lyst1728638742594.pdfSoftware_Engineering_in_6_Hours_lyst1728638742594.pdf
Software_Engineering_in_6_Hours_lyst1728638742594.pdf
VanshMunjal7
 
"The Enigmas of the Riemann Hypothesis" by Julio Chai
"The Enigmas of the Riemann Hypothesis" by Julio Chai"The Enigmas of the Riemann Hypothesis" by Julio Chai
"The Enigmas of the Riemann Hypothesis" by Julio Chai
Julio Chai
 
ENERGY STORING DEVICES-Primary Battery.pdf
ENERGY STORING DEVICES-Primary Battery.pdfENERGY STORING DEVICES-Primary Battery.pdf
ENERGY STORING DEVICES-Primary Battery.pdf
TAMILISAI R
 
하이플럭스 락피팅 카달로그 2025 (Lok Fitting Catalog 2025)
하이플럭스 락피팅 카달로그 2025 (Lok Fitting Catalog 2025)하이플럭스 락피팅 카달로그 2025 (Lok Fitting Catalog 2025)
하이플럭스 락피팅 카달로그 2025 (Lok Fitting Catalog 2025)
하이플럭스 / HIFLUX Co., Ltd.
 
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
May 2025: Top 10 Cited Articles in Software Engineering & Applications Intern...
sebastianku31
 
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
May 2025 - Top 10 Read Articles in Artificial Intelligence and Applications (...
gerogepatton
 
Tesia Dobrydnia - A Leader In Her Industry
Tesia Dobrydnia - A Leader In Her IndustryTesia Dobrydnia - A Leader In Her Industry
Tesia Dobrydnia - A Leader In Her Industry
Tesia Dobrydnia
 
Software Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha TasnuvaSoftware Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha Tasnuva
tanishatasnuva76
 
Software Developer Portfolio: Backend Architecture & Performance Optimization
Software Developer Portfolio: Backend Architecture & Performance OptimizationSoftware Developer Portfolio: Backend Architecture & Performance Optimization
Software Developer Portfolio: Backend Architecture & Performance Optimization
kiwoong (daniel) kim
 
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning ModelEnhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
Enhanced heart disease prediction using SKNDGR ensemble Machine Learning Model
IRJET Journal
 
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra OpticaPruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
OmarAlfredoDelCastil
 
Highway Engineering - Pavement materials
Highway Engineering - Pavement materialsHighway Engineering - Pavement materials
Highway Engineering - Pavement materials
AmrutaBhosale9
 
Influence line diagram for truss in a robust
Influence line diagram for truss in a robustInfluence line diagram for truss in a robust
Influence line diagram for truss in a robust
ParthaSengupta26
 

Python Numpy Source Codes

  • 1. 1 | P a g e ############################Python Numpy Source Codes###################### Note: Loops are slower than numpy arrays! ############### import numpy as np l=[1,2] nplist = np.array(l) print(nplist) RESULT: [1 2] ################## import numpy as np L=[1,2,3] NA = np.array(L) for i in L: print(i) RESULT: 1 2 3 #################### import numpy as np L=[1,2,3] NA = np.array(L) for i in NA: print(i) RESULT: 1 2 3 ######################## import numpy as np L=[1,2,3] L1=L+[4] #append print(L1) RESULT: [1, 2, 3, 4]
  • 2. 2 | P a g e ######################### import numpy as np import numpy as np L=[1,2,3,4] L.append(5) print(L) RESULT: [1, 2, 3, 4, 5] ######################### import numpy as np L=[1,2,3] NA = np.array(L) print(NA) #NA=[1 2 3] NA1 = NA + [4] #vector addition, 4 is added to each element print(NA1) #NA1=[5 6 7] ########################## import numpy as np L=[1,2,3] NA = np.array(L) print(NA) #NA=[1 2 3] NA.append(8) RESULT: AttributeError: 'numpy.ndarray' object has no attribute 'append' ############################# import numpy as np L=[1,2,3] NA = np.array(L)#[1 2 3] NA2 = NA + NA print(NA2) #Vector addition:[1 2 3]+[1 2 3]=[2 4 6] L2 = L + L print(L2) #List addition is simply concatenation [1, 2, 3, 1, 2, 3] ##########vector addition in list######### L= [1,2,3] L3 = [] for i in L: L3.append(i+i) print(L3) #[2,4,6]
  • 3. 3 | P a g e #########vector multipication###### import numpy as np L=[1,2,3] NA = np.array(L)#[1 2 3] print(2*NA) # vector multiplication [2 4 6] print(2*L) #list multiplication :[1, 2, 3, 1, 2, 3] #############Square operation######## import numpy as np L=[1,2,3] NA = np.array(L)#[1 2 3] print(NA**2) # Square operation: [1 4 9] print(L**2) #TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int' ############List Square operation####### L= [1,2,3] L3 = [] for i in L: L3=i*i print(L3) #[1,4,9] ############Square root operation###### import numpy as np L=[1,2,3] NA = np.array(L)#[1 2 3] print(np.sqrt(NA)) # Square operation: [1. 1.41421356 1.73205081] ############log operation############ import numpy as np L=[1,2,3] NA = np.array(L)#[1 2 3] print(np.log(NA))# log operation:[0. 0.69314718 1.09861229] ###########exponential operation###### import numpy as np L=[1,2,3] NA = np.array(L)#[1 2 3] print(np.exp(NA))# exponential operation:[ 2.71828183 7.3890561 20.08553692] ############################# import numpy as np a=np.array([1,2,3]) b=np.array([[1,2], [3,4], [5,6]]) print(a[0]) #a[0]=1 print(b[0]) #b[0]=[1 2]
  • 4. 4 | P a g e print(b[0][0])# b[0][0]= 1 print(b[0][1])# b[0][1]= 2 M=np.matrix([[1,2], [3,4], [5,6]]) print(M) #Matrix form print(M[0][0]) #M[0][0]=[[1 2]] print(M[0,0]) #M[0,0]=1 ############################ import numpy as np a=np.array([1,2,3]) b=np.array([[1,2], [3,4], [5,6]]) print(b) RESULT: [[1 2] [3 4] [5 6]] #####################Transpose operation############## import numpy as np a=np.array([1,2,3]) b=np.array([[1,2], [3,4], [5,6]]) print(b.T) # Transpose operation RESULT: [[1 3 5] [2 4 6]] #############No. of rows and cols ############## import numpy as np b=np.array([[1,2], [3,4], [5,6]]) print(b.shape) # rows x cols= (3, 2) c=b.T print(c.shape) # rows x cols=(2, 3) ###############To check dimension of array############# import numpy as np a=np.array([1,2,3]) print(a.ndim) #a is 1 dimensional array b=np.array([[1,2], [3,4], [5,6]]) print(b.ndim) #b is 2 dimensional array c=b.T print(c.ndim) #c is 2 dimensional array
  • 5. 5 | P a g e ##############To check total no. of elements in an array########### import numpy as np a=np.array([1,2,3]) print(a.size) #a has 3 elements b=np.array([[1,2], [3,4], [5,6]]) print(b.size) #b has 6 elements c=b.T print(c.size) #c has 6 elements ##############To check data type of an array########### import numpy as np a=np.array([1,2,3]) print(a.dtype) #int32 b=np.array([[1,2], [3,4], [5,6]]) print(b.dtype) #int32 c=b.T print(c.dtype) #int32 ##########data type conversion######## import numpy as np a=np.array([1,2,3]) b=np.array([1,2,3], dtype=np.float32) print(b) #[1. 2. 3.] ##########Check each elemenet size######### import numpy as np a=np.array([1,2,3]) print(a.itemsize) #4 bytes b=np.array([1,2,3], dtype=np.float64) print(b.itemsize) #8 bytes ##########Check minimum and maximum elemenet ####### import numpy as np a=np.array([1,2,3]) print(a.min()) #minimum element is 1 print(a.max()) #maximum element is 3 ##########Check sum of elemenets####### import numpy as np a=np.array([1,2,3]) print(a.sum()) #sum of all elements is 6
  • 6. 6 | P a g e ##########axis sum########## import numpy as np b=np.array([[1,2], [3,4], [5,6]]) print(b.sum(axis=0)) #1+3+5=9, 2+4+6=12, print(b.sum(axis=1)) #1+2=3, 3+4=7, 5+6=11 ########### import numpy as np a=np.zeros((2,3)) #2 rows and 3 cols print(a) RESULT: [[0. 0. 0.] [0. 0. 0.]] ############### import numpy as np b=np.ones((3,2)) print(b) RESULT: [[1. 1.] [1. 1.] [1. 1.]] ########### import numpy as np b=np.ones((3,2), dtype=np.int16) print(b) RESULT: [[1 1] [1 1] [1 1]] ##########Crete random data########### import numpy as np print(np.empty((3,3))) RESULT: [[0.00000000e+000 0.00000000e+000 0.00000000e+000] [0.00000000e+000 0.00000000e+000 1.91697471e-321] [1.93101617e-312 1.93101617e-312 0.00000000e+000]]
  • 7. 7 | P a g e ####################### import numpy as np print(np.empty([3,3])) RESULT: [[6.23042070e-307 3.56043053e-307 1.60219306e-306] [7.56571288e-307 1.89146896e-307 1.37961302e-306] [1.05699242e-307 8.01097889e-307 0.00000000e+000]] ######################## import numpy as np print(np.empty([3,3], dtype=np.int16)) RESULT: After first execution: [[4 0 0] [0 4 0] [0 0 0]] After second execution: [[0 0 0] [0 0 0] [0 0 0]] ################# import numpy as np print(np.arange(0,5)) #[0 1 2 3 4] print(np.arange(0,5, 0.5)) #[0. 0.5 1. 1.5 2. 2.5 3. 3.5 4. 4.5] ################## import numpy as np print(np.linspace(0,5)) #Linearly space the value in range 0 to 5 #By default it takes 50 values RESULT: [0. 0.10204082 0.20408163 0.30612245 0.40816327 0.51020408 0.6122449 0.71428571 0.81632653 0.91836735 1.02040816 1.12244898 1.2244898 1.32653061 1.42857143 1.53061224 1.63265306 1.73469388 1.83673469 1.93877551 2.04081633 2.14285714 2.24489796 2.34693878 2.44897959 2.55102041 2.65306122 2.75510204 2.85714286 2.95918367 3.06122449 3.16326531 3.26530612 3.36734694 3.46938776 3.57142857 3.67346939 3.7755102 3.87755102 3.97959184 4.08163265 4.18367347 4.28571429 4.3877551 4.48979592 4.59183673 4.69387755 4.79591837 4.89795918 5.]
  • 8. 8 | P a g e ############## import numpy as np print(np.linspace(1,5,10)) RESULT: [1. 1.44444444 1.88888889 2.33333333 2.77777778 3.22222222 3.66666667 4.11111111 4.55555556 5. ] ############Create random numbers############# import numpy as np print(np.random.random((2,3))) #dimension is 2 rows X 3 cols RESULT: [[0.13945931 0.16273058 0.56452845] [0.61644482 0.18447141 0.17318377]] ############Reshaping array dimension############# import numpy as np c=np.zeros((2,3)) print(c) print(c.reshape(6,1)) print(c.reshape(3,2)) ############Reshaping array dimension############# import numpy as np c=np.zeros((2,5)) print(c) print(c.reshape(5,-1)) # here 5 rows are created while cols created automatically by using '-1' #############Stack vertically#################### import numpy as np c=np.zeros((2,5)) d=np.ones((1,5)) e=np.vstack((c,d)) print(e) RESULT; [[0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [1. 1. 1. 1. 1.]] #############Stack vertically####################
  • 9. 9 | P a g e import numpy as np c=np.zeros((2,5)) d=np.ones((1,5)) e=np.vstack((d,c)) print(e) RESULT: [[1. 1. 1. 1. 1.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.]] #############Stack horizontally#################### import numpy as np c=np.zeros((1,5)) d=np.ones((1,5)) e=np.hstack((d,c)) print(e) RESULT: [[1. 1. 1. 1. 1. 0. 0. 0. 0. 0.]] #########Vertical array Split########### import numpy as np b=np.array([[1,2], [3,4], [5,6]]) print(b) e=np.vsplit(b , 3) #vertical split in 3 parts print(e) RESULT: [[1 2] [3 4] [5 6]] [array([[1, 2]]), array([[3, 4]]), array([[5, 6]])] #########Horizontal array Split########### import numpy as np b=np.array([[1,2], [3,4], [5,6]]) print(b) e=np.hsplit(b , 2) print(e)
  • 10. 10 | P a g e RESULT: [[1 2] [3 4] [5 6]] [array([[1], [3], [5]]), array([[2], [4], [6]])]