Skip to content

Instantly share code, notes, and snippets.

@joseph-rickert
Last active March 15, 2016 22:12
Show Gist options
  • Save joseph-rickert/c081945a62fb027f6dc6 to your computer and use it in GitHub Desktop.
Save joseph-rickert/c081945a62fb027f6dc6 to your computer and use it in GitHub Desktop.
Model code for blog post: Scoring R Models with Excel
# GBM Model from AsureML Vignette
# https://cran.r-project.org/web/packages/AzureML/vignettes/getting_started.html
library(AzureML)
library(MASS)
library(gbm)
# My Workspace Credentials
# Note my credentials are stored in a file
ws <- workspace(
config="azureml-settings.json"
)
# Fit the gbm model
set.seed(123)
gbm1 <- gbm(medv ~ .,
distribution = "gaussian",
n.trees = 5000,
interaction.depth = 8,
n.minobsinnode = 1,
shrinkage = 0.01,
cv.folds = 5,
data = Boston,
n.cores = 1) # n.cores = NULL to use all cores
# Select the best iteration for predictions
best.iter <- gbm.perf(gbm1, method = "cv", plot = FALSE)
# Prediction Function
mypredict <- function(newdata) {
require(gbm)
predict(gbm1, newdata, best.iter)
}
# Make Predictions on Test Data
test <- Boston[1:5, 1:13] # Data to test predictions
write.csv(test, "ML_test.csv") # Write to file for later use
mypredict(test)
#> mypredict(test)
#[1] 24.54431 21.15155 33.88859 34.06615 34.93906
# Publish the prediction function as an Azure ML Web service
ep <- publishWebService(ws = ws,
fun = mypredict,
name = "AzureML-vignette-gbm",
inputSchema = test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment