The code begins by loading the Haberman’s Survival dataset, which contains information about breast cancer patients’ survival rates. It then preprocesses the data by normalizing the numerical features (age, year of operation, and number of positive lymph nodes) to a 0-1 scale, which helps in training the neural network. The survival status is converted to a categorical variable. Next, the data is split into training (70%) and testing (30%) sets to allow for model evaluation. An Artificial Neural Network (ANN) is then constructed with one hidden layer containing three neurons. This ANN is trained on the training data to learn the relationship between the input features and patient survival. After training, the model makes predictions on the test set. The code includes a prediction function that converts the network’s output to binary classifications. Finally, the model’s performance is evaluated by calculating its accuracy and plotting a visual representation of the neural network’s structure.
The Haberman’s Survival dataset contains information about patients who underwent surgery for breast cancer between 1958 and 1970 at the University of Chicago’s Billings Hospital. It includes 306 instances with 4 attributes:
Age of the patient at the time of operation Year of operation (1900-1960) Number of positive axillary nodes detected Survival status (1 = survived 5+ years, 2 = died within 5 years)
The head(haberman) function is called twice, which allows a quick peek at the first few rows of the dataset before and after converting the survival column to a factor. This is a very basic form of data exploration.
library(neuralnet)
library(caret)
## Loading required package: ggplot2
## Loading required package: lattice
#Load the dataset
url <- "https://archive.ics.uci.edu/ml/machine-learning-databases/haberman/haberman.data"
haberman <- read.csv(url, header = FALSE, col.names = c("age", "year", "nodes", "survival"))
head(haberman)
#convert survival to 1 = survived 5+ years and 2 = died within 5 years
haberman$survival <- as.factor(haberman$survival)
head(haberman)
The survival status is converted from numeric to categorical (factor) data type. A min-max normalization is applied to the numerical features (age, year, and nodes) to scale them between 0 and 1. This helps in bringing all features to a similar scale. The dataset is split into training (70%) and testing (30%) sets.
# Normalizing
normalize <- function(x) {
return((x - min(x)) / (max(x) - min(x)))
}
haberman_norm <- as.data.frame(lapply(haberman[, 1:3], normalize))
haberman_norm$survival <- haberman$survival
# Split the data
set.seed(123)
train_indices <- sample(1:nrow(haberman_norm), 0.7 * nrow(haberman_norm))
train_data <- haberman_norm[train_indices, ]
test_data <- haberman_norm[-train_indices, ]
A simple Artificial Neural Network (ANN) with one hidden layer containing 3 neurons is used. The input layer has 3 nodes (one for each feature), and the output layer has 1 node for binary classification.
The code doesn’t explicitly specify a loss function, but neuralnet typically uses Sum of Squared Errors (SSE) by default for regression tasks. For classification tasks like this, it internally adapts to use a form of cross-entropy loss.
Hidden layer structure: 1 layer with 3 neurons Maximum steps for convergence (stepmax): 1,000,000 Linear output: FALSE (appropriate for classification)
# creating formula
nn_formula <- survival ~ age + year + nodes
# training
nn_model <- neuralnet(
formula = nn_formula,
data = train_data,
hidden = c(3), # Single hidden layer with 3 neurons
linear.output = FALSE,
stepmax = 1e6 # Increase maximum steps for convergence
)
# Makeing predictions test set
predict_neuralnet <- function(model, newdata) {
# Compute the output
output <- compute(model, newdata)$net.result
# Convert to class labels
predicted <- ifelse(output > 0.5, "2", "1")
return(factor(predicted, levels = levels(haberman$survival)))
}
predicted_classes <- predict_neuralnet(nn_model, test_data[, c("age", "year", "nodes")])
The model’s performance is evaluated using accuracy on the test set. The accuracy is calculated as the percentage of correct predictions out of all predictions made.
# accuracy
accuracy <- mean(predicted_classes == test_data$survival)
# print accuracy
cat("Accuracy of the model is:", round(accuracy * 100, 2), "%\n")
## Accuracy of the model is: 50 %