Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Create a WhizzML Script (Part 4)

DZone's Guide to

How to Create a WhizzML Script (Part 4)

Dive deeper into the world of machine learning automation via WhizzML and learn the BigML Python bindings way to create WhizzML scripts.

· AI Zone
Free Resource

Bring the power of Artificial Intelligence to IT Operations. Brought to you in partnership with BMC.

As the final installment of our WhizzML blog series (here's Part 1, Part 2, and Part 3), it's time to learn the BigML Python bindings way to create WhizzML scripts. With this last tool at your disposal, you'll officially be a WhizzML-script-creator genius!

About BigML Python Bindings

BigML Python bindings allow you to interact with BigML.io, the API for BigML. You can use it to easily create, retrieve, list, update, and delete BigML resources (i.e. sources, datasets, models, and predictions). This tool becomes even more powerful when it is used with WhizzML. Your WhizzML code can be stored and executed in BigML by using three kinds of resources: Scripts, Libraries, and Executions (see the first post).

WhizzML scripts can be executed in BigML's servers in a controlled, fully scalable environment that automatically takes care of their parallelization and fail-safe operation. Each execution uses an Execution resource to store the arguments and the results of the process. WhizzML Libraries store generic code to be shared or reused in other WhizzML scripts. But this is not really new, so let's get into some new tricks instead.

Bindings

You can create and execute your WhizzML script via the bindings to create powerful workflows. There are multiple languages supported by BigML bindings i.e. Python, C#, Java or PHP. In this post, we'll use the Python bindings but you can find the others on GitHub. If you need a BigML Python bindings refresher, please refer to the dedicated Documentation.

Scripts

In BigML, a script resource stores WhizzML source code and the results of its compilation. Once a WhizzML script is created, it's automatically compiled. If compilation is successful, the script can be run, that is, used as the input for a WhizzML execution resource. This little program creates a script to create a model from a dataset-id.

from bigml.api import BigML
api = BigML() 

# define the code include in the script
script_code = "(create-model {\"dataset\" dataset-id})"

# create the script
script = api.create_script(script_code)

# waiting for the script compilation to finish
api.ok(script)

You can also define some variables as the script inputs. In the code below, we use the same script code as in the example above, and we set a specific dataset id as input.

from bigml.api import BigML
api = BigML()

# define the code include in the script
script_code = "(create-model {\"dataset\" dataset-id})" 

# define parameters
inputs = {"inputs": [{"name": "dataset-id",
                      "type": "string",
                      "default": "dataset/598ceafe4006830450000046",
                      "description": "Datset identifier"}]}

# create the script
script = api.create_script(script_code, inputs)

# waiting for the script compilation to finish
api.ok(script)

Executions

To execute a compiled WhizzML script in BigML, you need to create an execution resource. Each execution is run under its associated user credentials and its particular environment constraints. Furthermore, a script can be shared, and you can execute the same script several times under different usernames by creating a different execution. For this, you need to first identify the script you want to execute and determine your dictionary of inputs. In this execution, the parameters are the script inputs stored in the variable named input. It's a piece of cake!

from bigml.api import BigML
api = BigML() 

# choose workflow
script = 'script/591ee3d00144043f5c003061' 

# define parameters
inputs = {'x' : '2'} 

# execute and waite for the compilation to finish
api.ok(api.create_execution(script, inputs))

Libraries

We haven't created libraries in the previous posts, but there's always a first time. A library is a shared collection of WhizzML functions and definitions usable by any script that imports them. It's a special kind of compiled WhizzML source code that only defines functions and constants. Libraries can be imported in scripts. The attribute of a script can contain a list of library IDs whose defined functions and constants will be usable in the script. It's very easy to create WhizzML libraries by using BigML Python bindings.

Let's go through a simple example. We'll define one function get-max  and a constant. The function will get the max value in a list. In WhizzML language this can be expressed as below.

from bigml.api import BigML
api = BigML()

# define the library code
lib_code = "(define (get-max xs) (reduce (lambda (x y)" \
"(if (> x y) x y)) (head xs) xs))"

# create a library
library = api.create_library(lib_code)

# waiting for the library compilation to finish
api.ok(library)

For more examples, don't hesitate to take a look at the WhizzML resources section in the BigML Python Bindings documentation.

So that's it! You now know all the techniques needed to create and execute WhizzML scripts. We've covered the basic concepts of WhizzML, how to clone existing scripts or import them from Github (Part 1), how to create new scripts by using Scriptify and the editor (Part 2), how to use the BigMLer command line (Part 3), and finally the BigML Python bindings in this last post.

To go deeper into the world of machine learning automation via WhizzML, please check out our WhizzML tutorials such as the automated dataset transformation tutorial. If you need a WhizzML refresher, you can always visit the WhizzML documentation. Start with the Hello World section in the Primer Guide for a gentle introduction. And yes, please be on the lookout for more WhizzML related posts on our blog.

TrueSight is an AIOps platform, powered by machine learning and analytics, that elevates IT operations to address multi-cloud complexity and the speed of digital transformation.

Topics:
ai ,machine learning ,whizzml ,python ,tutorial

Published at DZone with permission of Barbara Martin, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}