<jupyter_start><jupyter_text>![title](media/DataRobot.png)  DataRobot provides R and Python package to access different functionalities in the API1 - Project   2 - Model             3 - Retraining    4- PredictingFull documentation of the R package can be found here: https://cran.r-project.org/web/packages/datarobot/index.htmlThe dataset we will be using today, is the well-known "readmissions dataset". You can also find it online but it will also be available when you download this notebook.  Getting startedYou can install datarobot using install.packages command from any computer with internet access!<jupyter_code>install.packages('datarobot')

#require(devtools)
#install_version("datarobot", version = "2.16.0", repos = "http://cran.us.r-project.org")<jupyter_output><empty_output><jupyter_text>Loading the libraries<jupyter_code>library(datarobot)
library(ggplot2)
library(reshape2)
library(MLmetrics)<jupyter_output><empty_output><jupyter_text>CredentialsTo access the DataRobot API user need to connect to it. To make sure authorize users are accessing the DataRobot API user need to use their username, password or API token.You also need to ensure your "API Access" configuration is ON (please ask your administrator if not).To find your API Token, visit YOUR_API_HOST, log in and follow the instructions below: ![title](media/credentials_1.png) ![title](media/credentials_2.png) ![title](media/credentials_3.png)<jupyter_code>ConnectToDataRobot(endpoint = "YOUR_DATAROBOT_HOST", 
                   token = "YOUR_API_KEY")

readmissions = read.csv("data/10k_diabetes_training.csv")
head(readmissions)
project <- StartProject(dataSource = readmissions,
                        projectName = "Readmission",
                        target = "readmitted",
                        workerCount = -1, #-1 = max worker count
                        wait = TRUE)<jupyter_output><empty_output><jupyter_text>Interacting with autopilot<jupyter_code>PauseQueue(project) #Pause project
UnpauseQueue(project) #Unpause project
WaitForAutopilot(project, checkInterval = 20, timeout = NULL, verbosity = 1) #Wait for autopilot to complete
initialJobs <- ListModelJobs(project) #  This gets the currently inprogress and queued jobs<jupyter_output><empty_output><jupyter_text>Pick another project  Where to find the project ID?![title](media/model_id.png)  What if I don't want to use my browser<jupyter_code>#Print id's and names of first 5 projects
ListProjects()$projectId[1:5]
ListProjects()$projectName[1:5]
#Choose another project
another_project <-GetProject("YOUR_PROJECT_ID")<jupyter_output><empty_output><jupyter_text>Take a look at finished models<jupyter_code>#Get names of top 15 models
for(i in 1:15){
    print(ListModels(project)[[i]]$modelType)
}
#Pick best model
best_model <- GetRecommendedModel(project, type = "Recommended for Deployment")

#Print accuracy metrics for the best model
print(best_model$metrics$AUC)
print(best_model$metrics$`Gini Norm`)
#Plot ROC Curve
roc_data <- GetRocCurve(best_model, source = DataPartition$VALIDATION)

ggplot(roc_data$rocPoints, aes(x = falsePositiveRate, y = truePositiveRate)) + 
  geom_point(color = "green") + xlab("False Positive Rate (Fallout)") + ylab("True Positive Rate (Sensitivity)") + 
  theme_dark() + 
  annotate("text", x = .75, y = .25, color = "white", 
           label = paste("AUC =", round(Area_Under_Curve(roc_data$rocPoints$falsePositiveRate, 
                                                         roc_data$rocPoints$truePositiveRate), 4)))<jupyter_output><empty_output><jupyter_text>Plotting Feature Impact<jupyter_code>feature_impact <- GetFeatureImpact(best_model)

ggplot(data = feature_impact, aes(x = reorder(featureName, impactNormalized), y = impactNormalized)) + 
  geom_bar(stat = "identity") + coord_flip() + ylab("Effect") + xlab("") +
  scale_y_continuous(labels = function(x){ paste0(x*100, "%") })<jupyter_output><empty_output><jupyter_text>Train on 100% of Data<jupyter_code>#Unlock holdout
UpdateProject(project, holdoutUnlocked = TRUE)

#Wait a bit for this task to finish running. This command returns the model job id.

#Get blueprint of the best model
blueprint <- GetBlueprint(project, best_model$blueprintId)

jobId <- RequestNewModel(project, blueprint, samplePct = 100)
best_retrained_model <- GetModelFromJobId(project, jobId)<jupyter_output><empty_output><jupyter_text>Predictions Modelling APIYou can use the modelling API if you use Python or R and there are multiple ways you can interact with it. Prediction APIAny project can be called with the Prediction API if you have prediction servers. This is a simple REST API. Click on a model in the UI, then "Deploy Model" and "Activate now". You'll have access to a Python code snippet to help you interact with it. You can also deploy the model through the python API.  Using the Modelling API<jupyter_code>test_df <- read.csv("data/10k_diabetes_test.csv") #Load testing data

# Uploading the testing dataset
scoring <- UploadPredictionDataset(project, dataSource = test_df)

# Requesting prediction
predict_job_id <- RequestPredictions(project, modelId = best_model$modelId, datasetId = scoring$id)

# Grabbing predictions
predictions_prob <- GetPredictions(project, 
                                   predictId = predict_job_id, 
                                   type = "probability")

# Output
head(data.frame(True_Class = test_df$readmitted, Probability = predictions_prob))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09360146252285192, 'fraction_numerical': 0.008592321755027423, 'mean_word_length': 4.908207343412527, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 37, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '265450', 'license': 'Apache-2.0', 'n_tokens_mistral': 1717, 'n_tokens_neox': 1611, 'n_words': 473, 'path': 'Classification/R/predict_hospital_readmissions/src/readmissions_tutorial.ipynb', 'repo_name': 'DalilaB000/tutorials-for-data-scientists', 'size': '5470'}