0% found this document useful (0 votes)
10 views33 pages

Python Data Operations and Visualization

The document contains multiple Python programs demonstrating various data structures and algorithms, including operations on lists, tuples, sets, and dictionaries, as well as data visualization techniques using scatter plots, box plots, and heatmaps. It also implements the Hill Climbing algorithm, Best First Search (BFS), and A* algorithm. Each section includes sample outputs and user input prompts for interactive execution.
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)
10 views33 pages

Python Data Operations and Visualization

The document contains multiple Python programs demonstrating various data structures and algorithms, including operations on lists, tuples, sets, and dictionaries, as well as data visualization techniques using scatter plots, box plots, and heatmaps. It also implements the Hill Climbing algorithm, Best First Search (BFS), and A* algorithm. Each section includes sample outputs and user input prompts for interactive execution.
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

1.

Simple python program using condi onal statements, looping , performing opera ons such as Insert ,
Update, Delete, Display, Sor ng and searching on data types like List, Tuple, Set, Dic onary.

def list_operations():
my_list = []
print("\n--- List Operations ---")
print("1. Insert")
print("2. Update")
print("3. Delete")
print("4. Display")
print("5. Sort")
print("6. Search")
print("7. Exit")

while True:
choice = input("Enter choice: ")

if choice == '1':
value = input("Enter value to insert: ")
my_list.append(value)
elif choice == '2':
old = input("Enter value to update: ")
if old in my_list:
new = input("Enter new value: ")
my_list[my_list.index(old)] = new
else:
print("Value not found.")
elif choice == '3':
value = input("Enter value to delete: ")
if value in my_list:
my_list.remove(value)
else:
print("Value not found.")
elif choice == '4':
print("List contents:", my_list)
elif choice == '5':
my_list.sort()
print("Sorted List:", my_list)
elif choice == '6':
search = input("Enter value to search: ")
print("Found!" if search in my_list else "Not found.")
elif choice == '7':
break
else:
print("Invalid choice.")

def tuple_operations():
my_tuple = ("apple", "banana", "cherry")
print("\n--- Tuple Operations (Immutable) ---")
print("Original Tuple:", my_tuple)
print("1. Display")
print("2. Search")
print("3. Convert to List and Add Item")
choice = input("Enter choice: ")
if choice == '1':
print("Tuple Contents:", my_tuple)
elif choice == '2':
item = input("Enter item to search: ")
print("Found!" if item in my_tuple else "Not found.")
elif choice == '3':
item = input("Enter item to add: ")
temp = list(my_tuple)
[Link](item)
my_tuple = tuple(temp)
print("Updated Tuple:", my_tuple)
else:
print("Invalid choice.")

def set_operations():
my_set = set()
print("\n--- Set Operations ---")
print("1. Insert")
print("2. Delete")
print("3. Display")
print("4. Search")
print("5. Exit")
while True:
choice = input("Enter choice: ")

if choice == '1':
value = input("Enter value to insert: ")
my_set.add(value)
elif choice == '2':
value = input("Enter value to delete: ")
my_set.discard(value)
elif choice == '3':
print("Set contents:", my_set)
elif choice == '4':
value = input("Enter value to search: ")
print("Found!" if value in my_set else "Not found.")
elif choice == '5':
break
else:
print("Invalid choice.")

def dict_operations():
my_dict = {}
print("\n--- Dictionary Operations ---")
print("1. Insert")
print("2. Update")
print("3. Delete")
print("4. Display")
print("5. Search")
print("6. Exit")
while True:
choice = input("Enter choice: ")
if choice == '1':
key = input("Enter key: ")
value = input("Enter value: ")
my_dict[key] = value
elif choice == '2':
key = input("Enter key to update: ")
if key in my_dict:
value = input("Enter new value: ")
my_dict[key] = value
else:
print("Key not found.")
elif choice == '3':
key = input("Enter key to delete: ")
if key in my_dict:
del my_dict[key]
else:
print("Key not found.")
elif choice == '4':
print("Dictionary contents:", my_dict)
elif choice == '5':
key = input("Enter key to search: ")
print("Found!" if key in my_dict else "Not found.")
elif choice == '6':
break
else:
print("Invalid choice.")

# Main Program Loop


print("\n===== Main Menu =====")
print("1. List")
print("2. Tuple")
print("3. Set")
print("4. Dictionary")
print("5. Exit")

while True:
main_choice = input("Enter Choice: ")

if main_choice == '1':
list_operations()
elif main_choice == '2':
tuple_operations()
elif main_choice == '3':
set_operations()
elif main_choice == '4':
dict_operations()
elif main_choice == '5':
print("Exiting Program.")
break
else:
print("Invalid choice. Try again.")
Sample output:

===== Main Menu =====


1. List
2. Tuple
3. Set
4. Dictionary
5. Exit
Enter Choice: 1

--- List Operations ---


1. Insert
2. Update
3. Delete
4. Display
5. Sort
6. Search
7. Exit
Enter choice: 1
Enter value to insert: mango
Enter choice: 1
Enter value to insert: apple
Enter choice: 4
List contents: ['mango', 'apple']
Enter choice: 5
Sorted List: ['apple', 'mango']
Enter choice: 6
Enter value to search: mango
Found!
Enter choice: 7

Enter Choice: 2

--- Tuple Operations (Immutable) ---


Original Tuple: ('apple', 'banana', 'cherry')
1. Display
2. Search
3. Convert to List and Add Item
Enter choice: 2
Enter item to search: banana
Found!

Enter Choice: 3

--- Set Operations ---


1. Insert
2. Delete
3. Display
4. Search
5. Exit
Enter choice: 1
Enter value to insert: orange
Enter choice: 3
Set contents: {'orange'}
Enter choice: 4
Enter value to search: apple
Not found.
Enter choice: 5

Enter Choice: 4

--- Dictionary Operations ---


1. Insert
2. Update
3. Delete
4. Display
5. Search
6. Exit
Enter choice: 1
Enter key: name
Enter value: Alice
Enter choice: 1
Enter key: age
Enter value: 25
Enter choice: 4
Dictionary contents: {'name': 'Alice', 'age': '25'}
Enter choice: 2
Enter key to update: age
Enter new value: 26
Enter choice: 5
Enter key to search: name
Found!
Enter choice: 6

Enter Choice: 5
Exiting Program.
2. Visualize the n-dimensional data using Sca er plots, box plot, heat maps, contour plots, 3D surface plots
using python packages.

pip install numpy pandas seaborn matplotlib

import numpy as np
import pandas as pd
import seaborn as sns
import [Link] as plt
from mpl_toolkits.mplot3d import Axes3D

# Generate some random n-dimensional data


[Link](42)
n = 100
data = [Link]({
'X': [Link](0, 1, n),
'Y': [Link](0, 1, n),
'Z': [Link](0, 1, n),
'Category': [Link](['A', 'B', 'C'], n)
})

# -------- Scatter Plot (2D) --------


def scatter_plot():
[Link](figsize=(6, 4))
[Link](data=data, x='X', y='Y', hue='Category')
[Link]("2D Scatter Plot")
[Link]()

# -------- Box Plot --------


def box_plot():
[Link](figsize=(6, 4))
[Link](data=data, x='Category', y='Z')
[Link]("Box Plot of Z by Category")
[Link]()

# -------- Heatmap --------


def heatmap():
correlation = data[['X', 'Y', 'Z']].corr()
[Link](figsize=(5, 4))
[Link](correlation, annot=True, cmap='coolwarm')
[Link]("Heatmap of Correlation Matrix")
[Link]()

# -------- Contour Plot --------


def contour_plot():
x = [Link](-3, 3, 100)
y = [Link](-3, 3, 100)
X, Y = [Link](x, y)
Z = [Link](X**2 + Y**2)

[Link](figsize=(6, 5))
cp = [Link](X, Y, Z, cmap='viridis')
[Link](cp)
[Link]("Contour Plot of sin(X² + Y²)")
[Link]("X")
[Link]("Y")
[Link]()

# -------- 3D Surface Plot --------


def surface_plot():
fig = [Link](figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')

x = [Link](-3, 3, 100)
y = [Link](-3, 3, 100)
X, Y = [Link](x, y)
Z = [Link]([Link](X**2 + Y**2))

surf = ax.plot_surface(X, Y, Z, cmap='viridis', edgecolor='none')


[Link](surf)
ax.set_title("3D Surface Plot")
[Link]()

# -------- Call All Plots --------


scatter_plot()
box_plot()
heatmap()
contour_plot()
surface_plot()
3. Write a program to implement Hill Climbing Algorithm.

import random

# Objective function (maximize this)


def objective_function(x):
return -x**2 + 5

# Hill Climbing algorithm


def hill_climbing(start_x, step_size, max_iterations):
current_x = start_x
current_score = objective_function(current_x)

for i in range(max_iterations):
# Try a new solution nearby
new_x = current_x + [Link](-step_size, step_size)
new_score = objective_function(new_x)

print(f"Iteration {i+1}: x = {current_x:.4f}, f(x) = {current_score:.4f}")

# If the new solution is better, move to it


if new_score > current_score:
current_x = new_x
current_score = new_score
else:
# No improvement; stop if you want a simple version
pass

print("\nFinal Solution:")
print(f"x = {current_x:.4f}, f(x) = {current_score:.4f}")
return current_x, current_score

# Run the algorithm


# best_x, best_score = hill_climbing(start_x=[Link](-5, 5), step_size=0.1,
max_iterations=100)
best_x, best_score = hill_climbing(start_x=0.1, step_size=0.05, max_iterations=5)

Sample output:

Itera on 1: x = 0.1000, f(x) = 4.9900

Itera on 2: x = 0.0587, f(x) = 4.9966

Itera on 3: x = 0.0235, f(x) = 4.9994

Itera on 4: x = -0.0112, f(x) = 4.9999

Itera on 5: x = -0.0421, f(x) = 4.9982

Final Solu on:

x = -0.0112, f(x) = 4.9999


4. a) Write a program to implement the Best First Search (BFS) algorithm.

import heapq

class Node:
def __init__(self, name, heuristic, parent=None):
[Link] = name
[Link] = heuristic
[Link] = parent

def __lt__(self, other):


return [Link] < [Link]

def best_first_search(graph, start, goal, heuristic_values):


open_list = []
closed_list = set()

[Link](open_list, Node(start, heuristic_values[start]))

while open_list:
current_node = [Link](open_list)

if current_node.name == goal:
path = []
while current_node:
[Link](current_node.name)
current_node = current_node.parent
return path[::-1]

if current_node.name in closed_list:
continue

closed_list.add(current_node.name)

for neighbor in [Link](current_node.name, []):


if neighbor not in closed_list:
[Link](open_list, Node(neighbor, heuristic_values[neighbor],
current_node))

return None

# Take custom input for the graph and heuristic values


def get_input():
graph = {}
heuristic_values = {}

print("Enter the graph structure:")


n = int(input("Enter number of nodes: "))

for _ in range(n):
node = input("Enter node name: ")
neighbors = input(f"Enter neighbors for {node} (comma separated): ").split(",")
graph[node] = [[Link]() for neighbor in neighbors]
print("\nEnter heuristic values:")
for _ in range(n):
node = input("Enter node name for heuristic: ")
heuristic = int(input(f"Enter heuristic value for {node}: "))
heuristic_values[node] = heuristic

start = input("\nEnter the start node: ")


goal = input("Enter the goal node: ")

return graph, heuristic_values, start, goal

# Main execution
if __name__ == "__main__":
graph, heuristic_values, start_node, goal_node = get_input()
path = best_first_search(graph, start_node, goal_node, heuristic_values)

if path:
print(f"\nPath from {start_node} to {goal_node}: {path}")
else:
print(f"\nNo path found from {start_node} to {goal_node}.")

Sample output:

Enter the graph structure:

Enter number of nodes: 5

Enter node name: A

Enter neighbors for A (comma separated): B, C

Enter node name: B

Enter neighbors for B (comma separated): A, D, E

Enter node name: C

Enter neighbors for C (comma separated): A, F

Enter node name: D

Enter neighbors for D (comma separated): B

Enter node name: E

Enter neighbors for E (comma separated): B, G

Enter heuris c values:

Enter node name for heuris c: A

Enter heuris c value for A: 6

Enter node name for heuris c: B

Enter heuris c value for B: 4

Enter node name for heuris c: C


Enter heuris c value for C: 3

Enter node name for heuris c: D

Enter heuris c value for D: 7

Enter node name for heuris c: E

Enter heuris c value for E: 2

Enter the start node: A

Enter the goal node: G

Path from A to G: ['A', 'C', 'F', 'E', 'G']


4b) Write a program to implement the A* algorithm.

import heapq

class Node:
def __init__(self, name, heuristic, parent=None):
[Link] = name
[Link] = heuristic
[Link] = parent

def __lt__(self, other):


return [Link] < [Link]

def best_first_search(graph, start, goal, heuristic_values):


open_list = []
closed_list = set()

[Link](open_list, Node(start, heuristic_values[start]))

while open_list:
current_node = [Link](open_list)

