DWM Final Exps

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

DWM

EXP 1 PERFECT

EXP 2 PERFECT

EXP 3 PERFECT

EXP 4 PERFECT

EXP 4 EXCEL

EXP 5

def predict(test, p_pass, p_fail):

p_pass_result = p_study.get(test[0], 0) * p_sports.get(test[1], 0) * p_grade.get(test[2], 0) * p_pass

p_fail_result = p_study.get(test[0], 0) * p_sports.get(test[1], 0) * p_grade.get(test[2], 0) * p_fail

sum_prob = p_pass_result + p_fail_result

if sum_prob == 0:

print("Prediction: Fail")

else:

p_pass_result = p_pass_result / sum_prob

p_fail_result = p_fail_result / sum_prob

print("Probability of passing: ", p_pass_result)

print("Probability of failing: ", p_fail_result)

if p_pass_result > p_fail_result:

print("Prediction: Pass")

else:

print("Cannot make a prediction due to insufficient data.")

def testInput():

string = input("Enter comma-separated test tuple (e.g., 'Good','High','A'): ")

test = string.split(',')

return test

data = [

['Good', 'High', 'A', 'Pass'],

['Good', 'Low', 'B', 'Pass'],


['Poor', 'High', 'C', 'Fail'],

['Poor', 'Low', 'B', 'Fail'],

['Good', 'Low', 'A', 'Pass'],

['Good', 'High', 'C', 'Pass'],

p_study = {}

p_sports = {}

p_grade = {}

p_pass_count = 0

p_fail_count = 0

for row in data:

study, sports, grade, result = row

if result == "Pass":

p_study[study] = p_study.get(study, 0) + 1

p_sports[sports] = p_sports.get(sports, 0) + 1

p_grade[grade] = p_grade.get(grade, 0) + 1

p_pass_count += 1

else:

p_fail_count += 1

total_students = len(data)

p_pass = p_pass_count / total_students

p_fail = p_fail_count / total_students

for key in p_study:

p_study[key] /= total_students

for key in p_sports:

p_sports[key] /= total_students
for key in p_grade:

p_grade[key] /= total_students

test = testInput()

predict(test, p_pass, p_fail)

EXP 6 PERFECT

EXP 7

import java.io.*;

class Kmean {

public static void main(String args[]) {

int N = 9;

int arr[] = { 2, 4, 10, 12, 3, 20, 30, 11, 25 }; // initial data

int i, m1, m2, a, b, n = 0;

boolean flag = true;

float sum1 = 0, sum2 = 0;

a = arr[0];

b = arr[1];

m1 = a;

m2 = b;

int cluster1[] = new int[9];

int cluster2[] = new int[9];

int k, j; // Declare k and j outside the loop

for (i = 0; i < 9; i++)

System.out.print(arr[i] + "\t");

System.out.println();
do {

n++;

k = 0;

j = 0;

for (i = 0; i < 9; i++) {

if (Math.abs(arr[i] - m1) <= Math.abs(arr[i] - m2)) {

cluster1[k] = arr[i];

k++;

} else {

cluster2[j] = arr[i];

j++;

sum1 = 0;

sum2 = 0;

for (i = 0; i < k; i++)

sum1 = sum1 + cluster1[i];

for (i = 0; i < j; i++)

sum2 = sum2 + cluster2[i];

a = m1;

b = m2;

m1 = Math.round(sum1 / k);

m2 = Math.round(sum2 / j);

if (m1 == a && m2 == b)

flag = false;
else

flag = true;

System.out.println("After iteration " + n + " , cluster 1 :\n");

for (i = 0; i < k; i++) {

if (cluster1[i] != 0) {

System.out.print(cluster1[i] + "\t");

System.out.println("\n");

System.out.println("After iteration " + n + " , cluster 2 :\n");

for (i = 0; i < j; i++) {

if (cluster2[i] != 0) {

System.out.print(cluster2[i] + "\t");

} while (flag);

System.out.println("Final cluster 1 :\n");

for (i = 0; i < k; i++) {

if (cluster1[i] != 0) {

System.out.print(cluster1[i] + "\t");

System.out.println();

System.out.println("Final cluster 2 :\n");


for (i = 0; i < j; i++) {

if (cluster2[i] != 0) {

System.out.print(cluster2[i] + "\t");

EXP 8

import numpy as np

import matplotlib.pyplot as plt

from scipy.cluster.hierarchy import dendrogram, linkage

# Sample data

data = np.array([[1, 2], [2, 3], [8, 7], [8, 8]])

# Function to compute pairwise distances

def compute_distances(data):

n = len(data)

distances = np.zeros((n, n))

for i in range(n):

for j in range(n):

distances[i, j] = np.sqrt(np.sum((data[i] - data[j]) ** 2))

return distances

# Function for single linkage clustering


def single_linkage(data):

distances = compute_distances(data)

linkage_matrix = linkage(distances, method='single')

return linkage_matrix

# Function for complete linkage clustering

def complete_linkage(data):

distances = compute_distances(data)

linkage_matrix = linkage(distances, method='complete')

return linkage_matrix

# Function for average linkage clustering

def average_linkage(data):

distances = compute_distances(data)

linkage_matrix = linkage(distances, method='average')

return linkage_matrix

# Function to display a dendrogram

def display_dendrogram(linkage_matrix, linkage_type):

plt.figure(figsize=(10, 6))

dendrogram(linkage_matrix)

plt.title(f'{linkage_type.capitalize()} Linkage Dendrogram')

plt.show()

# Function to calculate minimum, maximum, and average linkage distances

def calculate_distances(linkage_matrix):

min_distance = linkage_matrix[0, 2]

max_distance = linkage_matrix[-1, 2]

avg_distance = np.mean(linkage_matrix[:, 2])

return min_distance, max_distance, avg_distance


# User input for linkage type

linkage_type = input("Choose linkage type (single, complete, or average): ").lower()

# Ensure the user's choice is one of the accepted options

if linkage_type not in ['single', 'complete', 'average']:

print("Invalid linkage type. Please choose from 'single', 'complete', or 'average'.")

else:

if linkage_type == 'single':

linkage_matrix = single_linkage(data)

elif linkage_type == 'complete':

linkage_matrix = complete_linkage(data)

else:

linkage_matrix = average_linkage(data)

display_dendrogram(linkage_matrix, linkage_type)

min_dist, max_dist, avg_dist = calculate_distances(linkage_matrix)

print(f"Minimum Linkage Distance: {min_dist}")

print(f"Maximum Linkage Distance: {max_dist}")

print(f"Average Linkage Distance: {avg_dist}")

EXP 9

from collections import Counter


from itertools import combinations

data = [
['T100', ['I1', 'I2', 'I5']],
['T200', ['I2', 'I4']],
['T300', ['I2', 'I3']],
['T400', ['I1', 'I2', 'I4']],
['T500', ['I1', 'I3']],
['T600', ['I2', 'I3']],
['T700', ['I1', 'I3']],
['T800', ['I1', 'I2', 'I3', 'I5']],
['T900', ['I1', 'I2', 'I3']]
]

init = []
for i in data:
for q in i[1]:
if(q not in init):
init.append(q)
init = sorted(init)

# Minimum support threshold


sp = 0.4
s = int(sp * len(init))

c = Counter()
for i in init:
for d in data:
if i in d[1]:
c[i] += 1

print("C1:")
for i in c:
print(f"{[i]}: {c[i]}")
print()

l = Counter()
for i in c:
if c[i] >= s:
l[frozenset([i])] += c[i]

print("L1:")
for i in l:
print(f"{list(i)}: {l[i]}")
print()

pl = l
pos = 1
for count in range(2, 1000):
nc = set()
temp = list(l)
for i in range(0, len(temp)):
for j in range(i + 1, len(temp)):
t = temp[i].union(temp[j])
if len(t) == count:
nc.add(temp[i].union(temp[j]))
nc = list(nc)
c = Counter()
for i in nc:
c[i] = 0
for q in data:
temp = set(q[1])
if i.issubset(temp):
c[i] += 1

print(f"C{count}:")
for i in c:
print(f"{list(i)}: {c[i]}")
print()

l = Counter()
for i in c:
if c[i] >= s:
l[i] += c[i]

print(f"L{count}:")
for i in l:
print(f"{list(i)}: {l[i]}")
print()

if len(l) == 0:
break
pl = l
pos = count

print("Result:")
print(f"L{pos}:")
for i in pl:
print(f"{list(i)}: {pl[i]}")
print()

for l in pl:
c = [frozenset(q) for q in combinations(l, len(l) - 1)]
mmax = 0
for a in c:
b=l-a
ab = l
sab = 0
sa = 0
sb = 0
for q in data:
temp = set(q[1])
if a.issubset(temp):
sa += 1
if b.issubset(temp):
sb += 1
if ab.issubset(temp):
sab += 1
print(f"{list(a)} -> {list(b)} = {sab / sa * 100}%")
print(f"{list(b)} -> {list(a)} = {sab / sb * 100}%")
print()

EXP 10

# Step 1: Define a different dataset

data = [

["A", "B", "C"],

["B", "C", "D"],

["C", "A"],

["D", "A", "B"],

["E", "D"],

# Step 2: Initialize page rank for each webpage


web_pages = {}

for row in data:

web_pages[row[0]] = 1

# Damping factor

damping_factor = 0.85

# Step 5: Repeat until convergence

convergence_criteria = 0.0001

delta = 1

while delta > convergence_criteria:

new_ranks = {}

for page, rank in web_pages.items():

total_links = sum(1 for row in data if page in row[1:])

linked_pages = [row[0] for row in data if page in row[1:]]

linked_rank = sum(web_pages[other_page] / sum(1 for row in data if


other_page in row) for other_page in linked_pages)

new_rank = (1 - damping_factor) + damping_factor * linked_rank

new_ranks[page] = new_rank

delta = sum(abs(new_ranks[page] - web_pages[page]) for page in


web_pages)

web_pages = new_ranks
# Step 6: Find the page with the highest rank

highest_page = max(web_pages, key=web_pages.get)

# Store the results in a list of lists

table = [["Page", "Page Rank"]]

for page, rank in web_pages.items():

table.append([page, rank])

# Print the results in a table format

for row in table:

print(row)

print(f"\nWebpage with the highest Page Rank: {highest_page}")

You might also like