In this post I will share my experiences with using a spot AMI instance for heavy parallel processing of R scripts.
Fist we start a Rstudio AMI:
Start a spot instance:
Request:
Search AMI:
Search for Rstudio:
select the needed compute power:
Its recommended to look at the pricing history:
In this case, we select a General Purpuse 16 CPU machine, which has a fair price per hour. Since we only use the machine for some heavy processing, we will decommission it in a few hours.
Leave the other settings as is.
select next, be sure to create a new key pair, in case you do not have the original key pair.
Its also good to add a new security group which has port 80 open:
open the ports:
The click 'Launch' instance, and then click op the capacity link;
Look up the public IP:
You should now be able to log on:
Log on credentials can be found here: http://www.louisaslett.com/RStudio_AMI/
Now that the server is up and running we can start using R packages to allow parallel processing:
In order to initialize we; - set the number of cores. - initialize a cluster -and we need to export our functions, variables and datasets to the clusters:
#prep parralel processing # Calculate the number of cores no_cores <- detectCores() - 1 # Initiate cluster cl <- makeCluster(no_cores) registerDoMC(no_cores) clusterExport(cl=cl, varlist=c("splitter", "create_ngram_table"))
There are a number of options for parralel processing roughly speaking you can choose from ParLapply and forEach. When you would like to split your dataset in multiple subsets and want to have a worker node perform an operation this can easily be done with a forEach, see example below. In both cases a list is returned. While we usualy would like to have a full dataset in return. In order to achieve this result I used the code do.call("rbind", result).
Many more examples can be found on: http://gforge.se/2015/02/how-to-go-parallel-in-r-basics-tips/
In this post I will show how to run an R script from Power BI which will execute an Association rule learning script to perform market basket analysis.
In this example we will not look at products sold, but products sharing shelf space.
The dataset
Our basic dataset looks like this.
Our products:
The distribution / presence of products on the shelf of a customer:
The Power BI building blocks
The data model
As for the DAX part we will start with this post of Marco Russo and Alberto Ferrari.
So the data model in Power BI looks like this:
The R visualization
We will look at the DAX part later on. First we add an R component with a script that will return the AR rules it found.
The table contains the basic output that is to be expected from AR. We will try to build these measures in DAX later on.
Unfortunately Power BI initializes a new R sessions each time the R visualization is run / cross filtered. Therefore I tried to use a much base R as possible. As for the libraries that need to be loaded. I put these in a separate folder on my local drive and specified the folder name in the library command.
Building it in DAX
Support
The output of the arules R script can be built in DAX whenever it concerns single item combinations, so X -> Y. So not A, B -> Y. The 'support' measure is basically the '[Orders with Both Products %]' described by Russo and Ferrari. Just to show how its implemented on our dataset.
Customers with Both Products % = IF ( NOT ( [SameProductSelection] ); DIVIDE ( [Customers with Both Products]; [Unique Customers All] ) )
plaatje = readPNG("c:/TFS/test.png") x <- data.frame(1:100,1,1,1,1,1) colnames(x) = c("t", "x","y", "r", "g", "b") for (a in 1:3) { teller = 1 for (i in 1:length(plaatje[,1,a])) { #Hoogte for (j in 1:length(plaatje[i,,a])) { #Breedte x[teller,c(1:3,3+a)] = c(teller,j,i,plaatje[i,j,a]) teller = teller + 1 } } }