0% found this document useful (0 votes)
22 views41 pages

Python Lab fileVANSH-1

It's lab file of having various 10 python ml program

Uploaded by

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

Python Lab fileVANSH-1

It's lab file of having various 10 python ml program

Uploaded by

Vansh Munjal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Vansh 2822649

PANIPAT INSTITUTE OF
ENGINEERING AND TECHNOLOGY

Department of AI & ML

PYTHON LAB - II
(PC-CS-AIML - 220 A)

Submitted to: Submitted by:


Ms. JYOTI VANSH
Asst. Professor B. Tech CSE - AIML
4th Semester (2822649)

Affiliated to:

KURUKSHETRA UNIVERSITY, KURUKSHETRA, INDIA

1|Page
Vansh 2822649

INDEX

Serial Program Date Teacher’s


No. Signature
Write a program to implement of
1 Basic Python Libraries-numpy,
scipy.
Write a program to implement of
2 Basic Python Libraries-matplotlib,
pandas.
Write a program to create samples
3 from population.

Write a program to evaluate Mean,


4 Median, Mode of dataset.

Write a program to implement


5 Central Limit Theorem in dataset.

Write a program to implement


6 Measure of Spread in datset.
Write a program to implement
7 program to differentiate between
descriptive and inferential statistics.
Write a program to implement pmf,
8 pdf and cdf.

Write a program to implement


9 different visualization techniques on
sample
Write a program to implement
10 different hypothesis test on sample
dataset.

2|Page
Vansh 2822649

Program No. 1
Aim: Write a program to implement numpy and scipy library of python.

Program:-

# numpy Library

# one dimensional array

import numpy as np

# Creating a one-dimensional array


arr1 = np.array([1, 2, 3, 4, 5])
print("One-dimensional array (arr1):")
print(arr1)

# two dimensional array

# Creating a two-dimensional array


arr2 = np.array([[1, 2, 3], [4, 5, 6]])
print("\nTwo-dimensional array (arr2):")
print(arr2)

# three dimensional array

# Creating a three-dimensional array


arr3 = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [1, 2, 3]]])
print("\nThree-dimensional array (arr3):")
print(arr3)

Output:

3|Page
Vansh 2822649

#The Basics

import numpy as np

# Create the numpy array


a = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

# Print the numpy array


print("Array 'a':")
print(a)

# Print the data type of the array


print("\nData type of 'a':")
print(a.dtype)

# Print the size of each element in the array (in bytes)


print("\nSize of each element in 'a' (itemsize):")
print(a.itemsize)

# Print the total number of bytes consumed by the elements of the array
print("\nTotal number of bytes (nbytes):")
print(a.nbytes)

# Print the initial memory location of the array


print("\nInitial memory location of 'a':")
print(a.data)

# Print the strides of the array


print("\nStrides of 'a':")
print(a.strides)

# Print the number of dimensions of the array


print("\nNumber of dimensions (ndim):")
print(a.ndim)

# Print the shape of the array


print("\nShape of 'a':")
print(a.shape)

Output:

4|Page
Vansh 2822649

#Append

import numpy as np

# Appending one-dimensional arrays


a1 = np.array([5, 8])
a2 = np.array([9, 2])
print("Appending one-dimensional arrays:")
print(np.append(a1, a2, axis=0))

# Appending two-dimensional arrays along axis 1


a1 = np.array([[5, 8, 3], [2, 6, 4]])
a2 = np.array([[9, 2, 8], [2, 4, 2]])
print("\nAppending two-dimensional arrays along axis 1:")
5|Page
Vansh 2822649
print(np.append(a1, a2, axis=1))

Output:

#Reshape

import numpy as np

# Original array
a = np.array([[1, 2, 3], [4, 5, 6]])

# Create a view of the array


c = a.view()

# Reshape the view to (3, 2)


c = c.reshape(3, 2)

# Print the original array


print("Original array 'a':")
print(a)

# Print the reshaped view 'c'


print("\nReshaped array 'c':")
print(c)

Output:

# Zeros, Ones, and Custom Values

import numpy as np

6|Page
Vansh 2822649
# Creating an array of zeros with shape (2, 3)
e = np.zeros([2, 3])
print("Array 'e' filled with zeros:")
print(e)

# Creating an array of ones with shape (2, 3)


f = np.ones([2, 3])
print("\nArray 'f' filled with ones:")
print(f)

# Creating an array filled with the value 5 with shape (2, 3)


g = np.full([2, 3], 5)
print("\nArray 'g' filled with fives:")
print(g)

Output:

#Arithmetic operations

import numpy as np
a = np.array([[1, 2, 2, 3], [1, 3, 4, 5], [1, 2, 3, 4], [1, 3, 4, 2]])
b = np.array([[2, 2, 2, 3], [2, 3, 4, 5], [2, 2, 3, 4], [2, 3, 4, 2]])

# Arithmetic Operators
add = a + b
sub = a - b
mul = a * b
div = a / b
exp = a ** 2
mod = a % 3

print("\nArithmetic Operators:")
7|Page
Vansh 2822649
print(f"Addition:\n{add}")
print(f"Subtraction:\n{sub}")
print(f"Multiplication:\n{mul}")
print(f"Division:\n{div}")
print(f"Exponentiation:\n{exp}")
print(f"Modulus:\n{mod}")

Output:

#Statistical operations
8|Page
Vansh 2822649

import numpy as np

# 1D Array
a1= np.array([1, 2, 3, 4, 5])

# Statistical measures for 1D Array


print("\n1D Array Statistical Functions:")
print("Mean:", np.mean(a1))

print("Median:", np.median(a1))
print("Minimum:", np.min(a1))
print("Maximum:", np.max(a1))
print("Standard Deviation:", np.std(a1))
print("Variance:", np.var(a1))
print("Sum:", np.sum(a1))
print("Product:", np.prod(a1))
print("Count Nonzero:", np.count_nonzero(a1))

Output:

# Statistical operations by axis for 3D Array

import numpy as np

# 3D Array
a3 = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]]])
print("\n3D Array - Statistical Operations by Axis:")
print("Depth-wise Minimum:")
print(np.min(a3, axis=0))
print("Height-wise Minimum:")
print(np.min(a3, axis=1))
print("Width-wise Minimum:")
print(np.min(a3, axis=2))
9|Page
Vansh 2822649
print("Depth-wise Maximum:")
print(np.max(a3, axis=0))
print("Height-wise Maximum:")
print(np.max(a3, axis=1))
print("Width-wise Maximum:")
print(np.max(a3, axis=2))
print("Depth-wise Mean:")
print(np.mean(a3, axis=0))
print("Depth-wise Median:")

print(np.median(a3, axis=0))
print("Cumulative Sum along Depth:")
print(np.cumsum(a3, axis=0))

Output:

10 | P a g e
Vansh 2822649

#Comparison

import numpy as np

# Check if each element of array 'a' is less than 5


a = np.array([1, 2, 3, 4])

11 | P a g e
Vansh 2822649
print(a < 5)

# Check if each element of array 'b' is less than each element of array 'a'
a = np.array([1, 2, 3, 4])
b = np.array([5, 6, 7, 8])
print(b < a)

# Check if the arrays 'a' and 'b' are equal element-wise


print(np.array_equal(a, b))

Output:

#Bitwise

#bitwise operation(and)

import numpy as np

a1= np.array([1,3])
a2= np.array([4,5])
a3 = np.bitwise_and(a1, a2)
print ("Bitwise operation(and) =",a3)

#bitwise operation(or)

a1= np.array([5,8])
a2= np.array([9,2])
a3 = np.bitwise_and(a1, a2)

print ("Bitwise operation(or) =",a3)

#bitwise operation(XOR)

a1= np.array([5,6])

a2= np.array([9,3])
a3 = np.bitwise_xor(a1, a2)
print ("Bitwise operation(XOR) =" ,a3)

#bitwise operation(left shift)

12 | P a g e
Vansh 2822649
a1= np.array([5,8])
a2= np.array([9,2])
a3 = np.left_shift(a1, a2)
print ("Bitwise operation(left shift) =",a3)

Output:

#Copying

import numpy as np

c = np.array([[2, 5, 6, 3], [8, 3, 5, 5], [1, 2, 3, 4]])

# No copy: e is just another reference to c


e=c
print("No copy (e):")
print(e)

# Shallow copy: f is a new array object, but shares data with c


f = c.view()
print("Shallow copy (f):")
print(f)

# Reshape c to check if it affects f


c_reshaped =c.reshape(4,3) # Note: reshape doesn't modify the original array in
place
print("After reshaping c (does not affect f):")
print(f)

# Check if f is the same object as c


print("Is f the same object as c?", f is c)

# Deep copy: g is a new array object with a copy of the data from c
g = np.copy(c)

print("Deep copy (g):")


print(g)

