Data Analytics Lab Manual DEPT OF MCA,EWIT
Data Analytics Lab
Lab program 1:
Write a python program to perform Linear search
def linear_Search(list,n,x):
# Searching list1 sequentially
for i in range(0, n):
if (array[i] == x):
return i
return -1
array = [1 ,3, 5, 4, 7, 9]
n = len(array)
x=int(input("enter the item"))
res = linear_Search(list,n,x)
if(res == -1):
print("Element not found")
else:
print("Element found at index: ", res)
OUTPUT:
enter the item 9
Element found at index: 5
Lab program 2:
Write a python program to insert an element into a sorted list
def insert(list, n):
index = len(list)
# Searching for the position
for i in range(len(list)):
if list[i] > n:
index = i
break
Shwetha shri K - Asst.Professor Page 1
Data Analytics Lab Manual DEPT OF MCA,EWIT
# Inserting n in the list
if index == len(list):
list = list[:index] + [n]
else:
list = list[:index] + [n] + list[index:]
return list
# Driver function
list = [1, 2, 4]
n=3
print(insert(list, n))
output:
[1, 2, 3, 4]
Lab program 3:
Write a python program using object oriented programming to demonstrate encapsulation, overloading
and inheritance
##parent class
class Operations:
#constructor
def __init__(self,a,b):
self.a=a
self.b=b
def add(self):
sum=self.a+self.b
print ("sum of a and b is : ", sum)
#Child class
class Myclass(Operations):
#constructor
def _init__(self,a,b):
#invoking parent class constructor
super(self,Operations).__init__(a,b)
self.a=a
self.b=b
def sub(self):
sub=self.a-self.b
print("Subtraction of C and D is : ", sub)
ob1=Myclass(20,30)
ob2=Myclass("ONE","TWO")
ob1.add()#to sum up integers
ob2.add()#to sum up String
ob1.sub()
Shwetha shri K - Asst.Professor Page 2
Data Analytics Lab Manual DEPT OF MCA,EWIT
OUTPUT:
sum of a and b is : 70
sum of a and b is : ONETWO
Subtraction of C and D is : -10
Lab program 5:
Implement a Python program to demonstrate the following using Numpy
5A. Array manipulation,searching,sorting and splitting
import numpy as np
import numpy as np2
a = np.array([[1, 4, 2],
[3, 4, 6],
[0, -1, 5]])
#array before sorting
print ("Array elements before sorting:\n")
print(a)
# sorted array
print ("Array elements in sorted order:\n")
print(np.sort(a, axis = None))
i = np.where(a == 6)
print("i = {}".format(i))
# specify sort algorithm
print ("Column wise sort by applying merge-sort:\n")
print(np.sort(a, axis = 0, kind = 'mergesort'))
# looking for value 30 in arr and storing its index in i
#Splitting of Arrays
b = np.array([[3, 4, 5],
[6, 7, 8],
[9, 10, 11]])
print(b)
print(b[0:3,1])
Shwetha shri K - Asst.Professor Page 3
Data Analytics Lab Manual DEPT OF MCA,EWIT
OUTPUT:
Array elements before sorting:
[[ 1 4 2]
[ 3 4 6]
[ 0 -1 5]]
Array elements in sorted order:
[-1 0 1 2 3 4 4 5 6]
i = (array([1]), array([2]))
Column wise sort by applying merge-sort:
[[ 0 -1 2]
[ 1 4 5]
[ 3 4 6]]
[[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
[ 4 7 10]
5B. Broadcasting and plotting numpy arrays
import numpy as np
import matplotlib.pyplot as plt
#To Demonstrate Numpy Broadcasting
A = np.array([[11, 22, 33], [10, 20, 30]])
print(A)
b=4
print(b)
C=A+b
print(C)
# Plotting using Broadcasting
# Computes x and y coordinates for
# points on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
Shwetha shri K - Asst.Professor Page 4
Data Analytics Lab Manual DEPT OF MCA,EWIT
# Plot the points using matplotlib
plt.plot(x, y_sin)
plt.plot(x, y_cos)
plt.xlabel('x axis label')
plt.ylabel('y axis label')
plt.title('Sine and Cosine')
plt.legend(['Sine', 'Cosine'])
plt.show()
OUTPUT:
Shwetha shri K - Asst.Professor Page 5
Data Analytics Lab Manual DEPT OF MCA,EWIT
Lab program 6:
Implement python program to demonstrate
Data Visualization with various types of graphs using Numpy
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
# create 2D array of table given above
data = [['E001', 'M', 34, 123, 'Normal', 350],
['E002', 'F', 40, 114, 'Overweight', 450],
['E003', 'F', 37, 135, 'Obesity', 169],
['E004', 'M', 30, 139, 'Underweight', 189],
['E005', 'F', 44, 117, 'Underweight', 183],
['E006', 'M', 36, 121, 'Normal', 80],
['E007', 'M', 32, 133, 'Obesity', 166],
['E008', 'F', 26, 140, 'Normal', 120],
['E009', 'M', 32, 133, 'Normal', 75],
['E010', 'M', 36, 133, 'Underweight', 40] ]
# dataframe created with
# the above data array
df = pd.DataFrame(data, columns = ['EMPID', 'Gender', 'Age', 'Sales','BMI', 'Income'] )
# create histogram for numeric data
df.hist()
# show plot
plt.show()
Shwetha shri K - Asst.Professor Page 6
Data Analytics Lab Manual DEPT OF MCA,EWIT
OUTPUT:
(Histogram)
# Dataframe of previous code is used here
# Plot the bar chart for numeric values
# a comparison will be shown between
# all 3 age, income, sales
df.plot.bar()
# plot between 2 attributes
plt.bar(df['Age'], df['Sales'])
plt.xlabel("Age")
plt.ylabel("Sales")
plt.show()
OUTPUT:
(Column Chart)
Shwetha shri K - Asst.Professor Page 7
Data Analytics Lab Manual DEPT OF MCA,EWIT
Shwetha shri K - Asst.Professor Page 8