0% found this document useful (0 votes)
14 views37 pages

Machine Learning Lab Exercises in Python

The document outlines a series of experiments for an M.Tech course in Machine Learning with Python, covering various machine learning methods including linear regression, logistic regression, support vector machines, exploratory data analysis, decision trees, neural networks, and naive Bayesian classifiers. Each experiment includes code snippets demonstrating the implementation of these methods using Python libraries such as scikit-learn and pandas. The document serves as a practical guide for students to apply machine learning techniques on real-world datasets.

Uploaded by

Ays Sai Kiran
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views37 pages

Machine Learning Lab Exercises in Python

The document outlines a series of experiments for an M.Tech course in Machine Learning with Python, covering various machine learning methods including linear regression, logistic regression, support vector machines, exploratory data analysis, decision trees, neural networks, and naive Bayesian classifiers. Each experiment includes code snippets demonstrating the implementation of these methods using Python libraries such as scikit-learn and pandas. The document serves as a practical guide for students to apply machine learning techniques on real-world datasets.

Uploaded by

Ays Sai Kiran
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

LENORA COLLEGE OF ENGINEERING

RAMPACHODAVARAM

[Link] - C.S.E. – R19 - MACHINE LEARNING WITH PYTHON LAB

1|Page
EXPERIMENT 1:

Exercises to solve the real-world problems using the following machine learning methods:

1. Linear regression
2. Logistic regression

1. Linear regression

#Three lines to make our compiler able to draw:

import sys

import matplotlib

[Link]('Agg')

import [Link] as plt

x = [5,7,8,7,2,17,2,9,4,11,12,9,6]

y = [99,86,87,88,111,86,103,87,94,78,77,85,86]

[Link](x, y)

[Link]()

#Two lines to make our compiler able to draw:

[Link]([Link])

[Link]()

2|Page
Output:

3|Page
2. Logistic regression

import numpy

from sklearn import linear_model

X = [Link]([3.78, 2.44, 2.09, 0.14, 1.72, 1.65, 4.92, 4.37, 4.96, 4.52, 3.69, 5.88]).reshape(-1,1)

y = [Link]([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1])

logr = linear_model.LogisticRegression()

[Link](X,y)

log_odds = logr.coef_

odds = [Link](log_odds)

print(odds)

Output:

4|Page
Experiment 2:

Write a program to implement support vector machines.

import pandas as pd
import math

heart = pd.read_csv("heart_disease")

nrows = [Link]([Link][0] * 0.8)

training = [Link][:nrows]
test = [Link][nrows:]
from [Link] import SVC

model = SVC()
[Link](training[["age", "chol"]], training["present"])
predictions = [Link](test[["age", "chol"]])

accuracy = sum(test["present"] == predictions) / [Link][0]

output:

accuracy
0.4666666666666667

5|Page
Experiment 3:

Exploratory data analysis for classification using pandas and matplotlib.

We need to install Pandas, NumPy, Matplotlib and Seaborn libraries in


python to proceed further.
import pandas as pd

import numpy as np

import [Link] as plt

import seaborn as sns

import warnings as wr

[Link]('ignore')

df = pd.read_csv("/content/[Link]")

print([Link]())

[Link]

[Link]()

[Link]()

[Link]()

[Link]().sum()

[Link]()

quality_counts = df['quality'].value_counts()

[Link](figsize=(8, 6))

[Link](quality_counts.index, quality_counts, color='deeppink')

[Link]('Count Plot of Quality')

[Link]('Quality')

[Link]('Count')

[Link]()

6|Page
7|Page
8|Page
9|Page
Experiment 4:

Implement a program for bias, variance and cross validation.

# Import the necessary libraries

from [Link] import load_iris

from sklearn.model_selection import train_test_split

from [Link] import DecisionTreeClassifier

from [Link] import BaggingClassifier

from [Link] import bias_variance_decomp

import warnings

[Link]('ignore')

# Load the dataset

X, y = load_iris(return_X_y=True)

# Split train and test dataset

X_train, X_test,\

y_train, y_test = train_test_split(X, y,

test_size=0.25,

random_state=23,

shuffle=True,

stratify=y)

# Build the classification model

tree = DecisionTreeClassifier(random_state=123)

clf = BaggingClassifier(base_estimator=tree,

n_estimators=50,

10 | P a g e
random_state=23)

# Bias variance decompositions

avg_expected_loss, avg_bias, \

avg_var = bias_variance_decomp(clf,

X_train, y_train,

X_test, y_test,

loss='0-1_loss',

random_seed=23)

# Print the value

print('Average expected loss: %.2f' % avg_expected_loss)

print('Average bias: %.2f' % avg_bias)

print('Average variance: %.2f' % avg_var)

Output:

Average expected loss: 0.06


Average bias: 0.05
Average variance: 0.02

11 | P a g e
Experiment 5:

Write a program to simulate a perception network for pattern classification and function
approximation.

from [Link] import load_breast_cancer

from sklearn.linear_model import Perceptron

from sklearn.model_selection import train_test_split

from sklearn import metrics

from [Link] import accuracy_score

from [Link] import confusion_matrix

# Load the Breast Cancer dataset

data = load_breast_cancer()

X = [Link]

y = [Link]

print([Link])

print([Link])

# Split the dataset into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

print(X_train.shape)

print(X_test.shape)

print(y_train.shape)

print(y_test.shape)

# Create a Perceptron model

clf = Perceptron(max_iter=1000, eta0=0.1)

# Train the model

[Link](X_train, y_train)

# Make predictions

y_pred = [Link](X_test)

12 | P a g e
print(y_pred)

# Evaluate accuracy

accuracy = accuracy_score(y_test, y_pred)

print("Accuracy:", accuracy)

# Calculate confusion matrix

conf_matrix = confusion_matrix(y_test, y_pred)

cm_display = [Link](confusion_matrix = conf_matrix, display_labels =


[False, True])

cm_display.plot()

[Link]()

Output:

13 | P a g e
Experiment 6:

Write a program to demonstrate the working of the decision tree based ID3algorithm. Use an
appropriate data set for bulking the decision tree and apply this knowledge to classify the new
sample.

import pandas as pd

import math

import numpy as np

data = pd.read_csv("Dataset/[Link]")

features = [feat for feat in data]

[Link]("answer")

class Node:

def __init__(self):

[Link] = []

[Link] = ""

[Link] = False

[Link] = ""

def entropy(examples):

pos = 0.0

neg = 0.0

for _, row in [Link]():

if row["answer"] == "yes":

pos += 1

else:

neg += 1

if pos == 0.0 or neg == 0.0:

return 0.0

14 | P a g e
else:

p = pos / (pos + neg)

n = neg / (pos + neg)

return -(p * [Link](p, 2) + n * [Link](n, 2))

def info_gain(examples, attr):

uniq = [Link](examples[attr])

#print ("\n",uniq)

gain = entropy(examples)

#print ("\n",gain)

for u in uniq:

subdata = examples[examples[attr] == u]

#print ("\n",subdata)

sub_e = entropy(subdata)

gain -= (float(len(subdata)) / float(len(examples))) * sub_e

#print ("\n",gain)

return gain

def ID3(examples, attrs):

root = Node()

max_gain = 0

max_feat = ""

for feature in attrs:

#print ("\n",examples)

gain = info_gain(examples, feature)

if gain > max_gain:

max_gain = gain

max_feat = feature

15 | P a g e
[Link] = max_feat

#print ("\nMax feature attr",max_feat)

uniq = [Link](examples[max_feat])

#print ("\n",uniq)

for u in uniq:

#print ("\n",u)

subdata = examples[examples[max_feat] == u]

#print ("\n",subdata)

if entropy(subdata) == 0.0:

newNode = Node()

[Link] = True

[Link] = u

[Link] = [Link](subdata["answer"])

[Link](newNode)

else:

dummyNode = Node()

[Link] = u

new_attrs = [Link]()

new_attrs.remove(max_feat)

child = ID3(subdata, new_attrs)

[Link](child)

[Link](dummyNode)

return root

def printTree(root: Node, depth=0):

for i in range(depth):

print("\t", end="")

16 | P a g e
print([Link], end="")

if [Link]:

print(" -> ", [Link])

print()

for child in [Link]:

printTree(child, depth + 1)

def classify(root: Node, new):

for child in [Link]:

if [Link] == new[[Link]]:

if [Link]:

print ("Predicted Label for new example", new," is:", [Link])

exit

else:

classify ([Link][0], new)

root = ID3(data, features)

print("Decision Tree is:")

printTree(root)

print ("------------------")

new = {"outlook":"sunny", "temperature":"hot", "humidity":"normal", "wind":"strong"}

classify (root, new)

17 | P a g e
Output:

18 | P a g e
Experiment 7:

Build an artificial neural network by implementing the back propagation algorithm and the test the
same using appropriate data sets.

import numpy as np

class NeuralNetwork:

def __init__(self, input_size, hidden_size, output_size):

self.input_size = input_size

self.hidden_size = hidden_size

self.output_size = output_size

self.weights_input_hidden = [Link](

self.input_size, self.hidden_size)

self.weights_hidden_output = [Link](

self.hidden_size, self.output_size)

self.bias_hidden = [Link]((1, self.hidden_size))

self.bias_output = [Link]((1, self.output_size))

def sigmoid(self, x):

return 1 / (1 + [Link](-x))

def sigmoid_derivative(self, x):

return x * (1 - x)

def feedforward(self, X):

self.hidden_activation = [Link](

19 | P a g e
X, self.weights_input_hidden) + self.bias_hidden

self.hidden_output = [Link](self.hidden_activation)

self.output_activation = [Link](

self.hidden_output, self.weights_hidden_output) + self.bias_output

self.predicted_output = [Link](self.output_activation)

return self.predicted_output

def backward(self, X, y, learning_rate):

output_error = y - self.predicted_output

output_delta = output_error * \

self.sigmoid_derivative(self.predicted_output)

hidden_error = [Link](output_delta, self.weights_hidden_output.T)

hidden_delta = hidden_error * self.sigmoid_derivative(self.hidden_output)

self.weights_hidden_output += [Link](self.hidden_output.T,

output_delta) * learning_rate

self.bias_output += [Link](output_delta, axis=0,

keepdims=True) * learning_rate

self.weights_input_hidden += [Link](X.T, hidden_delta) * learning_rate

self.bias_hidden += [Link](hidden_delta, axis=0,

keepdims=True) * learning_rate

def train(self, X, y, epochs, learning_rate):

for epoch in range(epochs):

output = [Link](X)

20 | P a g e
[Link](X, y, learning_rate)

if epoch % 4000 == 0:

loss = [Link]([Link](y - output))

print(f"Epoch {epoch}, Loss:{loss}")

X = [Link]([[0, 0], [0, 1], [1, 0], [1, 1]])

y = [Link]([[0], [1], [1], [0]])

nn = NeuralNetwork(input_size=2, hidden_size=4, output_size=1)

[Link](X, y, epochs=10000, learning_rate=0.1)

output = [Link](X)

print("Predictions after training:")

print(output)

Output:

21 | P a g e
Experiment 8:

Write a program to implement the naïve Bayesian classifier for iris data set. Compute the accuracy of
the classifier, considering few test data sets.

import csv

import random

import math

def loadcsv(filename):

lines = [Link](open(filename, "r"));

dataset = list(lines)

for i in range(len(dataset)):

#converting strings into numbers for processing

dataset[i] = [float(x) for x in dataset[i]]

return dataset

def splitdataset(dataset, splitratio):

#67% training size

trainsize = int(len(dataset) * splitratio);

trainset = []

copy = list(dataset);

while len(trainset) < trainsize:

#generate indices for the dataset list randomly to pick ele for training data

index = [Link](len(copy));

[Link]([Link](index))

return [trainset, copy]

22 | P a g e
def separatebyclass(dataset):

separated = {} #dictionary of classes 1 and 0

#creates a dictionary of classes 1 and 0 where the values are

#the instances belonging to each class

for i in range(len(dataset)):

vector = dataset[i]

if (vector[-1] not in separated):

separated[vector[-1]] = []

separated[vector[-1]].append(vector)

return separated

def mean(numbers):

return sum(numbers)/float(len(numbers))

def stdev(numbers):

avg = mean(numbers)

variance = sum([pow(x-avg,2) for x in numbers])/float(len(numbers)-1)

return [Link](variance)

def summarize(dataset): #creates a dictionary of classes

summaries = [(mean(attribute), stdev(attribute)) for attribute in zip(*dataset)];

del summaries[-1] #excluding labels +ve or -ve

return summaries

def summarizebyclass(dataset):

separated = separatebyclass(dataset);

#print(separated)

23 | P a g e
summaries = {}

for classvalue, instances in [Link]():

#for key,value in [Link]()

#summaries is a dic of tuples(mean,std) for each class value

summaries[classvalue] = summarize(instances) #summarize is used to cal to mean


and std

return summaries

def calculateprobability(x, mean, stdev):

exponent = [Link](-([Link](x-mean,2)/(2*[Link](stdev,2))))

return (1 / ([Link](2*[Link]) * stdev)) * exponent

def calculateclassprobabilities(summaries, inputvector):

probabilities = {} # probabilities contains the all prob of all class of test data

for classvalue, classsummaries in [Link]():#class and attribute information as


mean and sd

probabilities[classvalue] = 1

for i in range(len(classsummaries)):

mean, stdev = classsummaries[i] #take mean and sd of every attribute for


class 0 and 1 seperaely

x = inputvector[i] #testvector's first attribute

probabilities[classvalue] *= calculateprobability(x, mean, stdev);#use normal


dist

return probabilities

def predict(summaries, inputvector): #training and test data is passed

probabilities = calculateclassprobabilities(summaries, inputvector)

bestLabel, bestProb = None, -1

24 | P a g e
for classvalue, probability in [Link]():#assigns that class which has he highest
prob

if bestLabel is None or probability > bestProb:

bestProb = probability

bestLabel = classvalue

return bestLabel

def getpredictions(summaries, testset):

predictions = []

for i in range(len(testset)):

result = predict(summaries, testset[i])

[Link](result)

return predictions

def getaccuracy(testset, predictions):

correct = 0

for i in range(len(testset)):

if testset[i][-1] == predictions[i]:

correct += 1

return (correct/float(len(testset))) * 100.0

def main():

filename = '[Link]'

splitratio = 0.67

dataset = loadcsv(filename);

trainingset, testset = splitdataset(dataset, splitratio)

25 | P a g e
print('Split {0} rows into train={1} and test={2} rows'.format(len(dataset), len(trainingset),
len(testset)))

# prepare model

summaries = summarizebyclass(trainingset);

#print(summaries)

# test model

predictions = getpredictions(summaries, testset) #find the predictions of test data with the
training data

accuracy = getaccuracy(testset, predictions)

print('Accuracy of the classifier is : {0}%'.format(accuracy))

main()

Output

Split 768 rows into train=514 and test=254

26 | P a g e
Experiment 9:

Assuming a set of documents that need to be classified, use the naïve Bayesian classifer model to
perform this task. Built in java classes/api can be used to write the program. Calculate the accuracy,
precission and recall for your data set.

import pandas as pd

msg=pd.read_csv('[Link]',names=['message','label'])

print('The dimensions of the dataset',[Link])

msg['labelnum']=[Link]({'pos':1,'neg':0})

X=[Link]=[Link](X)

print(y)

#splitting the dataset into train and test data from

sklearn.model_selection import train_test_split

xtrain,xtest,ytrain,ytest=train_test_split(X,y)

print([Link])

print([Link])

print([Link])

print([Link])

#output of count vectoriser is a sparse matrix

from sklearn.feature_extraction.text

import CountVectorizercount_vect = CountVectorizer()

xtrain_dtm = count_vect.fit_transform(xtrain)

xtest_dtm=count_vect.transform(xtest)

print(count_vect.get_feature_names())

df=[Link](xtrain_dtm.toarray(),columns=count_vect.get_feature_names())

print(df)

#tabular representation

print(xtrain_dtm)

27 | P a g e
#sparse matrix representation

# Training Naive Bayes (NB) classifier on training data

from sklearn.naive_bayes import MultinomialNB clf

= MultinomialNB().fit(xtrain_dtm,ytrain)

predicted = [Link](xtest_dtm)

#printing accuracy metrics

from sklearn import metricsprint('Accuracy metrics')

print('Accuracy of the classifer is',metrics.accuracy_score(ytest,predicted))

print('Confusion matrix')

print(metrics.confusion_matrix(ytest,predicted))

print('Recall and Precison ')

print(metrics.recall_score(ytest,predicted))

print(metrics.precision_score(ytest,predicted))

'''docs_new = ['I like this place', 'My boss is not my saviour']

X_new_counts = count_vect.transform(docs_new)predictednew = [Link](X_new_counts)

for doc, category in zip(docs_new, predictednew):

print('%s->%s' % (doc, [Link][category]))'''

I love this sandwich,pos This is an amazing place,pos

I feel very good about these beers,posThis is my best work,pos

What an awesome view,pos

I do not like this restaurant,negI am tired of this stuff,neg

I can't deal with this,neg He is my sworn enemy,negMy boss is horrible,neg

This is an awesome place,pos

I do not like the taste of this juice,negI love to dance,pos

I am sick and tired of this place,negWhat a great holiday,pos

That is a bad locality to stay,neg

We will have good fun tomorrow,posI went to my enemy's house today,neg

28 | P a g e
29 | P a g e
Experiment 10:

Apply EM algorithm to cluster a Heart Disease Data Set. Use the same data set for clustering
usingkMeans 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.

import numpy as np

import [Link] as plt

from [Link].samples_generator

import make_blobsX, y_true = make_blobs(n_samples=100, centers =

4,Cluster_std=0.60,random_state=0)

X = X[:, ::-1]

#flip axes for better plotting

from [Link] import GaussianMixture

gmm = GaussianMixture (n_components = 4).fit(X)lables = [Link](X)

[Link](X[:, 0], X[:, 1], c=labels, s=40, cmap=‟viridis‟);probs = gmm.predict_proba(X)

print(probs[:5].round(3))

size = 50 * [Link](1) ** 2

# square emphasizes differences

[Link](X[:, 0], X[:, 1], c=labels, cmap=‟viridis‟, s=size);

from [Link] import Ellipse

def draw_ellipse(position, covariance, ax=None, **kwargs);

“””Draw an ellipse with a given position and covariance”””Ax

= ax or [Link]()

# Convert covariance to principal axes

if [Link] ==(2,2):

U, s, Vt = [Link](covariance)

Angle = [Link](np.arctan2(U[1, 0], U[0,0]))Width, height = 2 * [Link](s)

30 | P a g e
else:

angle = 0

width, height = 2 * [Link](covariance)

#Draw the Ellipse

for nsig in range(1,4):

ax.add_patch(Ellipse(position, nsig * width, nsig *height,angle, **kwargs))

def plot_gmm(gmm, X, label=True, ax=None):ax = ax or [Link]()

labels = [Link](X).predict(X)if label:

[Link](X[:, 0], x[:, 1], c=labels, s=40, cmap=‟viridis‟, zorder=2)else:

[Link](X[:, 0], x[:, 1], s=40, zorder=2)[Link](„equal‟)

w_factor = 0.2 / gmm.weights_.max()

for pos, covar, w in zip(gmm.means_, gmm.covariances_, gmm.weights_):draw_ellipse(pos, covar,

alpha=w * w_factor)

gmm = GaussianMixture(n_components=4, random_state=42)plot_gmm(gmm, X)

gmm = GaussianMixture(n_components=4, covariance_type=‟full‟,random_state=42)

plot_gmm(gmm, X)

31 | P a g e
K MEANS :

from [Link] import KMeans

#from sklearn import metricsimport numpy as np

import [Link] as plt

import pandas as pd

data=pd.read_csv("[Link]")

df1=[Link](data)

print(df1)

f1 = df1['Distance_Feature'].valuesf2 = df1['Speeding_Feature'].values

X=[Link](list(zip(f1,f2)))[Link]()

[Link]([0, 100])

[Link]([0, 50]) [Link]('Dataset') [Link]('speeding_feature')[Link]('Distance_Feature')

[Link](f1,f2)

[Link]()

# create new plot and data

[Link]()

colors = ['b', 'g', 'r']

markers = ['o', 'v', 's']

# KMeans algorithm#K = 3

kmeans_model = KMeans(n_clusters=3).fit(X)

[Link]()

for i, l in enumerate(kmeans_model.labels_):

[Link](f1[i], f2[i], color=colors[l], marker=markers[l],ls='None')[Link]([0, 100])

[Link]([0, 50])[Link]()

Driver_ID,Distance_Feature,Speeding_Feature

3423311935,71.24,28

3423313212,52.53,25

32 | P a g e
3423313724,64.54,27

3423311373,55.69,22

3423310999,54.58,25

3423313857,41.91,10

3423312432,58.64,20

3423311434,52.02,8

3423311328,31.25,34

3423312488,44.31,19

3423311254,49.35,40

3423312943,58.07,45

3423312536,44.22,22

3423311542,55.73,19

3423312176,46.63,43

3423314176,52.97,32

3423314202,46.25,35

3423311346,51.55,27

3423310666,57.05,26

3423313527,58.45,30

3423312182,43.42,23

3423313590,55.68,37

3423312268,55.15,18

33 | P a g e
Output:

34 | P a g e
Experiment 11:

Write a program to implement k-Nearest Neighbor algorithm to classify the iris data set. Print

both correct and wrong predictions.

import csv import random

import math import operator

def loadDataset(filename, split, trainingSet=[] , testSet=[]):with open(filename, 'rb') as csvfile:

lines = [Link](csvfile)dataset = list(lines)

for x in range(len(dataset)-1):for y in range(4):

dataset[x][y] = float(dataset[x][y])if [Link]() < split:

[Link](dataset[x])else:

[Link](dataset[x])

def euclideanDistance(instance1, instance2, length):distance = 0

for x in range(length):

distance += pow((instance1[x] - instance2[x]), 2)return [Link](distance)

def getNeighbors(trainingSet, testInstance, k):distances = []

length = len(testInstance)-1

for x in range(len(trainingSet)):

dist = euclideanDistance(testInstance, trainingSet[x], length)[Link]((trainingSet[x], dist))

[Link](key=[Link](1))neighbors = []

for x in range(k):

[Link](distances[x][0])return neighbors

def getResponse(neighbors):classVotes = {}

for x in range(len(neighbors)): response = neighbors[x][-1]if response in classVotes:

classVotes[response] += 1

else:

classVotes[response] = 1

35 | P a g e
sortedVotes = sorted([Link](),reverse=True)

return sortedVotes[0][0]

def getAccuracy(testSet, predictions): correct = 0 for x in range(len(testSet)):

key=[Link](1

),

if testSet[x][-1] == predictions[x]:correct += 1

return (correct/float(len(testSet))) * 100.0

def main():

# prepare data trainingSet=[] testSet=[]split = 0.67

loadDataset('[Link]', split, trainingSet, testSet) print('Train set: ' + repr(len(trainingSet)))

print('Test set: ' + repr(len(testSet)))

# generate predictions predictions=[]k=3

for x in range(len(testSet)):

neighbors = getNeighbors(trainingSet, testSet[x],k) result = getResponse(neighbors)

[Link](result)

print('> predicted=' + repr(result) + ', actual=' + repr(testSet[x][-1])) accuracy = getAccuracy(testSet,

predictions)

print('Accuracy: ' + repr(accuracy) +'%') main()

36 | P a g e

You might also like