[Go to site: main page, start]

0% found this document useful (0 votes)
4 views50 pages

DataAnalyticsUsingPython LAB MANUAL-2

The document outlines a laboratory course on Data Analytics using Python at Visvesvaraya Technological University, detailing the experiments and programming tasks students must complete. It includes various Python programming exercises, such as linear search, data visualization, and regression models, along with code examples. A certificate of completion is also provided for students who successfully finish the course requirements.

Uploaded by

ashakolipoti08
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views50 pages

DataAnalyticsUsingPython LAB MANUAL-2

The document outlines a laboratory course on Data Analytics using Python at Visvesvaraya Technological University, detailing the experiments and programming tasks students must complete. It includes various Python programming exercises, such as linear search, data visualization, and regression models, along with code examples. A certificate of completion is also provided for students who successfully finish the course requirements.

Uploaded by

ashakolipoti08
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

VISVESVARAYA TECHNOLOGICAL

UNIVERSITY
Center for Post Graduate Studies,
Mysuru Region.

DEPARTMENT OF CSE (MCA Programme)

Data Analytics using Python


Programming Laboratory

LAB INCHARGE
Dr KUMAR P K
Assistant Professor
Department of CSE,
VTU, Department of PG Studies, Mysuru
VISVESVARAYA TECHNOLOGICAL
UNIVERSITY
Center for Post Graduate Studies,
Mysuru Region.

DEPARTMENT OF CSE (MCA Programme)

LABORATORY CERTIFICATE

This is to certify that Mr./Ms __________________________________________ bearing USN

____________________________, has satisfactorily completed the course of experiments in Data

Analytics using Python Programming(20MCA36) prescribed by the Visvesvaraya

Technological University for III semester MCA course, during the academic year ____________
PROF. & HEAD LAB INCHARGE
(Dept. Of CSE) (Dept. Of MCA)

SL DATE CONTENT PG.


NO NO
1 Write a Python program to perform linear search

2 Write a Python program to insert an element into a sorted list.

3 Write a python program using object oriented programming to


demonstrate encapsulation, overloading and inheritance
4 Implement a python program to demonstrate
1) Importing Datasets
2) Cleaning the Data
3) Data frame manipulation using Numpy
5 [Link] a python program to demonstrate the following using
NumPy a) Array manipulation, Searching, Sorting and splitting.
b) broadcasting and Plotting NumPy arrays
6 Implement a python program to demonstrate
Data visualization with various Types of Graphs using Numpy

7 Write a Python program that creates a mxn integer arrayand Prints


its attributes using matplotlib

8 Write a Python program to demonstrate the generation of linear regression


models.
9 Write a Python program to demonstrate the generation of logistic regression
models using Python.

10 Write a Python program to demonstrate Timeseries analysis with Pandas.

11 Write a Python program to demonstrate Data Visualization using Seaborn.


1. Write a Python program to perform linear search

def linear_search(alist, key):


"""Return index of key in alist. Return -1 if key not present."""
for i in range(len(alist)):
if alist[i] == key:
return i
return -1

alist = input('Enter the list of numbers: ')


alist = [Link]()
alist = [int(x) for x in alist]
key = int(input('The number to search for: '))

index = linear_search(alist, key)


if index < 0:
print('{} was not found.'.format(key))
else:
print('{} was found at index {}.'.format(key, index))
OUTPUT:
2. Write a Python program to insert an element into a sorted list

def insert_spec_position(element, alist, position):


return alist[:position-1]+[element]+alist[position-1:]

alist = input('Enter the list of numbers: ')


alist = [Link]()
print("Original list:")
print(alist)
position = int(input('Enter the position to insert the element: '))
element = int(input('Enter the element to insert: '))
result = insert_spec_position(element, alist, position)
print("\nAfter inserting an element at kth position in the said list:")
print(result)

OUTPUT:
3 . Write a python program using object oriented programming
to demonstrate encapsulation, overloading and inheritance

# Deomonstration of Encapsulation
class Car:
print("Deomonstration of Encapsulation ")
print("================================")
def __init__(self):
self.__maxprice = 1000000
def sell(self):
print("Selling Price: {}".format(self.__maxprice))
def setMaxPrice(self, price):
self.__maxprice = price
c = Car()
[Link]()
# change the price
c.__maxprice = 500000
[Link]()
# using setter function
[Link](200000)
[Link]()

# Demonstration of Overloading (Polymorphism)


print('\n')
class MethodOverload():
print("Deomonstration of Overloading ")
print("================================")

def add(self, a=None, b=None):


print(f'The value of A is {a}')
print(f'The value of B is {b}')

ob=MethodOverload() // Object creation of class MethodOverload()


[Link]()
[Link](2)
[Link](2,3)
# Demonstration of Inheritance
# parent class
print('\n')
class ParentClass:
def __init__(self):
print("Deomonstration of Iheritance ")
print("================================")

def PClassMethod(self):
print("Its parent class method")

# child class
class ChildClass(ParentClass):

def ChildClassMethod(self):
print("Its Child class method")

Ob = ChildClass()
[Link]()
[Link]()

OUTPUT:
4. Implement a python program to demonstrate
1) Importing Datasets
2) Cleaning the Data
3) Data frame manipulation using Numpy

import pandas as pd

# 1. Loading the data


data = pd.read_csv('[Link]',header=None)
[Link] = ['sepal length', 'sepal width', 'petal length', 'petal width',
'class']
[Link]()
#create a data frame - dictionary is used here where keys get converted to
column names and values to row values.
data = [Link]({'Country':
['India','Nepal','Pakistan','Bangladesh','Bhutan'],
'Rank':[11,40,100,130,101]})
data

#We can do a quick analysis of any data set using:


[Link]()

#To get the complete information about the data set, we can use info()
function.
[Link]()
# 2. Cleaning the data
# To find and fill the missing data in the dataset we will use another function.
# There are 4 ways to find the null values if present in the dataset.

# 1. Using isnull() function:


[Link]()

# 2. This is the same as the isnull() function.


[Link]()
# 3. This function also gives a boolean value if any null value is present or not,
#but it gives results column-wise, not in tabular format.

[Link]().any()

# 4. Using isna(). sum() - This function gives the sum of the null values preset
in #the dataset column-wise.

[Link]().sum()

# Using isna().any().sum() - This function gives output in a single value if any


#null is present or not.
[Link]().any().sum()
# 3. Data frame manipulation using Numpy
#load the library and check its version, just to make sure we aren't using an
#older version
import numpy as np
np.__version__

#create a list comprising numbers from 0 to 20


L = list(range(21))
L

#converting integers to string - this style of handling lists is known as list


comprehension.
[str(c) for c in L]
#List comprehension offers a versatile way to handle list manipulations tasks
easily.
[type(item) for item in L]

#creating arrays
[Link](10, dtype='int')
#creating a 3 row x 8 column matrix
[Link]((3,8), dtype=float)

#creating a matrix with a predefined value


[Link]((3,5),1.23)

#create an array with a set sequence


[Link](0, 20, 2)
x1 = [Link]([4, 3, 4, 4, 8, 4])
x1

#assess value to index zero


x1[0]

#get the last value


x1[-1]

#get the second last value


x1[-2]
# Array Slicing¶
x = [Link](20)
x

#from start to 4th position


x[:5]

#from 4th position to end


x[4:]

#from 4th to 6th position


x[4:7]

#return elements at even place


x[ : : 2]

#return elements from first position step by two


x[1::2]

#reverse the array


x[::-1]

#You can concatenate two or more arrays at once.


x = [Link]([1, 2, 3])
y = [Link]([3, 2, 1])
z = [21,21,21]
[Link]([x, y,z])

#Split the arrays based on pre-defined positions.


x = [Link](10)
x
x1,x2,x3 = [Link](x,[3,6])
print(x1,x2,x3)

5. Implement a python program to demonstrate the following


using NumPy
a) Array manipulation, Searching, Sorting and splitting.
b) broadcasting and Plotting NumPy arrays

