BI-PROJECT

133598 Shirlene Ouma

Flower Recommendation Project: Model Creation and Execution

Introduction

This markdown document provides a step-by-step explanation of the R script for creating and executing a model for the flower recommendation project. The script covers the following milestones:

  1. Exploratory Data Analysis
  2. Preprocessing and Data Transformation
  3. Training the Model
  4. Hyper-Parameter Tuning and Ensembles
  5. Consolidation

Exploratory Data Analysis (Milestone 1)

The exploratory data analysis phase involves examining the dataset to understand its characteristics and identify patterns. This includes:

Measures of Frequency

Creating a frequency table to determine the number of observations for each flower type.

```r frequency_table <- table(recommendation$Flower) print(frequency_table)

Measures of Central Tendency

Calculating the mean, median, and mode for each numerical variable to understand the central tendency of the data.

r mean_values <- colMeans(recommendation) print(mean_values) ## Measure Distribution summary(recommendation) ## Measure of Relationship correlation_matrix <- cor(recommendation[, 1:4]) print(correlation_matrix)

Statistical Tests (ANOVA)

Performing an ANOVA (Analysis of Variance) test to determine if there is a significant difference in flower type across different levels of soil nutrients and moisture.

r aov_model <- aov(Flower ~ Nitrogen + Phosphorus + Potassium + Moisture, data = recommendation) summary(aov_model)

Multivariate Plots

Creating multivariate plots, such as scatter plots or heatmaps, to visualize the relationships between variables.

r ggplot(recommendation, aes(x = Nitrogen, y = Phosphorus)) + geom_point() + geom_smooth()

Univariate Plots

Creating univariate plots, such as histograms or density plots, to visualize the distribution of individual variables.

r ggplot(recommendation, aes(x = Moisture)) + geom_histogram()

Preprocessing and Data Transformation (Milestone 2)

The preprocessing and data transformation phase involves preparing the data for model training. This includes:

Confirmation of Missing Values

Checking for missing values in the dataset and identifying how they will be handled.

r missing_values <- colSums(is.na(recommendation)) print(missing_values)

Training the Model (Milestone 3)

The model training phase involves developing a predictive model using the prepared data. This includes:

Data Splitting

Dividing the data into training and testing sets to evaluate the model’s performance on unseen data.

r set.seed(123) train_index <- sample(1:nrow(recommendation), 0.75) train_data <- recommendation[train_index, ] test_data <- recommendation[-train_index, ]

Bootstrapping

Evaluating the model’s stability by performing bootstrapping, which involves resampling the data and retraining the model multiple times.

r boot <- boot(train_data, lm, R=100, i=train_index) print(boot)

Cross Validation

Assessing the model’s generalizability using cross validation, which involves splitting the training data into multiple folds and training the model on different combinations of folds.

Hyper-Parameter Tuning and Ensembles (Milestone 4)

The hyper-parameter tuning phase involves optimizing the model’s hyperparameters to improve its performance. This includes:

Using a search method, such as random search or grid search, to explore different combinations of hyperparameters and select the ones that provide the best performance.

```r set.seed(123) rs <- randomSearch(lm, data = train_data, tuneLength = 20) print(rs)

Consolidation (Milestone 5)

The consolidation phase involves creating an API to deploy the model and making it accessible to users. This includes:

Plumper API to Access the Model

Creating a Plumber API to expose the trained model as an endpoint that users can interact with.

r install.packages(“plumber”) library(plumber)

Create an API endpoint

#’ @title Predict flower type based on soil conditions #’ @param nitrogen Nitrogen level in soil #’ @param phosphorus Phosphorus level in soil #’ @param potassium Potassium level in soil #’ @param moisture Soil moisture level #’ @return Predicted flower type #’ @get /predict predict <- function(nitrogen, phosphorus, potassium, moisture) { # Load the trained model model <- loadModel(“model.rds”)

# Predict flower type prediction <- predict(model, newdata = data.frame(Nitrogen = nitrogen, Phosphorus = phosphorus, Potassium = potassium, Moisture = moisture))

# Return prediction return(prediction$Flower) }

Run the API

#’ @title Flower Prediction API #’ @description This API predicts the most suitable flower type based on soil conditions. #’ @get / #’ @get /docs #’ @get /predict #’ @get /status #’ @post /predict ROBS <- API(predict, port = 8000) ROBS$run(background = TRUE)