# Modify an element in c to see if it affects g


c[2][3] = 9 # Change element at position (2, 3) to 9
13 | P a g e
Vansh 2822649
print("After modifying c:")

print("c:")
print(c)
print("g (should not be affected):")
print(g)

# Check if g is the same object as c


print("Is g the same object as c?", g is c)

Output:

#scipy Library

from scipy import constants


print(constants.kilo)

from scipy.sparse import csr_matrix


14 | P a g e
Vansh 2822649
import numpy as np

a=np.array([[0,1,1,0,0],[1,2,0,0,0]])

print(csr_matrix(a))
print (csr_matrix(a).data)
print (csr_matrix(a).count_nonzero)
print(csr_matrix(a).eliminate_zeros)
print(csr_matrix(a).sum_duplicates)

Output:

#Root

from scipy.optimize import root


from math import cos
import numpy as np

def eq(x):
return x+np.cos(x)
myroot=root (eq,0)
print(myroot.x)

Output:

#Linear Algebra

import numpy as np

15 | P a g e
Vansh 2822649

a1= np.array([[5,8,3],[2,6,4]])
print(np.transpose(a1))
print(np.trace(a1))

Output:

Program No. 2
Aim: Write a program to implement matplotlib and pandas library of python.

16 | P a g e
Vansh 2822649
Program:-

#Line Graph
import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([0, 6])


ypoints = np.array([0, 250])

plt.plot(xpoints, ypoints)
plt.show()

Output:

#Two line grapgh

import matplotlib.pyplot as plt


import numpy as np

x1= np.array([0,1,2,])
y1= np.array([3,4,5])
x2= np.array([6,7,8])
y2= np.array([9,10,11])

plt.plot(x1,y1)
plt.plot(x2,y2)
plt.plot(x1,y1,x2,y2)
plt.show()

Output:
17 | P a g e
Vansh 2822649

#zigzag line
xpoints = np.array([2, 4, 8, 10])
ypoints = np.array([2, 4, 2, 4])

plt.plot(xpoints, ypoints)
plt.show()

OUTPUT:

#markers
ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, marker = 'o')


plt.show()

18 | P a g e
Vansh 2822649
OUTPUT:

#marker size, edge color and face color


ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, marker = 'o', ms = 20, mec = 'g', mfc = 'y')


plt.show()

OUTPUT:

#label and title


x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([50, 42, 40, 37, 30, 25, 20, 16, 9, 4])

plt.plot(x, y)
plt.title("alt./temp.")
plt.xlabel("Altitude")
19 | P a g e
Vansh 2822649
plt.ylabel("Temprature")
plt.show()

OUTPUT:

#subplots

#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(1, 2, 1)
plt.plot(x,y)

#plot 2:

x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.show()

OUTPUT:

20 | P a g e
Vansh 2822649

Program No. 3
21 | P a g e
Vansh 2822649

Aim: Write a program to create samples from population in python.

Program :-

import pandas as pd

#using dataset

df = pd.read_csv('data.csv')
print(df.sample(10))

Output:

#using series

a = [1, 7, 2, 8, 5, 12, 6, 18]


s = pd.Series(a)
print(s.sample(3))

Output:

#using dataframe

data = {
"student": ["Ram", "Shyam", "Geeta", "Seeta", "Kush", "Khushi", "Dhruv"],

"percentage": [58, 79, 45, 84, 67, 36, 97]


}

22 | P a g e
Vansh 2822649
df1 = pd.DataFrame(data)
print(df1.sample(3))

Output:

Program No. 4

23 | P a g e
Vansh 2822649
Aim: Write a program to evaluate mean, median and mode of a dataset in
python.

Program :-

import pandas as pd
a = [1, 6, 17, 23, 45, 23, 67, 84, 17, 96]
df = pd.Series(a)

mean = df.mean()
print(“Mean “,mean)

median = df.median()
print(“Median “,median)

mode = df.mode()
print(“Mode “,mode)

Output:

#Using datframes

import pandas as pd

data = {
'Name':['Vansh','Sujal','Sumit','Rounak','Ayush'],
'Age':[20,20,19,20,21],
'Class':['A','B','A','C','D'],
'Marks':[40,39,38,37,39]
}
c = pd.DataFrame(data)
print(c)

print("Mean of 'Marks' column:", c['Marks'].mean())

print("Minimum value in 'Marks' column:", c['Marks'].min())


print("Maximum value in 'Marks' column:", c['Marks'].max())
print("Variance of 'Marks' column:", c['Marks'].var())
24 | P a g e
Vansh 2822649
print("Median of 'Marks' column:", c['Marks'].median())

Output:

#CSV FILE

import pandas as pd

# Read the CSV file into a DataFrame


e = pd.read_csv(r'D:\4th SEMESTER\PYTHON LAB\FILE.csv')

# Print the DataFrame


print(e)

# Print descriptive statistics of the DataFrame


print(e.describe())

Output:

25 | P a g e
Vansh 2822649

Program No. 5

Aim: Write a program to implement Central Limit Theorem in dataset.

Program :-

import numpy as np
import matplotlib.pyplot as plt

num=[1,11,25,56,28,90,72,34]
means=[]
for j in num:
np.random.seed(1)
x=[np.mean(np.random.randint(-40,40,j)) for _i in range (1000)]
means.append(x)
k=0
fig,ax=plt.subplots(2,2,figsize=(8,8))

for i in range (0,2):


for j in range (0,2):
ax[i,j].hist(means[k],10,density=True)
ax[i,j].set_title(label=num[k])
k=k+1
plt.show()

Output:

26 | P a g e
Vansh 2822649

Program No. 6

Aim: Write a program to implement measures of spread in python.


Program :-

import numpy as np

# Creating a NumPy array


a = np.array([1, 2, 9, 23, 3, 32, 12, 11, 24, 12])

# Calculating and printing the standard deviation


std_dev = np.std(a)
print("Standard Deviation:", std_dev)

# Calculating and printing the variance


variance = np.var(a)
print("Variance:", variance)

Output:

27 | P a g e
Vansh 2822649

Program No. 7

Aim: Write a program to differentiate between descriptive and inferential


statistics.
Program:-
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Creating DataFrame with random normal distribution


data = pd.DataFrame({
'Height': np.random.normal(170, 5, 100),
'Weight': np.random.normal(65, 7, 100)
})

# Descriptive Statistics
print("Descriptive Statistics:")
print("Mean:")
print(data.mean())
print("\nStandard Deviation:")
print(data.std())
print("\nSummary Statistics:")
print(data.describe())

# Graphical Representation using Matplotlib


plt.figure(figsize=(14, 7))

# Histograms
plt.subplot(2, 2, 1)
plt.hist(data['Height'], bins=10, alpha=0.7, color='blue', edgecolor='black')
plt.title('Height Distribution')
plt.xlabel('Height (cm)')
plt.ylabel('Frequency')

plt.subplot(2, 2, 2)
plt.hist(data['Weight'], bins=10, alpha=0.7, color='green', edgecolor='black')
28 | P a g e
Vansh 2822649

plt.title('Weight Distribution')
plt.xlabel('Weight (kg)')
plt.ylabel('Frequency')

plt.xlabel('Height (cm)')
plt.ylabel('Weight (kg)')
plt.grid(True)
plt.show()

Output:

# Inferential Statistics (Example: T-test)


from scipy.stats import ttest_ind

sample1 = np.random.normal(170, 5, 30)


sample2 = np.random.normal(172, 5, 30)
29 | P a g e
Vansh 2822649

t_statistic, p_value = ttest_ind(sample1, sample2)


print("\nInferential Statistics (T-test):")
print("T-statistic:", t_statistic)
print("P-value:", p_value)
if p_value < 0.05:
print("The difference between the two samples is statistically significant.")
else:
print("The difference between the two samples is not statistically significant.")

Output:

30 | P a g e
Vansh 2822649

Program No. 8

Aim: Write a program to implement pmf, pdf and cdf.


Program :-

#PMF(probability mass function)

from scipy.stats import binom

n=20
p=0.5

r_values=list(range (n+1))
print(r_values)

mean,var=binom.stats(n,p)

print(mean)
print(var)

dist_1=[binom.pmf(r,n,p)for r in r_values]
print("r\tp(r)")

for i in range (n+1):


print(str(r_values[i])+"\t" +str(dist_1[i]))

print("mean ="+str(mean))
print("variance ="+str(var))

31 | P a g e
Vansh 2822649

Output:

#PDF(Probability Density Function)

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

N = 400

data = np.random.randn(N)
count,bins_count = np.histogram(data,density=True,bins=10)

print(count)
print(bins_count)

pdf=count/sum(count)
print(pdf)

plt.plot(bins_count[1:],pdf,label="PDF")
plt.legend()
plt.show()

32 | P a g e
Vansh 2822649

Output:

