Deploy R Code - 5Analytics Enterprise AI Platform

A Recommendation Engine in R

The most easiest way to create a Recommendation Engine is to use R's recommenderlab package.

The first step to define a recommendation engine with the 5Analytics Enterprise AI Platform is to define the data access:

	<table key="sales" ds="sales_demo" cache="false">
		select * from sales_table

This creates a 5Analytics Table Object named sales in the 5Analytics environment. You can access it in your R code like a regular data.frame. The R code for the recommendation engine web service (including data access) no becomes a simple script of a couple of lines of code. Lets call the file for this script rc.R

# load necessary libraries

empty_customer <- 0

# create customer product matrix
product_item <- acast(sales[,c("customerkey","productkey","internetsaleskey")], customerkey ~ productkey,length,value.var="internetsaleskey")

# create an empty customer for later use
empty_customer <-product_item[1,]*0

# create binary customer product matrix
product_item <- (as.matrix(product_item)==1)*1

# create sparse representation of matrix
sp_product_item <- as(product_item, "binaryRatingMatrix")

# create user based recommender engine
rec <- Recommender(sp_product_item[1:nrow(sp_product_item)],method="UBCF")

# the recommendation
fafun_predict_product <- function(customer_key) {
	# load customer
	cust <- sales[sales$customerkey==customer_key,c("customerkey","productkey","internetsaleskey")]

	# create a new customer with all products being 0 except for those that a customer
	# bought, this step is necessary since a customer usually only buys a small fraction
	# of the products in a web shop, but for the reco engine we need vector with all products
	products <- empty_customer
	products[names(empty_customer)%in% as.character(cust$customerkey)] <- 1
	sp_products <- as(rbind(products), "binaryRatingMatrix")
	# perform the prediction
	recom <- predict(rec, sp_products,n=3)

To deploy the recommendation engine, you just have to upload it to the 5Analytics AI Platform.

# upload file to server via webdav
> curl -u usr:pswd --digest -T rc.R 'http://localhost:5050/up/dav/'

Once you have uploaded the file. The 5Analytics Enterprise AI Platform will load the code and establish the web service end point shown above. Now your Web-Service is ready to be queried.

> curl "http://localhost:5050/if/json/R/v1/fafun_predict_product?_token=test_token&customer_key=30"
  "null": [{
    "products": ["50"    ,"37"    ,"1"]
get_appWould you like to try the demo with your own data or with your own code?
Register for our Community Edition!
get_appGet our Community Edition
or visit our Homepage!