0% found this document useful (0 votes)
5 views

FDS-LAB-PROGRAM

Uploaded by

sumaakilesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

FDS-LAB-PROGRAM

Uploaded by

sumaakilesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

a) NUMPY FUNCTIONS

PROGRAM
import random
import numpy as np
row= int(input("Enter the Number of Rows: "))
column=int(input("Enter the Number of Columns: "))
matrix=[]
for i in range(row):
d=[]
for j in range(column):
x = random.randint(100)
d.append(x)
matrix.append(d)

matrix=np.array(matrix)
print("\n Matrix = \n",matrix)

print("\n Dimension of Matrix = ",matrix.ndim)


print("\n Byte-Size of Each Element in the Matrix = ",matrix.itemsize)
print("\n Data Type of Matrix = ",matrix.dtype)
print("\n Total Number of Elements in the Matrix= ",matrix.size)
print("\n Shape of the Matrix = ",matrix.shape)
print("\n Reshaped Matrix = \n",matrix.reshape(column,row),"\n")
print("Printing entire row (1,2) of Matrix= \n",matrix[0:2],"\n") # Printing entire row (0,1)
print("Printing 3rd element of 2nd row of Matrix = \n",matrix[1,1],"\n")
print("Printing 4th element of 1st and 2nd row of Matrix = \n",matrix[0:2,3],"\n")
print("\nMaximum Element in the Matrix = ",matrix.max())
print("\nMinimum Element in the Matrix = ",matrix.min())
print("\nSum of All Element in the Matrix = ",matrix.sum())
print("\nSquare root of Each Element in the Matrix = ",np.sqrt(matrix))
print("\nStandard Deviation of the Matrix = ",np.std(matrix))
OUTPUT
Enter the Number of Rows: 5
Enter the Number of Columns: 4
Matrix =
[[59 15 74 73]
[71 35 61 68]
[24 58 45 59]
[34 93 54 47]
[ 7 25 14 17]]
Dimension of Matrix = 2
Byte-Size of Each Element in the Matrix = 4
Data Type of Matrix = int32
Total Number of Elements in the Matrix= 20
Shape of the Matrix = (5, 4)
Reshaped Matrix =
[[59 15 74 73 71]

[35 61 68 24 58]
[45 59 34 93 54]
[47 7 25 14 17]]
Printing entire row (1,2) of Matrix=
[[59 15 74 73]
[71 35 61 68]]
Printing 3rd element of 2nd row of Matrix = 35
Printing 4th element of 1st and 2nd row of Matrix = [73 68]
Maximum Element in the Matrix = 93
Minimum Element in the Matrix = 7
Sum of All Element in the Matrix = 933
Square root of Each Element in the Matrix = [[7.68114575 3.87298335 8.60232527 8.54400375]
[8.42614977 5.91607978 7.81024968 8.24621125]
[4.89897949 7.61577311 6.70820393 7.68114575]
[5.83095189 9.64365076 7.34846923 6.8556546]
[2.64575131 5. 3.74165739 4.12310563]]

Standard Deviation of the Matrix = 23.592954456786455


b) SCIPY FUNCTIONS
I. SPECIAL FUNCTIONS IN SCIPY
PROGRAM
from scipy.special import *
# cube root of 64
print(cbrt(64))
# cube root of elements in an array
arr = [64, 164, 564, 4, 640]
print(list(map(cbrt,arr)))
# combinations of input 4
print(comb(4,1))
# combinations of 4
print([comb(4,1),comb(4,2),comb(4,3),comb(4,4)])
# combinations of 6
print([comb(6,1),comb(6,2),comb(6,3),comb(6,4),comb(6,5),comb(6,6)])
# 10 to the power of 2
print(exp10(2))
# for a range
for i in range(1,10):
print(exp10(i))
print(gamma(56))
# calculate W value
print([lambertw(1),lambertw(0),lambertw(56),lambertw(68),lambertw(10)])
# permutations of 4
print([perm(4, 1), perm(4, 2), perm(4, 3),perm(4, 4), perm(4, 5)])
# permutations of 6
print([perm(6, 1), perm(6, 2), perm(6, 3),
perm(6, 4), perm(6, 5)])
print("sin values= ",sindg(0),"\t",sindg(30),"\t",sindg(45),"\t",sindg(60),"\t",sindg(90))
print("cos values= ",cosdg(0),"\t",cosdg(30),"\t",cosdg(45),"\t",cosdg(60),"\t",cosdg(90))
print("tan values= ",tandg(0),"\t",tandg(30),"\t",tandg(45),"\t",tandg(60),"\t",tandg(90))
print("cot values= ",cotdg(0),"\t",cotdg(30),"\t",cotdg(45),"\t",cotdg(60),"\t",cotdg(90))

OUTPUT
4.0
[4.0, 5.473703674798428, 8.26214922566535, 1.5874010519681994, 8.617738760127535]
4.0
[4.0, 6.0, 4.0, 1.0]
[6.0, 15.0, 20.0, 15.0, 6.0, 1.0]
100.0
10.0
100.0
1000.0
10000.0
100000.0
1000000.0
10000000.0
100000000.0
1000000000.0
1.2696403353658278e+73
[(0.5671432904097838+0j), 0j, (2.9451813101206707+0j), (3.0910098540499797+0j),
(1.7455280027406994+0j)]
[4.0, 12.0, 24.0, 24.0, 0.0]
[6.0, 30.0, 120.0, 360.0, 720.0]
sin values= 0.0 0.49999999999999994 0.7071067811865476 0.8660254037844387 1.0
cos values= 1.0 0.8660254037844387 0.7071067811865475 0.49999999999999994 -0.0
tan values= 0.0 0.5773502691896257 1.0 1.7320508075688767 inf
cot values= inf 1.7320508075688767 1.0 0.5773502691896257 0.0

II. LINEAR ALGEBRA FUNCTIONS IN SCIPY


PROGRAM
# Import the required libraries
from scipy import linalg
import numpy as np
# The function takes two arrays
a = np.array([[7, 2], [4, 5]])
b = np.array([8, 10])
print(a,"\n",b)
# Solving the linear equations
print("solving Linear Equations=",linalg.solve(a, b),"\n")
# Initializing the matrix
x = np.array([[7, 2], [4, 5]])
print("Matrix\n",x)
# Finding the inverse of matrix x
print("Inverse of Matrix",linalg.inv(x))
# Initializing the matrix
x = np.array([[8 , 2] , [3 , 5] , [1 , 3]])
print("Matrix\n",x)
# finding the pseudo inverse of matrix x
print("Pseudo Inverse of Matrix",linalg.pinv(x))
# Initializing the matrix A
x = np.array([[9 , 6] , [4 , 5]])
print("Matrix\n",x)
# Finding the determinant of matrix A
print("Determinant of Matrix",linalg.det(x))
# Initializing the matrix M
x = np.array([[9 , 3] , [2 , 4]])
print("Matrix\n",x)
# Passing the values to the eigen function
val , vect = linalg.eig(x)
# Display the Eigen values and Eigen vectors
print("Eigen Value = ",val,"\nEigen Vector =",vect)
# Initializing the matrix
x = np.array([[16 , 4] , [100 , 25]])
print("Matrix\n",x)
# Calculate and print the matrix square root
print("Matrix Square Root\n",linalg.sqrtm(x),"\n")
# Calculate and print the matrix exponential
print("Exponential of Matrix",linalg.expm(x),"\n")
# Calculate and print the matrix sin
print("Sin of Matrix",linalg.sinm(x),"\n")
# Calculate and print the matrix cos
print("Cosine of Matrix",linalg.cosm(x),"\n")
# Calculate and print the matrix tangent
print("Tan of Matrix",linalg.tanm(x))

OUTPUT
[[7 2]
[4 5]]
[ 8 10]
solving Linear Equations= [0.74074074 1.40740741]
Matrix
[[7 2]
[4 5]]
Inverse of Matrix [[ 0.18518519 -0.07407407]
[-0.14814815 0.25925926]]
Matrix
[[8 2]
[3 5]
[1 3]]
Pseudo Inverse of Matrix [[ 0.14251208 -0.03381643 -0.03864734]
[-0.07487923 0.16183575 0.11352657]]
Matrix
[[9 6]
[4 5]]
Determinant of Matrix 21.0
Matrix
[[9 3]
[2 4]]
Eigen Value = [10.+0.j 3.+0.j]
Eigen Vector = [[ 0.9486833 -0.4472136 ]
[ 0.31622777 0.89442719]]
Matrix
[[ 16 4]
[100 25]]
Matrix Square Root
[[ 2.49878019 0.62469505]
[15.61737619 3.90434405]]
Exponential of Matrix [[2.49695022e+17 6.24237555e+16]
[1.56059389e+18 3.90148472e+17]]
Sin of Matrix [[-0.06190153 -0.01547538]
[-0.38688456 -0.09672114]]
Cosine of Matrix [[ 0.22445296 -0.19388676]
[-4.84716897 -0.21179224]]
Tan of Matrix [[0.0626953 0.01567382]
[0.39184561 0.0979614 ]]
III. IMAGE PROCESSING FUNCTIONS IN SCIPY
PROGRAM
from scipy import misc,ndimage
import imageio
import matplotlib.pyplot as plt
import numpy as np
# reads a raccoon face
face = misc.face()
# save the image
imageio.imsave('raccoon.png', face)
plt.title("Raccoon's Image")
plt.imshow(face)
plt.show()
# Topic : PRINTING IMAGE'S DATATYPE AND SIZE
img = imageio.imread('raccoon.png')
# image size

print("Image's Shape = ",img.shape)


# image datatype
print("Image's Datatype = ",img.dtype)
# Topic : GRAYSCALING THE IMAGE
img = misc.face(gray = True)
x, y = img.shape
# Topic : CROPPING THE IMAGE
crop = img[x//3: - x//8, y//3: - y//8]
plt.imshow(crop)
plt.title("GRAYSCALED IMAGE")
plt.show()
# Topic : FLIPPING THE IMAGE
img = misc.face()
flip = np.flipud(img)
plt.title("FLIPPED IMAGE")
plt.imshow(flip)
plt.show()
# Topic : ROTATING THE IMAGE
img = misc.face()
rotate = ndimage.rotate(face, 30)
plt.imshow(rotate)
plt.title("ROTATED IMAGE")
plt.show()
# Topic: SHOWING BLUR IMAGE
img = misc.face()
blur = ndimage.gaussian_filter(img,sigma=5)
plt.imshow(blur)
plt.title("BLUR IMAGE")
plt.show()
# Topic : Sharpening Image
img = misc.face(gray=True).astype(float)
# showing sharp images
blur_G = ndimage.gaussian_filter(blur, 1)

alpha = 30
sharp = blur+alpha*(blur-blur_G)
plt.title("SHARPENED IMAGE")
plt.imshow(sharp)
plt.show()
# Topic : Denoising Image
#creating noisy image
img=misc.face(gray=True).astype(float)
img=img[40:100,30:100]
noise_img=img+0.9*img.std()*np.random.random(img.shape)
plt.title("NOISED IMAGE")
plt.imshow(noise_img)
plt.show()
# denoise the image
denoised = ndimage.gaussian_filter(noise_img, 2.2)
plt.title("DENOISED IMAGE")
plt.imshow(denoised)
plt.show()

OUTPUT
Image's Shape = (768, 1024, 3)
Image's Datatype = uint8
IV. FILE I/O FUNCTIONS IN SCIPY
PROGRAM

import scipy.io as syio


n = 44444
syio.savemat('num.mat', {'num': n})
matlab_file_contents = syio.loadmat('num.mat')
print(matlab_file_contents)
matlab_file_contents = syio.whosmat('num.mat')
print(matlab_file_contents)

OUTPUT
{'__header__': b'MATLAB 5.0 MAT-file Platform: nt, Created on: Mon Oct 17 20:22:11 2022',
'__version__': '1.0', '__globals__': [], 'num': array([[44444]])}
[('num', (1, 1), 'int32')]
V. INTEGRATION FUNCTIONS IN SCIPY
PROGRAM
from scipy.integrate import quad
def f(x):
return 3.0 * x * x
I, err = quad(f, 0, 1)
print(I)
print(err)
import scipy.integrate
from math import sqrt
f = lambda x, y : 16*x*y
g = lambda x : 1
h = lambda y : sqrt(1-4*y**2)
i, err = scipy.integrate.dblquad(f, 0, 0.5, g, h)
print(i)

OUTPUT
1.0
1.1102230246251565e-14
-0.5

VI. CONSTANTS FUNCTIONS IN SCIPY


PROGRAM
from scipy import constants as spc
r=float(input("Enter the Radius = "))
print("Area of Circle:",spc.pi*(r**2))

OUTPUT
Enter the Radius = 10
Area of Circle: 314.1592653589793
c) PANDAS FUNCTIONS
I. CONVERTING PANDAS SERIES INTO PYTHON LIST
PROGRAM
import pandas as pd
ds = pd.Series([2,4,6,8,10])
print(" Pandas Series = \n",ds,"\n Type = ",type(ds))
ds=ds.tolist()
print("After Conversion of Pandas Series to Python List = ",ds,"\n Type = ",type(ds))

OUTPUT
Pandas Series =
0 2
1 4
2 6
3 8
4 10
dtype: int64
Type = <class 'pandas.core.series.Series'>
After Conversion of Pandas Series to Python List = [2, 4, 6, 8, 10]
Type = <class 'list'>

II. DISPLAY MEAN AND STANDARD DEVIATION OF PANDAS SERIES


PROGRAM

S = pd.Series(data=[1,2,3,4,5,6,7,8,9,5,3])
print("Original Data Series =\n",S)
print("Mean of the said Data Series = ",S.mean())
print("Standard Deviation = ",S.std())

OUTPUT
Original Data Series =
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 5
10 3
dtype: int64
Mean of the said Data Series = 4.818181818181818
Standard Deviation = 2.522624895547565

You might also like