Experiment 2:
AIM:
Implementation of logical rules in python
CODES:
#1.AND operation
def and2():
for A in [True,False]:
for B in [True,False]:
print(f"{A}.{B} | {A and B}")
def and3():
for A in [True,False]:
for B in [True,False]:
for C in [True,False]:
print(f"{A}.{B}.{C} | {A and B and C}")
and2()
and3()
Output:
True.True | True
True.False | False
False.True | False
False.False | False
True.True.True | True
True.True.False | False
True.False.True | False
True.False.False | False
False.True.True | False
False.True.False | False
False.False.True | False
False.False.False | False
#2.OR operation
def or2():
for A in [True,False]:
for B in [True,False]:
print(f"{A}+{B} | {A or B}")
def or3():
for A in [True,False]:
for B in [True,False]:
for C in [True,False]:
print(f"{A}+{B}+{C} | {A or B or C}")
or2()
or3()
Output:
True+True | True
True+False | True
False+True | True
False+False | False
True+True+True | True
True+True+False | True
True+False+True | True
True+False+False | True
False+True+True | True
False+True+False | True
False+False+True | True
False+False+False | False
#3.not operation
def Not():
for A in [True,False]:
print(f"~A({A})={not A}")
Not()
Output:
~A(True)=False
~A(False)=True
#4.IMPLIES operation
def implies2():
for A in [True,False]:
for B in [True,False]:
print(f"A={A} => B={B} | {not A or B}")
def implies3():
for A in [True,False]:
for B in [True,False]:
for C in [True,False]:
print(f"A={A} => B={B} => C={C}| {(not A or B)or(not B or C)}")
implies2()
implies3()
Output:
A=True => B=True | True
A=True => B=False | False
A=False => B=True | True
A=False => B=False | True
A=True => B=True => C=True| True
A=True => B=True => C=False| True
A=True => B=False => C=True| True
A=True => B=False => C=False| True
A=False => B=True => C=True| True
A=False => B=True => C=False| True
A=False => B=False => C=True| True
A=False => B=False => C=False| True
#5.BIIMPLICATION operation
def biimp():
for A in [True,False]:
for B in [True,False]:
print(f"A={A} <=> B={B} | {A==B}")
biimp()
Output:
A=True <=> B=True | True
A=True <=> B=False | False
A=False <=> B=True | False
A=False <=> B=False | True
#6.XOR operation
def xor():
for A in [True,False]:
for B in [True,False]:
print(f"A={A} xor B={B} | {(A or B)and(not A or not B)}")
xor()
Output:
A=True xnor B=True | True
A=True xnor B=False | False
A=False xnor B=True | False
A=False xnor B=False | True
Result:
Hence implementing the logical rules using python is exceuted
Implementation of logical rules in python
def or_oper(p,q):
return(bool(p) or bool(q))
def and_oper(p,q):
return(bool(p) and bool(q))
def not_oper(p):
return(not (bool(p)))
def imply_oper(p,q):
return(not(bool(p)) or bool(q))
def doubleimply_oper(p,q):
m=bool(p)
n=bool(q)
return((not(m) or n) and (not(n) or m))
lis=["p","q","p or q","p^q","~p","~q","p->q","p<=>q"]
for i in lis:
print(i,end="\t")
print("\n")
for i in range(1,-1,-1):
for j in range(1,-1,-1):
A=[bool(i),bool(j),or_oper(i,j),and_oper(i,j),not_oper(i),not_oper(j),imply_oper(i,j),double
imply_oper(i,j)]
for i in A:
print(i,end="\t")
print("\n")
p q p or q p^q ~p ~q p->q p<=>q
True True True True False False True True
True False True False False True False False
False True True False True False True False
False False False False True True True True
EXPERIMENT-7
Aim: To explore statistical methods for Machine Learning
Code:
1. Mean:
import numpy as np
x=np.array([12,15,20,22,25,28,32,35,40,45])
m=np.mean(x)
print(f"mean of the data is {m}")
Output:
mean of the data is 27.4
2. Variance:
import numpy as np
x=np.array([12,15,20,22,25,28,32,35,40,45])
v=np.var(x)
print(f"variance of the data is {v}")
Output:
variance of the data is 102.83999999999999
3. Mode:
import numpy as np
from scipy import stats
x=np.array([12,15,20,20,22,25,28,32,35,40,45])
mod=stats.mode(x)
print(f"mode of the data is {mod.mode[0]}")
Output:
Mode of the data is 20
4. Median:
import numpy as np
x=np.array([12,15,20,22,25,28,32,35,40,45])
med=np.median(x)
print(f"median of the data is {med}")
Output:
median of the data is 26.5
5. Covariance:
import math
import numpy as np
x=np.array([12,15,20,22,25])
y=np.array([28,32,35,40,45])
m1=np.mean(x)
m2=np.mean(y)
v1=np.var(x)
v2=np.var(y)
cov=np.sum((x-m1)*(y-m2))/(len(x)-1)
print(f"mean of the data is {m1}")
print(f"mean of the data is {m2}")
print(f"The covariance of data is {cov}")
Output:
mean of the data is 18.8
mean of the data is 36.0
The covariance of data is 34.25
6. Standard deviation:
import numpy as np
x=np.array([12,15,20,22,25,28,32,35,40,45])
d=np.std(x)
print(f"standard deviation of the data is {d}")
Output:
standard deviation of the data is 10.141005867269774
7. Correlation coefficient:
import math
import numpy as np
x=np.array([12,15,20,22,25])
y=np.array([28,32,35,40,45])
m1=np.mean(x)
m2=np.mean(y)
v1=np.var(x)
v2=np.var(y)
cov=np.sum((x-m1)*(y-m2))/(len(x)-1)
r=cov/(math.sqrt(v1)*math.sqrt(v2))
print(f"mean of the data is {m1}")
print(f"mean of the data is {m2}")
print(f"The correlation coefficient is {r}")
Output:
mean of the data is 18.8
mean of the data is 36.0
The correlation coefficient is 1.21941265786219
8. Correlation plot:
import numpy as np
import matplotlib.pyplot as plt
x=np.array([12,15,20,22,25])
y=np.array([28,32,35,40,45])
m1=np.mean(x)
m2=np.mean(y)
v1=np.var(x)
v2=np.var(y)
num=np.sum((x-m1)*(y-m2))
den=np.sum((x-m1)**2)
m=num/den
c=m2-(m*m1)
p=(m*x)+c
cov=np.sum((x-m1)*(y-m2))/(len(x))
r=cov/(math.sqrt(v1)*math.sqrt(v2))
print(f"mean of the data is {m1}")
print(f"mean of the data is {m2}")
print(f"The correlation coefficient is {r}")
plt.scatter(x,y,label='datapoints',color='red')
plt.plot(x,p,label='regression line',color='blue')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
Output:
mean of the data is 18.8
mean of the data is 36.0
The correlation coefficient is 0.9755301262897568
9. Uniform Distribution:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy.stats as stats
uniform_data = np.random.uniform(0, 10, 100000)
pd.Series(uniform_data).plot(kind="density", figsize=(9, 9), xlim=(-1, 11))
plt.show()
Output:
10. Normal distribution:
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
prob_under_minus1 = stats.norm.cdf(x=-1, loc=0, scale=1)
prob_over_1 = 1 - stats.norm.cdf(x=1, loc=0, scale=1)
between_prob = 1 - (prob_under_minus1 + prob_over_1)
plt.rcParams["figure.figsize"] = (7, 7)
x_range1 = np.arange(-4, -1, 0.01)
x_range2 = np.arange(1, 4, 0.01)
x_range3 = np.arange(-1, 1, 0.01)
plt.fill_between(x=x_range1, y1=stats.norm.pdf(x_range1), facecolor='red',
alpha=0.35)
plt.fill_between(x=x_range2, y1=stats.norm.pdf(x_range2), facecolor='red',
alpha=0.35)
plt.fill_between(x=x_range3, y1=stats.norm.pdf(x_range3), facecolor='red',
alpha=0.35)
plt.text(x=-1.8, y=0.03, s=round(prob_under_minus1, 3))
plt.text(x=-0.2, y=0.1, s=round(between_prob, 3))
plt.text(x=1.4, y=0.03, s=round(prob_over_1, 3))
plt.show()
Output: