0% found this document useful (0 votes)
8 views133 pages

Record DataScience

The document is a practical work certificate for a B.E. student at University V.O.C. College of Engineering, detailing the experiments conducted in the Data Science Fundamentals Laboratory from January to April 2026. It includes a syllabus covering Python installation, data manipulation with Numpy and Pandas, statistical analysis, and machine learning applications. Additionally, it provides an index of experiments and sample programs demonstrating various data science techniques.

Uploaded by

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

Record DataScience

The document is a practical work certificate for a B.E. student at University V.O.C. College of Engineering, detailing the experiments conducted in the Data Science Fundamentals Laboratory from January to April 2026. It includes a syllabus covering Python installation, data manipulation with Numpy and Pandas, statistical analysis, and machine learning applications. Additionally, it provides an index of experiments and sample programs demonstrating various data science techniques.

Uploaded by

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

University V.O.C.

College of Engineering

THOOTHUKUDI – 628 008


2024

NAME :
DEGREE: B.E
BRANCH/SEMESTER: / SEMESTER

REGISTER NO:

Certificate
This is a bonafide record of Practical work done by the above-mentioned
candidate in OCS353/ DATA SCIENCE FUNDAMENTALS LABORATORY
at University V.O.C. College of Engineering, Thoothukudi during the period Jan
2026 - April 2026

STATION: THOOTHUKUDI-8.
DATE:

Faculty-in-Charge Head of the Department

Submitted for the Anna University Practical Examination held at University


V.O.C. College of Engineering, Thoothukudi on ……………………………

Internal Examiner External Examiner

1
Syllabus:
1. Download, install and explore the features of Python for data analytics.
2. Working with Numpy arrays
3. Working with Pandas data frames
4. Basic plots using Matplotlib
5. Statistical and Probability measures
a) Frequency distributions
b) Mean, Mode, Standard Deviation
c) Variability
d) Normal curves
e) Correlation and scatter plots
f) Correlation coefficient
g) Regression
6. Use the standard benchmark data set for performing the following:
a) Univariate Analysis: Frequency, Mean, Median, Mode, Variance,
Standard Deviation,
Skewness and Kurtosis.
b) Bivariate Analysis: Linear and logistic regression modelling.
7. Apply supervised learning algorithms and unsupervised learning
algorithms on any data set.
8. Apply and explore various plotting functions on any data set.
Note: Example data sets like: UCI, Iris, Pima Indians Diabetes etc.

2
Index
[Link] [Link] Date Name of the Experiment Page No
1 [Link] Download, install and explore the
features of Python for data 4
analytics
2 [Link]:2a Working with Numpy arrays 6
3 [Link]:2b Multi Dimensional arrays – Numpy 19
4 [Link]:2c Examples of Numpy arrays 24
5 [Link]:3a Working with Pandas Data frames 35
6 Ex.No3b Examples using Pandas 45
7 [Link] Basic plots using matplotlib 55
8 [Link]:5a Statistical and Probability measures
- Frequency 64
Distributions
9 [Link]:5b Statistical and Probability measures 67
- Mean, Mode, Standard Deviation
10 [Link]:5c Statistical and Probability measures
- Variability 73
11 [Link]:5d Statistical and Probability 74
measures- Normal curves
12 [Link]:5e Statistical and Probability 76
measures- scatter plots
13 [Link]:5f Statistical and Probability 79
measures- Correlation and Scatter
plot
14 [Link]:5g Statistical and Probability measures 81
- Regression
15 [Link]:6a Mean, Median, Mode, Variance, 83
Standard Deviation, Skewness and
Kurtosis
16 [Link]:6b Bivariant Analysis- Logistic 94
Regression
17 [Link]:7a Supervised Learning 98
18 [Link]:7b Unsupervised Learning 105
19 [Link] Various Plotting functions 114
20 [Link] Reading Files 121
21 [Link] Bivariate Analysis- Multiple 122
Regression
22 [Link] Histogram plot, Density Plot , 125
Contour Plot, 3D plot, Normal plot

3
[Link] Download, install and explore the features of Python for data
analytics

Date:

Aim: To Download, install and explore the features of Python for data
analytics

Procedure:

1. Download pyscripter from goggle search for your OS for 32 bit or 64


bit depending on your system specification
a. icon

2. Install PIP using the following link


[Link]

3. To Install matplotlib and numpy:


a. Run cmd as administrator
b. python -m pip install -u pip
c. python -m pip install -u matplotlib

4. To install pandas
a. pip install pandas

5. In pyscripter- Under Tools option click install packages with pip and
install
a. matplotlib
b. numpy
c. pandas

6. Now in pyscripter the user can work python scripts with matplotlib,
numpy and pandas

4
7. Downloading UCI data sets – iris data set
a. Go to [Link]
b. Type UCI Machine Learning Repository
c. Click on data sets
d. Click on iris image
e. Click on download option – iris data set will be downloaded
as zip file
f. Extract the zip file to particular folder for your use
8. Downloading UCI data sets – Diabetics data set
a. Go to [Link]
b. Type UCI Machine Learning Repository
c. Click on data sets
d. Click on Diabetics image
e. Click on download option – Diabetics data set will be
downloaded as zip file
f. Extract the zip file to particular folder for your use
g. Diabetics data will be in .tar format
h. Once again extract it to specific folder for your use

9. Installing Anaconda :
1. [Link]
2. After download run anaconda-3-2023.9-0-windows-x86_64
3. Follow the steps as displayed in your screen
4. Anaconda will be automatically installed in
C:\users\home\anoconda3
5. Install by selecting only “create start menu shortcuts”
6. Close the sign in / sign up dialog box
7. Close Anaconda navigator
8. Open start in windows- all apps- click anaconda – click anaconda
9. Now all applications like R, Jupyter… etc can be executed
Example – open Jupyter notebook- click new- ipython kernal and work
with the experiments

Result: Thus Download, install and explore the features of Python for data
Analytics is completed and the system is ready for further experiments.

5
[Link]:2a Working with Numpy arrays
Data:
Aim: To work with Numpy arrays in python
Theory:
List: Built in data type
Collection of different data types
Enclosed inside []
Separated by comma
Mutable – changeable
Eg: a=[1,2.3,’x’,’abc’,”abc”]
Positions are a[0]=1 a[1]=2.3 a[2]=’x’ a[3]=[‘abc’] a[4]=”abc”
Tuple: Built in data type
Collection of different data types
Enclosed inside ()
Separated by comma
Immutable – not changable
Eg: a=(1,2.3,’x’,’abc’)
Positions are a[0]=1 a[1]=2.3 a[2]=’x’ a[3]=[‘abc’]
Numpy array:
A numpy array is a set of values
All of the same type
Indexed by a tuple of nonnegative integers.
The number of dimensions is the rank of the array
U32 data type:
The u32 stands for 32-bit unsigned integer type.
It is an unsigned integer
It cannot contain a negative value like signed integers.
The range of u32 is 0 to 4294967295 .
csv file:
A CSV is a comma-separated values file
Data can be saved in a tabular format.
CSVs look like a spreadsheet but with a . csv extension.
CSV files can be used with most any spreadsheet program, such as
Microsoft Excel or Google Spreadsheets.

Programs:
Program1: 1D array using numpy
import numpy as np
arr=[Link]([1,2,3])
print(arr)

Output:
[1 2 3]

6
Program2: explore data type in numpy array

import numpy as np
arr=[Link]([1,2.,3.])
print(arr)

Output:
[1. 2. 3.]

Program3: 2D arrays using numpy

import numpy as np
arr=[Link]([[2,3.,5.],[1,2,3]])
print(arr)

Output:
[[2. 3. 5.]
[1. 2. 3.]]

Program4: Display array dimension

import numpy as np
arr=[Link]([[1,2.,3.],[2,.3,5]])
print(arr)
print([Link])

Output:

[[1. 2. 3. ]
[2. 0.3 5. ]]
2

Program5: create complex array

import numpy as np
arr=[Link]([1,2.,3],dtype=complex)
print(arr)
arr1=[Link]([4,5j,6+8j],dtype=complex)
print(arr1)

Output:

7
[1.+0.j 2.+0.j 3.+0.j]
[4.+0.j 0.+5.j 6.+8.j]

Program6: create matrix

import numpy as np
arr=[Link]([Link]('1.,2;3,4'))
print(arr)

Output:

[[1. 2.]
[3. 4.]]

Program7: convert list into numpy array

import numpy as np
L1=[1,2,3.2,'a',"xyz",'qwe']
print(L1)
L1[0]=10.9
arr=[Link](L1)
print(L1)
print(arr)

Output:

[1, 2, 3.2, 'a', 'xyz', 'qwe']


[10.9, 2, 3.2, 'a', 'xyz', 'qwe']
['10.9' '2' '3.2' 'a' 'xyz' 'qwe']

Program8: convert tuple into numpy array

import numpy as np
L1=(1,2,3.2,'a',"xyz",'qwe')
print(L1)
arr=[Link](L1)
print(L1)
print(arr,[Link])

Output:

8
(1, 2, 3.2, 'a', 'xyz', 'qwe')
(1, 2, 3.2, 'a', 'xyz', 'qwe')
['1' '2' '3.2' 'a' 'xyz' 'qwe'] <U32

Program9: Convert centigrade (Celsius) into Fahrenheit using numpy arrays

import numpy as np
Nc=[Link](10)
Nf=[Link](10)
n=int(input("Enter Total Centigrade or Celsius values"))
for i in range(0,n):
e=int(input("Enter Centigrade"))
Nc[i]=e
Nf[i]=Nc[i]*9/5+32
print(Nc)
print(Nf)
for i in range(0,n):
print(Nf[i])

Output:

Enter Total Centigrade or Celsius values3


Enter Centigrade90
Enter Centigrade45
Enter Centigrade67
[90. 45. 67. 0. 0. 0. 0. 0. 0. 0.]
[194. 113. 152.6 0. 0. 0. 0. 0. 0. 0. ]
194.0
113.0
152.6

Program10: Convert Fahrenheit into Celsius or centigrade using numpy arrays

import numpy as np
Nc=[Link](10)
Nf=[Link](10)
n=int(input("Enter Total Fahrenheit values"))
for i in range(0,n):
e=float(input("Enter Fahrenheit"))
Nf[i]=e
Nc[i]=(Nf[i]-32)*5/9
print(Nc)
print(Nf)

9
for i in range(0,n):
print(Nf[i])

Output:

Enter Total Fahrenheit values3


Enter Fahrenheit194
Enter Fahrenheit113
Enter Fahrenheit152.6
[90. 45. 67. 0. 0. 0. 0. 0. 0. 0.]
[194. 113. 152.6 0. 0. 0. 0. 0. 0. 0. ]
194.0
113.0
152.6

Program11: program to create real and complex parts of numpy array of


complex numbers

import numpy as np
NP=[Link](10)*1j
n=int(input("Enter Total values"))
for i in range(0,n):
r=float(input("Enter real part"))
im=float(input("Enter imaginary part"))
NP[i]=r+im*1j
for i in range(0,n):
print(NP[i].real,NP[i].imag)
NP=[Link]([1.+5*1j,2.3+6*1j],dtype="complex")
print(NP, [Link], [Link])

Output:

Enter Total values3


Enter real part1.2
Enter imaginary part2.3
Enter real part3.4
Enter imaginary part4.5
Enter real part5.6
Enter imaginary part6.7
1.2 2.3
3.4 4.5
5.6 6.7
[1. +5.j 2.3+6.j] [1. 2.3] [5. 6.]

10
Program11a: find Real and imaginary parts of complex numbers

# Importing the NumPy library with an alias 'np'


import numpy as np
# Calculating square root of a complex number
x = [Link]([1 + 0j])
# Calculating square root of another complex number
y = [Link]([0 + 1j])
# Printing the original array 'x' and 'y'
print("Original array:x ", x)
print("Original array:y ", y)
# Printing the real part of the array 'x' and 'y'
print("Real part of the array:")
print([Link])
print([Link])
# Printing the imaginary part of the array 'x' and 'y'
print("Imaginary part of the array:")
print([Link])
print([Link])

Output:

Original array:x [1.+0.j]


Original array:y [0.70710678+0.70710678j]
Real part of the array:
[1.]
[0.70710678]
Imaginary part of the array:
[0.]
[0.70710678]

Program12: Convert numpy array into .csv file

import numpy as np
a = [Link]([23,34,65,78,45,90])
print("The created array:",a)
csv_data = [Link]("[Link]",a,delimiter = ",")
print("array converted into csv file")
data_csv = [Link]("[Link]",delimiter = ",")
print("The data from the csv file:",data_csv)

11
Output:

The created array: [23 34 65 78 45 90]


array converted into csv file
The data from the csv file: [23. 34. 65. 78. 45. 90.]

Program13: Basic arithmetic operations in numpy array – addition

import numpy as np
a = [Link]([5, 72, 13, 100])
b = [Link]([2, 5, 10, 30])
# Performing addition using arithmetic operator
add_ans = a+b
print(add_ans)
# Performing addition using numpy function
add_ans = [Link](a, b)
print(add_ans)
# this would work
c = [Link]([1, 2, 3, 4])
add_ans = a+b+c
print(add_ans)
# but here NumPy only considers the first two arrays (a and b) and ignores th
e third one (c).
add_ans = [Link](a, b, c)
print(add_ans)

Output:

[ 7 77 23 130]
[ 7 77 23 130]
[ 8 79 26 134]
[ 7 77 23 130]

Program 14: Basic arithmetic operations in numpy array – subtraction

import numpy as np
a = [Link]([5, 72, 13, 100])
b = [Link]([2, 5, 10, 30])
# Performing subtraction using arithmetic operator
sub_ans = a-b
print(sub_ans)
# Performing subtraction using numpy function

12
sub_ans = [Link](a, b)
print(sub_ans)

# Performing subtraction using arithmetic operator


sub_ans = a-b-1
print(sub_ans)
# Performing subtraction using numpy function
#sub_ans = [Link](a, b, 1)--- error
print(sub_ans)

Output:

[ 3 67 3 70]
[ 3 67 3 70]
[ 2 66 2 69]
[ 2 66 2 69]

Program 15: Basic arithmetic operations in numpy – multiplication

import numpy as np

a = [Link]([5, 72, 13, 100])


b = [Link]([2, 5, 10, 30])
# Performing multiplication using arithmetic operator
mul_ans = a*b
print(mul_ans)
# Performing multiplication using numpy function
mul_ans = [Link](a, b)
print(mul_ans)

Output:

[ 10 360 130 3000]


[ 10 360 130 3000]

Program 16: Basic arithmetic operations in numpy – division

import numpy as np

a = [Link]([5, 72, 13, 100])


b = [Link]([2, 5, 10, 30])
# Performing division using arithmetic operators
div_ans = a/b

13
print(div_ans)
# Performing division using numpy functions
div_ans = [Link](a, b)
print(div_ans)

Output:

[ 2.5 14.4 1.3 3.33333333]


[ 2.5 14.4 1.3 3.33333333]

Program 17: Basic arithmetic operations in numpy – modulo division

import numpy as np

a = [Link]([5, 72, 13, 100])


b = [Link]([2, 5, 10, 30])
mod_ans = [Link](a, b)
print(mod_ans)
#Performing remainder on two matrices
rem_ans=[Link](a,b)
print(rem_ans)

Output:

[ 1 2 3 10]
[ 1 2 3 10]

Program18: Basic arithmetic operations in numpy – power

import numpy as np

a = [Link]([5, 72, 13, 100])


b = [Link]([2, 5, 10, 30])
pow_ans = [Link](a, b)
print(pow_ans)

a = [Link]([5, 72, 13., 100])


b = [Link]([2, 5, 10, 30])
pow_ans = [Link](a, b)
print(pow_ans)

Output:

14
[ 25 1934917632 419538377 0]
[2.50000000e+01 1.93491763e+09 1.37858492e+11 1.00000000e+60]

Program19: Basic arithmetic operations in numpy – sum, average(mean)

import numpy as np

a = [Link]([5, 72, 13, 100])


b = [Link]([2, 5, 10, 30])
mean_a = [Link](a)
print(mean_a)
# Getting average of all numbers in 'b'
mean_b = [Link](b)
print(mean_b)
# Getting sum of all numbers in 'a'
sum_a = [Link](a)
print(sum_a)
# Getting variance of all number in 'b'
var_b = [Link](b)
print(var_b)

Output:

47.5
11.75
190
119.1875

Program20: Transpose of an array – numpy

import numpy as np

a = [Link]([[1, 2], [3, 4]])


print(a)
b=[Link](a)
print(b)

Output:

[[1 2]
[3 4]]

15
[[1 3]
[2 4]]

Program21: Convert numpy array into csv file

import pandas as pd
import numpy as np
# create a dummy array
arr = [Link](1,11).reshape(2,5)
# display the array
print(arr)
# convert array into dataframe
DF = [Link](arr)
# save the dataframe as a csv file
DF.to_csv("[Link]")
df=pd.read_csv("[Link]")
print(df)
df=pd.read_csv("D:\college misellaneous\Lab2023EvenSem\[Link]")
print(df)

Output:

[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
Unnamed: 0 0 1 2 3 4
0 0 1 2 3 4 5
1 1 6 7 8 9 10
5.1 3.5 1.4 0.2 Iris-setosa
0 4.9 3.0 1.4 0.2 Iris-setosa
1 4.7 3.2 1.3 0.2 Iris-setosa
2 4.6 3.1 1.5 0.2 Iris-setosa
3 5.0 3.6 1.4 0.2 Iris-setosa
4 5.4 3.9 1.7 0.4 Iris-setosa
.. ... ... ... ... ...
144 6.7 3.0 5.2 2.3 Iris-virginica
145 6.3 2.5 5.0 1.9 Iris-virginica
146 6.5 3.0 5.2 2.0 Iris-virginica
147 6.2 3.4 5.4 2.3 Iris-virginica
148 5.9 3.0 5.1 1.8 Iris-virginica

[149 rows x 5 columns]

Program22: Sort array using numpy (Numeric array)

16
import numpy as np
arr = [Link]([3, 2, 0, 1])
print([Link](arr))

Output:

[0 1 2 3]

Program23: Sort array using numpy (String array)

import numpy as np
arr = [Link](['banana', 'cherry', 'apple'])
print([Link](arr))

Output:

['apple' 'banana' 'cherry']

Program24: Sort array using numpy (Boolean array)

import numpy as np
arr = [Link]([True, False, True])
print([Link](arr))

Output:

[False True True]

Program25: Sort array using numpy (2D array)

import numpy as np
arr = [Link]([[3, 2, 4], [5, 0, 1]])
print([Link](arr))

Output:

[[2 3 4]
[0 1 5]]

17
Program26: Sort array using numpy (using axis)

In a two-dimensional vector, the elements of axis 1 are rows and the elements
of axis 0 are columns.

# importing libraries
import numpy as np
# sort along the first axis (columnwise)
a = [Link]([[12, 15],
[10, 1]])
arr1 = [Link](a, axis = 0)
print ("Along first axis (columnwise) : \n", arr1)
# sort along the first axis (rowwise)
a = [Link]([[12, 15],
[10, 1]])
arr1 = [Link](a, axis = 1)
print ("Along first axis (rowwise) : \n", arr1)
a = [Link]([[12, 15], [10, 1]])
arr1 = [Link](a, axis = None)
print ("\nAlong none axis : \n", arr1)
Output:
Along first axis (columnwise) :
[[10 1]
[12 15]]
Along first axis (rowwise) :
[[12 15]
[ 1 10]]
Along none axis :
[ 1 10 12 15]

Result: Thus the python coding to perform basic arithmetic operations using
numpy , sorting using numpy are executed and the results are verified.

18
[Link]:2b Multi Dimensional arrays – Numpy
Date:

Aim: To understand multi dimentional arrays using numpy by writing python


code

Theory:

NumPy arrays are called ndarray objects. The nd stands for "N-dimensional",
which is indicates that an array can have any numbers of dimensions. A 1D
array is like a list, because you need only a single coordinate (or index) to
indicate an element. A 2D array is like a spreadsheet, because you need two
coordinates (a row and a column, or an X and a Y coordinate) to indicate an
element. Higher-dimensional arrays are also possible, although arrays of more
than three dimensions are difficult to mentally picture. (But this is a limitation
of your brain, not of NumPy!)

A real-life example of a 5D array might be an fMRI dataset (a brain scan) of


multiple participants. Here, the first three dimensions would be space (X, Y, Z),
because a brain is a 3D object. The fourth dimension would time, because an
fMRI scan measures brain activity over time. And the fifth dimension would be
the participant, because there are multiple participants. Therefore, you need five
numbers to identify an element in this dataset (X, Y, Z, time, participant
number).

Data types
A NumPy array has a single data type (its dtype), and all elements in the array
are of that type. This is different from a list object, which can contain elements
of different types. The most common data types for NumPy arrays
are bool, int, float, and str.

Creating an array
First, we're going to import numpy with the alias np . The np alias is by
convention, and has the advantage that it requires less typing than numpy.

Program: 5D array using numpy

import numpy as np
shape=(2,3,4,5,6)
fiveDarr=[Link](shape)
print(fiveDarr)
print("Array Dimension",[Link])
print("Total elements in array",[Link])

19
Output:

[[[[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]

[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]

[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]

[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]]

[[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]

[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]

[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]

20
[0. 0. 0. 0. 0. 0.]]

[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]]

[[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]

[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]

[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]

[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]]]

[[[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]

[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]

21
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]

[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]

[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]]

[[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]

[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]

[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]

[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]]

[[[0. 0. 0. 0. 0. 0.]

22
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]

[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]

[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]

[[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]]]]
Array Dimension 5
Total elements in array 720

Result:Thus the python program to create, display the 5D array, display the
dimension, display total number of elements is executed and the result is
verified.

23
[Link]:2c Examples of Numpy arrays
Date:

Aim: To create Numpy arrays

Program1: Create null vector of size 10 and update 6 th value as 11

# Importing the NumPy library with an alias 'np'


import numpy as np
# Creating a NumPy array 'x' filled with zeros of length 10
x = [Link](10)
# Printing the initial array 'x' filled with zeros
print(x)
# Modifying the sixth value (index 6, considering zero-
based indexing) of the array to 11
print("Update sixth value to 11")
x[6] = 11
# Printing the updated array 'x' after modifying the sixth value
print(x)

Output:

24
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
Update sixth value to 11
[ 0. 0. 0. 0. 0. 0. 11. 0. 0. 0.]

Program: Convert Numpy array into float type

import numpy as np
# Example NumPy array (integer)
integer_array = [Link]([100, 200, 300, 400, 500])
# In-place conversion to float
integer_array = integer_array.astype(float)
print("Converted Array (Float):", integer_array)

Output:

Converted Array (Float): [100. 200. 300. 400. 500.]

Program: NumPy program to create a 3x3 matrix with values ranging from 2 to
10

Program:

25
# Importing the NumPy library with an alias 'np'
import numpy as np
# Creating a NumPy array 'x' using arange() from 2 to 11 and reshaping it int
o a 3x3 matrix
x = [Link](2, 11).reshape(3, 3)
# Printing the resulting 3x3 matrix 'x'
print(x)

Output:

[[ 2 3 4]
[ 5 6 7]
[ 8 9 10]]

Program: NumPy program to convert a list of numeric values into a one-


dimensional NumPy array.

Program:

# Importing the NumPy library with an alias 'np'


import numpy as np
# Creating a Python list 'l' containing floating-point numbers
L = [12.23, 13.32, 100, 36.32]
# Printing the original Python list
print("Original List:", L)

26
# Creating a NumPy array 'a' from the Python list 'l'
a = [Link](L)
# Printing the one-dimensional NumPy array 'a'
print("One-dimensional NumPy array: ", a)

Output:

Original List: [12.23, 13.32, 100, 36.32]


One-dimensional NumPy array: [ 12.23 13.32 100. 36.32]

Program: Create empty array

To create an empty NumPy array, we can use the [Link]() function.


The empty() function in NumPy is used to create an empty array with a specified
shape. An empty NumPy array is an array that is initialized with arbitrary values,
and its shape is determined by the user. An array in NumPy that is empty has
random values as its initial values, and the user specifies how it should be shaped.

Code:

import numpy as np

empty_array_2d = [Link]((3, 4))


print(empty_array_2d)

Output1:

[[1.11258854e-306 1.11261502e-306 1.42410839e-306 7.56597770e-307]


[6.23059726e-307 1.42419530e-306 1.37962185e-306 1.37962049e-306]
[1.24610723e-306 1.37962185e-306 7.56591659e-307 1.42410974e-306]]

Output2:

[[6.23042070e-307 4.67296746e-307 1.69121096e-306 1.29061821e-306]


[1.42413555e-306 1.78019082e-306 1.37959740e-306 6.23057349e-307]
[1.02360935e-306 1.69120416e-306 1.78022342e-306 6.23058028e-307]]

Program: Create full array

To create a full NumPy array, you can use the [Link]() function.
The full() function in NumPy creates an array of a given shape and fills it with a
specified value. A full NumPy array is an array where all the elements have the

27
same predefined value. This is useful when you want to initialize an array with a
specific value.

Code:

import numpy as np

full_array_2d = [Link]((3, 4), 5)


print(full_array_2d)

Output:

[[5 5 5 5]
[5 5 5 5]
[5 5 5 5]]

Program: Convert tuple into Numpy array

import numpy as np

tuple1=(3,5,6)
print(tuple1)
L1=list(tuple1)
print(L1)
n1=[Link](L1)
print(n1)

Output:

(3, 5, 6)
[3, 5, 6]
[3 5 6]

Program: Array with 6 elements and count number of instances

# Importing the NumPy library


import numpy as np

# Creating two NumPy arrays


x = [Link]([10, -10, 10, -10, -10, 10])
y = [Link]([.85, .45, .9, .8, .12, .6])

# Displaying the original arrays

28
print("Original arrays:")
print(x)
print(y)

# Counting the number of instances where x equals 10 and y is greater than 0.5
result = [Link]((x == 10) & (y > 0.5))

# Printing the count of instances based on the conditions specified


print("\nNumber of instances of a value occurring in one array on the condition
of another array:")
print(result)

Output:

Original arrays:
[ 10 -10 10 -10 -10 10]
[0.85 0.45 0.9 0.8 0.12 0.6 ]
Number of instances of a value occurring in one array on the condition of
another array:
3

Program: Array with 2x3 elements and count number of sequences

# Importing NumPy library


import numpy as np
# Creating a NumPy array with specific values and data type (np.int32)
np_array = [Link]([[1, 2, 3], [2, 1, 2]], np.int32)
# Printing the original NumPy array and its type
print("Original NumPy array:")
print(np_array)
print("Type: ", type(np_array))
# Printing the sequence to search for in the NumPy array
print("Sequence: 1,2",)
# Counting the number of occurrences of the specified sequence "1, 2" in the
array representation
result = repr(np_array).count("1, 2")
# Printing the number of occurrences of the specified sequence in the array
print("Number of occurrences of the said sequence:", result)

Output:

Original NumPy array:


[[1 2 3]

29
[2 1 2]]
Type: <class '[Link]'>
Sequence: 1,2
Number of occurrences of the said sequence: 2

Program: Joining or merging arrays

# Importing the NumPy library


import numpy as np
# Creating three random arrays of shape (25, 25, 1)
arr1 = [Link](size=(2, 2))
arr2 = [Link](size=(2, 2))
arr3 = [Link](size=(2, 2))
# Printing the original arrays
print("Original arrays:")
print(arr1)
print(arr2)
print(arr3)
# Concatenating the arrays along the last axis (-1) using [Link]
result = [Link]((arr1, arr2, arr3), axis=-1)
# Printing the concatenated array
print("\nAfter concatenate:")
print(result)

Output:

Original arrays:
[[0.93635602 0.15110479]
[0.04689042 0.45330315]]
[[0.5648459 0.62626609]
[0.94375379 0.78884622]]
[[0.13426734 0.34890327]
[0.79431519 0.77580322]]

After concatenate:
[[0.93635602 0.15110479 0.5648459 0.62626609 0.13426734 0.34890327]
[0.04689042 0.45330315 0.94375379 0.78884622 0.79431519 0.77580322]]

Program: Combining Arrays

# Importing the NumPy library


import numpy as np
# Creating two lists

30
array1 = ['PHP', 'JS', 'C++']
array2 = ['Python', 'C#', 'NumPy']
# Printing the original arrays
print("Original arrays:")
print(array1)
print(array2)
# Combining the arrays using np.r_
result = np.r_[array1[:-1], [array1[-1] + array2[0]], array2[1:]]
# Printing the combined result
print("\nAfter Combining:")
print(result)

Output:

Original arrays:
['PHP', 'JS', 'C++']
['Python', 'C#', 'NumPy']

After Combining:
['PHP' 'JS' 'C++Python' 'C#' 'NumPy']

Program: Add border to Numpy array

# Importing the NumPy library with an alias 'np'


import numpy as np
# Creating a 3x3 NumPy array filled with ones
x = [Link]((3, 3))
# Printing the original array 'x'
print("Original array:")
print(x)
# Modifying the array 'x' to set 0s on the border and 1s inside the array using
the [Link] function
print("0 on the border and 1 inside in the array")
x = [Link](x, pad_width=1, mode='constant', constant_values=0)
# Printing the modified array 'x' with 0s on the border and 1s inside
print(x)

Output:

Original array:
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]

31
0 on the border and 1 inside in the array
[[0. 0. 0. 0. 0.]
[0. 1. 1. 1. 0.]
[0. 1. 1. 1. 0.]
[0. 1. 1. 1. 0.]
[0. 0. 0. 0. 0.]]

Program: Append values at the end of the Numpy array

# Importing the NumPy library with an alias 'np'


import numpy as np
# Creating a Python list
x = [10, 20, 30]
# Printing a message indicating the original array
print("Original array:")
# Printing the original array
print(x)
# Appending values to the end of the array using [Link]() and assigning t
he result back to 'x'
x = [Link](x, [[40, 50, 60], [70, 80, 90]])
# Printing a message indicating the array after appending values
print("After append values to the end of the array:")
# Printing the array after appending values
print(x)

Output:

Original array:
[10, 20, 30]
After append values to the end of the array:
[10 20 30 40 50 60 70 80 90]

Program: Convert python dictionary into Numpy array

# Importing necessary libraries


import numpy as np
from ast import literal_eval

# String representation of a dictionary


udict = """{"column0":{"a":1,"b":0.0,"c":0.0,"d":2.0},
"column1":{"a":3.0,"b":1,"c":0.0,"d":-1.0},
"column2":{"a":4,"b":1,"c":5.0,"d":-1.0},
"column3":{"a":3.0,"b":-1.0,"c":-1.0,"d":-1.0}

32
}"""

# Converting the string dictionary to a Python dictionary


t = literal_eval(udict)

# Printing the original dictionary and its type


print("\nOriginal dictionary:")
print(t)
print("Type: ", type(t))

# Creating a 2D NumPy array using dictionary comprehension


result_nparra = [Link]([[v[j] for j in ['a', 'b', 'c', 'd']] for k, v in [Link]()])

# Printing the generated NumPy array and its type


print("\nndarray:")
print(result_nparra)
print("Type: ", type(result_nparra))

Output:

Original dictionary:
{'column0': {'a': 1, 'b': 0.0, 'c': 0.0, 'd': 2.0}, 'column1': {'a': 3.0, 'b': 1, 'c': 0.0, 'd':
-1.0}, 'column2': {'a': 4, 'b': 1, 'c': 5.0, 'd': -1.0}, 'column3': {'a': 3.0, 'b': -
1.0, 'c': -1.0, 'd': -1.0}}
Type: <class 'dict'>

ndarray:
[[ 1. 0. 0. 2.]
[ 3. 1. 0. -1.]
[ 4. 1. 5. -1.]
[ 3. -1. -1. -1.]]
Type: <class '[Link]'>

Program: search index of one array in another array

# Importing NumPy library


import numpy as np
# Creating a NumPy array
np_array = [Link]([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
# Creating another NumPy array for searching
test_array = [Link]([4, 5, 6])
# Printing the original NumPy array and the array to be searched
print("Original NumPy array:")

33
print(np_array)
print("Searched array:")
print(test_array)
# Finding the index of the searched array in the original array
# Using [Link]() to locate rows where all elements match the test_array
result = [Link]((np_array == test_array).all(1))[0]
# Printing the index of the searched array in the original array
print("Index of the searched array in the original array:")
print(result)

Output:

Original NumPy array:


[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
Searched array:
[4 5 6]
Index of the searched array in the original array:
[1]

Result: Thus the Numpy example python codes are executed and the results are
verified.

34
[Link]:3a Working with Pandas Data frames
Date:

Aim: To write python code to understand Data frames using Pandas

Theory:

Pandas is a Python package providing fast, flexible, and expressive data


structures designed to make working with “relational” or “labelled” data both
easy and intuitive.
Data frame is a two-dimensional data structure

class [Link](data=None, index=None, columns=None, dtype=No


ne, copy=None )
Two-dimensional, size-mutable, potentially heterogeneous tabular data.
Data structure also contains labelled axes (rows and columns). Arithmetic
operations align on both row and column labels. Can be thought of as a dict-like
container for Series objects. The primary panda’s data structure.
Parameters:
datandarray (structured or homogeneous), Iterable, dict, or DataFrame
Dict can contain Series, arrays, constants, datac lass or list-like objects. If data
is a dict, column order follows insertion-order. If a dict contains Series which
have an index defined, it is aligned by its index. This alignment also occurs if
data is a Series or a Data Frame itself. Alignment is done on Series/DataFrame
inputs.
If data is a list of dicts, column order follows insertion-order.
indexIndex or array-like
Index to use for resulting frame. Will default to RangeIndex if no indexing
information part of input data and no index provided.
columnsIndex or array-like
Column labels to use for resulting frame when data does not have them,
defaulting to RangeIndex(0, 1, 2, …, n). If data contains column labels, will
perform column selection instead.
dtypedtype, default None
Only a single dtype is allowed.
Copy bool or None, default None
Copy data from inputs. For dict data, the default of None behaves
like copy=True. For DataFrame or 2d ndarray input, the default of None
behaves like copy=False. If data is a dict containing one or more Series
(possibly of different dtypes), copy=False will ensure that these inputs are not
copied.

35
Program1: Display csv file using pandas

import pandas as pd
df=pd.read_csv("D:\college misellaneous\Lab2023EvenSem\[Link]")
print(df)

Output:

5.1 3.5 1.4 0.2 Iris-setosa


0 4.9 3.0 1.4 0.2 Iris-setosa
1 4.7 3.2 1.3 0.2 Iris-setosa
2 4.6 3.1 1.5 0.2 Iris-setosa
3 5.0 3.6 1.4 0.2 Iris-setosa
4 5.4 3.9 1.7 0.4 Iris-setosa
.. ... ... ... ... ...
144 6.7 3.0 5.2 2.3 Iris-virginica
145 6.3 2.5 5.0 1.9 Iris-virginica
146 6.5 3.0 5.2 2.0 Iris-virginica
147 6.2 3.4 5.4 2.3 Iris-virginica
148 5.9 3.0 5.1 1.8 Iris-virginica

[149 rows x 5 columns]

Program: csv file- display specific rows, columns

import pandas as pd
df=pd.read_csv("D:\college misellaneous\Lab2023EvenSem\[Link]")
print(df)
#data[start:end]
#start is inclusive whereas end is exclusive
print(df[10:12])
# it will print the rows from 10 to 11.

# you can also save it in a variable for further use in analysis


sliced_data=df[10:12]
print(sliced_data)
specific_data=df[["5.1","Iris-setosa"]]
#data[["column_name1","column_name2","column_name3"]]

#now we will print the first 10 columns of the specific_data dataframe.


print(specific_data.head(10))
print([Link][df["Iris-setosa"] == "Iris-virginica"])

36
Output:

5.1 3.5 1.4 0.2 Iris-setosa


0 4.9 3.0 1.4 0.2 Iris-setosa
1 4.7 3.2 1.3 0.2 Iris-setosa
2 4.6 3.1 1.5 0.2 Iris-setosa
3 5.0 3.6 1.4 0.2 Iris-setosa
4 5.4 3.9 1.7 0.4 Iris-setosa
.. ... ... ... ... ...
144 6.7 3.0 5.2 2.3 Iris-virginica
145 6.3 2.5 5.0 1.9 Iris-virginica
146 6.5 3.0 5.2 2.0 Iris-virginica
147 6.2 3.4 5.4 2.3 Iris-virginica
148 5.9 3.0 5.1 1.8 Iris-virginica

[149 rows x 5 columns]


5.1 3.5 1.4 0.2 Iris-setosa
10 4.8 3.4 1.6 0.2 Iris-setosa
11 4.8 3.0 1.4 0.1 Iris-setosa
5.1 3.5 1.4 0.2 Iris-setosa
10 4.8 3.4 1.6 0.2 Iris-setosa
11 4.8 3.0 1.4 0.1 Iris-setosa
5.1 Iris-setosa
0 4.9 Iris-setosa
1 4.7 Iris-setosa
2 4.6 Iris-setosa
3 5.0 Iris-setosa
4 5.4 Iris-setosa
5 4.6 Iris-setosa
6 5.0 Iris-setosa
7 4.4 Iris-setosa
8 4.9 Iris-setosa
9 5.4 Iris-setosa
5.1 3.5 1.4 0.2 Iris-setosa
99 6.3 3.3 6.0 2.5 Iris-virginica
100 5.8 2.7 5.1 1.9 Iris-virginica
101 7.1 3.0 5.9 2.1 Iris-virginica
102 6.3 2.9 5.6 1.8 Iris-virginica
103 6.5 3.0 5.8 2.2 Iris-virginica
104 7.6 3.0 6.6 2.1 Iris-virginica
105 4.9 2.5 4.5 1.7 Iris-virginica
106 7.3 2.9 6.3 1.8 Iris-virginica
107 6.7 2.5 5.8 1.8 Iris-virginica

37
108 7.2 3.6 6.1 2.5 Iris-virginica
109 6.5 3.2 5.1 2.0 Iris-virginica
110 6.4 2.7 5.3 1.9 Iris-virginica
111 6.8 3.0 5.5 2.1 Iris-virginica
112 5.7 2.5 5.0 2.0 Iris-virginica
113 5.8 2.8 5.1 2.4 Iris-virginica
114 6.4 3.2 5.3 2.3 Iris-virginica
115 6.5 3.0 5.5 1.8 Iris-virginica
116 7.7 3.8 6.7 2.2 Iris-virginica
117 7.7 2.6 6.9 2.3 Iris-virginica
118 6.0 2.2 5.0 1.5 Iris-virginica
119 6.9 3.2 5.7 2.3 Iris-virginica
120 5.6 2.8 4.9 2.0 Iris-virginica
121 7.7 2.8 6.7 2.0 Iris-virginica
122 6.3 2.7 4.9 1.8 Iris-virginica
123 6.7 3.3 5.7 2.1 Iris-virginica
124 7.2 3.2 6.0 1.8 Iris-virginica
125 6.2 2.8 4.8 1.8 Iris-virginica
126 6.1 3.0 4.9 1.8 Iris-virginica
127 6.4 2.8 5.6 2.1 Iris-virginica
128 7.2 3.0 5.8 1.6 Iris-virginica
129 7.4 2.8 6.1 1.9 Iris-virginica
130 7.9 3.8 6.4 2.0 Iris-virginica
131 6.4 2.8 5.6 2.2 Iris-virginica
132 6.3 2.8 5.1 1.5 Iris-virginica
133 6.1 2.6 5.6 1.4 Iris-virginica
134 7.7 3.0 6.1 2.3 Iris-virginica
135 6.3 3.4 5.6 2.4 Iris-virginica
136 6.4 3.1 5.5 1.8 Iris-virginica
137 6.0 3.0 4.8 1.8 Iris-virginica
138 6.9 3.1 5.4 2.1 Iris-virginica
139 6.7 3.1 5.6 2.4 Iris-virginica
140 6.9 3.1 5.1 2.3 Iris-virginica
141 5.8 2.7 5.1 1.9 Iris-virginica
142 6.8 3.2 5.9 2.3 Iris-virginica
143 6.7 3.3 5.7 2.5 Iris-virginica
144 6.7 3.0 5.2 2.3 Iris-virginica
145 6.3 2.5 5.0 1.9 Iris-virginica
146 6.5 3.0 5.2 2.0 Iris-virginica
147 6.2 3.4 5.4 2.3 Iris-virginica
148 5.9 3.0 5.1 1.8 Iris-virginica

38
Program2:

import pandas as pd
import numpy as np
info = [Link](['P','a','n','d','a','s'])
a = [Link](info)
print(a)

Output:

0 P
1 a
2 n
3 d
4 a
5 s
dtype: object

Program3: Data frame using pandas

import pandas as pd
# a list of strings
x = ['Python', 'Pandas']
# Calling DataFrame constructor on list
df = [Link](x)
print(df)

Output:

0
0 Python
1 Pandas

Program4: Constructing data frame from a dictionary.

import pandas as pd
d = {'col1': [1, 2], 'col2': [3, 4]}
df = [Link](data=d)
print(df)
print([Link])

Output:

39
col1 col2
0 1 3
1 2 4
col1 int64
col2 int64
dtype: object

Program5: Constructing Data Frame from a dictionary including Series:

A Pandas Series is like a column in a table. It is a one-dimensional array holding


data of any type

import pandas as pd
d = {'col1': [0, 1, 2, 3], 'col2': [Link]([12, 13], index=[2, 3])}
d1=[Link](data=d, index=[0, 1, 2, 3])
print(d1)
print([Link])

Output:

col1 col2
0 0 NaN
1 1 NaN
2 2 12.0
3 3 13.0
col1 int64
col2 float64
dtype: object

Program6: Constructing Data Frame from numpy Nd array:


import pandas as pd
import numpy as np
df2 = [Link]([Link]([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
columns=['a', 'b', 'c'])
print(df2)

Output:

a b c
0 1 2 3
1 4 5 6
2 7 8 9

40
Program7: Constructing DataFrame from a numpy ndarray that has labelled
columns:
import pandas as pd
import numpy as np
data = [Link]([(1, 2, 3), (4, 5, 6), (7, 8, 9)],
dtype=[("a", "i4"), ("b", "i4"), ("c", "i8")])
df3 = [Link](data, columns=['c', 'a'])
print(df3)
print([Link])

Output:

c a
0 3 1
1 6 4
2 9 7
c int64
a int32
dtype: object

Program8: Constructing DataFrame from dataclass:


Theory: In Python, a data class is a class that is designed to only hold data
values. They aren't different from regular classes, but they usually don't have
any other methods. They are typically used to store information that will be
passed between different parts of a program or a system.
Program:
import pandas as pd
from dataclasses import make_dataclass
Point = make_dataclass("Point", [("x", int), ("y", int)])
p1=[Link]([Point(0, 0), Point(0, 3), Point(2, 3)])
print(p1)

Output:

xy
0 0 0
1 0 3
2 2 3

41
Program9: Constructing Data Frame from Series/Data Frame:
import pandas as pd
ser = [Link]([1, 2, 3], index=["a", "b", "c"])
df = [Link](data=ser, index=["a", "c"])
print(df)

Output:

0
a 1
c 3

Program10: Constructing data frame

import pandas as pd
df1 = [Link]([1, 2, 3], index=["a", "b", "c"], columns=["x"])
df2 = [Link](data=df1, index=["a", "c"])
print(df2)
Output:

x
a 1
c 3

Program: Pandas program to get the powers of an array values element-wise

# Importing the NumPy library


import numpy as np
# Creating an array from 0 to 6
x = [Link](7)
# Displaying the original array
print("Original array:")
print(x)
# Raising the elements of the array to the power of 3 and displaying the result
print("First array elements raised to powers from second array, element-wise:")
print([Link](x, 3))

42
Output:

Original array:
[0 1 2 3 4 5 6]
First array elements raised to powers from second array, element-wise:
[ 0 1 8 27 64 125 216]

Program: Data frame using dictionary

import pandas as pd
# dictionary of lists
name=['aparna', 'pankaj', 'sudhir', 'Geeku']
degree=['MBA','BCA', '[Link]', 'MBA']
score=[90, 40, 80, 98]
dict = {'name':name,
'degree':degree ,
'score':score}
df = [Link](dict,index=['Rollno1','Rollno2','Rollno3','Rollno4'])
print(df)

Output:

name degree score


Rollno1 aparna MBA 90
Rollno2 pankaj BCA 40
Rollno3 sudhir [Link] 80
Rollno4 Geeku MBA 98

Program: Data frame – drop row by index

# import pandas library


import pandas as pd
# dictionary with list object in values
details = {
'Name': ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'],
'Age': [23, 21, 22, 21],
'University': ['BHU', 'JNU', 'DU', 'BHU'],
}
# creating a Dataframe object
df = [Link](details, columns=['Name', 'Age', 'University'],
index=['a', 'b', 'c', 'd'])
print(df)

43
# return a new dataframe by dropping a
# row 'c' from dataframe
update_df = [Link]('c')
print(update_df)

Output:

Name Age University


a Ankit 23 BHU
b Aishwarya 21 JNU
c Shaurya 22 DU
d Shivangi 21 BHU
Name Age University
a Ankit 23 BHU
b Aishwarya 21 JNU
d Shivangi 21 BHU

Result: The python codes to understand data frames using pandas are executed
and the results are verified.

44
Ex.No3b Examples using Pandas
Date:

Aim: To write sample Python codes using Pandas

Program: pow() for data frame

import pandas as pd

df = [Link]({'X':[78,85,96,80,86], 'Y':[84,94,89,83,86],'Z':[86,97,96,72,
83]});
print(df)
print([Link](2))

Output:

X Y Z
0 78 84 86
1 85 94 97
2 96 89 96
3 80 83 72
4 86 86 83
X Y Z
0 6084 7056 7396
1 7225 8836 9409
2 9216 7921 9216
3 6400 6889 5184
4 7396 7396 6889

Program: dictionary to data frame, select specific rows, colums

import pandas as pd
import numpy as np
#dictionary exam_data
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael'
, 'Matthew', 'Laura', 'Kevin', 'Jonas'],'score': [12.5, 9, 16.5, [Link], 9, 20, 14.5, n
[Link], 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'ye
s', 'no', 'no', 'yes']}
print(exam_data)
#convert dictionary into data frame
df_in = [Link](data=exam_data)
print(df_in)

45
print(df_in["score"])
print(df_in["score"].iloc[0])

Output:

{'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', '


Laura', 'Kevin', 'Jonas'], 'score': [12.5, 9, 16.5, nan, 9, 20, 14.5, nan, 8, 19], 'atte
mpts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1], 'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', '
no', 'no', 'yes']}
name score attempts qualify
0 Anastasia 12.5 1 yes
1 Dima 9.0 3 no
2 Katherine 16.5 2 yes
3 James NaN 3 no
4 Emily 9.0 2 no
5 Michael 20.0 3 yes
6 Matthew 14.5 1 yes
7 Laura NaN 1 no
8 Kevin 8.0 2 no
9 Jonas 19.0 1 yes
0 12.5
1 9.0
2 16.5
3 NaN
4 9.0
5 20.0
6 14.5
7 NaN
8 8.0
9 19.0
Name: score, dtype: float64
12.5

Program: Count the number of rows and columns of data frame

import pandas as pd
import numpy as np
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael
', 'Matthew', 'Laura', 'Kevin', 'Jonas'],
'score': [12.5, 9, 16.5, [Link], 9, 20, 14.5, [Link], 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

46
df = [Link](exam_data , index=labels)
total_rows=len([Link][0])
total_cols=len([Link][1])
print("Number of Rows: "+str(total_rows))
print("Number of Columns: "+str(total_cols))

Output:

Number of Rows: 10
Number of Columns: 4

Program: Group by columns

import pandas as pd
df = [Link]( {'col1':['C1','C1','C2','C2','C2','C3','C2'],
'col2':[1,2,3,3,4,6,5]})
print("Original DataFrame")
print(df)
df = [Link]('col1')['col2'].apply(list)
print("\nGroup on the col1:")
print(df)

Original DataFrame
col1 col2
0 C1 1
1 C1 2
2 C2 3
3 C2 3
4 C2 4
5 C3 6
6 C2 5

Group on the col1:


col1
C1 [1, 2]
C2 [3, 3, 4, 5]
C3 [6]
Name: col2, dtype: object

Program: To check whether a column is present in the data frame or not

import pandas as pd
d = {'col1': [1, 2, 3, 4, 7], 'col2': [4, 5, 6, 9, 5], 'col3': [7, 8, 12, 1, 11]}

47
df = [Link](data=d)
print("Original DataFrame")
print(df)
if 'col4' in [Link]:
print("Col4 is present in DataFrame.")
else:
print("Col4 is not present in DataFrame.")
if 'col1' in [Link]:
print("Col1 is present in DataFrame.")
else:
print("Col1 is not present in DataFrame.")

Output:

Original DataFrame
col1 col2 col3
0 1 4 7
1 2 5 8
2 3 6 12
3 4 9 1
4 7 5 11
Col4 is not present in DataFrame.
Col1 is present in DataFrame.

Program: Dictionary to data frame with labels

import pandas as pd
import numpy as np

exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael


', 'Matthew', 'Laura', 'Kevin', 'Jonas'],
'score': [12.5, 9, 16.5, [Link], 9, 20, 14.5, [Link], 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df = [Link](exam_data , index=labels)
print(df)

Output:

name score attempts qualify


a Anastasia 12.5 1 yes
b Dima 9.0 3 no

48
c Katherine 16.5 2 yes
d James NaN 3 no
e Emily 9.0 2 no
f Michael 20.0 3 yes
g Matthew 14.5 1 yes
h Laura NaN 1 no
i Kevin 8.0 2 no
j Jonas 19.0 1 yes

Program: Display data with number of attempts > 2

import pandas as pd
import numpy as np

exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily',


'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'],
'score': [12.5, 9, 16.5, [Link], 9, 20, 14.5, [Link], 8, 19],
'attempts' : [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

df = [Link](exam_data , index=labels)
print("Number of attempts in the examination is greater than 2:")
print(df[df['attempts'] > 2])

Output:

Number of attempts in the examination is greater than 2:


name score attempts qualify
b Dima 9.0 3 no
d James NaN 3 no
f Michael 20.0 3 yes

Program: Display first 3 rows of the data frame

import pandas as pd
import numpy as np

exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael


', 'Matthew', 'Laura', 'Kevin', 'Jonas'],
'score': [12.5, 9, 16.5, [Link], 9, 20, 14.5, [Link], 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}

49
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df = [Link](exam_data , index=labels)
print("First three rows of the data frame:")
print([Link][:3])

Output:

First three rows of the data frame:


name score attempts qualify
a Anastasia 12.5 1 yes
b Dima 9.0 3 no
c Katherine 16.5 2 yes

Program: Display rows with missing score

import pandas as pd
import numpy as np
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael
', 'Matthew', 'Laura', 'Kevin', 'Jonas'],
'score': [12.5, 9, 16.5, [Link], 9, 20, 14.5, [Link], 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df = [Link](exam_data , index=labels)
print("Rows where score is missing:")
print(df[df['score'].isnull()])

Output:

Rows where score is missing:


name score attempts qualify
d James NaN 3 no
h Laura NaN 1 no

Program: Numeric representation of array

import pandas as pd
df = [Link]({
'Name': ['Alberto Franco','Gino Mcneill','Ryan Parkes', 'Eesha Hinton', 'Gino
Mcneill'],
'Date_Of_Birth ':
['17/05/2002','16/02/1999','25/09/1998','11/05/2002','15/09/1997'],
'Age': [18.5, 21.2, 22.5, 22, 23]

50
})
print("Original DataFrame:")
print(df)
label1, unique1 = [Link](df['Name'])
print("\nNumeric representation of an array by identifying distinct values:")
print(label1)
print(unique1)

Output:

Original DataFrame:
Name Date_Of_Birth Age
0 Alberto Franco 17/05/2002 18.5
1 Gino Mcneill 16/02/1999 21.2
2 Ryan Parkes 25/09/1998 22.5
3 Eesha Hinton 11/05/2002 22.0
4 Gino Mcneill 15/09/1997 23.0

Numeric representation of an array by identifying distinct values:


[0 1 2 3 1]
Index(['Alberto Franco', 'Gino Mcneill', 'Ryan Parkes', 'Eesha Hinton'], dtype='o
bject')

Program: Check Inequality of 2 data frames

import pandas as pd
df1 = [Link]({'W':[68,75,86,80,None],'X':[78,85,None,80,86], 'Y':[84,94
,89,83,86],'Z':[86,97,96,72,83]});
df2 = [Link]({'W':[78,75,86,80,None],'X':[78,85,96,80,76], 'Y':[84,84,8
9,83,86],'Z':[86,97,96,72,83]});
print("Original DataFrames:")
print(df1)
print(df2)
print("\nCheck for inequality of the said dataframes:")
print([Link](df2))

Output:

Original DataFrames:
W X Y Z
0 68.0 78.0 84 86
1 75.0 85.0 94 97
2 86.0 NaN 89 96

51
3 80.0 80.0 83 72
4 NaN 86.0 86 83
W X Y Z
0 78.0 78 84 86
1 75.0 85 84 97
2 86.0 96 89 96
3 80.0 80 83 72
4 NaN 76 86 83

Check for inequality of the said dataframes:


W X Y Z
0 True False False False
1 False False True False
2 False True False False
3 False False False False
4 True True False False

Program: Display first n records of the frame

import pandas as pd
d = {'col1': [1, 2, 3, 4, 7, 11], 'col2': [4, 5, 6, 9, 5, 0], 'col3': [7, 5, 8, 12, 1,11]}
df = [Link](data=d)
print("Original DataFrame")
print(df)
print("\nFirst 3 rows of the said DataFrame':")
df1 = [Link](3)
print(df1)

Output:

Original DataFrame
col1 col2 col3
0 1 4 7
1 2 5 5
2 3 6 8
3 4 9 12
4 7 5 1
5 11 0 11

First 3 rows of the said DataFrame':


col1 col2 col3
0 1 4 7

52
1 2 5 5
2 3 6 8

Program: Select all columns except one column in data frame

import pandas as pd
d = {'col1': [1, 2, 3, 4, 7], 'col2': [4, 5, 6, 9, 5], 'col3': [7, 8, 12, 1, 11]}
df = [Link](data=d)
print("Original DataFrame")
print(df)
print("\nAll columns except 'col3':")
df = [Link][:, [Link] != 'col3']
print(df)

Output:

Original DataFrame
col1 col2 col3
0 1 4 7
1 2 5 8
2 3 6 12
3 4 9 1
4 7 5 11

All columns except 'col3':


col1 col2
0 1 4
1 2 5
2 3 6
3 4 9
4 7 5
Program: append (add at end ), slice, delete rows in data frame using pandas
import pandas as pd
from [Link] import randint
# append rows
df = [Link](columns=['lib', 'qty1', 'qty2'])
for i in range(5):
[Link][i] = ['name' + str(i)] + list(randint(10, size=2))
print("After Append")
print(df)
[Link][5]=[20,30,40]
print("After Append")
print(df)

53
#slicing rows
print("Slicing")
df1=[Link][1:3]
print(df1)
print([Link][[1,4]])
#delete rows
print("delete rows")
df2=[Link]([1,2])
print(df2)

Output:
After Append
lib qty1 qty2
0 name0 6 2
1 name1 9 2
2 name2 6 5
3 name3 8 3
4 name4 5 9
After Append
lib qty1 qty2
0 name0 6 2
1 name1 9 2
2 name2 6 5
3 name3 8 3
4 name4 5 9
5 20 30 40
Slicing
lib qty1 qty2
1 name1 9 2
2 name2 6 5
3 name3 8 3
lib qty1 qty2
1 name1 9 2
4 name4 5 9
delete rows
lib qty1 qty2
0 name0 6 2
3 name3 8 3
4 name4 5 9
5 20 30 40

Result: Thus the example python codes using Pandas are executed and the
results are verified.

54
[Link] Basic plots using matplotlib
Date:

Aim: To draw plot using matplotlib in python

Program1: Basic plot

# importing the required module


import [Link] as plt
# x axis values
x = [1,2,3]
# corresponding y axis values
y = [2,4,1]
# plotting the points
[Link](x, y)
# naming the x axis
[Link]('x - axis')
# naming the y axis
[Link]('y - axis')
# giving a title to my graph
[Link]('My first graph!')
# function to show the plot
[Link]() # to save plot, at the bottom of the plot save option will be present

Output:

55
Program2: simple graph with numpy values – r: red line

import [Link] as plt


import numpy as np

xpoints = [Link]([1, 8])


ypoints = [Link]([3, 10])
[Link](xpoints, ypoints,'r')
[Link]()

Output:

Program3: Draw only 2 points (1,3) and (8,10)- attribute-o

import [Link] as plt


import numpy as np

xpoints = [Link]([1, 8])


ypoints = [Link]([3, 10])
[Link](xpoints, ypoints, 'o')
[Link]()

56
Output:

Program4: Draw a line in a diagram from position (1, 3) to (2, 8) then to (6, 1)
and finally to position (8, 10):

import [Link] as plt


import numpy as np

xpoints = [Link]([1, 2, 6, 8])


ypoints = [Link]([3, 8, 1, 10])

[Link](xpoints, ypoints)
[Link]()

57
Output:

Program5: Default X-Points -If we do not specify the points on the x-axis, they
will get the default values 0, 1, 2, 3 etc., depending on the length of the y-points.

import [Link] as plt


import numpy as np

ypoints = [Link]([3, 8, 1, 10, 5, 7])

[Link](ypoints)
[Link]()

Output:

58
Program 6: Draw only points with + symbol

import [Link] as plt


import numpy as np

xpoints = [Link]([1, 2, 6, 8])


ypoints = [Link]([3, 8, 1, 10])
[Link](xpoints, ypoints,'r+')
[Link]()

Output:

Program7: control appearances

import [Link] as plt


import numpy as np

x = [Link]([1, 2, 6, 8])
y = [Link]([3, 8, 1, 10])
[Link](x, y, color='green', marker='o', linestyle='dashed',linewidth=2, markersiz
e=12)
[Link]()

59
Output:

Program8: Multiple plots

import [Link] as plt


import numpy as np

x1 = [Link]([1, 2, 6, 8])
y1 = [Link]([3, 8, 1, 10])
[Link](x1, y1, color='green', marker='o', linestyle='dashed',linewidth=2, marker
size=12)
x2 = [Link]([11, 12, 16, 18])
y2 = [Link]([3, 8, 1, 10])
[Link](x2, y2, color='brown', marker='o', linestyle='dashed',linewidth=2, marke
rsize=12)
[Link]()

Output:

60
Program9: x- 1D, y- 2D

import numpy as np
import [Link] as plt
x = [1, 2, 3]
y = [Link]([[1, 2], [3, 4], [5, 6]])
[Link](x, y)
[Link]()

Output:

61
Program10: Sin wave

from matplotlib import pyplot as plt


import numpy as np
import math #needed for definition of pi
x = [Link](0, [Link]*2, 0.05)
y = [Link](x)
[Link](x,y)
[Link]("angle")
[Link]("sine")
[Link]('sine wave')
[Link]()

Output:

Program11:
matplotlib basic plot
Program6: To save plot in default location

import [Link] as plt


import numpy as np
# Data for plotting

62
t = [Link](0.0, 2.0,0.01)
s = 1 + [Link](2 * [Link] * t)
fig,ax = [Link]()
[Link](t, s)
[Link](xlabel='time (s)', ylabel='voltage (mV)',
title='first plot')
[Link]()
[Link]("[Link]")
[Link]()

Output:

Result: Thus the basic plot is drawn using matplotlib is executed and the result
is verified.

63
[Link]:5a Statistical and Probability measures - Frequency
Distributions
Date:

Aim: To write python code for frequency distributions


Frequency : The number of times a value occurs in a data.
Program1: head() method – returns first 5 rows of the data set

import pandas as pd
import numpy as np
# reading csv file as pandas dataframe
data = pd.read_csv('d:\college misellaneous\Lab2023EvenSem\[Link]')
[Link]()

Output:

5.1 3.5 1.4 0.2 Iris-setosa


0 4.9 3.0 1.4 0.2 Iris-setosa
1 4.7 3.2 1.3 0.2 Iris-setosa
2 4.6 3.1 1.5 0.2 Iris-setosa
3 5.0 3.6 1.4 0.2 Iris-setosa
4 5.4 3.9 1.7 0.4 Iris-setosa

Program2: value_counts() – returns the total elements of each specific value

import pandas as pd
import numpy as np
# reading csv file as pandas dataframe
data = pd.read_csv('d:\college misellaneous\Lab2023EvenSem\[Link]')
df=data['Iris-setosa'].value_counts()
print(df)

Output:

Iris-setosa
Iris-versicolor 50
Iris-virginica 50
Iris-setosa 49
Name: count, dtype: int64

64
Program3: Frequency table using crosstab

import pandas as pd
import numpy as np
# reading csv file as pandas dataframe
data = pd.read_csv('d:\college misellaneous\Lab2023EvenSem\[Link]')
freq_table=[Link](data['Iris-setosa'], 'no_of_Iris-setosa')
freq_table

Output:

Iris-setosa
Iris-versicolor 50
Iris-virginica 50
Iris-setosa 49
Name: count, dtype: int64

Program4: Frequency table with sum/len

import pandas as pd
import numpy as np
# reading csv file as pandas dataframe
data = pd.read_csv('d:\college misellaneous\Lab2023EvenSem\[Link]')
freq_table=[Link](data['Iris-setosa'], 'no_of_Iris-setosa')
print(freq_table)
freq_table/len(data)

Output:

col_0 no_of_Iris-setosa
Iris-setosa
Iris-setosa 49
Iris-versicolor 50
Iris-virginica 50
col_0 no_of_Iris-setosa
Iris-setosa
Iris-setosa 0.328859
Iris-versicolor 0.335570
Iris-virginica 0.335570

65
Program5: Two-way Frequency table- frequency table – relationship between 2
different variables

import pandas as pd
import numpy as np
# reading csv file as pandas dataframe
data = pd.read_csv('d:\college misellaneous\Lab2023EvenSem\[Link]')

freq_table=[Link](data['Iris-setosa'], data['5.1'])
print(freq_table)

Output:

5.1 4.3 4.4 4.5 4.6 4.7 4.8 ... 7.2 7.3 7.4 7.6 7.7 7.9
Iris-setosa ...
Iris-setosa 1 3 1 4 2 5 ... 0 0 00 00
Iris-versicolor 0 0 0 0 0 0 ... 0 0 0 0 0 0
Iris-virginica 0 0 0 0 0 0 ... 3 1 1 1 4 1

[3 rows x 35 columns]

Result: Thus, the python codes for frequency distributions are executed and the
results are verified.

66
[Link]:5b Statistical and Probability measures - Mean, Mode, Standard
Deviation
Date:

Aim: To write Python code for Mean, Mode and standard Deviation.

Program1: mean - [Link]() calculates the arithmetic mean, which is the


sum of elements divided by their count.

import statistics
L = [1, 3, 8, 15]
print([Link](L))

Output:

6.75

Program2: mean using sum() and len()

L = [1, 3, 8, 15]
print(sum(L) / len(L))

Output:

6.75

Program3: median(), low(), high() - If the number of data points is odd, all three
functions return the middle value directly.

import statistics
L = [3, 1, 8]
print([Link](L))
print(statistics.median_low(L))
print(statistics.median_high(L))

Output:

3
3
3

Program4: median(), low(), high() - If the number of data points is


even, [Link]() returns the arithmetic mean of the two middle

67
values, statistics.median_low() returns the smaller value,
and statistics.median_high() returns the larger value.

import statistics
L = [3, 1, 8, 15]
print([Link](L))
print(statistics.median_low(L))
print(statistics.median_high(L))

Output:

5.5
3
8

Program5: mode- most frequently occurring value-


[Link]() and [Link]() allow you to find the mode, which is
the most frequently occurring value.

import statistics
L = [3, 2, 3, 2, 1, 2]
print([Link](L))
print([Link](L))

Output:

2
[2]

Program6: mode- most frequently occurring value- If multiple modes


exist, [Link]() returns the first one.

import statistics
L = [3, 2, 3, 2, 1, 2, 3]
print([Link](L))
print([Link](L))

Output:

3
[3, 2]

68
Program7: [Link]() computes the population variance - The
population variance σ2 is calculated as follows for a population consisting of n
data points with mean µ.

69
import statistics
L= [10, 1, 3, 7, 1]
print([Link](L))

Output:

12.64

Program8: Variance – by entering mean

By default, the mean is automatically calculated. However, the optional second


argument, mu, allows you to specify the mean value directly. For example, if
you've already calculated the mean, providing it through mu can help avoid
recalculations.

import statistics
L= [10, 1, 3, 7, 1]
mu = [Link](L)
print([Link](L, mu))

Output:

12.64

Program9: variance using sun and len

L= [10, 1, 3, 7, 1]
print(sum((x - sum(L) / len(L)) ** 2 for x in L) / len(L))

Output:

12.64

Program10: Sample variance

import statistics
L= [10, 1, 3, 7, 1]
print([Link](L))

Output:

15.8

70
Sample variance is given by

Program11: Calculating mean and the variance to avoid recalculations

import statistics
L= [10, 1, 3, 7, 1]
xbar = [Link](L)
print([Link](L, xbar))

Output:

15.8

Program12: Variance using sum() and len()

import statistics
L= [10, 1, 3, 7, 1]
print(sum((x - sum(L) / len(L)) ** 2 for x in L) / (len(L) - 1))

Output:

15.8

Program13: Population standard deviation


The population standard deviation is a parameter, which is a fixed value
calculated from every individual in the population. A sample standard deviation
is a statistic. This means that it is calculated from only some of the individuals
in a population.

import statistics
L= [10, 1, 3, 7, 1]
print([Link](L))

Output:

3.5552777669262356

71
Program14: Population standard deviation -The population standard deviation is
the square root of the population variance.
The population standard deviation is the square root of the population variance.

import math
import statistics
L= [10, 1, 3, 7, 1]
print([Link]([Link](L)))

Output:

3.5552777669262356

Program15: [Link]() returns the sample standard deviation.


import statistics
L= [10, 1, 3, 7, 1]
print([Link](L))

Output:

3.9749213828703582

Program16: The sample standard deviation is the square root of the sample
variance.

import statistics
import math
L= [10, 1, 3, 7, 1]
print([Link]([Link](L)))

Output:

3.9749213828703582

Result: Thus the python code to calculate variance, standard deviation are
executed and the results are verified.

72
[Link]:5c Statistical and Probability measures - Variability
Date:

Aim: To write python code to calculate variability

Variability refers to variance, standard deviation, range and


IQR (Inter Quartile Range)

Program1: Range

L= [10, 1, 3, 7, 1]
range1=(max(L)-min(L))
print(range1)

Output:
9

Program2: IQR
The interquartile range, often denoted “IQR”, is a way to measure the spread
of the middle 50% of a dataset. It is calculated as the difference between the
first quartile* (the 25th percentile) and the third quartile (the 75th percentile) of
a dataset. It is calculated using the NumPy. Percentile() function.

import numpy as np
#define array of data
data = [Link]([14, 19, 20, 22, 24, 26, 27, 30, 30, 31, 36, 38, 44, 47])
#calculate interquartile range
q3, q1 = [Link](data, [75 ,25])
iqr = q3 - q1
#display interquartile range
print(iqr)

Output:
12.25

Result: Thus the python code to calculate range, IQR are executed and the
results are verified.

73
[Link]:5d Statistical and Probability measures- Normal curves
Date:

Aim: To Python code to generate normal curves.

Probability Density is given as follows

Modules Needed
 Matplotlib is python’s data visualization library which is widely used for
the purpose of data visualization.
 Numpy is a general-purpose array-processing package. It provides a high-
performance multidimensional array object, and tools for working with
these arrays. It is the fundamental package for scientific computing with
Python.
 Scipy is a python library that is useful in solving many mathematical
equations and algorithms.
 Statistics module provides functions for calculating mathematical
statistics of numeric data.
Functions used
 To calculate mean of the data
Syntax:
mean(data)
 To calculate standard deviation of the data
Syntax:
stdev(data)
 To calculate normal probability density of the data [Link] is used, it
refers to the normal probability density function which is a module in
scipy library that uses the above probability density function to calculate
the value.
Syntax:
[Link](Data, loc, scale)
Here, loc parameter is also known as the mean and the scale parameter is also
known as standard deviation.
Approach
 Import module
 Create data
 Calculate mean and deviation

74
 Calculate normal probability density
 Plot using above calculated values
 Display plot

Program:

import numpy as np
import [Link] as plt
from [Link] import norm
import statistics

# Plot between -10 and 10 with .001 steps.


x_axis = [Link](-20, 20, 0.01)

# Calculating mean and standard deviation


mean = [Link](x_axis)
sd = [Link](x_axis)

[Link](x_axis, [Link](x_axis, mean, sd))


[Link]()

Output:

Result: Thus, the Python code to generate normal curves is executed and the
result is verified
75
[Link]: 5e Statistical and Probability measures- scatter plots
Date:

Aim: To write Python code to generate scatter plots

Program1: Reading data

import pandas as pd
con = pd.read_csv('D:\college misellaneous\Lab2023EvenSem\[Link]')
con

Output:

5.1 3.5 1.4 0.2 Iris-setosa


0 4.9 3.0 1.4 0.2 Iris-setosa
1 4.7 3.2 1.3 0.2 Iris-setosa
2 4.6 3.1 1.5 0.2 Iris-setosa
3 5.0 3.6 1.4 0.2 Iris-setosa
4 5.4 3.9 1.7 0.4 Iris-setosa
.. ... ... ... ... ...
144 6.7 3.0 5.2 2.3 Iris-virginica
145 6.3 2.5 5.0 1.9 Iris-virginica
146 6.5 3.0 5.2 2.0 Iris-virginica
147 6.2 3.4 5.4 2.3 Iris-virginica
148 5.9 3.0 5.1 1.8 Iris-virginica

[149 rows x 5 columns]

Program2: Changing column headings

import pandas as pd
con = pd.read_csv('D:\college misellaneous\Lab2023EvenSem\[Link]')
[Link](columns={'5.1': 'Col1', '3.5': "Col2",
'1.4': 'Col3', '0.2': 'Col4',
'Iris-setosa': 'Iris-variety'}, inplace=True)
[Link]()

Output:

Col1 Col2 Col3 Col4 Iris-variety


0 4.9 3.0 1.4 0.2 Iris-setosa
1 4.7 3.2 1.3 0.2 Iris-setosa
2 4.6 3.1 1.5 0.2 Iris-setosa

76
3 5.0 3.6 1.4 0.2 Iris-setosa
4 5.4 3.9 1.7 0.4 Iris-setosa

inplace=True means the table will be modified without returning a copy of the
data or the original data.

Program3: List the column names with inplace=True

import pandas as pd
con = pd.read_csv('D:\college misellaneous\Lab2023EvenSem\[Link]')
[Link](columns={'5.1': 'Col1', '3.5': "Col2",
'1.4': 'Col3', '0.2': 'Col4',
'Iris-setosa': 'Iris-variety'},inplace=True)
print(list([Link]))

Output:

['5.1', '3.5', '1.4', '0.2', 'Iris-setosa']

Program4: List the column names with inplace=False

import pandas as pd
con = pd.read_csv('D:\college misellaneous\Lab2023EvenSem\[Link]')
[Link](columns={'5.1': 'Col1', '3.5': "Col2",
'1.4': 'Col3', '0.2': 'Col4',
'Iris-setosa': 'Iris-variety'},inplace=False)
print(list([Link]))

Output:

['5.1', '3.5', '1.4', '0.2', 'Iris-setosa']

Program5: Scatter plot

To create a scatterplot, we must do four things:


1. Load the seaborn library
2. Specify the source data frame
3. Set the x axis, which is generally the name of a predictor/independent
variable
4. Set the y axis, which is generally the name of a response/dependent
variable

Program using Jupyter

77
import pandas as pd
import seaborn as sns

con = pd.read_csv('D:\college misellaneous\Lab2023EvenSem\[Link]')


[Link](columns={'5.1': 'Col1', '3.5': "Col2",
'1.4': 'Col3', '0.2': 'Col4',
'Iris-setosa': 'Iris-variety'},inplace=True)
[Link](x="Col1", y="Iris-variety", data=con);
ax.set_title("Column1 vs. Iris Variety")
ax.set_xlabel("Column1");
ax.set_ylabel("Iris-Variety");
Program using Pyscripter
import pandas as pd
import [Link] as plt
con = pd.read_csv('D:\college misellaneous\Lab2023EvenSem\[Link]')
[Link](columns={'5.1': 'Col1', '3.5': "Col2",
'1.4': 'Col3', '0.2': 'Col4',
'Iris-setosa': 'Iris-variety'},inplace=True)
[Link](x="Col1", y="Iris-variety", data=con);
[Link]("Column1 vs. Iris Variety")
[Link]("Column1");
[Link]("Iris Variety")
[Link]()
Output:

Result: Thus, the Python code to generate scatter plots are executed and the
results are verified.
78
[Link]: 5f Statistical and Probability measures- Correlation and Scatter plot
Date:

Aim: To write python code for correlation and scatter plot

Program: Correlation coefficient

A correlation coefficient (typically denoted r) is a single number that describes


the extent of the linear relationship between two variables. A value of +1
indicates perfect linearity (the two variables move together, like “height in
inches” and “height in centimeters”). A value of r = 0 indicates no correlation
(the variables are independent) and r = -1 indicates the variables are inversely
correlated (an increase in one variable is associated with a decrease in the
other).

Correlation means an association, It is a measure of the extent to which two


variables are related.
1. Positive Correlation: When two variables increase together and decrease
together. They are positively correlated. ‘1’ is a perfect positive correlation. For
example – demand and profit are positively correlated the more the demand for
the product, the more profit hence positive correlation.

2. Negative Correlation: When one variable increases and the other variable
decreases together and vice-versa. They are negatively correlated. For example,
If the distance between magnet increases their attraction decreases, and vice-
versa. Hence, a negative correlation. ‘-1’ is no correlation

79
3. Zero Correlation( No Correlation): When two variables don’t seem to be
linked at all. ‘0’ is a perfect negative correlation. For Example, the amount of
tea you take and level of intelligence.

Program:
import pandas as pd
import [Link] as plt
import numpy as np
y = [Link]([1, 2, 3, 4, 3, 5, 4])
x = [Link]([1, 2, 3, 4, 5, 6, 7])
correlation = [Link](x)
print(correlation)
[Link](x, y)
[Link]([Link](x), np.poly1d([Link](x, y, 1))
([Link](x)), color='red')
[Link]('Correlation')
[Link]('x axis')
[Link]('y axis')
[Link]()
Output:
0.8603090020146067 # Correlation coefficient nearing 1 hence +ive correlation

Result: Thus the Python code to generate the correlation is executed and the
result is verified.

80
[Link]:5g Statistical and Probability measures - Regression
Date:

Aim: To write Python code to find regression

 Classification: It predicts the class of the dataset based on the independent


input variable. Class is the categorical or discrete values. like the image of
an animal is a cat or dog?
 Regression: It predicts the continuous output variables based on the
independent input variable. like the prediction of house prices based on
different parameters like house age, distance from the main road, location,
area, etc.
 Linear regression is a statistical method that is used to predict a
continuous dependent variable(target variable) based on one or more
independent variables(predictor variables). This technique assumes a linear
relationship between the dependent and independent variables, which
implies that the dependent variable changes proportionally with changes in
the independent variables. In other words, linear regression is used to
determine the extent to which one or more variables can predict the value
of the dependent variable. 
 Linear relationship: The relationship between response and feature
variables should be linear. The linearity assumption can be tested using
scatter plots.

Program

import numpy as np
import [Link] as plt
def estimate_coef(x, y):
# number of observations/points
n = [Link](x)
# mean of x and y vector
m_x = [Link](x)
m_y = [Link](y)
# calculating cross-deviation and deviation about x
SS_xy = [Link](y*x) - n*m_y*m_x
SS_xx = [Link](x*x) - n*m_x*m_x
# calculating regression coefficients
b_1 = SS_xy / SS_xx
b_0 = m_y - b_1*m_x
return (b_0, b_1)
def plot_regression_line(x, y, b):

81
# plotting the actual points as scatter plot
[Link](x, y, color = "m",
marker = "o", s = 30)
# predicted response vector
y_pred = b[0] + b[1]*x
# plotting the regression line
[Link](x, y_pred, color = "g")
# putting labels
[Link]('x')
[Link]('y')
[Link]()
def main():
# observations / data
x = [Link]([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
y = [Link]([1, 3, 2, 5, 7, 8, 8, 9, 10, 12])
# estimating coefficients
b = estimate_coef(x, y)
print(b)
plot_regression_line(x,y,b)
main()

Output:
(1.2363636363636363, 1.1696969696969697)

Result: Thus the python code for linear regression is executed and the result is
verified.

82
[Link]: 6a Mean, Median, Mode, Variance, Standard Deviation,
Skewness and Kurtosis
Date:

Aim: Using the standard benchmark data set for performing the following:
Univariate Analysis: Mean, Median, Mode, Variance, Standard Deviation,
Skewness and Kurtosis.

Program1: mean - [Link]() calculates the arithmetic mean, which is the


sum of elements divided by their count.

import pandas as pd
import numpy as np
# reading csv file as pandas dataframe
data = pd.read_csv('d:\college misellaneous\Lab2023EvenSem\[Link]')
[Link]()
import statistics
print(data["5.1"])
print([Link](data["3.5"]))
print([Link](data["5.1"]))

Output:

0 4.9
1 4.7
2 4.6
3 5.0
4 5.4
...
144 6.7
145 6.3
146 6.5
147 6.2
148 5.9
Name: 5.1, Length: 149, dtype: float64
3.051006711409396
5.8483221476510066

Program2-median()

import pandas as pd
import numpy as np
# reading csv file as pandas dataframe

83
data = pd.read_csv('d:\college misellaneous\Lab2023EvenSem\[Link]')
[Link]()
import statistics
print(data["5.1"])
print([Link](data["5.1"]))
print(statistics.median_low(data["5.1"]))
print(statistics.median_high(data["5.1"]))

Output:

0 4.9
1 4.7
2 4.6
3 5.0
4 5.4
...
144 6.7
145 6.3
146 6.5
147 6.2
148 5.9
Name: 5.1, Length: 149, dtype: float64
5.8
5.8
5.8

Program3: Median() – with all column values instead of …

import pandas as pd
import numpy as np
# reading csv file as pandas dataframe
data = pd.read_csv('d:\college misellaneous\Lab2023EvenSem\[Link]')
import statistics
L1=data['5.1'].tolist()
print(L1)
print([Link](data["5.1"]))
print(statistics.median_low(data["5.1"]))
print(statistics.median_high(data["5.1"]))

Output:

[4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5.4, 4.8, 4.8, 4.3, 5.8, 5.7, 5.4, 5.1, 5.7,
5.1, 5.4, 5.1, 4.6, 5.1, 4.8, 5.0, 5.0, 5.2, 5.2, 4.7, 4.8, 5.4, 5.2, 5.5, 4.9, 5.0, 5.5, 4

84
.9, 4.4, 5.1, 5.0, 4.5, 4.4, 5.0, 5.1, 4.8, 5.1, 4.6, 5.3, 5.0, 7.0, 6.4, 6.9, 5.5, 6.5, 5.
7, 6.3, 4.9, 6.6, 5.2, 5.0, 5.9, 6.0, 6.1, 5.6, 6.7, 5.6, 5.8, 6.2, 5.6, 5.9, 6.1, 6.3, 6.1
, 6.4, 6.6, 6.8, 6.7, 6.0, 5.7, 5.5, 5.5, 5.8, 6.0, 5.4, 6.0, 6.7, 6.3, 5.6, 5.5, 5.5, 6.1,
5.8, 5.0, 5.6, 5.7, 5.7, 6.2, 5.1, 5.7, 6.3, 5.8, 7.1, 6.3, 6.5, 7.6, 4.9, 7.3, 6.7, 7.2, 6
.5, 6.4, 6.8, 5.7, 5.8, 6.4, 6.5, 7.7, 7.7, 6.0, 6.9, 5.6, 7.7, 6.3, 6.7, 7.2, 6.2, 6.1, 6.
4, 7.2, 7.4, 7.9, 6.4, 6.3, 6.1, 7.7, 6.3, 6.4, 6.0, 6.9, 6.7, 6.9, 5.8, 6.8, 6.7, 6.7, 6.3
, 6.5, 6.2, 5.9]
5.8
5.8
5.8

Program4: Variance

import pandas as pd
import numpy as np
# reading csv file as pandas dataframe
data = pd.read_csv('d:\college misellaneous\Lab2023EvenSem\[Link]')
import statistics
L1=data['3.5'].tolist()
print(L1)
print([Link](L1))
print([Link](data['3.5']))

Output:

[3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1, 3.7, 3.4, 3.0, 3.0, 4.0, 4.4, 3.9, 3.5, 3.8,
3.8, 3.4, 3.7, 3.6, 3.3, 3.4, 3.0, 3.4, 3.5, 3.4, 3.2, 3.1, 3.4, 4.1, 4.2, 3.1, 3.2, 3.5, 3
.1, 3.0, 3.4, 3.5, 2.3, 3.2, 3.5, 3.8, 3.0, 3.8, 3.2, 3.7, 3.3, 3.2, 3.2, 3.1, 2.3, 2.8, 2.
8, 3.3, 2.4, 2.9, 2.7, 2.0, 3.0, 2.2, 2.9, 2.9, 3.1, 3.0, 2.7, 2.2, 2.5, 3.2, 2.8, 2.5, 2.8
, 2.9, 3.0, 2.8, 3.0, 2.9, 2.6, 2.4, 2.4, 2.7, 2.7, 3.0, 3.4, 3.1, 2.3, 3.0, 2.5, 2.6, 3.0,
2.6, 2.3, 2.7, 3.0, 2.9, 2.9, 2.5, 2.8, 3.3, 2.7, 3.0, 2.9, 3.0, 3.0, 2.5, 2.9, 2.5, 3.6, 3
.2, 2.7, 3.0, 2.5, 2.8, 3.2, 3.0, 3.8, 2.6, 2.2, 3.2, 2.8, 2.8, 2.7, 3.3, 3.2, 2.8, 3.0, 2.
8, 3.0, 2.8, 3.8, 2.8, 2.8, 2.6, 3.0, 3.4, 3.1, 3.0, 3.1, 3.1, 3.1, 2.7, 3.2, 3.3, 3.0, 2.5
, 3.0, 3.4, 3.0]
0.18666006035764154
0.18666006035764154

Program5: Standard Deviation

import pandas as pd
import numpy as np
# reading csv file as pandas dataframe

85
data = pd.read_csv('d:\college misellaneous\Lab2023EvenSem\[Link]')
import statistics
L1=data['1.4'].tolist()
print(L1)
print([Link](L1))
print([Link](data['1.4']))

Output:

[1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 1.5, 1.6, 1.4, 1.1, 1.2, 1.5, 1.3, 1.4, 1.7,
1.5, 1.7, 1.5, 1.0, 1.7, 1.9, 1.6, 1.6, 1.5, 1.4, 1.6, 1.6, 1.5, 1.5, 1.4, 1.5, 1.2, 1.3, 1
.5, 1.3, 1.5, 1.3, 1.3, 1.3, 1.6, 1.9, 1.4, 1.6, 1.4, 1.5, 1.4, 4.7, 4.5, 4.9, 4.0, 4.6, 4.
5, 4.7, 3.3, 4.6, 3.9, 3.5, 4.2, 4.0, 4.7, 3.6, 4.4, 4.5, 4.1, 4.5, 3.9, 4.8, 4.0, 4.9, 4.7
, 4.3, 4.4, 4.8, 5.0, 4.5, 3.5, 3.8, 3.7, 3.9, 5.1, 4.5, 4.5, 4.7, 4.4, 4.1, 4.0, 4.4, 4.6,
4.0, 3.3, 4.2, 4.2, 4.2, 4.3, 3.0, 4.1, 6.0, 5.1, 5.9, 5.6, 5.8, 6.6, 4.5, 6.3, 5.8, 6.1, 5
.1, 5.3, 5.5, 5.0, 5.1, 5.3, 5.5, 6.7, 6.9, 5.0, 5.7, 4.9, 6.7, 4.9, 5.7, 6.0, 4.8, 4.9, 5.
6, 5.8, 6.1, 6.4, 5.6, 5.1, 5.6, 6.1, 5.6, 5.5, 4.8, 5.4, 5.6, 5.1, 5.1, 5.9, 5.7, 5.2, 5.0
, 5.2, 5.4, 5.1]
1.75373635121875
1.75373635121875

Program6: Skewness - Skewness is a measure of the asymmetry of a


distribution.

86
 Skewness = 0: Then normally distributed.
 Skewness > 0: Then more weight in the left tail of the distribution.
 Skewness < 0: Then more weight in the right tail of the distribution.

import pandas as pd
data = [[10, 18, 11], [13, 15, 8], [9, 20, 3]]
df = [Link](data)
print([Link]())

Output:

0 1.293343
1 -0.585583
2 -0.722109
dtype: float64

Program:

import pandas as pd
data = pd.read_csv('d:\college misellaneous\Lab2023EvenSem\[Link]')
import statistics
L1=data['1.4'].tolist()
print(L1)
df = [Link](L1)
print([Link]())

Output:

87
[1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 1.5, 1.6, 1.4, 1.1, 1.2, 1.5, 1.3, 1.4, 1.7,
1.5, 1.7, 1.5, 1.0, 1.7, 1.9, 1.6, 1.6, 1.5, 1.4, 1.6, 1.6, 1.5, 1.5, 1.4, 1.5, 1.2, 1.3, 1
.5, 1.3, 1.5, 1.3, 1.3, 1.3, 1.6, 1.9, 1.4, 1.6, 1.4, 1.5, 1.4, 4.7, 4.5, 4.9, 4.0, 4.6, 4.
5, 4.7, 3.3, 4.6, 3.9, 3.5, 4.2, 4.0, 4.7, 3.6, 4.4, 4.5, 4.1, 4.5, 3.9, 4.8, 4.0, 4.9, 4.7
, 4.3, 4.4, 4.8, 5.0, 4.5, 3.5, 3.8, 3.7, 3.9, 5.1, 4.5, 4.5, 4.7, 4.4, 4.1, 4.0, 4.4, 4.6,
4.0, 3.3, 4.2, 4.2, 4.2, 4.3, 3.0, 4.1, 6.0, 5.1, 5.9, 5.6, 5.8, 6.6, 4.5, 6.3, 5.8, 6.1, 5
.1, 5.3, 5.5, 5.0, 5.1, 5.3, 5.5, 6.7, 6.9, 5.0, 5.7, 4.9, 6.7, 4.9, 5.7, 6.0, 4.8, 4.9, 5.
6, 5.8, 6.1, 6.4, 5.6, 5.1, 5.6, 6.1, 5.6, 5.5, 4.8, 5.4, 5.6, 5.1, 5.1, 5.9, 5.7, 5.2, 5.0
, 5.2, 5.4, 5.1]
0 -0.289459
dtype: float64

Program7: Kurtosis - Kurtosis describes how much of a probability


distribution falls in the tails instead of its center.

It is also a statistical term and an important characteristic of frequency


distribution. It determines whether a distribution is heavy-tailed in respect of the
normal distribution. It provides information about the shape of a frequency
distribution.
 kurtosis for normal distribution is equal to 3.
 For a distribution having kurtosis < 3: It is called playkurtic.
 For a distribution having kurtosis > 3, It is called leptokurtic and it signifies
that it tries to produce more outliers rather than the normal distribution.

Execute only in Anaconda


Program:

from [Link] import kurtosis

# Creating a dataset
dataset = [88, 85, 82, 97, 67, 77, 74, 86,
81, 95, 77, 88, 85, 76, 81]

88
# Calculate the kurtosis
print(kurtosis(dataset, axis=0, bias=True))

Output:

-0.29271198374234686

Program: Only in Anaconda

from [Link] import kurtosis


import numpy as np
import pylab as p

x1 = [Link]( -5, 5, 1000 )


y1 = 1./([Link](2.*[Link])) * [Link]( -.5*(x1)**2 )

[Link](x1, y1, '*')

print( '\nKurtosis for normal distribution :', kurtosis(y1))

print( '\nKurtosis for normal distribution :',


kurtosis(y1, fisher = False))

print( '\nKurtosis for normal distribution :',


kurtosis(y1, fisher = True))

Output:

Kurtosis for normal distribution : -0.3073930877422071


Kurtosis for normal distribution : 2.692606912257793
Kurtosis for normal distribution : -0.3073930877422071

89
Program : Only in Anaconda

from [Link] import kurtosis

# Creating a dataset
import pandas as pd
data = pd.read_csv('d:\college misellaneous\Lab2023EvenSem\[Link]')
import statistics
L1=data['1.4'].tolist()

# Calculate the kurtosis


print(kurtosis(L1, axis=0, bias=True))

Output:

-1.3789609562635352

Program: only in anaconda

from [Link] import kurtosis


#import numpy as np
import pylab as p

90
import pandas as pd
data = pd.read_csv('d:\college misellaneous\Lab2023EvenSem\[Link]')
import statistics
L1=data['1.4'].tolist()
L2=data['3.5'].tolist()
[Link](L1, L2, '*')

print( '\nKurtosis for normal distribution :', kurtosis(L2))

print( '\nKurtosis for normal distribution :',


kurtosis(L2, fisher = False))

print( '\nKurtosis for normal distribution :',


kurtosis(L2, fisher = True))

Output:

Kurtosis for normal distribution : 0.2680613942622583


Kurtosis for normal distribution : 3.2680613942622583
Kurtosis for normal distribution : 0.2680613942622583

91
Program: Perform Univariate analysis with the following pandas DataFrame
'points': [1, 1, 2, 3.5, 4, 4, 4, 5, 5, 6.5, 7, 7.4, 8, 13, 14.2]
'assists': [5, 7, 7, 9, 12, 9, 9, 4, 6, 8, 8, 9, 3, 2, 6]
'rebounds': [11, 8, 10, 6, 6, 5, 9, 12, 6, 6, 7, 8, 7, 9, 15]

import pandas as pd
df=[Link]({'points': [1, 1, 2, 3.5, 4, 4, 4, 5, 5, 6.5, 7, 7.4, 8, 13, 14.2],
'assists': [5, 7, 7, 9, 12, 9, 9, 4, 6, 8, 8, 9, 3, 2, 6],
'rebounds': [11, 8, 10, 6, 6, 5, 9, 12, 6, 6, 7, 8, 7, 9, 15]})
print(df)
import statistics
print([Link](df["points"]))
print([Link](df["assists"]))
print([Link](df["rebounds"]))
print(statistics.median_low(df["rebounds"]))
print(statistics.median_high(df["rebounds"]))
print([Link](df['points']))
print([Link](df['assists']))
print([Link]())
import pylab as p
from [Link] import kurtosis
x1=[1, 1, 2, 3.5, 4, 4, 4, 5, 5, 6.5, 7, 7.4, 8, 13, 14.2]
y1=[5, 7, 7, 9, 12, 9, 9, 4, 6, 8, 8, 9, 3, 2, 6]
[Link](x1, y1, '*')

print( '\nKurtosis for normal distribution :', kurtosis(y1))

print( '\nKurtosis for normal distribution :',


kurtosis(y1, fisher = False))

print( '\nKurtosis for normal distribution :',


kurtosis(y1, fisher = True))

Output:

points assists rebounds


0 1.0 5 11
1 1.0 7 8
2 2.0 7 10
3 3.5 9 6
4 4.0 12 6
5 4.0 9 5

92
6 4.0 9 9
7 5.0 4 12
8 5.0 6 6
9 6.5 8 6
10 7.0 8 7
11 7.4 9 8
12 8.0 3 7
13 13.0 2 9
14 14.2 6 15
5.706666666666667
6.933333333333334
8
8
8
13.893955555555555
2.5681813712344295
points 1.053813
assists -0.207120
rebounds 1.106667
dtype: float64

Kurtosis for normal distribution : -0.46249663980935907


Kurtosis for normal distribution : 2.537503360190641
Kurtosis for normal distribution : -0.46249663980935907

Result: Thus, using the standard benchmark data set the Univariate Analysis:
Mean, Median, Mode, Variance, Standard Deviation, Skewness and Kurtosis is
executed and the result is verified.
93
[Link]: 6b Bivariant Analysis- Logistic Regression
Date:

Aim: To write python code for Logistic regression

Logistic Regression
Logistic regression aims to solve classification problems. It does this by
predicting categorical outcomes, unlike linear regression that predicts a
continuous outcome.
In the simplest case there are two outcomes, which is called binomial, an example
of which is predicting if a tumor is malignant or benign. Other cases have more
than two outcomes to classify, in this case it is called multinomial. A common
example for multinomial logistic regression would be predicting the class of an
iris flower between 3 different species.
Here we will be using basic logistic regression to predict a binomial variable. This
means it has only two possible outcomes.

Store the independent variables in X.


Store the dependent variable in y.
#X represents the size of a tumor in centimeters.
#Note: X has to be reshaped into a column from a row for the
LogisticRegression() function to work.
#y represents whether or not the tumor is cancerous (0 for "No", 1 for "Yes").
y = [Link]([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1])
From the sklearn module we will use the LogisticRegression() method to create
a logistic regression object.
This object has a method called fit() that takes the independent and dependent
values as parameters and fills the regression object with data that describes the
relationship:
#predict if tumor is cancerous where the size is 3.46mm:

94
Program:

import numpy
from sklearn import linear_model

#Reshaped for Logistic function.


X = [Link]([3.78, 2.44, 2.09, 0.14, 1.72, 1.65, 4.92, 4.37, 4.96, 4.52, 3.69,
5.88]).reshape(-1,1)
y = [Link]([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1])

logr = linear_model.LogisticRegression()
[Link](X,y)

#predict if tumor is cancerous where the size is 3.46mm:


predicted = [Link]([Link]([3.46]).reshape(-1,1))
print(predicted)

Output:

We have predicted that a tumor with a size of 3.46mm will not be cancerous.

Program: Perform Bivariate analysis using the pandas DataFrame that contains
information about two variables: (1) Hours spent studying and (2) Exam score
received by 20 different

import pandas as pd
import [Link] as plt
#create DataFrame
df = [Link]({'hours': [1, 1, 1, 2, 2, 2, 3, 3, 3, 3,
3, 4, 4, 5, 5, 6, 6, 6, 7, 8],
'score': [75, 66, 68, 74, 78, 72, 85, 82, 90, 82,
80, 88, 85, 90, 92, 94, 94, 88, 91, 96]})
#view first five rows of DataFrame
#[Link]()

#create scatterplot of hours vs. score


[Link]([Link], [Link])
[Link]('Hours Studied vs. Exam Score')
[Link]('Hours Studied')
[Link]('Exam Score')
[Link]()

95
Output:

Program: Bivariate Analysis- Anaconda

import pandas as pd
import [Link] as plt
import [Link] as sm
df = [Link]({'hours': [1, 1, 1, 2, 2, 2, 3, 3, 3, 3,
3, 4, 4, 5, 5, 6, 6, 6, 7, 8],
'score': [75, 66, 68, 74, 78, 72, 85, 82, 90, 82,
80, 88, 85, 90, 92, 94, 94, 88, 91, 96]})
print([Link]())
[Link]()
#define response variable
y = df['score']
#define explanatory variable
x = df[['hours']]
#add constant to predictor variables
x = sm.add_constant(x)
#fit linear regression model

96
model = [Link](y, x).fit()
#view model summary
print([Link]())

Output:

hours score
0 1 75
1 1 66
2 1 68
3 2 74
4 2 78
OLS Regression Results
Dep. Variable: score R-squared: 0.794
Model: OLS Adj. R-squared: 0.783
Method: Least Squares F-statistic: 69.56
Date: Mon, 29 Jan 2024 Prob (F-statistic): 1.35e-07
Time: 10:43:46 Log-Likelihood: -55.886
No. Observations: 20 AIC: 115.8
Df Residuals: 18 BIC: 117.8
Df Model: 1
Covariance Type: nonrobust
coef std err t P>|t| [0.025 0.975]
const 69.0734 1.965 35.149 0.000 64.945 73.202
hours 3.8471 0.461 8.340 0.000 2.878 4.816
Omnibus: 0.171 Durbin-Watson: 1.404
Prob(Omnibus): 0.918 Jarque-Bera (JB): 0.177
Skew: 0.165 Prob(JB): 0.915
Kurtosis: 2.679 Cond. No. 9.37
Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly
specified.

Result: Thus the Python code for logistic regression is executed and the result is
verified.
Pima Diabetics Dataset
Use the below link:
[Link]
database?resource=download
sign in with any gmail accout and download

All code executed using iris data can be executed using Diabetics dataset

97
[Link]:7a Supervised Learning
Date:

Aim: To write Python code to perform supervised learning

Supervised learning is a category of machine learning that uses labeled datasets


to train algorithms to predict outcomes and recognize patterns.

The ultimate goal of the supervised learning algorithm is to predict Y with the
max accuracy for a given new input X.
In supervised learning, we start by importing a dataset containing training
attributes and the target attributes. The supervised learning algorithm will learn
the relation between training examples and their associated target variables, then
apply that learned relationship to classify entirely new inputs (without targets).

Supervised learning is a machine learning task where an algorithm is trained to


find patterns using a dataset. The supervised learning algorithm uses this training
to make input-output inferences on future datasets. In the same way a teacher
(supervisor) would give a student homework to learn and grow knowledge,
supervised learning gives algorithms datasets so it too can learn and make
inferences.

To illustrate how supervised learning works, let’s consider an example of


predicting the marks of a student based on the number of hours he studied.
Mathematically,
Y = f(X)+ C
is broken down as follows:

 f will be the relation between the marks and number of hours the student
prepared for an exam.
 X is the INPUT (Number of hours he prepared).
 Y is the output (Marks the student scored in the exam).
 C will be a random error.
The ultimate goal of the supervised learning algorithm is to predict Y with the
maximum accuracy for a given new input X. There are several ways to implement
supervised learning and we’ll explore some of the most commonly used
approaches.
Based on the given data sets, the machine learning problem is categorized into
two types: classification and regression. If the given data has both input
(training) values and output (target) values, then it is a classification problem. If
the dataset has continuous numerical values of attributes without any target labels,
then it is a regression problem.

98
Classification:
Consider the example of a medical researcher who wants to analyze breast cancer
data to predict one of three specific treatments a patient should receive. This data
analysis task is called classification, and a model or classifier is constructed to
predict class labels, such as “treatment A,” “treatment B” or “treatment C.”
Classification is a prediction problem that predicts the categorical class labels,
which are discrete and unordered. It is a two-step process, consisting of a learning
step and a classification step.
METHODS IN CLASSIFICATION AND CHOOSING THE BEST.
There are several classification techniques that one can choose based on the type
of dataset they're dealing with. Below is a list of a few widely used traditional
classification techniques:

1. K— nearest neighbor
2. Decision trees
3. Naïve Bayes
4. Support vector machines
In the first step, the classification model builds the classifier by analyzing the
training set. Next, the

In the first step, the classification model builds the classifier by analyzing the
training set. Next, the class labels for the given data are predicted. The dataset
tuples and their associated class labels under analysis are split into a training set
and test set. The individual tuples that make up the training set are randomly
sampled from the dataset under analysis. The remaining tuples form the test set
and are independent of the training tuples, meaning they will not be used to build
the classifier.
The test set is used to estimate the predictive accuracy of a classifier. The accuracy
of a classifier is the percentage of test tuples that are correctly classified by the
classifier. To achieve higher accuracy, the best way is to test out different
algorithms and try different parameters within each algorithm. The best one can
be selected by cross-validation.
To choose a good algorithm for a problem, parameters such as accuracy, training
time, linearity, number of parameters and special cases must be taken into
consideration for different algorithms.
IMPLEMENTING KNN IN SCIKIT-LEARN ON IRIS DATASET TO
CLASSIFY THE TYPE OF FLOWER BASED ON THE GIVEN INPUT

Program:

from sklearn import datasets


import pandas as pd
import [Link] as plt

99
# Loading IRIS dataset from scikit-learn object into iris variable.
iris = datasets.load_iris()

# Prints the type/type object of iris


print(type(iris))
# <class '[Link]'>

# prints the dictionary keys of iris data


print([Link]())

# prints the type/type object of given attributes


print(type([Link]), type([Link]))

# prints the no of rows and columns in the dataset


print([Link])

# prints the target set of the data


print(iris.target_names)

# Load iris training dataset


X = [Link]

# Load iris target set


Y = [Link]

# Convert datasets' type into dataframe


df = [Link](X, columns=iris.feature_names)

# Print the first five tuples of dataframe.


print([Link]())

Output:

<class '[Link]._bunch.Bunch'>
dict_keys(['data', 'target', 'frame', 'target_names', 'DESCR', 'feature_names',
'filename', 'data_module'])
<class '[Link]'> <class '[Link]'>
(150, 4)
['setosa' 'versicolor' 'virginica']
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
0 5.1 3.5 1.4 0.2
1 4.9 3.0 1.4 0.2

100
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2

K-NEAREST NEIGHBORS IN SCIKIT-LEARN.


An algorithm is said to be a lazy learner if it simply stores the tuples of the
training set and waits until the test tuple is given. Only when it sees the test tuple
does it perform generalization to classify the tuple based on its similarity to the
stored training tuples.
K-nearest neighbor (k-NN) classifier is a lazy learner.
Based on learning by analogy, k-NN compares a given test tuple with training
tuples that are similar to it. The training tuples are described by n attributes. Each
tuple represents a point in an n-dimensional space. In this way, all training tuples
are stored in n-dimensional pattern space. When given an unknown tuple, a k-NN
classifier searches the pattern space for the k training tuples that are closest to the
unknown tuple. These k training tuples are the k “nearest neighbors” of the
unknown tuple.
“Closeness” is defined regarding a distance metric, such as Euclidean distance. A
good value for K is determined experimentally.
In this snippet, we give import k-NN classifier from sklearn and apply to our input
data which then classifies the flowers. ([Link] on GitHub)

Program:

from sklearn import datasets


from [Link] import KNeighborsClassifier

# Load iris dataset from sklearn


iris = datasets.load_iris()

# Declare an of the KNN classifier class with the value with neighbors.
knn = KNeighborsClassifier(n_neighbors=6)

# Fit the model with training data and target values


[Link](iris['data'], iris['target'])

# Provide data whose class labels are to be predicted


X=[
[5.9, 1.0, 5.1, 1.8],
[3.4, 2.0, 1.1, 4.8],
]

# Prints the data provided

101
print(X)

# Store predicted class labels of X


prediction = [Link](X)

# Prints the predicted class labels of X


print(prediction)

Output:

[[5.9, 1.0, 5.1, 1.8], [3.4, 2.0, 1.1, 4.8]]


[1 1]

Here,

0 corresponds versicolor
1 corresponds virginica
2 corresponds setosa

Program: For diabetics data set

from sklearn import datasets, linear_model


import [Link] as plt
import numpy as np

# Load the diabetes dataset


diabetes = datasets.load_diabetes()

# Use only one feature for training


diabetes_X = [Link][:, [Link], 2]

# Split the data into training/testing sets


diabetes_X_train = diabetes_X[:-20]
diabetes_X_test = diabetes_X[-20:]

# Split the targets into training/testing sets


diabetes_y_train = [Link][:-20]
diabetes_y_test = [Link][-20:]

# Create linear regression object

102
regr = linear_model.LinearRegression()

# Train the model using the training sets


[Link](diabetes_X_train, diabetes_y_train)

# Input data
print('Input Values')
print(diabetes_X_test)

# Make predictions using the testing set


diabetes_y_pred = [Link](diabetes_X_test)

# Predicted Data
print("Predicted Output Values")
print(diabetes_y_pred)

# Plot outputs
[Link](diabetes_X_test, diabetes_y_test, color='black')
[Link](diabetes_X_test, diabetes_y_pred, color='red', linewidth=1)

[Link]()

Output:

Input Values
[[ 0.07786339]
[-0.03961813]
[ 0.01103904]
[-0.04069594]
[-0.03422907]
[ 0.00564998]
[ 0.08864151]
[-0.03315126]
[-0.05686312]
[-0.03099563]
[ 0.05522933]
[-0.06009656]
[ 0.00133873]
[-0.02345095]
[-0.07410811]
[ 0.01966154]
[-0.01590626]
[-0.01590626]

103
[ 0.03906215]
[-0.0730303 ]]
Predicted Output Values
[225.9732401 115.74763374 163.27610621 114.73638965 120.80385422
158.21988574 236.08568105 121.81509832 99.56772822 123.83758651
204.73711411 96.53399594 154.17490936 130.91629517 83.3878227
171.36605897 137.99500384 137.99500384 189.56845268 84.3990668 ]

Result: Thus the Python code for supervised learning is executed and the result is
verified.

104
[Link]:7b Unsupervised Learning
Date:

Aim: To write Python code for unsupervised learning

Unsupervised learning is a class of machine learning (ML) techniques used to


find patterns in data. The data given to unsupervised algorithms is not labelled,
which means only the input variables (x) are given with no corresponding output
variables. In unsupervised learning, the algorithms are left to discover interesting
structures in the data on their own.

In supervised learning, the system tries to learn from the previous examples
given. In unsupervised learning, the system attempts to find the patterns directly
from the example given. So, if the dataset is labeled it is a supervised problem,
and if the dataset is unlabelled then it is an unsupervised problem.
Below is a simple pictorial representation of how supervised and unsupervised
learning can be viewed.

The left image an example of supervised learning (we use regression techniques
to find the best fit line between the features). In unsupervised learning the inputs
are segregated based on features and the prediction is based on which cluster it
belonged to.

 Feature: An input variable used in making predictions.


 Predictions: A model’s output when provided with an input example.
 Example: One row of a dataset. An example contains one or more features
and possibly a label.
 Label: Result of the feature.

105
Preparing Data for Unsupervised Learning
For our example, we'll use the Iris dataset to make predictions. The dataset
contains a set of 150 records under four attributes — petal length, petal width,
sepal length, sepal width, and three iris classes: setosa, virginica and versicolor.
We'll feed the four features of our flower to the unsupervised algorithm and it will
predict which class the iris belongs to.
We use the scikit-learn library in Python to load the Iris dataset and matplotlib for
data visualization. Below is the code snippet for exploring the dataset.
On GitHub: iris_dataset.py

Program:

from sklearn import datasets


import [Link] as plt

# Loading dataset
iris_df = datasets.load_iris()

# Available methods on dataset


print(dir(iris_df))

# Features
print(iris_df.feature_names)

# Targets
print(iris_df.target)

# Target Names
print(iris_df.target_names)
label = {0: 'red', 1: 'blue', 2: 'green'}

# Dataset Slicing
x_axis = iris_df.data[:, 0] # Sepal Length
y_axis = iris_df.data[:, 2] # Sepal Width

# Plotting
[Link](x_axis, y_axis, c=iris_df.target)
[Link]()

Output:

['DESCR', 'data', 'data_module', 'feature_names', 'filename', 'frame', 'target',


'target_names']

106
['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0000000000000111111111111111111111111
1111111111111111111111111122222222222
2222222222222222222222222222222222222
2 2]
['setosa' 'versicolor' 'virginica']

Clustering
In clustering, the data is divided into several groups with similar traits.

107
In the image above, the left is raw data without classification, while the right is
clustered based on its features. When an input is given which is to be predicted
then it checks in the cluster it belongs to based on its features, and the prediction
is made.
K-MEANS CLUSTERING IN PYTHON
K-means clustering is an iterative unsupervised clustering algorithm that aims to
find local maxima in each iteration. Initially, desired number of clusters are
chosen. In our example, we know there are three classes involved, so we program
the algorithm to group the data into three classes by passing the parameter
“n_clusters” into our k-means model. Randomly, three points (inputs) are
assigned into three clusters. Based on the centroid distance between each point,
the next given inputs are segregated into respected clusters and the centroids are
re-computed for all the clusters.
Each centroid of a cluster is a collection of feature values which define the
resulting groups. Examining the centroid feature weights can be used to
qualitatively interpret what kind of group each cluster represent.
We import the k-means model from scikit-learn library, fit out features and
predict.
K-means implementation in Python on GitHub: clustering_iris.py

Program:

# Importing Modules
from sklearn import datasets
from [Link] import KMeans

# Loading dataset
iris_df = datasets.load_iris()

# Declaring Model
model = KMeans(n_clusters=3)

# Fitting Model
[Link](iris_df.data)

# Predicitng a single input


predicted_label = [Link]([[7.2, 3.5, 0.8, 1.6]])

# Prediction on the entire data


all_predictions = [Link](iris_df.data)

# Printing Predictions
print(predicted_label)

108
print(all_predictions)

Output:

[0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0000000000000112111111111111111111111
1112111111111111111111111121222212222
2211222212121221122222122221222122212
2 1]

HIERARCHICAL CLUSTERING
As its name implies, hierarchical clustering is an algorithm that builds a
hierarchy of clusters. This algorithm begins with all the data assigned to a
cluster, then the two closest clusters are joined into the same cluster. The
algorithm ends when only a single cluster is left.
The completion of hierarchical clustering can be shown using dendrogram. Now
let’s look at an example of hierarchical clustering using grain data.
Hierarchical clustering implementation in Python on GitHub: hierchical-
[Link]

Program:

# Importing Modules
from [Link] import linkage, dendrogram
import [Link] as plt
import pandas as pd

# Reading the DataFrame


seeds_df = pd.read_csv(
"[Link]
python/master/[Link]")

# Remove the grain species from the DataFrame, save for later
varieties = list(seeds_df.pop('grain_variety'))

# Extract the measurements as a NumPy array


samples = seeds_df.values

"""
Perform hierarchical clustering on samples using the
linkage() function with the method='complete' keyword argument.
Assign the result to mergings.

109
"""
mergings = linkage(samples, method='complete')

"""
Plot a dendrogram using the dendrogram() function on mergings,
specifying the keyword arguments labels=varieties, leaf_rotation=90,
and leaf_font_size=6.
"""
dendrogram(mergings,
labels=varieties,
leaf_rotation=90,
leaf_font_size=6,
)

[Link]()

Output:

DIFFERENCE BETWEEN K-MEANS AND HIERARCHICAL


CLUSTERING

 Hierarchical clustering can’t handle big data very well but k-means
clustering can. This is because the time complexity of k-means is linear
i.e. O(n) while that of hierarchical clustering is quadratic i.e. O(n2).

110
 K-means clustering starts with an arbitrary choice of clusters, and the
results generated by running the algorithm multiple times might differ.
Results are reproducible in hierarchical clustering.
 K-means is found to work well when the shape of the clusters is
hyperspherical (like a circle in 2D or a sphere in 3D).
 K-means doesn't allow noisy data, while hierarchical clustering can
directly use the noisy dataset for clustering.

T-SNE CLUSTERING
One of the unsupervised learning methods for visualization is t-distributed
stochastic neighbor embedding, or t-SNE. It maps high-dimensional space
into a two or three-dimensional space which can then be visualized.
Specifically, it models each high-dimensional object by a two- or three-
dimensional point in such a way that similar objects are modeled by nearby
points and dissimilar objects are modeled by distant points with high
probability.
T-SNE Implementation in Python on Iris dataset: t_sne_clustering.py

Program:
# Importing Modules
from sklearn import datasets
from [Link] import TSNE
import [Link] as plt

# Loading dataset
iris_df = datasets.load_iris()

# Defining Model
model = TSNE(learning_rate=100)

# Fitting Model
transformed = model.fit_transform(iris_df.data)

# Plotting 2d t-Sne
x_axis = transformed[:, 0]
y_axis = transformed[:, 1]

[Link](x_axis, y_axis, c=iris_df.target)


[Link]()

Output:

111
Violet: Setosa, Green: Versicolor, Yellow: Virginica
Here, the Iris dataset has four features (4d) and is transformed and represented
in the two-dimensional figure. Similarly, t-SNE model can be applied to a
dataset which has n-features.

DBSCAN CLUSTERING
Density-based spatial clustering of applications with noise, or DBSCAN, is a
popular clustering algorithm used as a replacement for k-means in predictive
analytics. To run it doesn’t require an input for the number of clusters but it does
need to tune two other parameters.
The scikit-learn implementation provides a default for the eps and min_samples
parameters, but you’re generally expected to tune those. The eps parameter is
the maximum distance between two data points to be considered in the same
neighborhood. The min_samples parameter is the minimum amount of data
points in a neighborhood to be considered a cluster.

Program:
# Importing Modules
from [Link] import load_iris
import [Link] as plt
from [Link] import DBSCAN
from [Link] import PCA

112
# Load Dataset
iris = load_iris()

# Declaring Model
dbscan = DBSCAN()

# Fitting
[Link]([Link])

# Transoring Using PCA


pca = PCA(n_components=2).fit([Link])
pca_2d = [Link]([Link])

# Plot based on Class


for i in range(0, pca_2d.shape[0]):
if dbscan.labels_[i] == 0:
c1 = [Link](pca_2d[i, 0], pca_2d[i, 1], c='r', marker='+')
elif dbscan.labels_[i] == 1:
c2 = [Link](pca_2d[i, 0], pca_2d[i, 1], c='g', marker='o')
elif dbscan.labels_[i] == -1:
c3 = [Link](pca_2d[i, 0], pca_2d[i, 1], c='b', marker='*')

[Link]([c1, c2, c3], ['Cluster 1', 'Cluster 2', 'Noise'])


[Link]('DBSCAN finds 2 clusters and Noise')
[Link]()
Output:

Result: Thus the Python code for unsupervised learning is executed and the
result is verified.

113
[Link] Various Plotting functions
Date:

Aim: To write Python code for various plotting functions

Matplotlib is a powerful Python library that serves as a numerical and


mathematical extension for the NumPy library. One of its key components is
Pyplot, which offers a state-based interface to the Matplotlib module, presenting
users with a familiar MATLAB-like environment. Through
[Link]() function in Python, users can effortlessly create a variety
of plots, including Line Plots, Contour Plots, Histograms, Scatter Plots, 3D Plots,
and more. This versatility makes Matplotlib an invaluable tool for data
visualization and analysis in the Python programming language.

The [Link]() function is a fundamental component of the


Matplotlib library, specifically within the Pyplot module. It serves the purpose
of generating a 2D hexagonal binning plot based on the given data points
represented by the variables x and y. It connects data points with lines, allowing
customization of the plot’s appearance through parameters such as line styles
and markers. This versatile function is widely used for data visualization in
various domains.
Syntax: [Link](*args, scalex=True, scaley=True, data=None,
**kwargs)
 Parameters:
 x, y: These parameters represent the horizontal and vertical
coordinates of the data points. The ‘x’ values are optional, allowing
flexibility in the plotting process.
 fmt: This is an optional parameter that contains a string value. It is
used to specify the format of the plot, defining the line style,
marker, and color.
 data: An optional parameter, ‘data’ refers to an object with labeled
data. It provides a convenient way to pass data directly, enhancing
the readability and ease of use.
 Returns: The plot() function returns a list of Line2D objects, each
representing a segment of the plotted data. These Line2D objects
encapsulate the characteristics and attributes of the plotted lines,
allowing for further customization and analysis.
Types
 Basic Line Plot
 Multiple Lines Plot
 Scatter Plot with Multiple Markers
 Two Curves Plot
Etc.

114
Program: Line Plot

import [Link] as plt


import numpy as np
# Plotting a simple line graph
[Link]([1, 2, 3])
# Setting the title
[Link]('Matplotlib Line Plot Example')
# Updating and displaying the plot
[Link]()
[Link]()

Output:

Program: Multiple line plots

import [Link] as plt


import numpy as np

# Generate data
x = [Link](0, 2*[Link], 100)
y1, y2 = [Link](x), [Link](x)
115
# Plotting multiple lines on a single plot
[Link](x, y1, label='Sin(x)', color='b')
[Link](x, y2, label='Cos(x)', color='r', linestyle='--')

# Adding labels and title


[Link]('X-axis')
[Link]('Y-axis')
[Link]('Multiple Lines Plot')

# Displaying the legend and the plot


[Link]()
[Link]()

Output:

Program: Scatter plot

import [Link] as plt


import numpy as np
# Generate random data

116
[Link](42)
x = [Link](50)
y = [Link](50)
# Plotting a scatter plot with custom markers
[Link](x, y, marker='o', linestyle='', markersize=8, color='r', label='Scatter Plot'
)
# Adding labels and title
[Link]('X-axis')
[Link]('Y-axis')
[Link]('Scatter Plot Example')
# Displaying the legend
[Link]()
# Display the plot
[Link]()

Output:

Program: Multiple curves

import [Link] as plt


import numpy as np
# Fixing random state for reproducibility

117
[Link](19680801)
# create random data
xdata = [Link]([2, 10])
# split the data into two parts
xdata1 = xdata[0, :]
xdata2 = xdata[1, :]
# sort the data so it makes clean curves
[Link]()
[Link]()
# create some y data points
ydata1 = xdata1 ** 2
ydata2 = 1 - xdata2 ** 3
# plot the data
[Link](xdata1, ydata1, color ='tab:blue')
[Link](xdata2, ydata2, color ='tab:orange')

# set the limits


[Link]([0, 1])
[Link]([0, 1])
[Link]('[Link]() example 2')
# display the plot
[Link]()
Output:

118
Download:

[Link]

from the above link download concrete strength data set. Extract the files to
required directory

Its extension will be xls. Use save as to convert it to xlxs file by choosing excel
workbook (in save as option)

Program:

import pandas as pd

# read by default 1st sheet of an excel file


dataframe1 = pd.read_excel('D:\Concrete_Data.xlsx')
L1=dataframe1['Cement (component 1)(kg in a m^3 mixture)'].tolist()
L2=dataframe1['Blast Furnace Slag (component 2)(kg in a m^3
mixture)'].tolist()
[Link](L1,L2)
[Link]()
#print([Link]())

Output:

119
Program: Scatter Plot - for the following Pandas DataFrame with Team name
and Rank Points as x and y axis

["Australia", 2500],["Bangladesh", 1000],["England", 2000],["India",


3000],["Srilanka", 1500]

import pandas as pd

# Prepare data
data={'Team Name':["Australia", "Bangladesh", "England","India", "Srilanka"],

'Rank':[2500, 1000, 2000, 3000, 1500]}

# Load data into DataFrame


df = [Link](data = data);

# Draw a scatter plot


[Link](x = 'Team Name', y = 'Rank');

Output:

Result: Thus, the different plots in python are executed and the results are
verified.

120
[Link] Reading Files
Date:

Aim: To write python program to read txt file, excel file.

Program: read txt file

#Create txt file using notepad

# Program to read the entire file using read() function


file = open("D:\[Link]", "r")
content = [Link]()
print(content)
[Link]()

Output:

Welcome
Good morning All

Program: read excel file - Anaconda

#Create excel file

import pandas as pd

# read by default 1st sheet of an excel file


dataframe1 = pd.read_excel('D:\[Link]')

print(dataframe1)

Output:

Unnamed: 0 Unnamed: 1 Unnamed: 2 Unnamed: 3 Unnamed: 4


0 NaN NaN NaN NaN NaN
1 NaN NaN NaN NaN NaN
2 NaN NaN 98.0 99.0 100.0

Result: Thus the Python code to read txt file, excel files are executed and the
results are verified

121
[Link] Bivariate Analysis- Multiple Regression
Date:

Aim: To write python code for multiple regression

linear regress only has one independent variable impacting the slope of the
relationship, multiple regression incorporates multiple independent variables.

Bivariate analysis is one of the simplest forms of quantitative analysis. It


involves the analysis of two variables, for the purpose of determining the
empirical relationship between them.

Steps Involved in any Multiple Linear Regression Model


Step #1: Data Pre Processing
1. Importing The Libraries.
2. Importing the Data Set.
3. Encoding the Categorical Data.
4. Avoiding the Dummy Variable Trap.
5. Splitting the Data set into Training Set and Test Set.
Step #2: Fitting Multiple Linear Regression to the Training set
Step #3: Predict the Test set results.

Program: - 3D Plot

import numpy as np
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import [Link] as plt

def generate_dataset(n):
x = []
y = []
random_x1 = [Link]()
random_x2 = [Link]()
for i in range(n):
x1 = i
x2 = i/2 + [Link]()*n
[Link]([1, x1, x2])
[Link](random_x1 * x1 + random_x2 * x2 + 1)
return [Link](x), [Link](y)

x, y = generate_dataset(200)

122
[Link]['[Link]'] = 12

fig = [Link]()
ax = fig.add_subplot(projection ='3d')

[Link](x[:, 1], x[:, 2], y, label ='y', s = 5)


[Link]()
ax.view_init(45, 0)

[Link]()

Output:

Program2: Car data set multiple regression

Download car data set from the below link

[Link]

Program:

import pandas
from sklearn import linear_model

123
df = pandas.read_csv("D:\college misellaneous\Lab2023EvenSem\[Link]")

X = df[['Weight', 'Volume']]
y = df['CO2']

regr = linear_model.LinearRegression()
[Link](X, y)

#predict the CO2 emission of a car where the weight is 2300kg, and the volume
is 1300cm3:
predictedCO2 = [Link]([[2300, 1300]])

print(predictedCO2)

Output:

[107.2087328]

Result: Thus the python code for multiple regression is executed and the result
is verified.

124
[Link] Histogram plot, Density Plot , Contour Plot, 3D plot, Normal plot
Date:

Aim: To write Python code for different plots like density plot, contour plot, 3D
plot, histogram plot, normal plot

A Histogram represents data provided in the form of some groups.

Program:

import [Link] as plt


import numpy as np

# Generate random data for the histogram


data = [Link](1000)

# Plotting a basic histogram


[Link](data, bins=30, color='skyblue', edgecolor='black')

# Adding labels and title


[Link]('Values')
[Link]('Frequency')
[Link]('Basic Histogram')

# Display the plot


[Link]()

Output:

125
Density Plot is a type of data visualization tool. It is a variation of the histogram
that uses ‘kernel smoothing’ while plotting the values. It is a continuous and
smooth version of a histogram inferred from a data.
Density plots uses Kernel Density Estimation (so they are also known as Kernel
density estimation plots or KDE) which is a probability density function. The
region of plot with a higher peak is the region with maximum data points
residing between those values.
Density plots can be made using pandas, seaborn, etc. In this article, we will
generate density plots using Pandas. We will be using two datasets of the
Seaborn Library namely – ‘car_crashes’ and ‘tips’.

Program:

import pandas as pd
import seaborn as sns
import [Link] as plt

# loading the dataset


# from seaborn library
data = sns.load_dataset('car_crashes')

# viewing the dataset


print([Link](4))

[Link](color='green')
[Link]('Density plot for Speeding')
[Link]()

Output:

total speeding alcohol not_distracted no_previous ins_premium \


0 18.8 7.332 5.640 18.048 15.040 784.55
1 18.1 7.421 4.525 16.290 17.014 1053.48
2 18.6 6.510 5.208 15.624 17.856 899.47
3 22.4 4.032 5.824 21.056 21.280 827.34
ins_losses abbrev
0 145.08 AL
1 133.93 AK
2 110.35 AZ
3 142.39 AR

126
Contour plots also called level plots are a tool for doing multivariate analysis
and visualizing 3-D plots in 2-D space. If we consider X and Y as our variables
we want to plot then the response Z will be plotted as slices on the X-Y plane
due to which contours are sometimes referred as Z-slices or iso-response.

Program:

import [Link] as plt


import numpy as np

feature_x = [Link](0, 50, 2)


feature_y = [Link](0, 50, 3)

# Creating 2-D grid of features


[X, Y] = [Link](feature_x, feature_y)

fig, ax = [Link](1, 1)

Z = [Link](X / 2) + [Link](Y / 4)

# plots contour lines


[Link](X, Y, Z)
127
ax.set_title('Contour Plot')
ax.set_xlabel('feature_x')
ax.set_ylabel('feature_y')

[Link]()

Output:

Contoutf plot:

contour() and contourf() draw contour lines and filled contours, respectively.
Except as noted, function signatures and return values are the same for both
versions. contourf() differs from the MATLAB version in that it does not draw
the polygon edges. To draw edges, add line contours with calls to contour() .

Program:

import numpy as np
import [Link] as plt
x = [Link](1, 10)
y = [Link](-1, 1)

128
h=x*y

fig, (ax1, ax2) = [Link](ncols=2)


ax1.set_title("origin='upper'")
ax2.set_title("origin='lower'")
[Link](h, levels=[Link](5, 70, 5), extend='both', origin="upper")
[Link](h, levels=[Link](5, 70, 5), extend='both', origin="lower")
[Link]()

Output:

3D Plot:

For plotting lines in 3D we will have to initialize three variable points for the
line equation.

Program:

# Implementation of matplotlib function


from mpl_toolkits import mplot3d
import numpy as np

129
import [Link] as plt

fig = [Link]()

# syntax for 3-D projection


ax = [Link](projection ='3d')

# defining all 3 axis


z = [Link](0, 1, 100)
x = z * [Link](25 * z)
y = z * [Link](25 * z)

# plotting
ax.plot3D(x, y, z, 'green')
ax.set_title('3D line plot geeks for geeks')
[Link]()

Output:

Normal Plot:

Normal distribution is a continuous probability distribution wherein values lie


in a symmetrical fashion mostly situated around the mean.

130
Program:

import numpy as np
import [Link] as plt
from [Link] import norm

#x-axis ranges from -3 and 3 with .001 steps


x = [Link](-3, 3, 0.001)

#plot normal distribution with mean 0 and standard deviation 1


[Link](x, [Link](x, 0, 1))

Output:

imshow(), colorbar()

The matplotlib function imshow() creates an image from a 2-dimensional


numpy array. The image will have one square for each element of the array. The
color of each square is determined by the value of the corresponding array
element and the color map used by imshow() .

Colorbars are a visualization of the mapping from scalar values to colors.


131
Program:

# Implementation of matplotlib function


import [Link] as plt
import numpy as np
from [Link] import LogNorm

dx, dy = 0.015, 0.05


y, x = [Link][slice(-4, 4 + dy, dy),
slice(-4, 4 + dx, dx)]
z = (1 - x / 3. + x ** 5 + y ** 5) * [Link](-x ** 2 - y ** 2)
z = z[:-1, :-1]
z_min, z_max = -[Link](z).max(), [Link](z).max()
c = [Link](z, cmap ='Greens', vmin = z_min, vmax = z_max,
extent =[[Link](), [Link](), [Link](), [Link]()],
interpolation ='nearest', origin ='lower')
[Link](c)
[Link]('[Link]() function Example',
fontweight ="bold")
[Link]()

Output:

132
Clabel:

Program: (Anaconda)

# importing the required libraries


import numpy
import [Link]
# creating the graph
delta = 0.025
x = [Link](-3.0, 3.0, delta)
y = [Link](-2.0, 2.0, delta)
X, Y = [Link](x, y)
Z1 = [Link](-X**2 - Y**2)
Z2 = [Link](-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2
# adding labels to the line contours
fig, ax = [Link]()
CS = [Link](X, Y, Z)
[Link](CS, inline=1, fontsize=10)
ax.set_title('Simplest default with labels')

Output:

Result: Thus the python codes to generate different plots are executed and the
results are verified.

133

You might also like