import numpy as np
from matplotlib import pyplot as plt
# A: Array manipulation, Searching, Sorting and splitting.
# Array manipulation
#creating arrays

print(" Array manipulation ")


print(" ======================")
[Link](10, dtype='int')

#creating a 3 row x 8 column matrix


[Link]((3,8), dtype=float)

#create an array with a set sequence


[Link](0, 20, 2)

#creating a matrix with a predefined value


[Link]((3,5),1.23)

x1 = [Link]([4, 3, 4, 4, 8, 4])
print ( "the array elements are : " ,x1)

#assess value to index zero


print("After accessing the value of index zero in array is : ",x1[0] )

#get the last value


print("After accessing the last index value in array is : ",x1[-1])

#Split the arrays based on pre-defined positions.


print("\n")
print(" Splitting the Array ")
print(" ======================")
x = [Link](10)

print("Before Spliting array : the elements are = ", x)


x1,x2,x3 = [Link](x,[3,6])

print("After Spliting the arrays ")


print(x1)
print(x2)
print(x3)
print("\n")

# Sorting the array


print(" Sorting an Array ")
print(" ======================")
arr = [Link]([3, 2, 0, 1])
print("Before Sorting the array, Elements are ", arr)

print("After Sorting the array, Elements are ", [Link](arr))

arr = [Link](['banana', 'cherry', 'apple'])


print("Before Sorting the array of string, Elements are ", arr)

print("After Sorting the array of string Elements are ", [Link](arr))

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


print("Before Sorting the array of two dimensional Elements are ", arr)
print("After Sorting the array of two dimensional Elements are ", [Link](arr))

print("\n")
# Searching array

print(" Searching an Array ")


print(" ======================")
arr = [Link]([1, 2, 3, 4, 5, 4, 4])
print("Before Searching the array, Elements are ", arr)

# Find the indexes where the value is 4:

x = [Link](arr == 4)
print("The element after Find the indexes where the value is 4:", x)

# It will find the value 4 and return the index is present at index 3, 5, and 6.

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

#Find the indexes where the values are even:


x = [Link](arr%2 == 0)
print("The element after Find the indexes where the values are even:", x)

#Find the indexes where the values are odd:


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

x = [Link](arr%2 == 1)

print("The element after Find the indexes where the values are odd:", x)

#Search Sorted
# Method called searchsorted() which performs a binary search in the array,
and #returns the index
# where the specified value would be inserted to maintain the search order.
print("\n")
print(" Search Sorted an Array ")
print(" ========================")

arr = [Link]([6, 7, 10, 9])


x = [Link](arr, 8)

print(" Order of the specified value is ", x)

print("\n")

# B . broadcasting and Plotting NumPy arrays


# The term broadcasting describes how numpy treats arrays with different
#shapes during arithmetic operations.
# Subject to certain constraints, the smaller array is “broadcast” across the
#larger array so that they have compatible shapes.
print(" Broad casting ")
print(" ===============")
a = [Link]([1.0, 2.0, 3.0])
b = [Link]([2.0, 2.0, 2.0])

print(" Broad casting of a* b is :" , a * b)

a = [Link]([1.0, 2.0, 3.0])


b = 3.0
print(" Broad casting of a* b is :" , a * b)

# General Broadcasting Rules


#When operating on two arrays, NumPy compares their shapes element-wise.
It #starts with the trailing (i.e. rightmost) dimensions and works its way left.
Two #dimensions are compatible when

# 1. they are equal, or

# 2. one of them is 1
#If these conditions are not met, a ValueError: operands could not be
broadcast #together exception is thrown,

x = [Link](4)
xx = [Link](4,1)
y = [Link](5)
z = [Link]((3,4))

print(" [Link] is=", [Link])


print("\n")
print(" [Link] is=",[Link])
print("\n")
print(" [Link] is=",[Link])
print("\n")
print(" [Link] is=",[Link])
print("\n")
print(" XX+[Link] is=\n",(xx + y).shape)
print("\n")
print(" XX+Y is=\n",xx + y)
print("\n")
print(" [Link] is=",[Link])
print("\n")
print(" [Link] is=\n",[Link])

print("\n")
print(" X+Z .shape is=\n",(x + z).shape)
print("\n")
print(" X + Z is=\n",x + z)

# Plotting graph
print("\n")
print(" Plotting graph of an Array ")
print(" =============================")
x = [Link]([5, 10, 15])
#x = [Link](1,11)
y=1*x+5
[Link]("Matplotlib demo")
[Link]("x axis caption")
[Link]("y axis caption")
[Link](x,y)
[Link]()

OUTPUT
6. Implement a python program to demonstrate
Data visualization with various Types of Graphs using
Numpy

Data Visualization

Data Visualization is the graphical representation of Data. It involves producing


efficient visual elements like charts, dashboards, graphs, mappings, etc.

 To install matplotlib, go to anaconda prompt and run the following


command

pip install matplotlib

 Verify whether the matplotlib is properly installed using the following


command in Jupyter notebook.

import matplotlib matplotlib.__version__

How to use Matplotlib

Before using matplotlib, we need to import the package. This can be done
using the ‘import’ method in Jupyter notebook. PyPlot is the graphical module
in matplotlib which is mostly used for data visualization, importing PyPlot is
sufficient to work around data visualization.

Create a Simple Plot


Here we will be depicting a basic plot using some random numbers generated
using NumPy. The simplest way to create a graph is using the ‘plot()’ method.
To generate a basic plot, we need two axes (X) and (Y), and we will generate
two random numbers using the ‘linspace()’ method from Numpy.
# import matplotlib library as mpl

import matplotlib as mpl

#import the pyplot module from matplotlib as plt (short name used for
referring the object)

import [Link] as plt

# import the NumPy package

import numpy as np

# generate random number using NumPy, generate two sets of random


numbers and store in x, y

x = [Link](0,50,100)

y = x * [Link](100,150,100)

# Create a basic plot

[Link](x,y)

Adding Elements to Plot

The plot generated above does not have all the elements to understand it
better. Let’s try to add different elements for the plot for better interpretation.
The elements that could be added for the plot includes title, x-Label, y-label, x-
limits, y-limits.

# set different elements to the plot generated above


# Add title using ‘[Link]’
# Add x-label using ‘[Link]’
# Add y-label using ‘[Link]’
# set x-axis limits using ‘[Link]’
# set y-axis limits using ‘[Link]’
# Add legend using ‘[Link]’

x = [Link](0,50,100)

y = x * [Link](100,200,100)

[Link](x,y)

[Link]("Basic Plot")

[Link]("X-Axis")

[Link]("Y-Axis")

[Link](0,60)

[Link](0,15000)

Add few more elements to the plot like color, markers, line customization.
# add color, style, width to line element

[Link](x, y, c = 'r', linestyle = '--', linewidth=2)

# add markers to the plot, marker has different elements i.e., style, color, size
etc.,

[Link] (x, y, marker='*', markersize=3, c='g')

[Link] (x, y, marker='*', markersize=3, c='g', label='normal')


# add grid using grid() method

[Link](True)

# add legend and label

[Link]()

# A legend is an area describing the elements of the graph. In the matplotlib library, there’s a
# function called legend() which is used to Place a legend on the axes.

Bar Graph:

Bar graph represents the data using bars either in Horizontal or Vertical directions.
Function:

•The function used to show bar graph is ‘[Link]()’

•The bar() function expects two lists of values one on x-coordinate and another on y-coordinate

Customizations:

[Link]() function has the following specific arguments that can be used for configuring the plot.

•Width, Color, edge colour, line width, tick_label, align, bottom,

•Error Bars – xerr, yerr

# simple bar chart

# x-axis is shows the subject and y -axis shows the markers in each subject

subject = ['Python','IOT','SPM','AJP','Software Testing']

marks =[70,80,50,30,78]

[Link](subject,marks) [Link]()

# customizations

#width – shows the bar width and default value is 0.8


#color – shows the bar color

#bottom – value from where the y – axis starts in the chart i.e., the lowest value on y-axis shown

#align – to move the position of x-label, has two options ‘edge’ or ‘center’

#edgecolor – used to color the borders of the bar

#linewidth – used to adjust the width of the line around the bar

#tick_label – to set the customized labels for the x-axis

[Link](subject, marks, color='g', width=0.5, bottom=10, align ='center', dgecolor='r', linewidth=2,


tick_label=subject)

Pie Chart:

Pie charts display the proportion of each value against the total sum of values. This chart requires a
single series to display. The values on the pie chart shows the percentage contribution in terms of a
pie called Wedge/Widget.

Function:

•The function used for pie chart is ‘[Link]()’

•To draw a pie chart, we need only one list of values, each wedge is calculated as proportion
converted into angle.

Customisations: [Link]() function has the following specific arguments that can be used for
configuring the plot.

# pie plot
# Assume that we have a Vaccination data on each day

# We would like to know the vaccination in terms of tickets closed in the week

# data

Doses = [102, 20, 80, 35, 30, 25,60]

Days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']

# create pie chart

[Link](Doses, labels=Days)

#Let’s add additional parameters to pie plot

#explode – to move one of the wedges of the plot

#autopct – to add the contribution %

explode = [0.2,0.1,0,0.1,0,0,1]

[Link](Doses, labels = Days, explode=explode, autopct='%1.1f%%')

Scatter Plot
Scatterplot is used to visualize the relationship between two columns/series of data.

Function:

The function used for the scatter plot is ‘[Link]()’

# Simple scatter plot

# generate the data with random numbers

x = [Link](1000)

y = [Link](1000)

[Link](x,y)

7. Write a Python program that creates a mxn integer array


and Prints its attributes using matplotlib
import numpy as np

import [Link] as plt

q1=[Link]([[1,1,5],
[3,3,3],
[1,1,5]])
[Link](q1)

[Link]()

[Link]()

q2=[Link](range(12,24))

q2=[Link](3,4)

[Link](q2,cmap='rainbow')

[Link]()

[Link]()

q3=[Link](range(1,201))

q3=[Link](20,10)

q3[2,2]=100

q3[5,3]=9

[Link](q3,cmap='jet')

[Link]()

[Link]()
OUTPUT:

import numpy as np
import [Link] as plt

# create a 8x8 matrix of two numbers-0 and 1.

# O represents dark color and 1 represents bright color

arr=[Link]([[1,0]*4,[0,1]*4]*4)

print(arr)

# use the imshow function to display the image made from the above array

[Link](arr)

OUTPUT:

8. Write a Python program to demonstrate the generation


of linear regression models.
Linear Regression Model Representation
Linear regression is an attractive model because the representation is so
simple.
linear regression, a straight-line fit to data. A straight-line fit is a model of the
form y = ax + b where a is commonly known as the slope, and b is commonly
known as the intercept.

# linear regression
import numpy as np
from sklearn.linear_model import LinearRegression
from matplotlib import pyplot as plt

x=[Link]([1,0,20,40,50,70,80,90,120])
y=[Link]([3,20,90,110,130,170,150,200,260])

linreg=LinearRegression()
x=[Link](-1,1)
[Link](x,y)
y_pred=[Link](x)
[Link](x,y)
[Link](x,y_pred,color='red')
[Link]()

OUTPUT:
9. Write a Python program to demonstrate the generation
of logistic regression models using Python.
Logistic Regression is a Supervised Machine Learning model which works
on binary or multi categorical data variables as the dependent variables.
That is, it is a Classification algorithm which segregates and classifies the
binary or multilabel values separately.
For example, if a problem wants us to predict the outcome as ‘Yes’ or ‘No’, it is
then the Logistic regression to classify the dependent data variables and figure
out the outcome of the data.

import pandas as pd
import numpy as np
import [Link] as plt

#Loading dataset – User_Data

dataset = pd.read_csv('User_Data.csv')

#Now, to predict whether a user will purchase the product or not, one needs to
#find out the relationship between Age and Estimated Salary. Here User ID
and #Gender are not important factors for finding out this.

# input

x = [Link][:, [2, 3]].values

y = [Link][:, 4].values

#Splitting the dataset to train and test. 75% of data is used for training the
#model and 25% of it is used to test the performance of our model.
from sklearn.model_selection import train_test_split

#from sklearn.cross_validation import train_test_split

xtrain, xtest, ytrain, ytest = train_test_split( x, y, test_size = 0.25, random_state =


0)

from [Link] import StandardScaler

sc_x = StandardScaler()

xtrain = sc_x.fit_transform(xtrain)

xtest = sc_x.transform(xtest)

print (xtrain[0:10, :])

# Finally, we are training our Logistic Regression model.

from sklearn.linear_model import LogisticRegression

classifier = LogisticRegression(random_state = 0)

[Link](xtrain, ytrain)

#After training the model, it time to use it to do prediction on testing data.

y_pred = [Link](xtest)

# test the performance of our model – Confusion Matrix

from [Link] import confusion_matrix

cm = confusion_matrix(ytest, y_pred)
print ("Confusion Matrix : \n", cm)
# Out of 100 :TruePostive + TrueNegative = 65 + 24 FalsePositive + FalseNegative = 3 + 8

#Performance measure – Accuracy

from [Link] import accuracy_score


print ("Accuracy : ", accuracy_score(ytest, y_pred))

#Visualizing the performance of our model.

from [Link] import ListedColormap


X_set, y_set = xtest, ytest
X1, X2 = [Link]([Link](start = X_set[:, 0].min() - 1,
stop = X_set[:, 0].max() + 1, step = 0.01),
[Link](start = X_set[:, 1].min() - 1,
stop = X_set[:, 1].max() + 1, step = 0.01))

[Link](X1, X2, [Link](


[Link]([[Link](), [Link]()]).T).reshape(
[Link]), alpha = 0.75, cmap = ListedColormap(('red', 'green')))

[Link]([Link](), [Link]())
[Link]([Link](), [Link]())

for i, j in enumerate([Link](y_set)):
[Link](X_set[y_set == j, 0], X_set[y_set == j, 1],
c = ListedColormap(('red', 'green'))(i), label = j)

[Link]('Classifier (Test set)')


[Link]('Age')
[Link]('Estimated Salary')
[Link]()
[Link]()

OUTPUT:
10. Write a Python program to demonstrate Timeseries
analysis with Pandas.

o plot a time series in Python using matplotlib, we can take the following steps −
 Create x and y points, using numpy.
 Plot the created x and y points using the plot() method.
 To display the figure, use the show() method.

rc (runtime configuration) settings in a python script or interactively from the python shell. All rc
settings are stored in a dictionary-like variable called [Link]

[Link](2019, 2, 15, 18, 54, 58, 291224)

The output is in the following order: ‘year’, ‘month’, ‘date’, ‘hour’,


‘minute’, ‘seconds’, ‘microseconds’. To get the date alone, use
the [Link]() instead.

import [Link] as plt


import datetime
import numpy as np
[Link]["[Link]"] = [7.50, 3.50]
[Link]["[Link]"] = True
x = [Link]([[Link](2021, 1, 1, i, 0) for i in range(24)])
y = [Link](100, size=[Link])
[Link](x, y)
[Link]()

OUTPUT:
11. Write a Python program to demonstrate Data
Visualization using Seaborn

Seaborn is an visualization library for statistical graphics plotting in Python. It


is built on the top of matplotlib library and also closely integrated into the
data structures from pandas.
# Importing libraries
import numpy as np
import seaborn as sns

# Selecting style as white, dark, whitegrid, darkgrid or ticks


[Link]( style = "white" )

# Generate a random univariate dataset


rs = [Link]( 10 )
d = [Link]( size = 50 )

# Plot a simple histogram with binsize determined automatically


[Link](d, color = "g")

OUTPUT:

You might also like