AAI Pract 3 To 12
AAI Pract 3 To 12
External Examiner
2
3 Implement Bayes Theorem using Python
4 Implement Conditional Probability and
joint probability using Python
5 Write a Program to implement BFS
algorithm.
6
7
8
10
11
12
Bayes’ Theorem states the following for any two events A and B:
where:
For example, suppose the probability of the weather being cloudy is 40%.
If it’s cloudy outside on a given day, what is the probability that it will rain that
day?
Solution:
P(cloudy) = 0.40
P(rain) = 0.20
P(cloudy | rain) = 0.85
If it’s cloudy outside on a given day, the probability that it will rain that day
is 42.5%.
We can create the following simple function to apply Bayes’ Theorem in Python:
P(rain) = 0.20
P(cloudy) = 0.40
P(cloudy | rain) = 0.85
#define probabilities
pRain = 0.2
pCloudy = 0.4
pCloudyRain = 0.85
0.425
Conditional Probability
Where:
Joint Probability
Joint Probability represents the likelihood that two events, X and Y, occur together.
It’s denoted as P(X = x, Y = y), which can be read as the probability that both X = x
and Y = y are true.
The formula for calculating joint probability, P(X = x, Y = y), using conditional
probabilities, is:
This expression means that the joint probability of X and Y can be calculated by:
Let’s use a dataset of individuals categorized by age group and interest in sports and
compute joint and conditional probabilities.
We’ll calculate the joint probability of a person being in the “Teen” age group and
having an interest in sports (“Yes”).
To make these calculations more flexible, we’ll define two functions: one for joint
probability and one for conditional probability.
# Joint Probability
joint_prob = calculate_joint_probability(df, ("Age_Group",
"Teen"), ("Sports_Interest", "Yes"))
print("Joint Probability (Teen and Sports Interest Yes):",
joint_prob)
# Conditional Probability
conditional_prob = calculate_conditional_probability(df,
("Age_Group", "Teen"), ("Sports_Interest", "Yes"))
print("Conditional Probability (Sports Interest Yes | Age Group
Teen):", conditional_prob)
Output:
Program:
Output:
import math
2. Euler’s Number
import math
print (math.e)
3. Pi
import math
print (math.pi)
import math
r=4
pie = math.pi
print(pie * r * r)
import math
a=5
import math
test_int = 4
test_neg_int = -3
test_float = 0.00
print (math.exp(test_int))
print (math.exp(test_neg_int))
print (math.exp(test_float))
import math
print (math.log2(16))
print (math.log10(10000))
import math
print(math.sqrt(0))
print(math.sqrt(4))
print(math.sqrt(3.5))
import math
a = math.pi/6
print (math.sin(a))
print (math.cos(a))
print (math.tan(a))
print(constants.liter)
def eqn(x):
return x + cos(x)
myroot = root(eqn, 0)
print(myroot.x)
def eqn(x):
return x**2 + x + 2
print(mymin)
import numpy as np
from scipy.sparse.csgraph import dijkstra
from scipy.sparse import csr_matrix
arr = np.array([
[0, 1, 2],
[1, 0, 0],
[2, 0, 0]
])
newarr = csr_matrix(arr)
5. Use the floyd_warshall() method to find shortest path between all pairs of
elements.
import numpy as np
from scipy.sparse.csgraph import floyd_warshall
from scipy.sparse import csr_matrix
arr = np.array([
[0, 1, 2],
[1, 0, 0],
[2, 0, 0]
])
newarr = csr_matrix(arr)
print(floyd_warshall(newarr, return_predecessors=True))
6. Use bellman ford(), Find shortest path from element 1 to 2 with given graph
with a negative weight:
import numpy as np
from scipy.sparse.csgraph import bellman_ford
arr = np.array([
[0, -1, 2],
[1, 0, 0],
[2, 0, 0]
])
newarr = csr_matrix(arr)
import numpy as np
from scipy.sparse.csgraph import depth_first_order
from scipy.sparse import csr_matrix
arr = np.array([
[0, 1, 0, 1],
[1, 1, 1, 1],
[2, 1, 1, 0],
[0, 1, 0, 1]
])
newarr = csr_matrix(arr)
print(depth_first_order(newarr, 1))
import numpy as np
from scipy.sparse.csgraph import breadth_first_order
from scipy.sparse import csr_matrix
arr = np.array([
[0, 1, 0, 1],
[1, 1, 1, 1],
newarr = csr_matrix(arr)
print(breadth_first_order(newarr, 1))
Program 1:
# Import pandas
import pandas as pd
df = pd.read_csv("example1.csv")
print(df)
Output:
import pandas as pd
# Assign data
df = pd.DataFrame(data)
# Display data
print(df)
Output:
1)Vector
Output:
2)2D Matrix
Output:
1.Bar Graph
import numpy as np
courses = list(data.keys())
values = list(data.values())
plt.xlabel("Courses offered")
plt.show()
Output:
import numpy as np
data = np.random.randn(1000)
plt.xlabel('Values')
plt.ylabel('Frequency')
plt.title('Basic Histogram')
plt.show()
Output:
Program:
import numpy as np
x = np.array([1, 2, 3, 4])
y = x*2
plt.plot(x, y)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.figure()
x1 = [2, 4, 6, 8]
plt.show()
Output:
Program:
x =[5, 7, 8, 7, 2, 17, 2, 9,
4, 11, 12, 9, 6]
plt.scatter(x, y, c ="blue")
plt.show()
Output:
# Splitting the dependent and independent features of the dataset into training
and testing dataset
X_train, X_test, Y_train, Y_test = train_test_split( X, Y, test_size = 0.3, rand
om_state = 10 )
# Creating an array of predictions made by the model for the unseen or test da
taset
Y_pred = lr.predict( X_test )
# The value of the coefficients for the independent feature through the multipl
e regression model
print("Value of the oefficients: \n", lr.coef_)
Output: