knitr::opts_chunk$set(echo = TRUE)
# Import the PAC learning algorithm package
library(PAC)
Vector containing a set of apples
# Defines a vector named 'fruits' for simplification all fruits are apples.
fruits <- rep("Apple", 20)
#Create a vector names 'colors' by repeating a sequence of colors for each apple in the vector 'fruits'
colors <- sample(c("Red", "Yellow", "Green", "Purple"), length(fruits), replace = TRUE)
sizes <- sample(c("Small", "Medium", "Large"), length(fruits), replace = TRUE)
sweetness <- sample(c("Sweet", "Not sweet"), length(fruits), replace = TRUE)
fruits_df <- data.frame(Fruit = fruits, Color = colors, Size = sizes, Sweetness = sweetness)
fruits_df
## Fruit Color Size Sweetness
## 1 Apple Yellow Large Sweet
## 2 Apple Purple Small Not sweet
## 3 Apple Green Medium Not sweet
## 4 Apple Red Small Sweet
## 5 Apple Yellow Small Sweet
## 6 Apple Green Small Not sweet
## 7 Apple Red Large Sweet
## 8 Apple Yellow Large Not sweet
## 9 Apple Green Large Not sweet
## 10 Apple Green Large Sweet
## 11 Apple Yellow Small Not sweet
## 12 Apple Green Large Sweet
## 13 Apple Green Small Sweet
## 14 Apple Green Large Not sweet
## 15 Apple Purple Medium Sweet
## 16 Apple Red Medium Not sweet
## 17 Apple Red Medium Not sweet
## 18 Apple Red Medium Sweet
## 19 Apple Green Large Sweet
## 20 Apple Red Large Not sweet
concept_class
that takes a fruit as input and returns its classification (“Ripe” or “Not Ripe”) based on the specified fruit names.concept_class <- function(fruit) {
if (color %in% c("Red", "Purple")) {
return("Ripe")
} else {
return("Not Ripe")
}
}
#Define a function named hypothesis_class
that takes color, size, and sweetness as inputs and returns the classification (“Ripe” or “Not Ripe”) based on specific conditions defined within the function.
# Define the hypothesis class
hypothesis_class <- function(color, size, sweetness) {
if (color %in% c("Red", "Purple") || (size == "Large" && sweetness == "Sweet")) {
return("Ripe")
} else {
return("Not Ripe")
}
}
# Classify each fruit in the data frame
# A loop that iterates over each row of the `fruits_df` data frame and extracts the values for each attribute (fruit, color, #size, sweetness) from the current row of the data frame.
for (i in 1:nrow(fruits_df)) {
fruit <- fruits_df[i, "Fruit"]
color <- fruits_df[i, "Color"]
size <- fruits_df[i, "Size"]
sweetness <- fruits_df[i, "Sweetness"]
# Classify using the hypothesis class
#calls the `hypothesis_class` function with the extracted attribute values and assigns the resulting classification to the
# 'result' variable.
result <- hypothesis_class(color, size, sweetness)
# Print the instance and the classification
cat("Fruit:", fruit, "Classification:", result, "\n")
}
## Fruit: Apple Classification: Ripe
## Fruit: Apple Classification: Ripe
## Fruit: Apple Classification: Not Ripe
## Fruit: Apple Classification: Ripe
## Fruit: Apple Classification: Not Ripe
## Fruit: Apple Classification: Not Ripe
## Fruit: Apple Classification: Ripe
## Fruit: Apple Classification: Not Ripe
## Fruit: Apple Classification: Not Ripe
## Fruit: Apple Classification: Ripe
## Fruit: Apple Classification: Not Ripe
## Fruit: Apple Classification: Ripe
## Fruit: Apple Classification: Not Ripe
## Fruit: Apple Classification: Not Ripe
## Fruit: Apple Classification: Ripe
## Fruit: Apple Classification: Ripe
## Fruit: Apple Classification: Ripe
## Fruit: Apple Classification: Ripe
## Fruit: Apple Classification: Ripe
## Fruit: Apple Classification: Ripe
# Define a loss function that calculates the loss incurred by the hypothesis class for a given fruit based on its classification
loss_function <- function(true_class, predicted_class) {
# Define the loss values
loss_values <- ifelse(true_class != predicted_class, 1, 0) # 0-1 loss function
# Calculate the total loss
total_loss <- sum(loss_values)
return(total_loss)
}
# Calculate the loss for each fruit in the data frame
# Iterate over each row of the `fruits_df` data frame to calculate the loss for each fruit classification
for (i in 1:nrow(fruits_df)) {
fruit <- fruits_df[i, "Fruit"]
color <- fruits_df[i, "Color"]
size <- fruits_df[i, "Size"]
sweetness <- fruits_df[i, "Sweetness"]
# True classification based on the concept class
true_class <- concept_class(color)
# Predicted classification based on the hypothesis class
predicted_class <- hypothesis_class(color, size, sweetness)
# Calculate loss using the loss function
fruit_loss <- loss_function(true_class, predicted_class)
# Print the instance, true class, predicted class, and loss
cat("Fruit: ", fruit, " True Class: ", true_class, " Predicted Class: ", predicted_class, " Loss: ", fruit_loss, "\n")
}
## Fruit: Apple True Class: Not Ripe Predicted Class: Ripe Loss: 1
## Fruit: Apple True Class: Ripe Predicted Class: Ripe Loss: 0
## Fruit: Apple True Class: Not Ripe Predicted Class: Not Ripe Loss: 0
## Fruit: Apple True Class: Ripe Predicted Class: Ripe Loss: 0
## Fruit: Apple True Class: Not Ripe Predicted Class: Not Ripe Loss: 0
## Fruit: Apple True Class: Not Ripe Predicted Class: Not Ripe Loss: 0
## Fruit: Apple True Class: Ripe Predicted Class: Ripe Loss: 0
## Fruit: Apple True Class: Not Ripe Predicted Class: Not Ripe Loss: 0
## Fruit: Apple True Class: Not Ripe Predicted Class: Not Ripe Loss: 0
## Fruit: Apple True Class: Not Ripe Predicted Class: Ripe Loss: 1
## Fruit: Apple True Class: Not Ripe Predicted Class: Not Ripe Loss: 0
## Fruit: Apple True Class: Not Ripe Predicted Class: Ripe Loss: 1
## Fruit: Apple True Class: Not Ripe Predicted Class: Not Ripe Loss: 0
## Fruit: Apple True Class: Not Ripe Predicted Class: Not Ripe Loss: 0
## Fruit: Apple True Class: Ripe Predicted Class: Ripe Loss: 0
## Fruit: Apple True Class: Ripe Predicted Class: Ripe Loss: 0
## Fruit: Apple True Class: Ripe Predicted Class: Ripe Loss: 0
## Fruit: Apple True Class: Ripe Predicted Class: Ripe Loss: 0
## Fruit: Apple True Class: Not Ripe Predicted Class: Ripe Loss: 1
## Fruit: Apple True Class: Ripe Predicted Class: Ripe Loss: 0