forked from elchulito88/MLOps-Best-Practices
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pushed from Domino: https://ykb.domino-eval.com/workspace/petter/mlop…
- Loading branch information
Petter Olsson
committed
Nov 14, 2024
1 parent
20b6da1
commit 985086a
Showing
9 changed files
with
114 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"R2":[0.108],"MSE":[0.15]} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,86 @@ | ||
library(mlflow) | ||
library(jsonlite) | ||
|
||
print("Reading in data") | ||
project_name <- Sys.getenv('DOMINO_PROJECT_NAME') | ||
path <- paste('/mnt/data/',project_name,'/WineQualityData.csv') | ||
path <- paste('/mnt/data/', project_name, '/credit_card_default.csv') | ||
path <- gsub(" ", "", path, fixed = TRUE) | ||
data <- read.csv(file=path) | ||
data <- read.csv(file = path) | ||
head(data) | ||
|
||
#mlflow_set_experiment(experiment_name=paste(Sys.getenv('DOMINO_PROJECT_NAME'), Sys.getenv('DOMINO_STARTING_USERNAME'))) | ||
mlflow_set_experiment(experiment_name = paste0(Sys.getenv('DOMINO_PROJECT_NAME'), " ", Sys.getenv('DOMINO_STARTING_USERNAME'), " ", Sys.getenv('MLFLOW_NAME'))) | ||
|
||
data$is_red <- as.integer(data$type != 'white') | ||
|
||
data <-na.omit(data) | ||
dim(data)[1]-sum(complete.cases(data)) | ||
# Rename the target column to "DEFAULT" | ||
if ("default_payment_next_month" %in% colnames(data)) { | ||
colnames(data)[colnames(data) == "default_payment_next_month"] <- "DEFAULT" | ||
} else { | ||
stop("Column 'default_payment_next_month' not found in the data.") | ||
} | ||
|
||
train <-data[sample(nrow(data), round(dim(data)[1]*0.75)),] | ||
# test <- data[(round(dim(data)[1]*0.75)+1):dim(data)[1], 2:dim(data)[2]] | ||
test <- data[(data$id %in% train$id)==FALSE,] | ||
train <- subset(train, select = -c(id) ) | ||
test <- subset(test, select = -c(id) ) | ||
# Verify the renaming | ||
print("Columns in data after renaming:") | ||
print(colnames(data)) | ||
|
||
train_matrix <- as.matrix(train) | ||
test_matrix <- as.matrix(test) | ||
label_matrix <- as.matrix(train$quality) | ||
test_lab_matrix <- as.matrix(test$quality) | ||
|
||
dim(train)+dim(test) | ||
|
||
with(mlflow_start_run(), { | ||
mlflow_set_tag("Model_Type", "R") | ||
print("Training Model") | ||
|
||
lm_model <- lm(formula = quality ~., data = train) | ||
lm_model | ||
|
||
|
||
RSQUARE = function(y_actual,y_predict){ | ||
cor(y_actual,y_predict)^2 | ||
} | ||
# Define MLflow experiment | ||
mlflow_set_experiment(experiment_name = paste0(Sys.getenv('DOMINO_PROJECT_NAME'), " ", Sys.getenv('DOMINO_STARTING_USERNAME'), " ", Sys.getenv('MLFLOW_NAME'))) | ||
|
||
preds_lm <- predict(lm_model, newdata = test) | ||
# Remove missing values | ||
data <- na.omit(data) | ||
print(paste("Number of rows with missing values removed:", dim(data)[1] - sum(complete.cases(data)))) | ||
|
||
rsquared_lm <-round(RSQUARE(preds_lm, test$quality),3) | ||
print(rsquared_lm[1]) | ||
# Split data into training and testing sets | ||
set.seed(123) # Set seed for reproducibility | ||
train <- data[sample(nrow(data), round(dim(data)[1] * 0.75)), ] | ||
test <- data[!(rownames(data) %in% rownames(train)), ] | ||
|
||
#mse | ||
mse_lm<- round(mean((test_lab_matrix - preds_lm)^2),3) | ||
print(mse_lm) | ||
# Verify that the train and test sets include the "DEFAULT" column | ||
if (!("DEFAULT" %in% colnames(train))) { | ||
stop("Column 'DEFAULT' is not present in the training set.") | ||
} | ||
|
||
mlflow_log_metric("R2", rsquared_lm[1]) | ||
mlflow_log_metric("MSE", mse_lm) | ||
# Define target and feature columns | ||
target_variable <- "DEFAULT" | ||
features <- setdiff(names(data), target_variable) | ||
|
||
diagnostics = list("R2" = rsquared_lm[1], | ||
"MSE"=mse_lm) | ||
library(jsonlite) | ||
fileConn<-file("dominostats.json") | ||
writeLines(toJSON(diagnostics), fileConn) | ||
close(fileConn) | ||
train_matrix <- as.matrix(train[, features]) | ||
test_matrix <- as.matrix(test[, features]) | ||
label_matrix <- as.matrix(train[[target_variable]]) | ||
test_lab_matrix <- as.matrix(test[[target_variable]]) | ||
|
||
save(lm_model, file="/mnt/code/models/R_linear_model.Rda") | ||
}) | ||
dim(train) + dim(test) | ||
|
||
# install.packages("SHAPforxgboost") | ||
# install.packages("SHAPforxgboost") | ||
# library("SHAPforxgboost") | ||
# shap_values <- shap.values(xgb_model = mod, X_train = dataX) | ||
# Start MLflow run | ||
with(mlflow_start_run(), { | ||
mlflow_set_tag("Model_Type", "R") | ||
print("Training Model") | ||
|
||
# Train the model (update formula for new dataset) | ||
lm_model <- lm(formula = as.formula(paste(target_variable, "~ .")), data = train) | ||
print(lm_model) | ||
|
||
# Define RSQUARE function | ||
RSQUARE <- function(y_actual, y_predict) { | ||
cor(y_actual, y_predict)^2 | ||
} | ||
|
||
# Predict and calculate metrics | ||
preds_lm <- predict(lm_model, newdata = test) | ||
|
||
rsquared_lm <- round(RSQUARE(test[[target_variable]], preds_lm), 3) | ||
print(rsquared_lm) | ||
|
||
# Mean Squared Error | ||
mse_lm <- round(mean((test_lab_matrix - preds_lm)^2), 3) | ||
print(mse_lm) | ||
|
||
# Log metrics to MLflow | ||
mlflow_log_metric("R2", rsquared_lm) | ||
mlflow_log_metric("MSE", mse_lm) | ||
|
||
# Save diagnostics to JSON | ||
diagnostics <- list("R2" = rsquared_lm, "MSE" = mse_lm) | ||
fileConn <- file("dominostats.json") | ||
writeLines(toJSON(diagnostics), fileConn) | ||
close(fileConn) | ||
|
||
# Save model | ||
save(lm_model, file = "/mnt/code/models/R_linear_model.Rda") | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters