Quickstart

Welcome to the mlrequest API documentation. The mlrequest API was designed to rapidly integrate machine learning into any application without dependencies or servers.

"Hello, World!" - Classifying Iris Flower Types

The Iris dataset is one of the most well known in the machine learning community. Let's use it to get started with mlrequest.

It's recommended to install the mlrequest Python client which makes it faster and even simpler to use mlrequest in the model training and validation phase. To install, run

$pip install mlrequest

Python 3.5 or greater is required to use the client. Otherwise, the other language tabs will show how to use the API with standard POST requests.

Scikit-Learn Models

Deploying

We will locally train a Random Forest classifier to detect the difference between Iris Flower types based on the input features: sepal length, sepal width, petal length, and petal width.

The model is then deployed to 5 data centers around the world with a single line of Python. Predictions can then be received with a request from any application.

If you have a free or single-region account, please refer to the deploying scikit-learn models guide to specify a region for deployment and predictions.

Python
from mlrequest import SKLearn
from sklearn.ensemble import RandomForestClassifier
import requests
X = requests.get("https://mlrequest.com/data/iris_train_features_sklearn.json").json()['iris-data']
y = requests.get("https://mlrequest.com/data/iris_train_labels_sklearn.json").json()['iris-data']
clf = RandomForestClassifier()
clf.fit(X, y)
sklearn = SKLearn('your-api-key')
sklearn.deploy(clf, 'iris-flower-types')

Predicting

Predictions are now received with a POST request. Prediction requests are latency-routed to the nearest data center to receive the quickest response.

Python Client
Python
Javascript
Java
Go
Ruby
C#
X = requests.get("https://mlrequest.com/data/iris_test_features_sklearn.json").json()['iris-data']
y = requests.get("https://mlrequest.com/data/iris_test_labels_sklearn.json").json()['iris-data']
r = sklearn.predict(X, 'iris-flower-types')
predictions = r.predict_result
#Evaluate prediction accuracy
num_correct=0
for i in range(0, len(y)):
if predictions[i] == y[i]:
num_correct += 1
print(f'Accuracy of predictions: {num_correct/len(predictions)}')
X = requests.get("https://mlrequest.com/data/iris_test_features_sklearn.json").json()['iris-data']
y = requests.get("https://mlrequest.com/data/iris_test_labels_sklearn.json").json()['iris-data']
payload = {
'features': X,
'model_name': 'iris-flower-types'
}
r = requests.post('https://api.mlrequest.com/v1/sklearn/predict', json=payload, headers={'MLREQ-API-KEY':'your-api-key'})
predictions = r.json()['predict_result']
#Evaluate prediction accuracy
num_correct=0
for i in range(0, len(y)):
if predictions[i] == y[i]:
num_correct += 1
print(f'Accuracy of predictions: {num_correct/len(predictions)}')
Coming soon.
Coming soon.
Coming soon.
Coming soon.
Coming soon.

Online Learning Models

Training in the Cloud

Here we will teach the model to detect the type of Iris flower based on 4 input features: sepal length, sepal width, petal length, and petal width.

Python Client
Python
Javascript
Java
Go
Ruby
C#
from mlrequest import Classifier
import requests
iris_train = requests.get("https://mlrequest.com/data/iris_train.json").json()['iris-data']
classifier = Classifier('your-api-key')
classifier.learn(training_data=iris_train, model_name='iris-classifier', class_count=2)
import requests
iris_train = requests.get("https://mlrequest.com/data/iris_train.json").json()['iris-data']
payload = {'model_name': 'iris-flower-types',
'class_count': 2,
'training_data': iris_train}
r = requests.post('https://api.mlrequest.com/v1/classifier/batch/learn', json=payload, headers={'MLREQ-API-KEY':'your-api-key'})
Coming soon.
Coming soon.
Coming soon.
Coming soon.
Coming soon.

Predicting

Here we will use data that the model has not seen before, to test how well it can predict which type of Iris flower it is.

Python Client
Python
Javascript
Java
Go
Ruby
C#
iris_test = requests.get("https://mlrequest.com/data/iris_test.json").json()['iris-data']
response = classifier.predict(features=iris_test, model_name='iris-classifier', class_count=2)
predictions = response.predict_result
iris_test = requests.get("https://mlrequest.com/data/iris_test.json").json()['iris-data']
payload = {'model_name': 'iris-flower-types',
'class_count': 2,
'features': iris_test}
r = requests.post('https://api.mlrequest.com/v1/classifier/batch/predict', json=payload, headers={'MLREQ-API-KEY':'your-api-key'})
predictions = r.json()['predict_result']
print(predictions)
Coming soon.
Coming soon.
Coming soon.
Coming soon.
Coming soon.

Evaluating

We've received predictions from the unseen data, and now we want to know how well the model did. Let's compare how well the model predicted Iris flower types to the actual labels for each example.

Python Client
Python
Javascript
Java
Go
Ruby
C#
iris_test_with_labels = requests.get("https://mlrequest.com/data/iris_test_with_labels.json").json()['iris-data']
num_correct = 0
for i in range(0, len(predictions)):
if predictions[i] == iris_test_with_labels[i]['label']:
num_correct += 1
print(f'Accuracy of predictions: {num_correct/len(predictions)}')
iris_test_with_labels = requests.get("https://mlrequest.com/data/iris_test_with_labels.json").json()['iris-data']
num_correct = 0
for i in range(0, len(predictions)):
if predictions[i] == iris_test_with_labels[i]['label']:
num_correct += 1
print(f'Accuracy of predictions: {num_correct/len(predictions)}')
Coming soon.
Coming soon.
Coming soon.
Coming soon.
Coming soon.

Now we can predict Iris flowers for any app, with a single HTTP request! Not only that, but this model was also duplicated to 5 data centers around the world to provide the lowest latency and most available experience for your application's users.

This was just meant to be a toy example for getting started. However, you can apply this same method with different data to create intelligent applications for websites, mobile apps, ecommerce stores, IoT apps, and more.

Read on in the docs to see more examples and use cases for mlrequest.