Machine Learning Manual Final
Machine Learning Manual Final
AL3461
DEPARTMENT OF
ARTIFICIAL INTELLIGENCE AND DATA SCIENCE
UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Name : ________________________
Register No : ________________________
1 Canidate–Elimination
Algorithm
2 ID3 Algorithm
3 Backpropagation Algorithm
7 Expectation Maximization
Algorithm
8 K-Nearest Neighbour
Algorithm
AIM :
ALGORITHM :
PROGRAM :
for i, h in enumerate(concepts):
print("\nInstance", i+1 , "is ", h) if target[i]
== "yes":
print("Instance is Positive ") for
x in range(len(specific_h)):
if h[x]!= specific_h[x]:
specific_h[x] ='?'
general_h[x][x] ='?' if
target[i] == "no":
print("Instance is Negative ") 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] = '?'
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")
WEATHER DATASET :
outlook temperature humidity wind answer
sunny hot high weak no
OUTPUT :
Instances are:
Target Values are: ['no' 'no' 'yes' 'yes' 'yes' 'no' 'yes' 'no' 'yes' 'yes' 'yes' 'yes' 'yes' 'no']
Generic Boundary: [['?', '?', '?', '?'], ['?', '?', '?', '?'], ['?', '?', '?', '?'], ['?', '?', '?', '?']]
Instance is Negative
Generic Boundary after 1 Instance is [['?', '?', '?', '?'], ['?', '?', '?', '?'], ['?', '?', '?', '?'], ['?', '?',
'?','?']]
Instance is Negative
Generic Boundary after 2 Instance is [['?', '?', '?', '?'], ['?', '?', '?', '?'], ['?', '?', '?', '?'], ['?', '?',
'?', 'weak']]
Instance is Positive
Instance is Positive
Generic Boundary after 4 Instance is [['?', '?', '?', '?'], ['?', '?', '?', '?'], ['?', '?', '?', '?'], ['?', '?',
'?', 'weak']]
Final Specific_h:
Final General_h:
[['?', '?', '?', '?'], ['?', '?', '?', '?'], ['?', '?', '?', '?'], ['?', '?', '?', '?']]
RESULT
Thus the above program to implement and demonstrate the Candidate-Elimination algorithm to
output a description of the set of all hypotheses consistent with the training examples has been
executed successfully and the output is verified.
EX NO :
DATE : ID3 ALGORITHM
AIM :
To write a program to demonstrate the working of the decision tree based ID3
algorithm. Use an appropriate data set for building the decision tree and apply this
knowledge to classify a new sample.
ALGORITHM :
PROGRAM :
files.upload() data =
pd.read_csv("3-dataset.csv")
features.remove("answer")
class
Node:
def __init__(self):
self.children = []
self.value = ""
self.isLeaf = False
entropy(examples):
row in examples.iterrows():
if row["answer"] == "yes":
pos += 1 else:
neg += 1 if pos ==
np.unique(examples[attr])
= entropy(examples)
in uniq:
subdata = examples[examples[attr] == u]
#print ("\n",subdata) sub_e = entropy(subdata) gain
("\n",gain)
Node() max_gain = 0
info_gain(examples, feature)
uniq = np.unique(examples[max_feat])
== u]
#print ("\n",subdata) if
entropy(subdata) == 0.0:
newNode = Node()
newNode.pred =
np.unique(subdata["answer"]) root.children.append(newNode
else
:
dummyNode = Node() dummyNode.value
=u new_attrs =
attrs.copy()
new_attrs.remove(max_feat) child
= ID3(subdata, new_attrs)
dummyNode.children.append(child)
root.children.append(dummyNode)
print("\t", end="")
print(root.value, end="") if
root.children:
iris
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target,
test_size=0.3, random_state=0) clf.fit(X_train,y_train) plot_tree(clf) r
= export_text(clf, feature_names=iris['feature_names'])
print(r)
OUTPUT :
overcast -
wind
sunny
humidity
------------------
Predicted Label for new example {'outlook': 'sunny', 'temperature': 'hot', 'humidity': 'normal',
'wind': 'strong'} is: ['yes']
| |--- class: 0
| | |--- class: 1
RESULT :
Thus the above program to demonstrate the working of the decision tree based on
ID3 algorithm has been executed successfully and the output is verified.
EX NO :
DATE : BACKPROPAGATION ALGORITHM
AIM :
ALGORITHM :
Step4 : In this step, the algorithm moves back to the hidden layers again to optimize the
weights and reduce the error.
TRAINING EXAMPLES :
1 2 9 92
2 1 5 86
3 3 6 89
NORMALIZE THE INPUT :
2/3 =
1 0.66666667 9/9 = 1 0.92
1/3 =
2 0.33333333 5/9 = 0.55555556 0.86
PROGRAM :
import numpy as np
y = np.array(([92], [86],
(x):
x)) def
derivatives_sigmoid(x):
return x * (1 - x) epoch=5
lr=0.1 inputlayer_neurons
= 2 hiddenlayer_neurons
3 output_neurons = 1
wh=np.random.uniform(
size=(inputlayer_neuron
s,hiddenlayer_neurons))
bh=np.random.uniform(
size=(1,hiddenlayer_neu
rons)) wout=np.random.unifor
m(size=(hiddenlayer_ne
urons,output_neurons))
bout=np.random.unifor
m(size=(1,output_neuro
ns))
for i in range(epoch):
hinp1=np.dot(X,wh) hinp=hinp1
+ bh hlayer_act = sigmoid(hinp)
outinp1=np.dot(hlayer_act,wout )
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
\n"
,output)
import numpy as np x =
np.array([2,1,3]) y = np.array([9,5,6])
plt.scatter(x,y) plt.show()
OUTPUT :
———–Epoch- 1 Starts———-
Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
Output:
[[0.92] [0.86]
[0.89]]
Predicted Output:
[[0.81951208]
[0.8007242 ]
[0.82485744]]
Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.82033938]
[0.80153634]
[0.82568134]]
———–Epoch- 2 Ends———-
RESULT :
AIM :
To write a program to implement the naïve Bayesian classifier for a sample training
data set stored as a .CSV file and compute the accuracy with a few test data sets.
ALGORITHM :
Step2 : Find Likelihood probability with each attribute for each class.
Step3 : Put these value in Bayes Formula and calculate posterior probability.
Step4 : See which class has a higher probability, given the input belongs to the higher
probability class.
WEATHER DATASET :
PROGRAM :
OUTPUT :
Accuracy : 95.0
RESULT :
Thus the above program to implement the naïve Bayesian classifier for a sample
training data set stored as a .CSV file and compute the accuracy with a few test data sets
has been executed successfully and the output is verified.
EX NO :
DATE : NAÏVE BAYESIAN CLASSIFIER
AIM :
ALGORITHM :
Step2 : Find Likelihood probability with each attribute for each class.
Step3 : Put these value in Bayes Formula and calculate posterior probability.
Step4 : See which class has a higher probability, given the input belongs to the higher
probability class.
PROGRAM :
= fetch_20newsgroups(subset='train') newsgroups_test
= fetch_20newsgroups(subset='test') vectorizer =
CountVectorizer(stop_words='english')
X_train = vectorizer.fit_transform(newsgroups_train.data)
X_test = vectorizer.transform(newsgroups_test.data) nb =
MultinomialNB() nb.fit(X_train,
newsgroups_train.target) y_pred
= nb.predict(X_test)
print(f"Recall: {recall:.4f}")
OUTPUT :
Accuracy: 0.8023
Precision: 0.8130
Recall: 0.7942
RESULT :
Thus the above program to implement naïve Bayesian Classifier model to classify
a set of documents and measure the accuracy, precision, and recall has been executed
successfully and the output is verified.
EX NO :
DATE : BAYESIAN NETWORK
AIM :
ALGORITHM :
COVID DATASET :
LAST
UPDATE
PROGRAM :
numpy as np from
sklearn.mixture import
GaussianMixture data =
pd.read_csv('covid_19_data.csv') data =
data.select_dtypes(include=[np.number]) em
= GaussianMixture(n_components=3)
print(labels)
OUTPUT :
[000...012]
RESULT
Thus the above program to construct a Bayesian network to diagnose CORONA infection
using standard WHO Data Set has been executed successfully and the output is verified.
EX NO :
DATE : EXPECTATION MAXIMIZATION ALGORITHM
AIM :
ALGORITHM :
Step4 : Now check the values of latent variables whether it is converging or not and then
stop the process.
PROGRAM :
matplotlib.pyplot as plt
dataset=load_iris()
X=pd.DataFrame(dataset.data)
X.columns=['Sepal_Length','Sepal_Width','Petal_Length','Petal_Width']
y=pd.DataFrame(dataset.target)
plt.subplot(1,3,1)
plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[y.Targets],s=40
model.fit(X) predY=np.choose(model.labels_,[0,1,2]).astype(np.int64)
plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[predY],s=40 )
xsa=scaler.transform(X) xs=pd.DataFrame(xsa,columns=X.columns)
gmm=GaussianMixture(n_components=3) gmm.fit(xs)
y_cluster_gmm=gmm.predict(xs) plt.subplot(1,3,3)
plt.scatter(X.Petal_Length,X.Petal_Width,c=colormap[y_cluster_gmm],
RESULT :
Thus the above program to apply EM algorithm has been executed successfully
and the output is verified.
EX NO :
DATE : K-NEAREST NEIGHBOUR ALGORITHM
AIM :
ALGORITHM :
IRIS DATASET :
PROGRAM :
AIM :
ALGORITHM :
Step1 : Read the Given data Sample to X and the curve (linear or non linear) to Y.
Step2 : Set the value for Smoothening parameter or Free parameter say τ.
PROGRAM :
import matplotlib.pyplot as
= np.shape(xmat) weights
= np.mat(np.eye((m)))
for j in range(m):
k):
wei = kernel(point,xmat,k)
W = (X.T*(wei*X)).I*(X.T*(wei*ymat.T))
range(m): ypred[i] =
X = np.hstack((one.T,mbill.T)) ypred =
localWeightRegression(X,mtip,0.5)
ax.scatter(bill,tip, color='green')
OUTPUT :
RESULT :