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 (14)

DOCX
Isi makalah
nepriandari
 
PDF
Traditional and New Business Ideas
Ping Wu
 
PDF
木と電話と選挙(causalTree)
Shota Yasui
 
PPT
Circles
Erlyn Geronimo
 
PDF
Matematika Diskrit - 08 kombinatorial - 02
KuliahKita
 
PDF
Calculus Final Exam
Kuan-Lun Wang
 
PDF
1.1.1C Midpoint and Distance Formulas
smiller5
 
PPT
Regular Polygons
noeliacalle
 
PPTX
мономи,полиноми
Sneze Zlatkovska
 
PDF
Obj. 8 Classifying Angles and Pairs of Angles
smiller5
 
PDF
他社会計ソフトからの仕訳インポート(ミロク)
Money Forward, Inc.
 
PPTX
Trougao
LjiljanaMudrinic
 
PDF
Rationalizing the Denominator of a Radical Expression
REYBETH RACELIS
 
PPTX
ANOVA君とanovatan
Takashi Yamane
 
Isi makalah
nepriandari
 
Traditional and New Business Ideas
Ping Wu
 
木と電話と選挙(causalTree)
Shota Yasui
 
Matematika Diskrit - 08 kombinatorial - 02
KuliahKita
 
Calculus Final Exam
Kuan-Lun Wang
 
1.1.1C Midpoint and Distance Formulas
smiller5
 
Regular Polygons
noeliacalle
 
мономи,полиноми
Sneze Zlatkovska
 
Obj. 8 Classifying Angles and Pairs of Angles
smiller5
 
他社会計ソフトからの仕訳インポート(ミロク)
Money Forward, Inc.
 
Rationalizing the Denominator of a Radical Expression
REYBETH RACELIS
 
ANOVA君とanovatan
Takashi Yamane
 

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

PPTX
仿制LethbridgeOffer加拿大莱斯桥大学毕业证范本,Lethbridge成绩单
Taqyea
 
PDF
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PDF
aAn_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PPTX
Alan Turing - life and importance for all of us now
Pedro Concejero
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PDF
Digital water marking system project report
Kamal Acharya
 
PDF
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
PDF
NTPC PATRATU Summer internship report.pdf
hemant03701
 
PPTX
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPTX
darshai cross section and river section analysis
muk7971
 
PPTX
Knowledge Representation : Semantic Networks
Amity University, Patna
 
PDF
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
PPT
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
PPTX
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PPTX
Distribution reservoir and service storage pptx
dhanashree78
 
PDF
SERVERLESS PERSONAL TO-DO LIST APPLICATION
anushaashraf20
 
仿制LethbridgeOffer加拿大莱斯桥大学毕业证范本,Lethbridge成绩单
Taqyea
 
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
aAn_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
Alan Turing - life and importance for all of us now
Pedro Concejero
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Digital water marking system project report
Kamal Acharya
 
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
NTPC PATRATU Summer internship report.pdf
hemant03701
 
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
darshai cross section and river section analysis
muk7971
 
Knowledge Representation : Semantic Networks
Amity University, Patna
 
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
Distribution reservoir and service storage pptx
dhanashree78
 
SERVERLESS PERSONAL TO-DO LIST APPLICATION
anushaashraf20
 

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