ML Lab Manual

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

1.

Implement and demonstrate the FIND-S algorithm for finding the most specific hypothesis based on a
given set of training data samples. Read the training data from a .CSV file.
import csv
def loadCsv(filename):
lines = csv.reader(open(filename, "r"))
dataset = list(lines)
for i in range(len(dataset)):
dataset[i] = dataset[i]
return dataset

attributes = ['Sky','Temp','Humidity','Wind','Water','Forecast']
print('Attributes =',attributes)
num_attributes = len(attributes)

filename = "E:/ws.csv"
dataset = loadCsv(filename)
print(dataset)
hypothesis=['0'] * num_attributes
print("Intial Hypothesis")
print(hypothesis)
print("The Hypothesis are")
for i in range(len(dataset)):
target = dataset[i][-1]
if(target == 'Yes'):
for j in range(num_attributes):
if(hypothesis[j]=='0'):
hypothesis[j] = dataset[i][j]
if(hypothesis[j]!= dataset[i][j]):
hypothesis[j]='?'
print(i+1,'=',hypothesis)
print("Final Hypothesis")
print(hypothesis)

Result:
Attributes = ['Sky', 'Temp', 'Humidity', 'Wind', 'Water', 'Forecast']
[['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same', 'Yes'], ['Sunny',
'Warm', 'High', 'Strong', 'Warm', 'Same', 'Yes'], ['Rainy', 'Cold', 'High',
'Strong', 'Warm', 'Change', 'No'], ['Sunny', 'Warm', 'High', 'Strong',
'Cool', 'Change', 'Yes']]
Intial Hypothesis
['0', '0', '0', '0', '0', '0']
The Hypothesis are
1 = ['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same']
2 = ['Sunny', 'Warm', '?', 'Strong', 'Warm', 'Same']
4 = ['Sunny', 'Warm', '?', 'Strong', '?', '?']
Final Hypothesis
['Sunny', 'Warm', '?', 'Strong', '?', '?']

2. 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.read_csv('E:/ws.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")

Result:
initialization of specific_h and general_h
['Sunny' 'Warm' 'Normal' 'Strong' 'Warm' 'Same']
[['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?',
'?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?',
'?', '?'], ['?', '?', '?', '?', '?', '?']]
steps of Candidate Elimination Algorithm 4
Specific_h 4

['Sunny' 'Warm' '?' 'Strong' '?' '?']


general_h 4

[['Sunny', '?', '?', '?', '?', '?'], ['?', 'Warm', '?', '?', '?', '?'],
['?', '?', '?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?'], ['?', '?',
'?', '?', '?', '?'], ['?', '?', '?', '?', '?', '?']]
Final Specific_h:
['Sunny' 'Warm' '?' 'Strong' '?' '?']
Final General_h:
[['Sunny', '?', '?', '?', '?', '?'], ['?', 'Warm', '?', '?', '?', '?']]

Experiment-3: 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.

import pandas as pd
import numpy as np
dataset= pd.read_csv('E:\
PlayTennis.csv',names=['outlook','temperature','humidity','wind','class',])
def entropy(tcol):
ele,c = np.unique(tcol,return_counts = True)
entropy = np.sum([(-c[i]/np.sum(c))*np.log2(c[i]/np.sum(c)) for i in
range(len(ele))])
return entropy

def InfoGain(data,san,tname="class"):
tetry = entropy(data[tname])
vals,c= np.unique(data[san],return_counts=True)
WE =
np.sum([(c[i]/np.sum(c))*entropy(data.where(data[san]==vals[i]).dropna()
[tname]) for i in range(len(vals))])
Inf_Gain = tetry - WE
return Inf_Gain

def ID3(data,odata,fts,tan="class",pnc = None):


if len(np.unique(data[tan])) <= 1:
return np.unique(data[tan])[0]
elif len(data)==0:
return np.unique(odata[tan])
[np.argmax(np.unique(odata[tan],return_counts=True)[1])]
elif len(fts) ==0:
return pnc
else:
pnc = np.unique(data[tan])
[np.argmax(np.unique(data[tan],return_counts=True)[1])]
item_vals = [InfoGain(data,f,tan) for f in fts]
#Return the information gain values for the features in the dataset
bfi = np.argmax(item_vals)
bf = fts[bfi]
tree = {bf:{}}
fts = [i for i in fts if i != bf]
for value in np.unique(data[bf]):
value = value
sub_data = data.where(data[bf] == value).dropna()
subtree = ID3(sub_data,dataset,fts,tan,pnc)
tree[bf][value] = subtree
return(tree)

tree = ID3(dataset,dataset,dataset.columns[:-1])
print(' \nDisplay Tree\n',tree)

Output:
Display Tree
{'outlook': {'Overcast': 'Yes', 'Rain': {'wind': {'Strong': 'No', 'Weak':
'Yes'}}, 'Sunny': {'humidity': {'High': 'No', 'Normal': 'Yes'}}}}

Experiment-4: a). Linear Regression.