#CDF (Cumulative Distribution Function)

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

N = 400
data = np.random.randn(N)
count,bins_count = np.histogram(data,density=True,bins=10)

print(count)
print(bins_count)
pdf=count/sum(count)

cdf=np.cumsum(pdf)
print(cdf)

plt.plot(bins_count[1:],cdf,label="CDF")
plt.legend()
plt.show()

33 | P a g e
Vansh 2822649

Output:

34 | P a g e
Vansh 2822649

Program No. 9

Aim: Write a program to implement different visualization techniques on sample


dataset.
Program :-

#Bar Graph

import numpy as np
import matplotlib.pyplot as plt

# creating the dataset


data = {'C':20, 'C++':15, 'Java':30, 'Python':35}
courses = list(data.keys())
values = list(data.values())
fig = plt.figure(figsize = (10, 5))

# creating the bar plot


plt.bar(courses, values, color ='maroon', width = 0.4)
plt.xlabel("Courses offered")
plt.ylabel("No. of students enrolled")
plt.title("Students enrolled in different courses")
plt.show()

Output:

35 | P a g e
Vansh 2822649

# Histogram

import numpy as np
import matplotlib.pyplot as plt

# Creating the dataset for histogram


scores = [22, 45, 23, 67, 89, 90, 55, 47, 68, 90, 73, 87, 45, 58, 62, 71, 85, 42, 48, 60]
fig = plt.figure(figsize=(10, 5))

# Creating the histogram


plt.hist(scores, bins=10, color='blue', edgecolor='black')
plt.xlabel('Scores')
plt.ylabel('Number of Students')
plt.title('Distribution of Students\' Scores')
plt.show()

Output:

36 | P a g e
Vansh 2822649

#Pie Chart

import matplotlib.pyplot as plt

# Creating the dataset for pie chart


data = {'C': 20, 'C++': 15, 'Java': 30, 'Python': 35}
courses = list(data.keys())
values = list(data.values())

fig = plt.figure(figsize=(10, 5))

# Creating the pie chart


plt.pie(values, labels=courses, autopct='%1.1f%%', startangle=140)

plt.title('Distribution of Students in Different Courses')


plt.show()

Output:

37 | P a g e
Vansh 2822649

#Scatter Plot

import matplotlib.pyplot as plt

# Creating the dataset for scatter plot


math_scores = [22, 45, 23, 67, 89, 90, 55, 47, 68, 90]
science_scores = [20, 48, 21, 60, 85, 88, 50, 40, 65, 88]

fig = plt.figure(figsize=(10, 5))

# Creating the scatter plot


plt.scatter(math_scores, science_scores, color='green')

plt.xlabel('Math Scores')
plt.ylabel('Science Scores')
plt.title('Math vs Science Scores')
plt.show()

Output:

38 | P a g e
Vansh 2822649

#Line Plot

import matplotlib.pyplot as plt

# Creating the dataset for line plot


months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
students = [10, 15, 20, 30, 40, 55]

fig = plt.figure(figsize=(10, 5))

# Creating the line plot


plt.plot(months, students, marker='o', color='purple')

plt.xlabel('Months')
plt.ylabel('Number of Students')
plt.title('Growth in Number of Students Enrolled in Python Course')
plt.show()

Output:

39 | P a g e
Vansh 2822649

Program No. 10

Aim: Write a program to implement different hypothesis test on sample dataset.


Program :-

#T-Test (sample size <30)


#One Sample Test

from scipy.stats import ttest_1samp


import numpy as np

#we are having 10 ages and mean is 30 or not


ages=[32,34,29,29,22,39,38,37,36,30,26,22,22]
print(ages)

ages_mean=np.mean(ages)
print(ages_mean)
tset,pval=ttest_1samp(ages,30)
print("p-value =",pval)

if pval<0.05:
print("we are rejecting null hypothesis")
else:
print("we are accepting the null hypothesis")

Output:

40 | P a g e
Vansh 2822649

#Z-Test (sample size >30)

import scipy.stats as stats


import numpy as np

sample_mean=110
population_mean=100
population_std=15
population_size=50
alpha=0.05

z_score=(sample_mean-population_mean)/(population_std/np.sqrt(population_size))
print('z-score =',z_score)

p_value=1-stats.norm.cdf(z_score)
print('p-value',p_value)

if p_value<alpha:
print("we are rejecting null hypothesis")
else:
print("we are accepting the null hypothesis")

Output:

41 | P a g e

You might also like