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.
Updated dataset path in the /scripts dir as it is now static
Pushed from Domino: https://ykb.domino-eval.com/workspace/petter/mlops-best-practices?executionId=6734940e0b77b232dfd8c697
- Loading branch information
Petter Olsson
committed
Nov 17, 2024
1 parent
b704e2f
commit 5767504
Showing
6 changed files
with
137 additions
and
125 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 |
---|---|---|
@@ -1,67 +1,79 @@ | ||
library(mlflow) | ||
library(jsonlite) | ||
|
||
print("Reading in data") | ||
project_name <- Sys.getenv('DOMINO_PROJECT_NAME') | ||
path <- paste('/mnt/data/',project_name,'/credit_card_default.csv') | ||
path <- paste('/mnt/data/mlops-best-practices/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)) | ||
|
||
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(DEFAULT) ) | ||
test <- subset(test, select = -c(DEFAULT) ) | ||
|
||
train_matrix <- as.matrix(train) | ||
test_matrix <- as.matrix(test) | ||
label_matrix <- as.matrix(train$DEFAULT) | ||
test_lab_matrix <- as.matrix(test$DEFAULT) | ||
|
||
dim(train)+dim(test) | ||
# Verify the renaming | ||
print("Columns in data after renaming:") | ||
print(colnames(data)) | ||
|
||
with(mlflow_start_run(), { | ||
mlflow_set_tag("Model_Type", "R") | ||
print("Training Model") | ||
|
||
lm_model <- lm(formula = DEFAULT ~., 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$DEFAULT),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
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