if current_node.name == goal:
path = []
while current_node:
[Link](current_node.name)
current_node = current_node.parent
return path[::-1]

if current_node.name in closed_list:
continue

closed_list.add(current_node.name)

for neighbor in [Link](current_node.name, []):


if neighbor not in closed_list:
[Link](open_list, Node(neighbor, heuristic_values[neighbor],
current_node))

return None

# Take custom input for the graph and heuristic values


def get_input():
graph = {}
heuristic_values = {}

print("Enter the graph structure:")


n = int(input("Enter number of nodes: "))

for _ in range(n):
node = input("Enter node name: ")
neighbors = input(f"Enter neighbors for {node} (comma separated): ").split(",")
graph[node] = [[Link]() for neighbor in neighbors]

print("\nEnter heuristic values:")


for _ in range(n):
node = input("Enter node name for heuristic: ")
heuristic = int(input(f"Enter heuristic value for {node}: "))
heuristic_values[node] = heuristic

start = input("\nEnter the start node: ")


goal = input("Enter the goal node: ")

return graph, heuristic_values, start, goal

# Main execution
if __name__ == "__main__":
graph, heuristic_values, start_node, goal_node = get_input()
path = best_first_search(graph, start_node, goal_node, heuristic_values)

if path:
print(f"\nPath from {start_node} to {goal_node}: {path}")
else:
print(f"\nNo path found from {start_node} to {goal_node}.")

Sample Output:

Enter the graph structure:

Enter number of nodes: 5

Enter node name: A

Enter neighbors for A (comma separated): B, C

Enter node name: B

Enter neighbors for B (comma separated): A, D, E

Enter node name: C

Enter neighbors for C (comma separated): A, F

Enter node name: D

Enter neighbors for D (comma separated): B

Enter node name: E

Enter neighbors for E (comma separated): B, G

Enter heuris c values:

Enter node name for heuris c: A

Enter heuris c value for A: 6

Enter node name for heuris c: B

Enter heuris c value for B: 4


Enter node name for heuris c: C

Enter heuris c value for C: 3

Enter node name for heuris c: D

Enter heuris c value for D: 7

Enter node name for heuris c: E

Enter heuris c value for E: 2

Enter edge costs (cost between nodes):

Enter number of edges: 6

Enter edge (u, v, cost): A, B, 1

Enter edge (u, v, cost): A, C, 2

Enter edge (u, v, cost): B, D, 2

Enter edge (u, v, cost): B, E, 1

Enter edge (u, v, cost): C, F, 3

Enter edge (u, v, cost): E, G, 4

Enter the start node: A

Enter the goal node: G

Path from A to G: ['A', 'B', 'E', 'G']


5) Write a program to implement Min-Max algorithm and Alpha-beta pruning algorithm.

def minimax(depth, node_index, is_maximizing_player, scores, target_depth):


if depth == target_depth:
return scores[node_index]

if is_maximizing_player:
return max(
minimax(depth + 1, node_index * 2, False, scores, target_depth),
minimax(depth + 1, node_index * 2 + 1, False, scores, target_depth)
)
else:
return min(
minimax(depth + 1, node_index * 2, True, scores, target_depth),
minimax(depth + 1, node_index * 2 + 1, True, scores, target_depth)
)

def alphabeta(depth, node_index, is_maximizing_player, scores, target_depth, alpha, beta):


if depth == target_depth:
return scores[node_index]

if is_maximizing_player:
max_eval = float('-inf')
for i in range(2):
eval = alphabeta(depth + 1, node_index * 2 + i, False, scores, target_depth,
alpha, beta)
max_eval = max(max_eval, eval)
alpha = max(alpha, eval)
if beta <= alpha:
break # Beta cut-off
return max_eval
else:
min_eval = float('inf')
for i in range(2):
eval = alphabeta(depth + 1, node_index * 2 + i, True, scores, target_depth,
alpha, beta)
min_eval = min(min_eval, eval)
beta = min(beta, eval)
if beta <= alpha:
break # Alpha cut-off
return min_eval

# ---------------- Input Section ----------------

print("Enter the depth of the game tree (e.g., 3 for 8 leaf nodes):")
tree_depth = int(input("Depth: "))
num_leaves = 2 ** tree_depth

print(f"Enter {num_leaves} leaf node scores separated by space:")


scores_input = input("Scores: ")
scores = list(map(int, scores_input.strip().split()))

if len(scores) != num_leaves:
print(f"Error: Expected {num_leaves} scores, but got {len(scores)}.")
else:
optimal_value = minimax(0, 0, True, scores, tree_depth)
print(f"\nOptimal value using Minimax: {optimal_value}")

optimal_value_ab = alphabeta(0, 0, True, scores, tree_depth, float('-inf'),


float('inf'))
print(f"Optimal value using Alpha-Beta Pruning: {optimal_value_ab}")

Sample output:

Enter the depth of the game tree (e.g., 3 for 8 leaf nodes):

Depth: 3

Enter 8 leaf node scores separated by space:

Scores: 3 5 6 9 1 2 0 -1

Optimal value using Minimax: 5

Optimal value using Alpha-Beta Pruning: 5


6) Write a program to develop the Naive Bayes classifier based on split up of training and tes ng dataset as 90-10, 70-
30. a) Iris dataset b) Titanic dataset

import numpy as np
import pandas as pd
from [Link] import confusion_matrix
from sklearn.model_selection import train_test_split

# ------------------------------------------Build the classifier--------------------------


------

class NaiveBayesClassifier:
def __init__(self):
[Link] = {}
[Link] = {}

def fit(self, X, y):


[Link] = [Link](y)
for c in [Link]:
[Link][c] = [Link](y == c)

for feature in [Link]:


[Link][feature] = {}
for c in [Link]:
feature_values = X[feature][y == c]
[Link][feature][c] = {
'mean': [Link](feature_values),
'std': [Link](feature_values)
}

def predict(self, X):


y_pred = []
for _, sample in [Link]():
probabilities = {}
for c in [Link]:
probabilities[c] = [Link][c]
for feature in [Link]:
mean = [Link][feature][c]['mean']
std = [Link][feature][c]['std']
x = sample[feature]
probabilities[c] *= self._gaussian_pdf(x, mean, std)
y_pred.append(max(probabilities, key=[Link]))
return y_pred

def _gaussian_pdf(self, x, mean, std):


if std == 0:
return 1.0 if x == mean else 0.0
exponent = [Link](-((x - mean) ** 2) / (2 * std ** 2))
return (1 / ([Link](2 * [Link]) * std)) * exponent
# -------------------------------------------------a)Iris Dataset-------------------------
---------------

from [Link] import load_iris

# Load the iris dataset


iris = load_iris()
X_df = [Link]([Link], columns=iris.feature_names)
y_df = [Link]([Link], name="species")

# Function to run experiment for Iris dataset


def run_iris_experiment(test_size, label):
print(f"\n=== Iris Dataset - {label} Split (Test size = {test_size}) ===")
X_train, X_test, y_train, y_test = train_test_split(X_df, y_df, test_size=test_size)

classifier = NaiveBayesClassifier()
[Link](X_train, y_train)
y_pred = [Link](X_test)

cm = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:\n", cm)
accuracy = [Link](y_pred == y_test)
print("Accuracy:", accuracy)

# Run with 90-10 split


run_iris_experiment(test_size=0.1, label="90-10")

# Run with 70-30 split


run_iris_experiment(test_size=0.3, label="70-30")

# ----------------------------------------------------b)Titanic Dataset-------------------
--------------------------

# Load and preprocess Titanic dataset


df = pd.read_csv('[Link]')
df = df[['Survived', 'Pclass', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']]
df['Age'].fillna(df['Age'].median(), inplace=True)
df['Fare'].fillna(df['Fare'].median(), inplace=True)
df['Embarked'].fillna(df['Embarked'].mode()[0], inplace=True)
df['Embarked'] = df['Embarked'].map({'C': 0, 'Q': 1, 'S': 2})

# Function to run experiment with different splits


def run_titanic_experiment(test_size, label):
print(f"\n=== {label} Split (Test size = {test_size}) ===")
train, test = train_test_split(df, test_size=test_size)

X_train = [Link]('Survived', axis=1)


y_train = train['Survived']
X_test = [Link]('Survived', axis=1)
y_test = test['Survived']

classifier = NaiveBayesClassifier()
[Link](X_train, y_train)
y_pred = [Link](X_test)

cm = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:\n", cm)
accuracy = [Link](y_pred == y_test)
print("Accuracy:", accuracy)

# Run with 90-10 split


run_titanic_experiment(test_size=0.1, label="90-10")

# Run with 70-30 split


run_titanic_experiment(test_size=0.3, label="70-30")

Sample Output:

=== Iris Dataset - 90-10 Split (Test size = 0.1) ===


Confusion Matrix:
[[5 0 0]
[0 3 0]
[0 0 7]]
Accuracy: 1.0

=== Iris Dataset - 70-30 Split (Test size = 0.3) ===


Confusion Matrix:
[[15 0 0]
[ 0 15 0]
[ 0 2 13]]
Accuracy: 0.9555555555555556

=== 90-10 Split (Test size = 0.1) ===


Confusion Matrix:
[[48 6]
[ 8 17]]
Accuracy: 0.725

=== 70-30 Split (Test size = 0.3) ===


Confusion Matrix:
[[104 19]
[ 25 31]]
Accuracy: 0.7528089887640449
7) Write a program to develop the KNN classifier for the k values as 3,5,7 based on split up of training and tes ng
dataset as 90-10, 70-30,
a) Glass dataset
b) Fruit dataset
using the different distance metrics like Euclidean and Manha an distance.