# Import the packages and classes needed for this example:


import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# Create random data with numpy, and plot it with matplotlib:


rnstate = np.random.RandomState(1)
x = 10 * rnstate.rand(50)
y = 2 * x - 5 + rnstate.randn(50)
plt.scatter(x, y);
plt.show()

# Create a linear regression model based the positioning of the data and Intercept, and predict a Best Fit:
model = LinearRegression(fit_intercept=True)
model.fit(x[:, np.newaxis], y)
xfit = np.linspace(0, 10, 1000)
yfit = model.predict(xfit[:, np.newaxis])

# Plot the estimated linear regression line with matplotlib:


plt.scatter(x, y)
plt.plot(xfit, yfit);
plt.show()

Output:
b) Logistic Regression.

Experiment-6: Write a program to implement Categorical Encoding, One-hot Encoding

from numpy import argmax


# define input string
data = 'hello world'
print(data)
# define universe of possible input values
alphabet = 'abcdefghijklmnopqrstuvwxyz '
# define a mapping of chars to integers
char_to_int = dict((c, i) for i, c in enumerate(alphabet))
int_to_char = dict((i, c) for i, c in enumerate(alphabet))
# integer encode input data
integer_encoded = [char_to_int[char] for char in data]
print(integer_encoded)
# one hot encode
onehot_encoded = list()
for value in integer_encoded:
letter = [0 for _ in range(len(alphabet))]
letter[value] = 1
onehot_encoded.append(letter)
print(onehot_encoded)
# invert encoding
inverted = int_to_char[argmax(onehot_encoded[0])]
print(inverted)

Output:
hello world
[7, 4, 11, 11, 14, 26, 22, 14, 17, 11, 3]
[[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0]]
h

Experiment-7: Build an Artificial Neural Network by implementing the Back propagation algorithm and
test the same using appropriate data sets.

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) #maximum of X array longitudinally
y = y/100

#Sigmoid Function
def sigmoid (x):
return 1/(1 + np.exp(-x))

#Derivative of Sigmoid Function


def derivatives_sigmoid(x):
return x * (1 - x)

#Variable initialization
epoch=5 #Setting training iterations
lr=0.1 #Setting learning rate

inputlayer_neurons = 2 #number of features in data set


hiddenlayer_neurons = 3 #number of hidden layers neurons
output_neurons = 1 #number of neurons at output layer
#weight and bias initialization

wh=np.random.uniform(size=(inputlayer_neurons,hiddenlayer_neurons))
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))

#draws a random range of numbers uniformly of dim x*y


for i in range(epoch):
#Forward Propogation
hinp1=np.dot(X,wh)
hinp=hinp1 + bh
hlayer_act = sigmoid(hinp)
outinp1=np.dot(hlayer_act,wout)
outinp= outinp1+bout
output = sigmoid(outinp)

#Backpropagation
EO = y-output
outgrad = derivatives_sigmoid(output)
d_output = EO * outgrad
EH = d_output.dot(wout.T)
hiddengrad = derivatives_sigmoid(hlayer_act)#how much hidden layer wts contributed to error
d_hiddenlayer = EH * hiddengrad

wout += hlayer_act.T.dot(d_output) *lr # dotproduct of nextlayererror and currentlayerop


wh += X.T.dot(d_hiddenlayer) *lr

print ("-----------Epoch-", i+1, "Starts----------")


print("Input: \n" + str(X))
print("Actual Output: \n" + str(y))
print("Predicted Output: \n" ,output)
print ("-----------Epoch-", i+1, "Ends----------\n")
print("Input: \n" + str(X))
print("Actual Output: \n" + str(y))
print("Predicted Output: \n" ,output)

Output:
-----------Epoch- 1 Starts----------
Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.83330638]
[0.81393597]
[0.83458083]]
-----------Epoch- 1 Ends----------

-----------Epoch- 2 Starts----------
Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.83400283]
[0.81462023]
[0.83527413]]
-----------Epoch- 2 Ends----------

-----------Epoch- 3 Starts----------
Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.83468729]
[0.81529301]
[0.83595547]]
-----------Epoch- 3 Ends----------

-----------Epoch- 4 Starts----------
Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.83536007]
[0.81595461]
[0.83662517]]
-----------Epoch- 4 Ends----------

-----------Epoch- 5 Starts----------
Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.83602146]
[0.81660528]
[0.8372835 ]]
-----------Epoch- 5 Ends----------

Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.83602146]
[0.81660528]
[0.8372835 ]]

Experiment-8: Write a program to implement k-Nearest Neighbor algorithm to classify the iris data set.
Print both correct and wrong predictions.
#8 program

import numpy as np
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn import metrics

names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'Class']

# Read dataset to pandas dataframe


dataset = pd.read_csv("E:/8-dataset.csv", names=names)
X = dataset.iloc[:, :-1]
y = dataset.iloc[:, -1]
print(X.head())
Xtrain, Xtest, ytrain, ytest = train_test_split(X, y, test_size=0.10)
classifier = KNeighborsClassifier(n_neighbors=5).fit(Xtrain, ytrain)
ypred = classifier.predict(Xtest)

i=0
print("\n")
print ('%-25s %-25s %-25s' % ('Original Label', 'Predicted Label', 'Correct/Wrong'))

for label in ytest:


print ('%-25s %-25s' % (label, ypred[i]), end="")
if (label == ypred[i]):
print (' %-25s' % ('Correct'))
else:
print (' %-25s' % ('Wrong'))
i=i+1

print("\nConfusion Matrix:\n",metrics.confusion_matrix(ytest, ypred))


print("\nClassification Report:\n",metrics.classification_report(ytest, ypred))
print('Accuracy of the classifer is %0.2f' % metrics.accuracy_score(ytest,ypred))

Output:
sepal-length sepal-width petal-length petal-width
0 5.1 3.5 1.4 0.2
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2

Original Label Predicted Label Correct/Wrong


Iris-setosa Iris-setosa Correct
Iris-virginica Iris-virginica Correct
Iris-virginica Iris-virginica Correct
Iris-setosa Iris-setosa Correct
Iris-versicolor Iris-versicolor Correct
Iris-virginica Iris-virginica Correct
Iris-versicolor Iris-versicolor Correct
Iris-virginica Iris-virginica Correct
Iris-virginica Iris-versicolor Wrong
Iris-setosa Iris-setosa Correct
Iris-setosa Iris-setosa Correct
Iris-setosa Iris-setosa Correct
Iris-setosa Iris-setosa Correct
Iris-setosa Iris-setosa Correct
Iris-versicolor Iris-virginica Wrong

Confusion Matrix:
[[7 0 0]
[0 2 1]
[0 1 4]]

Classification Report:
precision recall f1-score support

Iris-setosa 1.00 1.00 1.00 7


Iris-versicolor 0.67 0.67 0.67 3
Iris-virginica 0.80 0.80 0.80 5
accuracy 0.87 15
macro avg 0.82 0.82 0.82 15
weighted avg 0.87 0.87 0.87 15

Accuracy of the classifer is 0.87

Experiment-9: Implement the non-parametric Locally Weighted Regression algorithm in order to fit data
points. Select appropriate data set for your experiment and draw graphs.

import numpy as np
from bokeh.plotting import figure, show, output_notebook
from bokeh.layouts import gridplot
from bokeh.io import push_notebook

def local_regression(x0, X, Y, tau):


x0 = np.r_[1, x0]
X = np.c_[np.ones(len(X)), X]
xw = X.T * radial_kernel(x0, X, tau)
beta = np.linalg.pinv(xw @ X) @ xw @ Y
return x0 @ beta

def radial_kernel(x0, X, tau):


return np.exp(np.sum((X - x0) ** 2, axis=1) / (-2 * tau * tau))

n=1000
# generate dataset
X = np.linspace(-3, 3, num=n)
Y = np.log(np.abs(X ** 2 - 1) + .5)

# jitter X
X += np.random.normal(scale=.1, size=n)
domain = np.linspace(-3, 3, num=300)

print("The Data Set ( 10 Samples) X :\n",X[1:10])


print("The Fitting Curve Data Set (10 Samples) Y :\n",Y[1:10])
print("Normalised (10 Samples) X :\n",X[1:10])
print(" Xo Domain Space(10 Samples) :\n",domain[1:10])

def plot_lwr(tau):
prediction = [local_regression(x0, X, Y, tau) for x0 in domain]
plot = figure(plot_width=400, plot_height=400)
plot.title.text='tau=%g' % tau
plot.scatter(X, Y, alpha=.3)
plot.line(domain, prediction, line_width=2, color='red')
return plot

show(gridplot([[plot_lwr(10.), plot_lwr(1.)] ,[plot_lwr(0.1), plot_lwr(0.01)]]))

output:
The Data Set ( 10 Samples) X :
[-3.06263705 -2.96975161 -2.88238588 -2.98089663 -2.94549014 -2.99930635
-3.11850852 -3.02930125 -2.99090601]
The Fitting Curve Data Set (10 Samples) Y :
[2.13582188 2.13156806 2.12730467 2.12303166 2.11874898 2.11445659
2.11015444 2.10584249 2.10152068]
Normalised (10 Samples) X :
[-3.06263705 -2.96975161 -2.88238588 -2.98089663 -2.94549014 -2.99930635
-3.11850852 -3.02930125 -2.99090601]
Xo Domain Space(10 Samples) :
[-2.97993311 -2.95986622 -2.93979933 -2.91973244 -2.89966555 -2.87959866
-2.85953177 -2.83946488 -2.81939799]

Experiment-10: Assuming a set of documents that need to be classified, use the naïve Bayesian Classifier
model to perform this task. Built-in Java classes/API can be used to write the program. Calculate the
accuracy, precision, and recall for your data set.

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score, confusion_matrix, precision_score, recall_score

msg = pd.read_csv('e:/document.csv', names=['message', 'label'])


print("Total Instances of Dataset: ", msg.shape[0])
msg['labelnum'] = msg.label.map({'pos': 1, 'neg': 0})
X = msg.message
y = msg.labelnum

Xtrain, Xtest, ytrain, ytest = train_test_split(X, y)

clf = MultinomialNB()
clf.fit(Xtrain_dm, ytrain)
pred = clf.predict(Xtest_dm)

for doc, p in zip(Xtrain, pred):


p = 'pos' if p == 1 else 'neg'
print("%s -> %s" % (doc, p))

print('Accuracy Metrics: \n')


print('Accuracy: ', accuracy_score(ytest, pred))
print('Recall: ', recall_score(ytest, pred))
print('Precision: ', precision_score(ytest, pred))
print('Confusion Matrix: \n', confusion_matrix(ytest, pred))
output:
Total Instances of Dataset: 18
My boss is horrible -> neg
This is an amazing place -> pos
He is my sworn enemy -> neg
I love to dance -> neg
I can't deal with this -> pos
Accuracy Metrics:

Accuracy: 0.6
Recall: 0.5
Precision: 0.5
Confusion Matrix:
[[2 1]
[1 1]]

Experiment-11: Apply EM algorithm to cluster a Heart Disease Data Set. Use the same data set for
clustering using k-Means algorithm. Compare the results of these two algorithms and comment on the
quality of clustering. You can add Java/Python ML library classes/API in the program.

from sklearn.cluster import KMeans


from sklearn.mixture import GaussianMixture
from sklearn import datasets
import sklearn.metrics as metrics
from sklearn.model_selection import train_test_split

