Cluster Analysis with the BigML Dashboard
7.5 Consuming BigML Centroids Programmatically
BigML provides plenty of means for developers to integrate BigML centroids within their apps. In the following sections, we will describe how you can use BigML REST API and BigML Python bindings to work with centroids.
7.5.1 Using Centroids via the BigML API
Centroids have full citizenship in the BigML API. This means you can programmatically create, update, list, and delete them. For example, this is how you can create a single centroid using the command line from a given cluster and defining the input data. This will require properly setting the BIGML_AUTH environment variable to contain your authentication credentials:
curl "https://bigml.io/centroid?$BIGML_AUTH" \
-X POST \
-H 'content-type: application/json' \
-d '{"cluster": "cluster/50650bdf3c19201b64000020",
"input_data": {"000001": 3, "000002":4.5, "000003": "2.2"}}}'
For more information on using centroids through the BigML API, please refer to centroid REST API documentation.
7.5.2 Using Centroids via BigML Bindings
BigML bindings provide a convenient way to access BigML REST API from your language of choice. They offer a higher-level view of BigML Machine Learning resources and algorithms in a number of languages, including Python, Node.js, Java, Swift, and Objective-C. For example, this is how you can create a centroid in Python using BigML bindings:
from bigml.api import BigML
api = BigML()
centroid = api.create_centroid("cluster/573d997058a27e0f620038df",
{"first field": 5,
"second field": 2.5},
{"name": "my centroid"})
BigML bindings also provide the means to calculate the nearest centroid locally, without ever hitting the network, which can greatly improve the latency of predicting from your apps. This is made possible by BigML clusters being white-box, meaning you can download and use them independently from BigML. For example, the following code snippet shows how you can download a cluster and use it for making a local prediction using BigML bindings for Python:
from bigml.cluster import Model
from bigml.api import BigML
api = BigML()
cluster = api.get_cluster("cluster/502fdbff15526876610002615",
query_string="only_model=true;limit=-1")
local_cluster = Cluster(cluster)
centroid = local_cluster.centroid({"first field": 3, "second field": 1})
For more information on using centroids through the BigML API, please refer to BigML bindings documentation.