0% found this document useful (0 votes)
5 views17 pages

Python Numpy

The document is a lab manual for a Python NumPy course at the Universal College of Engineering and Technology, covering installation, basic operations, array manipulation, mathematical and statistical operations, and data types. It includes practical examples and outputs for various NumPy functions across six weeks of study. The manual is designed for II B.Tech - I semester students in the Computer Science and Engineering department.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views17 pages

Python Numpy

The document is a lab manual for a Python NumPy course at the Universal College of Engineering and Technology, covering installation, basic operations, array manipulation, mathematical and statistical operations, and data types. It includes practical examples and outputs for various NumPy functions across six weeks of study. The manual is designed for II B.Tech - I semester students in the Computer Science and Engineering department.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

UNIVERSAL COLLEGE OF ENGINEERING & TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

PYTHON NUMPY LAB


LAB MANUAL

II [Link] – Isemester

UNIVERSAL COLE-LEGE OF ENGINEERING AND TECHNOLOGY

(Approved by A.I.C.T.E, Affiliated to JNTUK, Kakinada)

PERECHERLA,GUNTUR-522438.
WEEK-I :

NumPy installation using different scientific python distributions (Anaconda, Python


(x, y), WinPython, Pyzo)

Installing NumPy:
Before NumPy's functions and methods can be used, NumPy must be installed. Depending on
which distribution of Python you use, the installation method is slightly different.
Install NumPy on Anaconda
If you installed the Anaconda distribution of Python, NumPy comes pre-installed and no further
installation steps are necessary.
If you use a version of Python from [Link] or a version of Python that came with your operating
system, the Anaconda Prompt and conda or pip can be used to install NumPy.
Install NumPy with the Anaconda Prompt
To install NumPy, open the Anaconda Prompt and type:
 conda install numpy
Type y for yes when prompted.

Install NumPy with pip

To install NumPy with pip, bring up a terminal window and type:

$ pip install numpy


This command installs NumPy in the current working Python environment.

Verify NumPy installation

To verify NumPy is installed, invoke NumPy's version using the Python REPL. Import NumPy and
call the .__version__ attribute common to most Python packages.
In [1]:
A version number like '1.16.4' indicates a successful NumPy installation.

WEEK-II :

NumPy Basics ([Link], [Link], [Link], [Link] , [Link],


[Link], [Link])

import numpy as np
a=[Link]([[1,2,3],[4,2,5]], dtype='float')
print("Array created using passed list:\n",a)
b=[Link]((1,3,2))
print("Array created using passed tuple:\n",b)
c=[Link]((3,4))
print("\n an array initialized with all zeroes:\n",c)
d=[Link]((3,3),6,dtype='complex')
print("\n an array initialized with all 6s,array type is complex:\n",d)
e=[Link]((2,2))
print("\n A random array:\n",e)
f=[Link](0,30,5)
print("\n A sequential array with steps of 5:\n",f)
g=[Link](0,5,10)
print("\n a sequential array with 10 values between""0 and 5:\n,g")
arr=[Link]([[1,2,3,4],[5,2,4,2],[1,2,0,1]])
newarr=[Link](2,2,3)
print("\n original array:\n",arr)
print("reshaped array:\n",newarr)
arr=[Link]([[1,2,3],[4,5,6]])
flarr=[Link]()
print("\n original array:\n",arr)
print("flatend array:\n",flarr)

output:
Array created using passed list:
[[1. 2. 3.]
[4. 2. 5.]]
Array created using passed tuple:
[1 3 2]
an array initialized with all zeroes:
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
an array initialized with all 6s,array type is complex:
[[6.+0.j 6.+0.j 6.+0.j]
[6.+0.j 6.+0.j 6.+0.j]
[6.+0.j 6.+0.j 6.+0.j]]
A random array:
[[0.0972175 0.23693093]
[0.32500759 0.76096035]]
A sequential array with steps of 5:
[ 0 5 10 15 20 25]
a sequential array with 10 values between0 and 5:
,g
original array:
[[1 2 3 4]
[5 2 4 2]
[1 2 0 1]]
reshaped array:
[[[1 2 3]
[4 5 2]]

[[4 2 1]
[2 0 1]]]original array:
[[1 2 3]
[4 5 6]]flatend array:
[1 2 3 4 5 6]
WEEK-III :

Arrays([Link],len(array),[Link],[Link],[Link],[Link](type),
type(array))
import numpy as np

arr=[Link]([[1,2,3],[4,2,5]])

print("array of type:",type(arr))

print("no of dimentions :",[Link])

print("shape of the array:",[Link])

print("size of the array:",[Link])

print("array stores elements of size:",arr)

print("lenth of the array:",len(arr))

out put:
array of type: <class '[Link]'>

no of dimentions : 2

shape of the array: (2, 3)

size of the array: 6

array stores elements of size: [[1 2 3]

[4 2 5]]

lenth of the array: 2

WEEK-IV :
Array Manipulation([Link],[Link],[Link],[Link](type),type(array)
#Creating another array within a given interval
import numpy as np
a=[Link](1,40,4)
print(a)
#Transposing a 2D array
arr = [Link](1,40,4).reshape(5,2)
print(arr)
#Creating and slice 2D arrays
arr = [Link]([[20,25,30,35], [40,50,60,70], [45,48,51,54]])
print("slice arr\n")
#Extracting specific rows and columns through slicing
print(arr[0:2,0:2])
#Creating two arrays
arr1 = [Link]([[1,2,3,4], [2,3,7,11]])
arr2 = [Link]([[1,3,5,7], [2,4,6,8]])
#Concatenating both arrays
print("concatenated array\n")
print([Link]((arr1, arr2)))
#Creating and joining two 1D arrays
arr1 = [Link]([1,2,3,4])
arr2 = [Link]([1,3,5,7])
#Joining the arrays
print("joining array\n")
print([Link]((arr1, arr2), axis=1))
#Creating three 1D arrays
arr1 = [Link]([1,2,3,4])
arr2 = [Link]([1,3,5,7])
arr3 = [Link]([2,4,6,8])
#Joining the arrays
np.column_stack((arr1, arr2, arr3))

#Text Description automatically generated with medium confidence

#[Link]()method: adds the second array to the columns of the first array

#Joining the arr2 to arr1


print("hstack value\n")
print([Link]((arr1, arr2)))

# [Link]()method: combines the second array as new rows in the first array

#Joining the arr2 to arr1


print("vstack value\n")
print([Link]((arr1, arr2)))

OUTPUT:

[ 1 5 9 13 17 21 25 29 33 37]
[[ 1 5]
[ 9 13]
[17 21]
[25 29]
[33 37]]
slice arr
[[20 25]
[40 50]]
concatenated array

[[ 1 2 3 4]
[ 2 3 7 11]
[ 1 3 5 7]
[ 2 4 6 8]]
joining array

[[1 1]
[2 3]
[3 5]
[4 7]]
hstack value

[1 2 3 4 1 3 5 7]
vstack value

[[1 2 3 4]
[1 3 5 7]]

WEEK-V :
Mathematical Operations( [Link], [Link], [Link], [Link], [Link], [Link],
[Link], [Link], [Link], [Link]) , Statistical Operations( [Link], [Link], [Link],
[Link]( ) )
import numpy as np
a=[Link]([[2.7,-2,-19,0,0],[0,3.4,99.9,0,0],[10.6,0,13,0,0]])
b=[Link]([[4,8,12,16,20],[24,28,32,36,40],[44,48,52,56,60]])
c=a+b
print(c)

output:

[[ 6.7 6. -7. 16. 20. ]


[ 24. 31.4 131.9 36. 40. ]
[ 54.6 48. 65. 56. 60. ]]

import numpy as np
a=[Link]([[2.7,-2,-19,0,0],[0,3.4,99.9,0,0],[10.6,0,13,0,0]])
b=[Link]([[4,8,12,16,20],[24,28,32,36,40],[44,48,52,56,60]])
d=a-b
print(d)
output:

[[ -1.3 -10. -31. -16. -20. ]

[-24. -24.6 67.9 -36. -40. ]

[-33.4 -48. -39. -56. -60. ]]

import numpy as np
a=[Link]([[2.7,-2,-19,0,0],[0,3.4,99.9,0,0],[10.6,0,13,0,0]])
b=[Link]([[4,8,12,16,20],[24,28,32,36,40],[44,48,52,56,60]])
e=a*b
print(e)

Output:

[[ 10.8 -16. -228. 0. 0. ]

[ 0. 95.2 3196.8 0. 0. ]

[ 466.4 0. 676. 0. 0. ]]

import numpy as np
a=[Link]([[2.7,-2,-19,0,0],[0,3.4,99.9,0,0],[10.6,0,13,0,0]])
b=[Link]([[4,8,12,16,20],[24,28,32,36,40],[44,48,52,56,60]])
c=a/b
print(c)

output:

[[ 0.675 -0.25 -1.58333333 0. 0. ]

[ 0. 0.12142857 3.121875 0. 0. ]

[ 0.24090909 0. 0.25 0. 0. ]]

import numpy as np
# applying sqrt() method on integer numbers
a = [Link]([1, 4, 9, 16])
b = [Link]([6, 10, 18])
print("square-root of an array1 : ", a)
print("square-root of an array2 : ", b)

output:

square-root of an array1 : [1. 2. 3. 4.]


square-root of an array2 : [2.44948974 3.16227766 4.24264069]

import numpy as np # import numpy package


sin_90 = [Link](90) # sine value of degree 90 in degree
print("Sine value of angle 90 in degree = ",sin_90) # print sine value

out put:

Sine value of angle 90 in degree = 0.8939966636005579


import numpy as np
cos_180 = [Link](180)
print("cos value of angle 180 in degree = ",cos_180)

out put:

cos value of angle 180 in degree = -0.5984600690578581

# Python program explaining


# log() function
import numpy as np
in_array = [1, 3, 5, 2**8]
print ("Input array : ", in_array)
out_array = [Link](in_array)
print ("Output array : ", out_array)

out put:

Input array : [1, 3, 5, 256]


Output array : [0. 1.09861229 1.60943791 5.54517744]

# Python Program illustrating


# [Link]() method
import numpy as np
a = [Link]([[1, 4], [5, 6]])
b = [Link]([[2, 4], [5, 2]])
product = [Link](a, b)
print("Dot Product : \n", product)
product = [Link](b, a)
print("\nDot Product : \n", product)

output:

Dot Product :

[[22 12]

[40 32]]

Dot Product :

[[22 32]

[15 32]]
# Python program explaining

# [Link]() function
# importing numpy as np
import numpy as np
a = [1, 2, 3]
b = [Link](a)
print (b)

out put:

[-1.+1.41421356j -1.-1.41421356j]

import numpy as np
myarray4=[Link](-1,9.5,0.25)
n=[Link](14,3)
print(n)
print("mean value")
print([Link](n,axis=1))

output:

[[-1. -0.75 -0.5 ]

[-0.25 0. 0.25]

[ 0.5 0.75 1. ]

[ 1.25 1.5 1.75]

[ 2. 2.25 2.5 ]

[ 2.75 3. 3.25]

[ 3.5 3.75 4. ]

[ 4.25 4.5 4.75]

[ 5. 5.25 5.5 ]

[ 5.75 6. 6.25]

[ 6.5 6.75 7. ]

[ 7.25 7.5 7.75]

[ 8. 8.25 8.5 ]

[ 8.75 9. 9.25]]
mean value

[-0.75 0. 0.75 1.5 2.25 3. 3.75 4.5 5.25 6. 6.75 7.5

8.25 9. ]

# Python Program illustrating


# [Link]() method
import numpy as np
arr = [20, 7, 1, 34]
print("arr : ", arr)
print("median of arr : ", [Link](arr))

output:
arr : [20, 7, 1, 34]
median of arr : 13.5

import numpy as np
myarray4=[Link](-1,9.5,0.25)
n=[Link](14,3)
print(n)
print("std")
print([Link](n,axis=0))

output:
[[-1. -0.75 -0.5 ]
[-0.25 0. 0.25]
[ 0.5 0.75 1. ]
[ 1.25 1.5 1.75]
[ 2. 2.25 2.5 ]
[ 2.75 3. 3.25]
[ 3.5 3.75 4. ]
[ 4.25 4.5 4.75]
[ 5. 5.25 5.5 ]
[ 5.75 6. 6.25]
[ 6.5 6.75 7. ]
[ 7.25 7.5 7.75]
[ 8. 8.25 8.5 ]
[ 8.75 9. 9.25]]
std
[3.02334666 3.02334666 3.02334666]
WEEK-VI :
NumPy data types
mport numpy as np
"Below is a list of all data types in NumPy and the characters used to represent them.
i - integer
b - boolean
u - unsigned integer
f - float
c - complex float
m - timedelta
M - datetime
O - object
S - string
U - unicode string
V - fixed chunk of memory for other type ( void )"

#INT DATA TYPE


arr = [Link]([1, 2, 3, 4])
print([Link])

out put:
int64

#STRING DATA TYPE


arr = [Link]([1, 2, 3, 4], dtype='S')
print(arr)
print([Link])

out put:
[b'1' b'2' b'3' b'4']
|S1

#FLOAT DATA TYPE


arr = [Link]([1.1, 2.1, 3.1])
print([Link])
newarr = [Link](int)
print(newarr)
print([Link])

out put:
float64
[1 2 3]
int64

#BOOL DATA TYPE


arr = [Link]([1, 0, 3])
newarr = [Link](bool)
print(newarr)
print([Link])
out put:
[ True False True]
bool
WEEK-VII :
NumPy ndarray
#Numpy ndarray:
# Creating ndarray
# array creation techniques
import numpy as np
# Creating array from list with type float
a = [Link]([[1, 2, 4], [5, 8, 7]], dtype = 'float')
print ("Array created using passed list:\n", a)

# Creating array from tuple


b = [Link]((1 , 3, 2))
print ("\nArray created using passed tuple:\n", b)

# Creating a 3X4 array with all zeros


c = [Link]((3, 4))
print ("\nAn array initialized with all zeros:\n", c)

# Create a constant value array of complex type


d = [Link]((3, 3), 6, dtype = 'complex')
print ("\nAn array initialized with all 6s."
"Array type is complex:\n", d)

#attributes of ndarray, array a from above

# Printing type of arr object


print("Array is of type: ", type(a))

# Printing array dimensions (axes)


print("No. of dimensions: ", [Link])

# Printing shape of array


print("Shape of array: ", [Link])

# Printing size (total number of elements) of array


print("Size of array: ", [Link])

# Printing type of elements in array


print("Array stores elements of type: ", [Link])

#Basic Operations on array a from aboue

# add 1 to every element


print ("Adding 1 to every element:", a+1)

# subtract 3 from each element


print ("Subtracting 3 from each element:", a-3)

# multiply each element by 10


print ("Multiplying each element by 10:", a*10)

# square each element


print ("Squaring each element:", a**2)
# modify existing array
a *= 2
print ("Doubled each element of original array:", a)

# transpose of array

a = [Link]([[1, 2, 3], [3, 4, 5], [9, 6, 0]])

print ("\nOriginal array:\n", a)


print ("Transpose of array:\n", a.T)

out put:

Array created using passed list:

[[1. 2. 4.]

[5. 8. 7.]]

Array created using passed tuple:

[1 3 2]

An array initialized with all zeros:

[[0. 0. 0. 0.]

[0. 0. 0. 0.]

[0. 0. 0. 0.]]

An array initialized with all [Link] type is complex:

[[6.+0.j 6.+0.j 6.+0.j]

[6.+0.j 6.+0.j 6.+0.j]

[6.+0.j 6.+0.j 6.+0.j]]

Array is of type: <class '[Link]'>

No. of dimensions: 2

Shape of array: (2, 3)

Size of array: 6

Array stores elements of type: float64

Adding 1 to every element: [[2. 3. 5.]


[6. 9. 8.]]

Subtracting 3 from each element: [[-2. -1. 1.]

[ 2. 5. 4.]]

Multiplying each element by 10: [[10. 20. 40.]

[50. 80. 70.]]

Squaring each element: [[ 1. 4. 16.]

[25. 64. 49.]]

Doubled each element of original array: [[ 2. 4. 8.]

[10. 16. 14.]]

Original array:

[[1 2 3]

[3 4 5]

[9 6 0]]

Transpose of array:

[[1 3 9]

[2 4 6]

[3 5 0]]

WEEK-VIII:
NumPy String Operations
#Numpy String Operations

import numpy as np

# [Link]() function
print([Link](['GEEKS', 'FOR']))
print([Link]('GEEKS'))

# [Link]() function
print([Link]('geeks for geeks'))
print([Link]('geeks, for, geeks', sep = ','))

# [Link]() function
print([Link]('-', 'geeks'))
print([Link](['-', ':'], ['geeks', 'for']))

# [Link]() function
a=[Link](['geeks', 'for', 'geeks'])
print([Link](a,'geek'))
print([Link](a, 'fo'))

# [Link]() function
a=[Link](['geeks', 'for', 'geeks'])
print([Link](a,'geek'))
print([Link](a, 'fo'))

# [Link]() function
print([Link]('geeks'))
print([Link]('12geeks'))

# [Link]() function
a=[Link]('geeks','for')
print(a)

# [Link]() function
a=[Link]('geeks','for')
print(a)

# [Link]() function
a=[Link]('geeks','for')
print(a)

out put:

['geeks' 'for']

geeks

['geeks', 'for', 'geeks']

['geeks', ' for', ' geeks']

g-e-e-k-s

['g-e-e-k-s' 'f:o:r']

[1 0 1]

[0 1 0]

[ 0 -1 0]

[-1 0 -1]

False

False

False
True

True

WEEK-IX :
NumPy Financial functions:
import numpy_financial as npf
fv=float(input("Enter future value"))
rate=int(input("enter rate of interest"))
rate=(rate/100)/12
time=int(input("enter tenure"))
time=time*12
amt=int(input("enter amount"))
pv=[Link](rate,time,-amt,fv)
print("present value is ",pv)

out put:

enter future value15692.93


enter rate of interest5
enter tenure10
enter amount100
present value is -100.00067131625

WEEK-X :
NumPy Functional Programming
def my_fv1(rate,nper,pmt,pv):
for i in range(1,nper+1):
if(i==1):
pmt=(pv+pmt)+(pmt*(0.05/12))
else:
pmt=100+pmt+(pmt*(0.05/12))
print(pmt)

#main
rate = float(input("Enter rate of interest(annually):"))
rate = rate / 12
print(rate)
nper = int(input("Enter no of years:"))
nper = nper * 12
print(nper)
pmt = int(input("Enter payment:"))
pv = int(input("Enter present value:"))
my_fv1(rate,nper,pmt,pv)

out put:

Enter rate of interest(annually):4


0.3333333333333333
Enter no of years:3
36
Enter payment:5000
Enter present value:500
10145.355846463166

You might also like