-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwithoutPreprocessing.R
More file actions
32 lines (25 loc) · 1.16 KB
/
Copy pathwithoutPreprocessing.R
File metadata and controls
32 lines (25 loc) · 1.16 KB
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
library(e1071)
library(caret)
library(caTools)
# Read the dataset
ds <- read.csv("F:\\Year 2 Sem 1\\R programming\\Datasets\\student_portuguese_clean.csv")
ds
# Perform data splitting
split_ratio <- sample.split(ds, SplitRatio = 0.75)
training_dataset <- subset(ds, split_ratio == TRUE)
testing_dataset <- subset(ds, split_ratio == FALSE)
# Ensure 'final_grade' is a factor in both datasets with the same levels
training_dataset$final_grade <- as.factor(training_dataset$final_grade)
testing_dataset$final_grade <- as.factor(testing_dataset$final_grade)
# Create training and testing data frames with the selected columns
training_ds <- training_dataset[, 1:33]
testing_ds <- testing_dataset[, 1:33]
# Train the Naive Bayes model
set.seed(400)
model <- naiveBayes(final_grade ~ ., data = training_dataset)
# Make predictions on the testing dataset
predicted_results <- predict(model, newdata = testing_ds)
# Ensure predicted results are factors with the same levels as testing_dataset$final_grade
predicted_results <- factor(predicted_results, levels = levels(testing_dataset$final_grade))
# Create confusion matrix
confusionMatrix(predicted_results, testing_dataset$final_grade)