0% found this document useful (0 votes)
13 views46 pages

Aiml Lab Manual Final

The document outlines various Python programs implementing different algorithms including Breadth First Search (BFS), Depth First Search (DFS), Greedy and A* algorithms, non-parametric locally weighted regression, decision tree algorithm, and artificial neural networks using back propagation. Each section includes the aim, algorithm steps, program code, and results demonstrating successful implementation. The programs utilize libraries like NumPy, Pandas, and Scikit-learn for data manipulation and model evaluation.

Uploaded by

mbasilasil
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)
13 views46 pages

Aiml Lab Manual Final

The document outlines various Python programs implementing different algorithms including Breadth First Search (BFS), Depth First Search (DFS), Greedy and A* algorithms, non-parametric locally weighted regression, decision tree algorithm, and artificial neural networks using back propagation. Each section includes the aim, algorithm steps, program code, and results demonstrating successful implementation. The programs utilize libraries like NumPy, Pandas, and Scikit-learn for data manipulation and model evaluation.

Uploaded by

mbasilasil
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
You are on page 1/ 46

Ex.No.

1 IMPLEMENT BREADTH FIRST SEARCH


DATE:

Aim:
To write a Python program to implement Breadth First Search (BFS).

Algorithm:

Step 1. Start
Step 2. Put any one of the graph’s vertices at the back of the queue.
Step 3. Take the front item of the queue and add it to the visited list.
Step 4. Create a list of that vertex's adjacent nodes. Add those which are not within the
visited list to the rear of the queue.
Step 5. Continue steps 3 and 4 till the queue is
empty.
Step 6. Stop

Graph:

Program:

graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}

visited = [] # List for visited nodes.

queue = [] #Initialize a queue


def bfs(visited, graph, node): #function for BFS

visited.append(nod
e)
queue.append(nod
e)

while queue: # Creating loop to visit each node

m = queue.pop(0)
print (m, end = " ")

for neighbour in graph[m]:


if neighbour not in
visited:
visited.append(neighbou
r)
queue.append(neighbou
r)

# Driver Code

print("Following is the Breadth-First


Search")bfs(visited, graph, '5') # function
calling

The output of the above code will be as follow:

Following is the Breadth-First Search


537248

Result:

Thus the Python program to implement Breadth First Search (BFS) was
developed successfully.
Ex.No. 2 IMPLEMENT DEPTH FIRST SEARCH
DATE:

Aim:
To write a Python program to implement Depth First Search (DFS).

Algorithm:

Step 1.Start
Step 2.Put any one of the graph's vertex on top of the stack.
Step 3.After that take the top item of the stack and add it to the visited list of the vertex.
Step 4.Next, create a list of that adjacent node of the vertex. Add the ones which aren't in
thevisited list of vertexes to the top of the stack.
Step 5.Repeat steps 3 and 4 until the stack is
empty
Step 6.Stop

Graph:

Program:

graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}

visited = set() # Set to keep track of visited nodes of graph.

def dfs(visited, graph, node): #function for dfs


if node not in
visited:print
(node)
visited.add(node
)

for neighbour in graph[node]:


dfs(visited, graph,
neighbour)

# Driver Code
print("Following is the Depth-First Search")dfs(visited, graph, '5')

The output of the above code is as follow:

Following is the Depth-First Search


532487

Result:

Thus the Python program to implement Depth First Search (DFS) was developedsuccessfully
Ex.No. 3 IMPLEMENT AND COMPARE GREEDY & A* ALGORITHMS.

DATE:

Aim:
To write a Python program to implement Greedy & A* search algorithm.

Algorithm:

Step 1: Create a priority queue and push the starting node onto the queue.Initialize
minimum value (min_index) to location 0.
Step 2: Create a set to store the visited nodes.
Step 3: Repeat the following steps until the queue is empty:
3.1: Pop the node with the lowest cost + heuristic from the queue.
3.2: If the current node is the goal, return the path to the goal.
3.3: If the current node has already been visited, skip
it.
3.4: Mark the current node as visited.
3.5: Expand the current node and add its neighbors to the
queue.
Step 4: If the queue is empty and the goal has not been found, return None (no path found).
Step 5: Stop

Program:

from queue import PriorityQueue


def greedy(graph, start, goal, heuristic):
current = start
path = []
while current != goal:
path.append(current)
neighbors = graph[current]
current = min(neighbors, key=lambda x: heuristic[x])
path.append(goal)

return path
def astar(graph, start, goal, heuristic):
frontier = PriorityQueue()
frontier.put((0, start))

came_from = {}
cost_so_far = {}
came_from[start] = None
cost_so_far[start] = 0

while not frontier.empty():


_, current = frontier.get()

if current == goal:
break

for next_node in graph[current]:


new_cost = cost_so_far[current] + graph[current][next_node]
if next_node not in cost_so_far or new_cost < cost_so_far[next_node]:
cost_so_far[next_node] = new_cost

priority = new_cost + heuristic[next_node]


frontier.put((priority, next_node))
came_from[next_node] = current

path = []
while current != start:

path.append(current)
current = came_from[current]
path.append(start)
path.reverse()
return path

# Example usage
graph = {
'A': {'B': 1, 'C': 2},
'B': {'A': 1, 'D': 3},
'C': {'A': 2, 'D': 1},
'D': {'B': 3, 'C': 1}
}
heuristic = {'A': 2, 'B': 1, 'C': 1, 'D': 0}
start, goal = 'A', 'D'
print("Greedy Path:", greedy(graph, start, goal, heuristic))

print("A* Path:", astar(graph, start, goal, heuristic))

output:
Greedy Path: ['A', 'B', 'D']
A* Path: ['A', 'C', 'D']

Result:

Thus the Python program to implement Greedy and A* Algorthim was developed successfully
Ex.No. 4 Implement the non-parametric locally weighted regression
algorithm in order to fit data points. Select appropriate data set for your
experiment and draw graphs

DATE:

Aim:

To write a Python program to implement the non-parametric locally weighted


regression algorithm in order to fit data points. Select appropriate data set for your
experiment and draw graphs.

Algorithm:

Step 1. Import necessary libraries: numpy, pandas, matplotlib.pyplot,


LinearRegression,mean_squared_error, and r2_score.

Step 2. Create a numpy array for waist and weight values and store them in separate
variables.

Step 3. Create a pandas DataFrame with waist and weight columns using the numpyarrays.

Step 4. Extract input (X) and output (y) variables from the DataFrame.

Step 5. Create an instance of LinearRegression model.

Step 6. Fit the LinearRegression model to the input and output


variables.

Step 7. Create a new DataFrame with a single value of waist.

Step 8. Use the predict() method of the LinearRegression model to predict the weight for
thenew waist value.

Step 9. Calculate the mean squared error and R-squared values using
mean_squared_error()and r2_score() functions respectively.

Step 10. Plot the actual and predicted values using matplotlib.pyplot.scatter()
andmatplotlib.pyplot.plot() functions.
Program:

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

from sklearn.linear_model import LinearRegression

from sklearn.metrics import mean_squared_error, r2_score

# import sample data using pandas

waist = np.array([70, 71, 72, 73, 74, 75, 76, 77, 78, 79])

weight = np.array([55, 57, 59, 61, 63, 65, 67, 69, 71, 73])

data = pd.DataFrame({'waist': waist, 'weight': weight})

# extract input and output variables

X = data[['waist']]

y = data['weight']

# fit a linear regression model

model = LinearRegression()

model.fit(X, y)

# make predictions on new data

new_data = pd.DataFrame({'waist': [80]})

predicted_weight = model.predict(new_data[['waist']])

print("Predicted weight for new waist value:", int(predicted_weight))

#calculate MSE and R-squared

y_pred = model.predict(X)
mse = mean_squared_error(y, y_pred)

print('Mean Squared Error:', mse)

r2 = r2_score(y, y_pred)

print('R-squared:', r2)

# plot the actual and predicted values

plt.scatter(X, y, marker='*', edgecolors='g')

plt.scatter(new_data, predicted_weight, marker='*', edgecolors='r')

plt.plot(X, y_pred, color='y')

plt.xlabel('Waist (cm)')

plt.ylabel('Weight (kg)')

plt.title('Linear Regression Model')

plt.show()

OUTPUT:

Predicted weight for new waist value: 75

Mean Squared Error: 0.0

R-squared: 1.0
Result:

Thus the Python program to implement Greedy and A* Algorthim was developed successfully
was developed successfully
Ex.No. 5 Write a program to demonstrate the working of the decision tree based
algorithm.

DATE:

Aim:

To write a Python program to demonstrate the working of the decision tree based algorithm.

Algorthim:

1. Separate the features and target into 2 separate data frames

2. Split the data into training and testing sets– using train_test_split from sklearn
3. Apply the decision tree classifier – using DecisionTreeClassifier from sklearn
4. Predict the target for the test set

5. Evaluate model accuracy – using metrics from sklearn


6. Visualise the decision trees – using graphviz
7. Optimise the decision tree – using various parameters such as max_depth, min_samples_split,
etc.

Program:

import pandas as pd
from sklearn.tree import DecisionTree Classifier
from sklearn.model_selection import train_test_split
from sklearn import metrics

pima = pd.read_csv("D:\diabetes_dataset.csv")
print(pima.head())

feature_cols = ['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI',


'DiabetesPedigreeFunction', 'Age', 'Outcome']
X = pima[feature_cols]
y = pima.Outcome

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

clf = DecisionTreeClassifier(max_depth=5)

clf1= clf.fit(X_train,y_train)

y_pred = clf.predict(X_test)
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))

from matplotlib import pyplot as plt


from sklearn import tree

fig = plt.figure()

tree.plot_tree(clf1)
plt.show()
fig.savefig("decision_tree.png")

output:

================================================================
RESTART: C:\Users\admin\Desktop\id3.py
===============================================================
Pregnancies Glucose BloodPressure ... DiabetesPedigreeFunction Age Outcome
0 6 148 72 ... 0.627 50 1
1 1 85 66 ... 0.351 31 0
2 8 183 64 ... 0.672 32 1
3 1 89 66 ... 0.167 21 0
4 0 137 40 ... 2.288 33 1

[5 rows x 9 columns]
Accuracy: 1.0
>>>
Result:

Thus the Python program to implement decision tree based algorithm. was developed
successfully
Ex.No. 6 Build an artificial neural network by implementing the
back propagation algorithm and test the same using appropriate
data sets.
DATE:

Aim:

To write a Python program to Build an artificial neural network by


implementing the back propagation algorithm and test the same
using appropriate data sets.

Algorthim:
Program:

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.73994859]
[0.72235028]

[0.74349565]]

-----------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.74302162]

[0.7252485 ]

[0.74658857]]

-----------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.74599163]

[0.72805257]

[0.74957693]]

-----------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.74886344]

[0.73076681]

[0.75246569]]

-----------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.75164162]

[0.73339529]

[0.75525949]]

-----------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.75164162]

[0.73339529]
[0.75525949]]

>>>

Result:

Thus the Python program to Build an artificial neural network by implementing


the back propagation algorithm and test the same using appropriate data sets was
developed successfully

Ex.No. 7 Write a program to implement the naïve Bayesian classifier.


DATE:

ALGORITHM:

STEP 1: Load the training data set from the CSV file into a list of dictionaries, where
each dictionary represents a single instance (row) in the data set and the keys
representthe attribute names (columns) and the values represent the
corresponding attribute values for that instance.

STEP 2: Determine the class variable for each instance in the training data set and
add itas a new key-value pair to the corresponding dictionary

.STEP 3: Create a dictionary to store the prior probabilities for each class variable
in thetraining data set. The key-value pairs should be of the form
{class_variable:prior_probability

STEP 4: For each attribute in the training data set, create a dictionary to store
theconditional probabilities for each attribute value given each class variable. The
key-valuepairs should be of the form {(attribute, attribute_value,
class_variable):conditional_probability

STEP 5: Compute the prior probabilities for each class variable by counting the
numberof instances in the training data set that belong to each class variable and
dividing by thetotal number of instances.

STEP 6: For each attribute in the training data set, compute the conditional
probabilitiesfor each attribute value given each class variable by counting the
number of instances inthe training data set that have that attribute value and
belong to each class variable, anddividing by the number of instances that belong
to that class variable

.STEP 7: Load the test data sets from CSV files into lists of dictionaries, following
thesame format as the training data set.

STEP 8: For each instance in each test data set, compute the posterior probability
foreach class variable given the attribute values in that instance, using the Naive
Bayesianformula:P(class variable | attribute _values) = P(class variable)
*product(P(attribute_value | class variable) for attribute_value in
attribute_values)

STEP 9: Determine the predicted class variable for each instance in each test data
set asthe class variable with the highest posterior probability

.STEP 10: Compare the predicted class variables to the actual class variables in each
testdata set to compute the accuracy of the classifier

.STEP 11: Output the accuracy for each test data set.
Program:

# import necessary libarities

import pandas as pd

from sklearn import tree

from sklearn.preprocessing import LabelEncoder

from sklearn.naive_bayes import GaussianNB

# load data from CSV

data = pd.read_csv('tennisdata.csv')

print("THe first 5 values of data is :\n",data.head())

# obtain Train data and Train output

X = data.iloc[:,:-1]

print("\nThe First 5 values of train data is\n",X.head())

y = data.iloc[:,-1]

print("\nThe first 5 values of Train output is\n",y.head())

# Convert then in numbers

le_outlook = LabelEncoder()

X.Outlook = le_outlook.fit_transform(X.Outlook)
le_Temperature = LabelEncoder()

X.Temperature = le_Temperature.fit_transform(X.Temperature)

le_Humidity = LabelEncoder()

X.Humidity = le_Humidity.fit_transform(X.Humidity)

le_Windy = LabelEncoder()

X.Windy = le_Windy.fit_transform(X.Windy)

print("\nNow the Train data is :\n",X.head())

le_PlayTennis = LabelEncoder()

y = le_PlayTennis.fit_transform(y)

print("\nNow the Train output is\n",y)

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)

classifier = GaussianNB()

classifier.fit(X_train,y_train)

from sklearn.metrics import accuracy_score


print("Accuracy is:",accuracy_score(classifier.predict(X_test),y_test))

output:

THe first 5 values of data is :

Outlook Temperature Humidity Windy PlayTennis

0 Sunny Hot High False No

1 Sunny Hot High True No

2 Overcast Hot High False Yes

3 Rainy Mild High False Yes

4 Rainy Cool Normal False Yes

The First 5 values of train data is

Outlook Temperature Humidity Windy

0 Sunny Hot High False

1 Sunny Hot High True

2 Overcast Hot High False

3 Rainy Mild High False

4 Rainy Cool Normal False


The first 5 values of Train output is

0 No

1 No

2 Yes

3 Yes

4 Yes

Name: PlayTennis, dtype: object

Now the Train data is :

Outlook Temperature Humidity Windy

0 2 1 0 0

1 2 1 0 1

2 0 1 0 0

3 1 2 0 0

4 1 0 1 0

Now the Train output is

[0 0 1 1 1 0 1 0 1 1 1 1 1 0]

Accuracy is: 0.3333333333333333


Result:

Thus the Python program to implement the naïve Bayesian classifier was

developed successfully

Ex.No. 8. IMPLEMENTING NEURAL NETWORK USING SELF-ORGANIZING MAPS


DATE:

AIM:

To write a Python program to Implementing neural network using self-organizing maps.

ALGORITHM:

Step 1: Initialize the weights wij random value may be assumed. Initialize the

learning rate α.

Step 2: Calculate squared Euclidean distance.

D(j) = Σ (wij – xi)^2 where i=1 to n and j=1 to m

Step 3: Find index J, when D(j) is minimum that will be considered as winning

index.

Step 4: For each j within a specific neighborhood of j and for all i, calculate the

new weight.

wij(new)=wij(old) + α[xi – wij(old)]

Step 5: Update the learning rule by using :

α(t+1) = 0.5 * t

Step 6: Test the Stopping Condition.

Program:
import math

class SOM:

# Function here computes the winning vector

# by Euclidean distance

def winner(self, weights, sample):

D0 = 0

D1 = 0

for i in range(len(sample)):

D0 = D0 + math.pow((sample[i] - weights[0][i]), 2)

D1 = D1 + math.pow((sample[i] - weights[1][i]), 2)

# Selecting the cluster with smallest distance as winning cluster

if D0 < D1:

return 0

else:

return 1

# Function here updates the winning vector

def update(self, weights, sample, J, alpha):

# Here iterating over the weights of winning cluster and modifying them

for i in range(len(weights[0])):

weights[J][i] = weights[J][i] + alpha * (sample[i] - weights[J][i])

return weights
# Driver code

def main():

# Training Examples ( m, n )

T = [[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]]

m, n = len(T), len(T[0])

# weight initialization ( n, C )

weights = [[0.2, 0.6, 0.5, 0.9], [0.8, 0.4, 0.7, 0.3]]

# training

ob = SOM()

epochs = 3

alpha = 0.5

for i in range(epochs):

for j in range(m):

# training sample

sample = T[j]

# Compute winner vector

J = ob.winner(weights, sample)
# Update winning vector

weights = ob.update(weights, sample, J, alpha)

# classify test sample

s = [0, 0, 0, 1]

J = ob.winner(weights, s)

print("Test Sample s belongs to Cluster : ", J)

print("Trained weights : ", weights)

if __name__ == "__main__":

main()

OUTPUT:

Test Sample s belongs to Cluster : 0


Trained weights : [[0.6000000000000001, 0.8, 0.5, 0.9], [0.3333984375, 0.0666015625, 0.7,
0.3]]

Result:

Thus the Python program to implementing using neural networks using self organizing maps was
devolped successfully

Ex.No. 9. Implementing k-Means algorithm to cluster a set of data.

DATE:
AIM:
To write a Python program to Implementing k-Means algorithm to cluster a set of data.

ALGORITHM:

Step 1: Select the value of K to decide the number of clusters (n_clusters) to be

formed.

Step 2: Select random K points that will act as cluster centroids (cluster_centers).

Step 3: Assign each data point, based on their distance from the randomly

selected points (Centroid), to the nearest/closest centroid, which will form the

predefined clusters.

Step 4: Place a new centroid of each cluster.

Step 5: Repeat step no.3, which reassigns each datapoint to the new closest

centroid of each cluster.

Step 6: If any reassignment occurs, then go to step 4; else, go to step 7.

Step 7: Finish

Program:

from sklearn.cluster import KMeans


import numpy as np

# Sample data

data = np.array([[1, 2], [5, 8], [1.5, 1.8], [8, 8], [1, 0.6], [9, 11]])

# Specify the number of clusters (K)

kmeans = KMeans(n_clusters=2)

# Fit the data to the algorithm

kmeans.fit(data)

# Get the cluster centroids and labels

centroids = kmeans.cluster_centers_

labels = kmeans.labels_

print("Centroids:")

print(centroids)

print("Labels:")

print(labels)

OUTPUT:

Centroids:
[[1.16666667 1.46666667]
[7.33333333 9. ]]
Labels:
[0 1 0 1 0 1]

Example:2
import matplotlib.pyplot as plt

from sklearn import datasets

from sklearn.cluster import KMeans

import sklearn.metrics as sm

import pandas as pd

import numpy as np

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)

plt.figure(figsize=(14,7))

colormap = np.array(['red', 'lime', 'black'])


# Plot the Original Classifications

plt.subplot(1, 2, 1)

plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[y.Targets], s=40)

plt.title('Real Classification')

plt.xlabel('Petal Length')

plt.ylabel('Petal Width')

# Plot the Models Classifications

plt.subplot(1, 2, 2)

plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[model.labels_], s=40)

plt.title('K Mean Classification')

plt.xlabel('Petal Length')

plt.ylabel('Petal Width')

print('The accuracy score of K-Mean: ',sm.accuracy_score(y, model.labels_))

print('The Confusion matrixof K-Mean: ',sm.confusion_matrix(y,


model.labels_))

from sklearn import preprocessing

scaler = preprocessing.StandardScaler()

scaler.fit(X)

xsa = scaler.transform(X)
xs = pd.DataFrame(xsa, columns = X.columns)

#xs.sample(5)

from sklearn.mixture import GaussianMixture

gmm = GaussianMixture(n_components=3)

gmm.fit(xs)

y_gmm = gmm.predict(xs)

#y_cluster_gmm

plt.subplot(2, 2, 3)

plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[y_gmm], s=40)

plt.title('GMM Classification')

plt.xlabel('Petal Length')

plt.ylabel('Petal Width')

print('The accuracy score of EM: ',sm.accuracy_score(y, y_gmm))

print('The Confusion matrix of EM: ',sm.confusion_matrix(y, y_gmm))

output:

The accuracy score of K-Mean: 0.44


The Confusion matrixof K-Mean: [[50 0 0]

[ 0 2 48]

[ 0 36 14]]

The accuracy score of EM: 0.03333333333333333

The Confusion matrix of EM: [[ 0 0 50]

[45 5 0]

[ 0 50 0]]
Result:

Thus the Python program to Implementing k-Means algorithm to cluster a set of data was
devolped was successfully.

EX.NO. 10. IMPLEMENTING HIERARCHICAL CLUSTERING ALGORITHM

DATE:
AIM:

To write a Python program to Implementing hierarchical clustering algorithm

ALGORITHM:

Step-1.Data Pre-processing Steps:

Importing the libraries

Importing the dataset

Step-2.Extracting the matrix of features

Step-3: Finding the optimal number of clusters using the Dendrogram

Step-4: Training the hierarchical clustering model

Step-5: Visualizing the clusters

Program:
import numpy as nm

import matplotlib.pyplot as mtp

import pandas as pd

dataset = pd.read_csv('Mall_Customers.csv')

x = dataset.iloc[:, [3, 4]].values

#Finding the optimal number of clusters using the dendrogram

import scipy.cluster.hierarchy as shc

dendro = shc.dendrogram(shc.linkage(x, method="ward"))

mtp.title("Dendrogrma Plot")

mtp.ylabel("Euclidean Distances")

mtp.xlabel("Customers")

mtp.show()

from sklearn.cluster import AgglomerativeClustering


hc= AgglomerativeClustering(n_clusters=5, affinity='euclidean', linkage='ward')

y_pred= hc.fit_predict(x)

#visulaizing the clusters

mtp.scatter(x[y_pred == 0, 0], x[y_pred == 0, 1], s = 100, c = 'blue', label = 'Cluster 1')

mtp.scatter(x[y_pred == 1, 0], x[y_pred == 1, 1], s = 100, c = 'green', label = 'Cluster 2')

mtp.scatter(x[y_pred== 2, 0], x[y_pred == 2, 1], s = 100, c = 'red', label = 'Cluster 3')

mtp.scatter(x[y_pred == 3, 0], x[y_pred == 3, 1], s = 100, c = 'cyan', label = 'Cluster 4')

mtp.scatter(x[y_pred == 4, 0], x[y_pred == 4, 1], s = 100, c = 'magenta', label = 'Cluster 5')

mtp.title('Clusters of customers')

mtp.xlabel('Annual Income (k$)')

mtp.ylabel('Spending Score (1-100)')

mtp.legend()

mtp.show()

OUTPUT:
Result:

Thus the python program to implement hierarchical clustering algorithm was devolped
sucessfully

EXP:11 Analysis of breadth first and depth first search in


terms of time and space

Aim:

To implement the breadth first and depth first search in terms of time and space

Algorithm:

Step 1: Create a queue for BFS and initialize it with the starting node

Step 2: Create a set to mark visited nodes and mark the starting node as visited

Step 3: Begin the BFS loop

Step 4: Dequeue a node from the front of the queue

Step 5: Process the current node (in this example, print it)

Step 6: Explore neighbors of the current node

Step 7: Check if the neighbor has not been visited

Step 8: Enqueue the neighbor for further exploration

Step 9: Mark the neighbor as visited

Step 10: Define a graph as an adjacency list

Step 11: Start BFS from node 'A'

Step 12: End of the program

PROGRAM:
from collections import deque

# Function to perform Breadth-First Search

def bfs(graph, start):

queue = deque()

queue.append(start)

visited = set()

visited.add(start)

while queue:

current_node = queue.popleft()

print(current_node, end=' ')

for neighbor in graph[current_node]:

if neighbor not in visited:

queue.append(neighbor)

visited.add(neighbor)

# Main function

if __name__ == "__main__":

graph = {

'A': ['B', 'C'],

'B': ['A', 'D', 'E'],

'C': ['A', 'F', 'G'],

'D': ['B'],

'E': ['B', 'H'],

'F': ['C'],

'G': ['C'],

'H': ['E']

}
print("BFS traversal starting from node 'A':")

bfs(graph, 'A')

Output:

BFS traversal starting from node 'A':

ABCDEFGH

Result:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy