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)

DOC
Time and space complexity
Ankit Katiyar
 
PDF
Natural language processing (nlp)
Kuppusamy P
 
PPT
Pattern matching
shravs_188
 
PDF
Data visualization in Python
Marc Garcia
 
PPTX
The role of the parser and Error recovery strategies ppt in compiler design
Sadia Akter
 
PDF
Data Wrangling with Pandas
Luis Carrasco
 
PPTX
Supervised and unsupervised learning
Paras Kohli
 
PPTX
AI: AI & Problem Solving
DataminingTools Inc
 
PPT
Data preprocessing ng
datapreprocessing
 
PDF
Word Embeddings - Introduction
Christian Perone
 
PPT
Object-oriented concepts
BG Java EE Course
 
PPTX
NumPy.pptx
EN1036VivekSingh
 
PPT
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
sumitbardhan
 
PPTX
Data Structures in Python
Devashish Kumar
 
PPT
Big Data and Natural Language Processing
Michel Bruley
 
PPTX
Web development with Python
Raman Balyan
 
PPTX
Asymptotic Notation
Protap Mondal
 
PPTX
Sentiment Analysis Using Product Review
Abdullah Moin
 
PDF
Natural Language Processing (NLP)
Yuriy Guts
 
Time and space complexity
Ankit Katiyar
 
Natural language processing (nlp)
Kuppusamy P
 
Pattern matching
shravs_188
 
Data visualization in Python
Marc Garcia
 
The role of the parser and Error recovery strategies ppt in compiler design
Sadia Akter
 
Data Wrangling with Pandas
Luis Carrasco
 
Supervised and unsupervised learning
Paras Kohli
 
AI: AI & Problem Solving
DataminingTools Inc
 
Data preprocessing ng
datapreprocessing
 
Word Embeddings - Introduction
Christian Perone
 
Object-oriented concepts
BG Java EE Course
 
NumPy.pptx
EN1036VivekSingh
 
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
sumitbardhan
 
Data Structures in Python
Devashish Kumar
 
Big Data and Natural Language Processing
Michel Bruley
 
Web development with Python
Raman Balyan
 
Asymptotic Notation
Protap Mondal
 
Sentiment Analysis Using Product Review
Abdullah Moin
 
Natural Language Processing (NLP)
Yuriy Guts
 

Similar to Python Numpy Source Codes (20)

PPTX
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
tahirnaquash2
 
PPTX
NUMPY LIBRARY study materials PPT 2.pptx
CHETHANKUMAR274045
 
PPTX
numpy code and examples with attributes.pptx
swathis752031
 
PDF
Concept of Data science and Numpy concept
Deena38
 
PPTX
Data Preprocessing Introduction for Machine Learning
sonali sonavane
 
PDF
CE344L-200365-Lab2.pdf
UmarMustafa13
 
PPT
CAP776Numpy (2).ppt
ChhaviCoachingCenter
 
PPT
CAP776Numpy.ppt
kdr52121
 
PPTX
python_programming_NumPy_Pandas_Notes.pptx
sunilsoni446112
 
PDF
ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...
DineshThallapelly
 
PDF
Numpy python cheat_sheet
Zahid Hasan
 
PDF
Numpy python cheat_sheet
Nishant Upadhyay
 
PDF
Python_cheatsheet_numpy.pdf
AnonymousUser67
 
PDF
Numpy cheat-sheet
Arief Kurniawan
 
PPTX
UNIT-03_Numpy (1) python yeksodbbsisbsjsjsh
tony8553004135
 
PDF
Python for Data Science and Scientific Computing
Abhijit Kar Gupta
 
PPTX
Lecture 2 _Foundions foundions NumPyI.pptx
disserdekabrcha
 
PDF
Numpy questions with answers and practice
basicinfohub67
 
PPTX
THE NUMPY LIBRARY of python with slides.pptx
fareedullah211398
 
PPTX
Numpy_defintion_description_usage_examples.pptx
VGaneshKarthikeyan
 
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
tahirnaquash2
 
NUMPY LIBRARY study materials PPT 2.pptx
CHETHANKUMAR274045
 
numpy code and examples with attributes.pptx
swathis752031
 
Concept of Data science and Numpy concept
Deena38
 
Data Preprocessing Introduction for Machine Learning
sonali sonavane
 
CE344L-200365-Lab2.pdf
UmarMustafa13
 
CAP776Numpy (2).ppt
ChhaviCoachingCenter
 
CAP776Numpy.ppt
kdr52121
 
python_programming_NumPy_Pandas_Notes.pptx
sunilsoni446112
 
ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...
DineshThallapelly
 
Numpy python cheat_sheet
Zahid Hasan
 
Numpy python cheat_sheet
Nishant Upadhyay
 
Python_cheatsheet_numpy.pdf
AnonymousUser67
 
Numpy cheat-sheet
Arief Kurniawan
 
UNIT-03_Numpy (1) python yeksodbbsisbsjsjsh
tony8553004135
 
Python for Data Science and Scientific Computing
Abhijit Kar Gupta
 
Lecture 2 _Foundions foundions NumPyI.pptx
disserdekabrcha
 
Numpy questions with answers and practice
basicinfohub67
 
THE NUMPY LIBRARY of python with slides.pptx
fareedullah211398
 
Numpy_defintion_description_usage_examples.pptx
VGaneshKarthikeyan
 
Ad

More from Amarjeetsingh Thakur (20)

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

Recently uploaded (20)

PDF
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
PPTX
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PPT
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PPTX
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PPTX
Thermal runway and thermal stability.pptx
godow93766
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PPTX
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PDF
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
Thermal runway and thermal stability.pptx
godow93766
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
MRRS Strength and Durability of Concrete
CivilMythili
 
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 

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