-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom Forest.r
More file actions
23 lines (18 loc) · 874 Bytes
/
Random Forest.r
File metadata and controls
23 lines (18 loc) · 874 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#install.packages("randomForest")
# Load necessary libraries
library(randomForest)
# Load the built-in iris dataset
data(iris)
# Split the data into training and testing sets
set.seed(123) # Setting seed for reproducibility
train_indices <- sample(1:nrow(iris), 0.7 * nrow(iris)) # 70% for training
train_data <- iris[train_indices, ]
test_data <- iris[-train_indices, ]
# Create a Random Forest model using the training data
# We'll predict the 'Species' column based on other features
rf_model <- randomForest(Species ~ ., data = train_data)
# Make predictions on the test data using the Random Forest model
predicted_species <- predict(rf_model, newdata = test_data)
# Compare the predicted species with the actual species in the test data
accuracy <- sum(predicted_species == test_data$Species) / nrow(test_data)
cat("Accuracy:", accuracy)