-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAi_Lab_5_codes.py
192 lines (137 loc) · 5.2 KB
/
Ai_Lab_5_codes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import confusion_matrix, f1_score
from sklearn.preprocessing import LabelEncoder
import numpy as np
install.packages("bnlearn")
# Q.1
# Read the data from the file
library(bnlearn)
# Step 1: Read Data
data.grades <- read.table("2020_bn_nb_data.txt", header = TRUE)
# Convert variables to factors
data.grades <- lapply(data.grades, as.factor)
data.grades <- data.frame(data.grades)
# Step 2: Learn Dependencies (Bayesian Network Structure)
data.grades.net <- hc(data.grades[, -9], score = 'k2')
print (data.grades.net)
# Q.2
library(bnlearn)
# Step 1: Read Data
data.grades <- read.table("2020_bn_nb_data.txt", header = TRUE)
# Convert variables to factors
data.grades <- lapply(data.grades, as.factor)
# Exclude the QP node from the data
data.grades excluded_qp <- data.grades[, -9]
# Step 2: Learn Dependencies (Bayesian network Structure)
data.grades.net <- hc(data.grades excluded_qp, score = 'k2')
# Plot Bayesian Network Structure
plot(data.grades.net)
# Learn the conditional probability Tables (CPTS) for each node in the Bayesian network
cpt <- bn.fit(data.grades.net, data = data.grades excluded_qp)
# print the CPTS for each course node
print(cpt)
# Q.3
install.packages("e1071")
install.packages("pomegranate")
library(e1071)
# Step 1: Read Data
data.grades <- read.table("2020_bn_nb_data.txt", header = TRUE)
# Convert variables to factors
data.grades <- lapply(data.grades, as.factor)
# Define the variables for prediction
EC100 <- "DD"
IT101 <- "CC"
MA101 <- "CD"
# Train the Naive gayes (lassifier
nb_classifier <- nb_classifier(PH100 ~ EC100 + IT101 + MA101, data = data)
# Create new data frame for prediction
new_data <- data.frame(EC100 = factor(EC100, levels = levels(data$EC100)),
EC100 = factor(IT101, levels = levels(data$IT101)),
MA101 = factor(MA101, levels = levels(data$MA101)))
# predict the grade in
prediction <- predict( nb_classifier, newdata = new_data)
# print the predicted grade
print(prediction)
# Q.4
# Load the required library
library(e1071)
# Step 1: Read Data
data.grades <- read.table("2020_bn_nb_data.txt", header = TRUE)
# Function to split data into training and testing sets
split data function(data, train _ size) {
n nrow(data)
train_indices size = floor(train_size * n))
train_data <- data[train_indices,
test_data data[ -train _ indices,
= train_data, test_data = test_data))
# Function to train and test the naive Bayes classifier
train_and_test_nb test_data <- function(train_data, test_data) {
# Train naive Bayes classifier
nb_classifier <- naiveBayes(QP ~ ., data = train data)
# Predict on test dat
predictions <- predict(nb_classifier, test_data[, -ncol(test_data)])
# calculate accuracy
accuracy <- sum(predictions == test_data$QP) / nrow(test_data)
return (accuracy)
}
# Number of iterations
num iterations <- 20
# Store accuracies
accuracies <- numeric(num_iterations)
# Run experiments
for (i in l:num_iterations){
# Split data into training and testing sets
split <- split_data(data, 0.7)
# Train and test naive Bayes classifier
accuracies[i] train_and_test_nb(split$train_data, split$test_data)
}
# Report results
cat("Mean accuracy:" , mean(accuracies),"\n")
cat("standard deviation of accuracy:", sd(accuracies) ,"\n")
# Q.5
# Load the required library
library(bnlearn)
# Step 1: Read Data
data.grades <- read.table("2020_bn_nb_data.txt", header = TRUE)
# Function to split data into training and testing sets
split_data function(data, train_size) {
n <- nrow(data)
train_indices <- sample(1:n, size = floor(train size * n))
train_data <- data[train_indices, ]
test_data <- data[-train_indices, ]
return(list(train_data = train_data, test_data = test_data))
}
# Function to train and test the Bayesian network classifier
train_and_test_bn <- function(train_data, test_data) {
# Learn the structure of the Bayesian network using Hill-Climbing with BIC score
bn_structure <- hc(train_data, score = "bic")
# Handle level mismatch in PH160:
# Identify valid levels in the data and training set
valid_levels <- intersect(levels(train_data$PH160), levels(test_data$PH160))
# Remove unused levels
train_data$PH160 <- factor(train_data$PH160, levels = valid_levels)
trest_data$PH160 <- factor(train_data$PH160, levels = valid_levels)
# Learn the Conditional Probability Tables (CPTs) for each node
bn_params <- bn.fit(bn_structure, train_data)
# Predict on test data
predictions <- predict(bn_params, node = "QP", method = "bayes -lw", data = test_data)
# Calculate accuracy
accuracy <- sum(predictions == test_data$QP) / nrow(test_data)
return (accuracy)
}
# Number of iterations
num iterations <- 20
# Store accuracies
accuracies <- numeric(num_iterations)
# Run experiments
for (i in l:num_iterations){
# Split data into training and testing sets
split <- split_data(data, 0.7)
# Train and test naive Bayes classifier
accuracies[i] train_and_test_nb(split$train_data, split$test_data)
}
# Report results
cat("Mean accuracy:" , mean(accuracies),"\n")
cat("standard deviation of accuracy:", sd(accuracies) ,"\n")