ALTERYX INSPIRE | Join us this May for for a multi-day virtual analytics + data science experience like no other! Register Now

Alteryx Designer Knowledge Base

Definitive answers from Designer experts.

How To: Work With Custom R and Python Models in Alteryx Designer

NeilR
Alteryx Community Team
Alteryx Community Team
Created

Alteryx Designer comes with tools (based on both R and Python) to create and use predictive models without needing to write any code. But what if you've got custom models written in R or Python outside of Designer that you want to use in Designer, or vice versa?

 

For Python, use pickle or joblib to move the model in and out of Python. Here's an example that fits a decision tree model to the iris dataset and dumps the model to disk as a joblib file:

 

from ayx import Package
Package.installPackages(['joblib'])

from ayx import Alteryx
from sklearn import tree
from joblib import dump

train = Alteryx.read("#1")
clf = tree.DecisionTreeClassifier()
y_train = train.pop('Species').values
clf = clf.fit(train, y_train)
dump(clf, 'C:/Users/nryan/Desktop/treeModel.joblib')

 

Use RDS files in R:

 

library(rpart)

train <- read.Alteryx("#1", mode="data.frame")
treeFit <- rpart(Species ~ ., data=train, method = 'class')
saveRDS(treeFit, "C:/Users/nryan/Desktop/treeModel.rds")

 

Bringing the joblib file back into Alteryx is straightforward. Here we bring the decision tree model back into the Python tool in Designer to make predictions on new data:

 

from ayx import Package
Package.installPackages(['joblib'])

from ayx import Alteryx
import pandas as pd
from joblib import load

test = Alteryx.read("#1")
clf = load('C:/Users/nryan/Desktop/treeModel.joblib')
prediction = clf.predict(test)
Alteryx.write(pd.DataFrame({'Py_Prediction':prediction}), 1)

 

And the R tool equivalent:

 

treeModel <- readRDS("C:/Users/nryan/Desktop/treeModel.rds")
test <- read.Alteryx("#1", mode="data.frame")
R_Prediction <- predict(treeModel, newdata=test, type = 'class')
write.Alteryx(as.data.frame(R_Prediction), 1)

 

Fit the modelFit the model

 

Make predictionsMake predictions

See attached example workflows. To run these workflows you will need to edit the file paths to reflect a location on your machine. For help installing the joblib package, please see the article How To: Use Alteryx.installPackages() in Python tool

Attachments
Comments
billb007
5 - Atom

One of your examples here is just what I'm trying to do but I get an error when trying to load the package 'rpart' in the R tool.   I can see it's in the library but get these error messages:

 

R (59) Error: package or namespace load failed for 'AlteryxPredictive' in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]):

 

R (59) there is no package called 'rpart'

 

The R command that causes this is: library(rpart)

 

Can you help?  My company wants me to get this predictor on-line and this is the last step I need to do it!

 

Bill Bentley

ADP

 

NeilR
Alteryx Community Team
Alteryx Community Team

@billb007 Can you share your workflow?

billb007
5 - Atom
I'd love to send the workflow to you but our company has extreme security and I have to send it via their secure file process. That process will not recognize the email address you have as valid do you have a simpler, more standard email I can send this to?

Bill Bentley

----------------------------------------------------------------------
This message and any attachments are intended only for the use of the addressee and may contain information that is privileged and confidential. If the reader of the message is not the intended recipient or an authorized representative of the intended recipient, you are hereby notified that any dissemination of this communication is strictly prohibited. If you have received this communication in error, notify the sender immediately by return email and delete the message and any attachments from your system.
NeilR
Alteryx Community Team
Alteryx Community Team

@billb007 I'll private message you.