import numpy as np
import pandas as pd
from collections import Counter
from sklearn.model_selection import train_test_split

# Distance metrics
def euclidean_distance(x1, x2):
return [Link]([Link]((x1 - x2) ** 2))

def manhattan_distance(x1, x2):


return [Link]([Link](x1 - x2))

# KNN Classifier
class KNN:
def __init__(self, k, distance_metric):
self.k = k
self.distance_metric = distance_metric

def fit(self, X, y):


self.X_train = X
self.y_train = y

def predict(self, X):


return [self._predict(x) for x in X]

def _predict(self, x):


distances = [self.distance_metric(x, x_train) for x_train in self.X_train]
k_indices = [Link](distances)[:self.k]
k_nearest_labels = [self.y_train[i] for i in k_indices]
most_common = Counter(k_nearest_labels).most_common(1)
return most_common[0][0]

# Experiment function
def run_knn_experiment(X, y, dataset_name, test_size, distance_metric, metric_name):
print(f"\n--- {dataset_name} Dataset | Split: {int((1-test_size)*100)}-
{int(test_size*100)} | Distance: {metric_name} ---")
for k in [3, 5, 7]:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size,
random_state=42)
model = KNN(k=k, distance_metric=distance_metric)
[Link](X_train, y_train)
predictions = [Link](X_test)
accuracy = [Link](predictions == y_test) / len(y_test)
print(f"k={k} | Accuracy: {accuracy:.4f}")
# ---------------------- a) Glass Dataset ----------------------
glass_df = pd.read_csv('[Link]')
X_glass = glass_df.drop('Type', axis=1).values
y_glass = glass_df['Type'].values

# Run experiments for Glass dataset


for test_size in [0.1, 0.3]:
run_knn_experiment(X_glass, y_glass, dataset_name="Glass", test_size=test_size,
distance_metric=euclidean_distance, metric_name="Euclidean")
run_knn_experiment(X_glass, y_glass, dataset_name="Glass", test_size=test_size,
distance_metric=manhattan_distance, metric_name="Manhattan")

# ---------------------- b) Fruit Dataset ----------------------


fruit_df = pd.read_csv('[Link]')
X_fruit = fruit_df[['mass', 'width', 'height', 'color_score']].values
y_fruit = fruit_df['fruit_label'].values

# Run experiments for Fruit dataset


for test_size in [0.1, 0.3]:
run_knn_experiment(X_fruit, y_fruit, dataset_name="Fruit", test_size=test_size,
distance_metric=euclidean_distance, metric_name="Euclidean")
run_knn_experiment(X_fruit, y_fruit, dataset_name="Fruit", test_size=test_size,
distance_metric=manhattan_distance, metric_name="Manhattan")

Sample Output:
--- Glass Dataset | Split: 90-10 | Distance: Euclidean ---
k=3 | Accuracy: 0.7209
k=5 | Accuracy: 0.6977
k=7 | Accuracy: 0.6977

--- Glass Dataset | Split: 90-10 | Distance: Manha an ---


k=3 | Accuracy: 0.6977
k=5 | Accuracy: 0.6977
k=7 | Accuracy: 0.7209

--- Glass Dataset | Split: 70-30 | Distance: Euclidean ---


k=3 | Accuracy: 0.7385
k=5 | Accuracy: 0.7077
k=7 | Accuracy: 0.7077

--- Glass Dataset | Split: 70-30 | Distance: Manha an ---


k=3 | Accuracy: 0.6923
k=5 | Accuracy: 0.7077
k=7 | Accuracy: 0.6923

--- Fruit Dataset | Split: 90-10 | Distance: Euclidean ---


k=3 | Accuracy: 1.0000
k=5 | Accuracy: 1.0000
k=7 | Accuracy: 1.0000

--- Fruit Dataset | Split: 90-10 | Distance: Manha an ---


k=3 | Accuracy: 1.0000
k=5 | Accuracy: 1.0000
k=7 | Accuracy: 1.0000

--- Fruit Dataset | Split: 70-30 | Distance: Euclidean ---


k=3 | Accuracy: 0.9333
k=5 | Accuracy: 0.9333
k=7 | Accuracy: 0.9333

--- Fruit Dataset | Split: 70-30 | Distance: Manha an ---


k=3 | Accuracy: 0.9333
k=5 | Accuracy: 0.9333
k=7 | Accuracy: 0.9333
8) Write a program to perform unsupervised K-means clustering techniques
(pip install numpy matplotlib scikit-learn)

import numpy as np
import [Link] as plt
from [Link] import load_iris

def kmeans(X, K, max_iters=100):


centroids = X[:K]

for _ in range(max_iters):
# Assign each data point to the nearest centroid

expanded_x = X[:, [Link]]


euc_dist = [Link](expanded_x - centroids, axis=2)
labels = [Link](euc_dist, axis=1)

# Update the centroids based on the assigned point


new_centroids = [Link]([X[labels == k].mean(axis=0) for k in range(K)])

# If the centroids did not change, stop iterating


if [Link](centroids == new_centroids):
break

centroids = new_centroids

return labels, centroids

X = load_iris() .data
K=3
labels, centroids = kmeans(X, K)
print("Labels:", labels)
print("Centroids:", centroids)

[Link](X[:, 0], X[:, 1], c=labels)


[Link](centroids[:, 0], centroids[:, 1], marker='x', color='red', s=200)
[Link]('Sepal Length')
[Link]('Sepal Width')
[Link]('K-means Clustering of Iris Dataset')
[Link]()
Output:
9) Write a program to perform agglomera ve clustering based on single linkage, complete-linkage criteria.
import numpy as np
import [Link] as plt
from [Link] import dendrogram, linkage
from [Link] import load_iris

iris = load_iris()
data = [Link][:6]

def proximity_matrix(data):
n = [Link][0]
proximity_matrix = [Link]((n, n))
for i in range(n):
for j in range(i+1, n):
proximity_matrix[i, j] = [Link](data[i] - data[j])
proximity_matrix[j, i] = proximity_matrix[i, j]
return proximity_matrix

def plot_dendrogram(data, method):


linkage_matrix = linkage(data, method=method)
dendrogram(linkage_matrix)
[Link](f'Dendrogram - {method} linkage')
[Link]('Data Points')
[Link]('Distance')
[Link]()

# Calculate the proximity matrix


print("Proximity matrix:")
print(proximity_matrix(data))

# Plot the dendrogram using single-linkage


plot_dendrogram(data, 'single')

# Plot the dendrogram using complete-linkage


plot_dendrogram(data, 'complete')
Output:
10) Write a program to develop Principal Component Analysis (PCA) algorithms.
import numpy as np
import [Link] as plt
from [Link] import load_iris

class PCA:

def __init__(self, n_components):


self.n_components = n_components
[Link] = None
[Link] = None

def fit(self, X):


# Mean center the data
[Link] = [Link](X, axis=0)
X = X - [Link]

#Calculate covariance matrix


cov = [Link](X.T)

#Calculate eigenvalues and eigen vectors


eigenvalues, eigenvectors, = [Link](cov)

# Sort the vectors in decreasing order of eigenvalues


eigenvectors = eigenvectors.T
idxs = [Link](eigenvalues)[::-1]
eigenvalues = eigenvalues[idxs]
eigenvectors = eigenvectors[idxs]

# Take required number of components


[Link] = eigenvectors[:self.n_components]

def transform(self, X):


X = X - [Link]
return [Link](X, [Link].T)

X = load_iris().data
y = load_iris().target

pca = PCA(2)
[Link](X)
X_projected = [Link](X)

print("Shape of Data:", [Link])


print("Shape of transformed Data:", X_projected.shape)

pc1 = X_projected[:, 0]
pc2 = X_projected[:, 1]

[Link](pc1, pc2, c=y, cmap="jet")


[Link]("Principal Component 1")
[Link]("Principal Component 2")
[Link]()
11) Write a program to develop Linear Discriminant Analysis (LDA) algorithms.

