[go: up one dir, main page]

0% found this document useful (0 votes)
14 views34 pages

Pda Lab Programs

The document outlines a series of Python lab programs focusing on various programming concepts, including recursion, dictionaries, classes, method resolution order, and operator overloading. It also includes a comprehensive example of data analysis using pandas to compute correlation, joint probabilities, marginal probabilities, and conditional probabilities from a CSV file. Each lab program is designed to enhance understanding of Python's capabilities in data manipulation and object-oriented programming.

Uploaded by

spamyuvraj555
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)
14 views34 pages

Pda Lab Programs

The document outlines a series of Python lab programs focusing on various programming concepts, including recursion, dictionaries, classes, method resolution order, and operator overloading. It also includes a comprehensive example of data analysis using pandas to compute correlation, joint probabilities, marginal probabilities, and conditional probabilities from a CSV file. Each lab program is designed to enhance understanding of Python's capabilities in data manipulation and object-oriented programming.

Uploaded by

spamyuvraj555
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
You are on page 1/ 34

LAB PROGRAM 1

Write a python program to find sum of n natural


numbers using recursive function
LAB PROGRAM 2

Write a Python Program to Create a Dictionary with


Key as First Character and Value as Words Starting
with that Character.
LAB PROGRAM 3

Implement a Python program to count the numbers


of characters in the string and store them in a
dictionary data structure
LAB PROGRAM 4

Design and Develop a Python Program to Append,


Delete and Display Elements of a List Using Classes
and Objects.
LAB PROGRAM 5

Demonstrate the concept of Method Resolution


order in multiple inheritance in Python Program.
LAB PROGRAM 6

Design and Implement a Python Program to perform


addition, subtraction, multiplication of two complex
numbers using binary operators overloading.
LAB PROGRAM 7

Demonstrate with a python program to show the speed


of execution is more when using numpy array
LAB PROGRAM 8

Write a program to read the data and perform


correlation, Two way conditional probability, joint
probability and marginal probability.
STEPS
Use Python Code to create CSV
import pandas as pd

# Define data as a dictionary


data = {
'A': [1, 1, 0, 1],
'B': [0, 1, 1, 0]
}

# Convert data to DataFrame


df = pd.DataFrame(data)

# Save DataFrame to CSV


df.to_csv('data.csv', index=False)
File data.csv created with following content
A,B
1,0
1,1
0,1
1,0
Compute correlation
Correlation: We use the data.corr() method to calculate the correlation
matrix of the columns.

correlation = data.corr()
print("\nCorrelation between A and B:")
print(correlation)
Compute joint probabilities.

Joint Probability: pd.crosstab() is used to get the joint frequency table,


and we divide by the total count to get the joint probability.

joint_freq = pd.crosstab(data['A'], data['B’])


joint_prob = joint_freq / joint_freq.values.sum()
print("\nJoint Probability Distribution of A and B:")
print(joint_prob)
Compute marginal probabilities
Marginal Probability: Summing over rows or # Marginal probability of B
columns of the joint probability distribution
gives the marginal probabilities. marginal_prob_B =
joint_prob.sum(axis=0)
# Marginal probability of A print("\nMarginal Probability of B:")
marginal_prob_A = print(marginal_prob_B)
joint_prob.sum(axis=1)
print("\nMarginal Probability of •axis=1 sums across columns (for
A:") marginalizing over B to get P(A)).
•axis=0 sums across rows (for marginalizing
print(marginal_prob_A)
over A to get P(B)).
Marginal Probability Of A
Marginal Probability Of B
Compute conditional probabilities
Conditional Probability: By dividing the joint probability by the marginal
probability of one variable, we get the conditional probability.
# Conditional probability P(A | B) = P(A and B) / P(B)
conditional_prob_A_given_B = joint_prob.div(marginal_prob_B, axis=1)
print("\nConditional Probability P(A | B):")
print(conditional_prob_A_given_B)

# Conditional probability P(B | A) = P(A and B) / P(A)


conditional_prob_B_given_A = joint_prob.div(marginal_prob_A, axis=0)
print("\nConditional Probability P(B | A):")
print(conditional_prob_B_given_A)
import pandas as pd
import numpy as np
# Step 1: Load the data
# Assume we have a CSV file with columns A and B
data = pd.read_csv('data.csv')
# Display data
print("Data:")
print(data)
# Step 2: Calculate correlation between A and B
correlation = data.corr()
print("\nCorrelation between A and B:")
print(correlation)
# Step 3: Compute joint probability distribution of A and B
# Create a cross-tabulation (joint frequency table) of A and B
joint_freq = pd.crosstab(data['A'], data['B'])
joint_prob = joint_freq / joint_freq.values.sum()
print("\nJoint Probability Distribution of A and B:")
print(joint_prob)
# Step 4: Compute marginal probabilities
# Marginal probability of A
marginal_prob_A = joint_prob.sum(axis=1)
print("\nMarginal Probability of A:")
print(marginal_prob_A)
# Marginal probability of B
marginal_prob_B = joint_prob.sum(axis=0)
print("\nMarginal Probability of B:")
print(marginal_prob_B)
# Step 5: Compute two-way conditional probabilities
# Conditional probability P(A | B) = P(A and B) / P(B)
conditional_prob_A_given_B = joint_prob.div(marginal_prob_B, axis=1)
print("\nConditional Probability P(A | B):")
print(conditional_prob_A_given_B)
# Conditional probability P(B | A) = P(A and B) / P(A)
conditional_prob_B_given_A = joint_prob.div(marginal_prob_A, axis=0)
print("\nConditional Probability P(B | A):")
print(conditional_prob_B_given_A)

You might also like