iris = datasets.load_iris()
x_train,x_test,y_train,y_test=train_test_split(iris.data,iris.target)
model = KMeans(n_clusters = 3)
model.fit(x_train,y_train)
model.score
print('The accuracy score of K-Mean: ',metrics.accuracy_score(y_test,model.predict(x_test)))
print('The Confusion matrixof K-Mean:\n',metrics.confusion_matrix(y_test, model.predict(x_test)))
model12 = GaussianMixture(n_components = 3)
model12.fit(x_train,y_train)
model12.score
print('The accuracy score of EM: ',metrics.accuracy_score(y_test,model12.predict(x_test)))
print('The Confusion matrix of EM:\n ',metrics.confusion_matrix(y_test, model12.predict(x_test)))

Output:
The accuracy score of K-Mean: 0.3157894736842105
The Confusion matrixof K-Mean:
[[ 0 10 0]
[ 9 0 2]
[ 5 0 12]]
The accuracy score of EM: 0.4473684210526316
The Confusion matrix of EM:
[[ 0 10 0]
[ 8 0 3]
[ 0 0 17]]
Experiment-13: Write a Python program to construct a Bayesian network considering medical data. Use
this model to demonstrate the diagnosis of heart patients using standard Heart Disease Data Set

import numpy as np
import pandas as pd
import csv
from pgmpy.estimators import MaximumLikelihoodEstimator
from pgmpy.models import BayesianNetwork
from pgmpy.inference import VariableElimination
names = ['age', 'gender', 'cp', 'trestbps', 'chol', 'fbs', 'restecg', 'thalach', 'exang', 'oldpeak', 'slope',
'ca','thal', 'heartdisease']
heartDisease = pd.read_csv("E:/7-dataset.csv", names = names)
print('Sample instances from the dataset are given below')
print(heartDisease.head())
model= BayesianNetwork([('age','heartdisease'),('gender','heartdisease'),('exang','heartdisease'),
('cp','heartdisease'),('heartdisease','restecg'),('heartdisease','chol')])
model.fit(heartDisease,estimator=MaximumLikelihoodEstimator)
from pgmpy.inference import VariableElimination
HeartDisease_infer = VariableElimination(model)
print('\n Probability of HeartDisease given evidence : \n')
q = HeartDisease_infer.query(variables=['heartdisease'], evidence={'age':int(input('Enter age :')),
'gender':int(input('Enter Gender :'))},joint = False )
print(q['heartdisease'])

Output:
Sample instances from the dataset are given below
age gender cp trestbps chol fbs restecg thalach exang
oldpeak \
0 63 1 1 145 233 1 2 150 0 2.3
1 67 1 4 160 286 0 2 108 1 1.5
2 67 1 4 120 229 0 2 129 1 2.6
3 37 1 3 130 250 0 0 187 0 3.5
4 41 0 2 130 204 0 2 172 0 1.4

slope ca thal heartdisease


0 3 0 6 0
1 2 3 3 2
2 2 2 7 1
3 3 0 3 0
4 1 0 3 0

Probability of HeartDisease given evidence :

Enter age :49


Enter Gender :1
+-----------------+---------------------+
| heartdisease | phi(heartdisease) |
+=================+=====================+
| heartdisease(0) | 0.2507 |
+-----------------+---------------------+
| heartdisease(1) | 0.2351 |
+-----------------+---------------------+
| heartdisease(2) | 0.1396 |
+-----------------+---------------------+
| heartdisease(3) | 0.2351 |
+-----------------+---------------------+
| heartdisease(4) | 0.1396 |
+-----------------+---------------------+

Experiment-14: Write a program to Implement Support Vector Machines


#svm
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
bankdata = pd.read_csv("E:/bill_authentication.csv")
bankdata.shape
bankdata.head()
X = bankdata.drop('Class', axis=1)
y = bankdata['Class']
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20)
from sklearn.svm import SVC
svclassifier = SVC(kernel='linear')
svclassifier.fit(X_train, y_train)
y_pred = svclassifier.predict(X_test)
from sklearn.metrics import classification_report, confusion_matrix
print(confusion_matrix(y_test,y_pred))
print(classification_report(y_test,y_pred))
Output:
[[151 1]
[ 0 123]]
precision recall f1-score support

0 1.00 0.99 1.00 152


1 0.99 1.00 1.00 123

accuracy 1.00 275


macro avg 1.00 1.00 1.00 275
weighted avg 1.00 1.00 1.00 275

You might also like