import numpy as np
import [Link] as plt
from [Link] import load_iris

class LDA:
def __init__(self, n_components):
self.n_components = n_components
self.linear_discriminants = None

def fit(self, X, y):


n_features = [Link][1]
class_labels = [Link](y)

# Calculate SB and SW
mean_overall = [Link](X, axis=0)
SW = [Link]((n_features, n_features))
SB = [Link]((n_features, n_features))

for c in class_labels:
X_c = X[y == c]
mean_c = [Link](X_c, axis=0)
SW += (X_c - mean_c).[Link]((X_c - mean_c))

n_c = X_c.shape[0]
mean_diff = (mean_c - mean_overall).reshape(n_features, 1)
SB += n_c * (mean_diff).dot(mean_diff.T)

# Determine SW^-1 * SB
A = [Link](SW).dot(SB)

#Calculate eigenvalues and eigen vectors


eigenvalues, eigenvectors = [Link](A)

# Sort the vectors in decreasing order of eigenvalues


eigenvectors = eigenvectors.T
idxs = [Link](eigenvalues)[::-1]
eigenvalues = eigenvalues[idxs]
eigenvectors = eigenvectors[idxs]

# Take required number of components


self.linear_discriminants = eigenvectors[:self.n_components]

def transform(self, X):


return [Link](X, self.linear_discriminants.T)

X = load_iris().data
Y = load_iris().target

lda = LDA(2)
[Link](X, Y)
X_projected = [Link](X)
print("Shape of Data:", [Link])
print("Shape of transformed Data:", X_projected.shape)

ld1 = X_projected[:, 0]
ld2 = X_projected[:, 1]

[Link](ld1, ld2, c=Y, cmap="jet")


[Link]("Linear Discriminant 1")
[Link]("Linear Discriminant 2")

[Link]()
12) Write a Program to develop simple single layer perceptron to implement AND, OR Boolean func ons.

import numpy as np

# Define the Sigmoid activation function and its derivative (for backpropagation)
def sigmoid(x):
return 1 / (1 + [Link](-x))

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

class Perceptron:
def __init__(self, input_size):
# Initialize the weights with random values and a bias term
[Link] = [Link](input_size) # Random initialization of weights
[Link] = [Link](1) # Random bias initialization

def forward(self, inputs):


# Weighted sum (dot product) + bias
total_input = [Link](inputs, [Link]) + [Link]
# Apply the activation function (sigmoid)
output = sigmoid(total_input)
return output

def train(self, X, y, epochs=1000, learning_rate=0.1):


# Training the perceptron with the perceptron learning rule
for epoch in range(epochs):
for i in range([Link][0]):
# Forward pass
output = [Link](X[i])
# Calculate the error (difference between expected and predicted output)
error = y[i] - output
# Update the weights and bias using the perceptron learning rule
[Link] += learning_rate * error * X[i]
[Link] += learning_rate * error

# AND and OR dataset


X_and = [Link]([[0, 0], [0, 1], [1, 0], [1, 1]]) # Input for AND/OR functions
y_and = [Link]([0, 0, 0, 1]) # Expected output for AND function
y_or = [Link]([0, 1, 1, 1]) # Expected output for OR function

# Create perceptron instances for AND and OR


perceptron_and = Perceptron(input_size=2)
perceptron_or = Perceptron(input_size=2)

# Train the perceptrons


perceptron_and.train(X_and, y_and, epochs=1000, learning_rate=0.1)
perceptron_or.train(X_and, y_or, epochs=1000, learning_rate=0.1)

# Test the perceptrons


print("AND Function Predictions:")
for i in range(X_and.shape[0]):
print(f"Input: {X_and[i]} - Predicted Output:
{round(perceptron_and.forward(X_and[i]))}")
print("\nOR Function Predictions:")
for i in range(X_and.shape[0]):
print(f"Input: {X_and[i]} - Predicted Output:
{round(perceptron_or.forward(X_and[i]))}")

AND Function Predictions:

Input: [0 0] - Predicted Output: 0

Input: [0 1] - Predicted Output: 0

Input: [1 0] - Predicted Output: 0

Input: [1 1] - Predicted Output: 1

OR Function Predictions:

Input: [0 0] - Predicted Output: 0

Input: [0 1] - Predicted Output: 1

Input: [1 0] - Predicted Output: 1

Input: [1 1] - Predicted Output: 1

You might also like