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.
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.
from mlrequest import SKLearnfrom sklearn.ensemble import RandomForestClassifierimport 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')
Predictions are now received with a POST
request. Prediction requests are latency-routed to the nearest data center to receive the quickest response.
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 accuracynum_correct=0for 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 accuracynum_correct=0for 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.
​
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.
from mlrequest import Classifierimport requestsiris_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 requestsiris_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.
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.
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.
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.
iris_test_with_labels = requests.get("https://mlrequest.com/data/iris_test_with_labels.json").json()['iris-data']​num_correct = 0for 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 = 0for 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.