Shashidhar-18csl76 Final
Shashidhar-18csl76 Final
Shashidhar-18csl76 Final
A MANUAL FOR
(18CSL76)
VII SEMESTER
PREPARED BY
Mr. SHASHIDHAR V
Asst. Professor
Dept. of Computer Science & Engineering
R.R.C.E, Bangalore
Output:
Path
A -> F -> G -> I -> J
Cost
0 -> 3 -> 4 -> 7 -> 10
Program-2: Implement AO* Search algorithm.
Output:
Graphs-1
PROCESSING NODE : A
----------------------------------------------------------------
10 ['B', 'C']
PROCESSING NODE : B
----------------------------------------------------------------
6 ['G']
PROCESSING NODE : A
----------------------------------------------------------------
10 ['B', 'C']
PROCESSING NODE : G
----------------------------------------------------------------
4 ['I']
PROCESSING NODE : B
----------------------------------------------------------------
5 ['G']
PROCESSING NODE : A
----------------------------------------------------------------
9 ['B', 'C']
PROCESSING NODE : I
----------------------------------------------------------------
0 []
PROCESSING NODE : G
----------------------------------------------------------------
1 ['I']
PROCESSING NODE : B
----------------------------------------------------------------
2 ['G']
PROCESSING NODE : A
----------------------------------------------------------------
6 ['B', 'C']
PROCESSING NODE : C
----------------------------------------------------------------
2 ['J']
PROCESSING NODE : A
----------------------------------------------------------------
6 ['B', 'C']
PROCESSING NODE : J
----------------------------------------------------------------
0 []
PROCESSING NODE : C
----------------------------------------------------------------
1 ['J']
PROCESSING NODE : A
----------------------------------------------------------------
5 ['B', 'C']
FOR THE SOLUTION, TRAVERSE THE GRAPH FROM THE START NODE: A
------------------------------------------------------------
{'I': [], 'G': ['I'], 'B': ['G'], 'J': [], 'C': ['J'], 'A':
['B', 'C']}
------------------------------------------------------------
Program-3: For a given set of training data examples stored in a
.CSV file, implement and demonstrate the Candidate-Elimination
algorithm to output a description of the set of all hypotheses
consistent with the training examples.
import numpy as np
import pandas as pd
data = pd.DataFrame(data=pd.read_csv('play2.csv'))
concepts = np.array(data.iloc[:,0:-1])
target = np.array(data.iloc[:,-1])
def learn(concepts, target):
specific_h = concepts[0].copy()
print("initialization of specific_h and general_h")
print(specific_h)
general_h = [["?" for i in range(len(specific_h))] for i in
range(len(specific_h))]
print(general_h)
for i, h in enumerate(concepts):
if target[i] == "Yes":
for x in range(len(specific_h)):
if h[x] != specific_h[x]:
specific_h[x] = '?'
general_h[x][x] = '?'
if target[i] == "No":
for x in range(len(specific_h)):
if h[x] != specific_h[x]:
general_h[x][x] = specific_h[x]
else:
general_h[x][x] = '?'
print(" steps of Candidate Elimination Algorithm",i+1)
print("Specific_h ",i+1,"\n ")
print(specific_h)
print("general_h ", i+1, "\n ")
print(general_h)
indices = [i for i, val in enumerate(general_h) if val ==
['?', '?', '?', '?', '?', '?']]
for i in indices:
general_h.remove(['?', '?', '?', '?', '?', '?'])
return specific_h, general_h
s_final, g_final = learn(concepts, target)
print("Final Specific_h:", s_final, sep="\n")
print("Final General_h:", g_final, sep="\n")
Output:
[['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'],
['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'],
['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?']]
[['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'],
['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'],
['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?']]
[['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'],
['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'],
['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?']]
[['Sunny', '?', '?', '?', '?', '?'], ['?', 'Warm', '?', '?',
'?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?',
'?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?',
'?', 'Same']]
[['Sunny', '?', '?', '?', '?', '?'], ['?', 'Warm', '?', '?',
'?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?',
'?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?',
'?', '?']]
[['Sunny', '?', '?', '?', '?', '?'], ['?', 'Warm', '?', '?',
'?', '?']]
Program-4: Decision Tree ID3 Algorithm Machine Learning
def find_entropy(df):
Class = df.keys()[-1]
entropy = 0
values = df[Class].unique()
for value in values:
fraction =
df[Class].value_counts()[value]/len(df[Class])
entropy += -fraction*np.log2(fraction)
return entropy
def find_entropy_attribute(df,attribute):
Class = df.keys()[-1]
target_variables = df[Class].unique()
variables = df[attribute].unique()
entropy2 = 0
for variable in variables:
entropy = 0
for target_variable in target_variables:
num =
len(df[attribute][df[attribute]==variable][df[Class]
==target_variable])
den = len(df[attribute][df[attribute]==variable])
fraction = num/(den+eps)
entropy += -fraction*log(fraction+eps)
fraction2 = den/len(df)
entropy2 += -fraction2*entropy
return abs(entropy2)
def find_winner(df):
Entropy_att = []
IG = []
for key in df.keys()[:-1]:
IG.append(find_entropy(df)-
find_entropy_attribute(df,key))
return df.keys()[:-1][np.argmax(IG)]
def buildTree(df,tree=None):
Class = df.keys()[:-1]
node = find_winner(df)
attValue = np.unique(df[node])
if tree is None:
tree={}
tree[node] = {}
for value in attValue:
subtable = get_subtable(df,node,value)
clValue,counts =
np.unique(subtable['play'],return_counts=True)
if len(counts)==1:
tree[node][value] = clValue[0]
else:
tree[node][value] = buildTree(subtable)
return tree
import pandas as pd
import numpy as np
eps = np.finfo(float).eps
from numpy import log2 as log
df = pd.read_csv('play2.csv')
print("\n Given Play Tennis Data Set:\n\n",df)
tree= buildTree(df)
import pprint
pprint.pprint(tree)
"""test={'Outlook':'Sunny','Temperature':'Hot','Humidity':'High'
,'Wind':'Weak'}
def func(test, tree, default=None):
attribute = next(iter(tree))
print(attribute)
if test[attribute] in tree[attribute].keys():
print(tree[attribute].keys())
print(test[attribute])
result = tree[attribute][test[attribute]]
if isinstance(result, dict):
return func(test, result)
else:
return result
else:
return default
ans = func(test, tree)
print(ans)
"""
Output:
import numpy as np
X=np.array(([2,9],[1,5],[3,6]),dtype=float)
y=np.array(([92],[86],[89]),dtype=float)
X=X/np.amax(X,axis=0)
y=y/100
def sigmoid(x):
return 1/(1+np.exp(-x))
def derivatives_sigmoid(x):
return x*(1-x)
epoch=7000
lr=0.25
inputlayer_neurons=2
hiddenlayer_neurons=3
output_neurons=1
wh=np.random.uniform(size=(inputlayer_neurons,hiddenlayer_neuron
s))
bh=np.random.uniform(size=(1,hiddenlayer_neurons))
wout=np.random.uniform(size=(hiddenlayer_neurons,output_neurons)
)
bout=np.random.uniform(size=(1,output_neurons))
for i in range(epoch):
hinp1=np.dot(X,wh)
hinp=hinp1+bh
hlayer_act=sigmoid(hinp)
outinp1=np.dot(hlayer_act,wout)
outinp=outinp1+bout
output=sigmoid(outinp)
EO=y-output
outgrad=derivatives_sigmoid(output)
d_output=EO*outgrad
EH=d_output.dot(wout.T)
hiddengrad=derivatives_sigmoid(hlayer_act)
d_hiddenlayer=EH*hiddengrad
wout+=hlayer_act.T.dot(d_output)*lr
wh+=X.T.dot(d_hiddenlayer)*lr
print("Input=\n"+str(X))
print("Actual output:\n"+str(y))
print("predicated output:",output)
Output:
Input=
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual output:
[[0.92]
[0.86]
[0.89]]
predicated output: [[0.89494013]
[0.87827289]
[0.896573 ]]
Program-6: Write a program to implement the naive Bayesian
classifier for a sample training data set stored as a .CSV file.
Compute the accuracy of the classifier, considering few test
data sets.
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
data = pd.read_csv('tennis.csv')
print("The first 5 Values of data is :\n", data.head())
X = data.iloc[:, :-1]
print("\nThe First 5 values of the train attributes is\n",
X.head())
Y = data.iloc[:, -1]
print("\nThe First 5 values of target values is\n", Y.head())
obj1= LabelEncoder()
X.Outlook = obj1.fit_transform(X.Outlook)
print("\n The Encoded and Transformed Data in Outlook
\n",X.Outlook)
obj2 = LabelEncoder()
X.Temperature = obj2.fit_transform(X.Temperature)
obj3 = LabelEncoder()
X.Humidity = obj3.fit_transform(X.Humidity)
obj4 = LabelEncoder()
X.Wind = obj4.fit_transform(X.Wind)
print("\n The Encoded and Transformed Training Examples \n",
X.head())
obj5 = LabelEncoder()
Y = obj5.fit_transform(Y)
print("The class Label encoded in numerical form is",Y)
iris = datasets.load_iris()
X = pd.DataFrame(iris.data)
X.columns =
['Sepal_Length','Sepal_Width','Petal_Length','Petal_Width']
y = pd.DataFrame(iris.target)
y.columns = ['Targets']
model = KMeans(n_clusters=3)
model.fit(X) # model.labels_ : Gives cluster no for which
samples belongs to
plt.figure(figsize=(14,7))
colormap = np.array(['red', 'lime', 'black'])
plt.subplot(1, 3, 1)
plt.scatter(X.Petal_Length, X.Petal_Width,
c=colormap[y.Targets], s=40)
plt.title('Real Clusters')
plt.xlabel('Petal Length')
plt.ylabel('Petal Width')
plt.subplot(1, 3, 2)
plt.scatter(X.Petal_Length, X.Petal_Width,
c=colormap[model.labels_], s=40)
plt.title('K-Means Clustering')
plt.xlabel('Petal Length')
plt.ylabel('Petal Width')
scaler = preprocessing.StandardScaler()
scaler.fit(X)
xsa = scaler.transform(X)
xs = pd.DataFrame(xsa, columns = X.columns)
from sklearn.mixture import GaussianMixture
gmm = GaussianMixture(n_components=40)
gmm.fit(xs)
plt.subplot(1, 3, 3)
plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[0], s=40)
plt.title('GMM Clustering')
plt.xlabel('Petal Length')
plt.ylabel('Petal Width')
Output:
Program-8: Write a program to implement k-Nearest Neighbour
algorithm to classify the iris data set. Print both correct and
wrong predictions. Java/Python ML library classes can be used
for this problem.
Output:
import numpy as np
import matplotlib.pyplot as plt
def draw(tau):
prediction = [local_regression(x0, X, Y, tau) for x0 in
domain]
plt.plot(X, Y, 'o', color='black')
plt.plot(domain, prediction, color='red')
plt.show()
X = np.linspace(-3, 3, num=1000)
domain = X
Y = np.log(np.abs(X ** 2 - 1) + .5)
draw(10)
draw(0.1)
draw(0.01)
draw(0.001)
Output: