mlrequest-python is a Python client for the mlrequest machine learning API. The client allows you to do a few significant things with minimal code.
- Deploy latency-routed scikit-learn and online machine learning models to 5 data centers around the world, providing < 60ms global response time and automatic failover.
- Get thousands of model predictions per second
- Train online models with thousands of training examples per second
You will need an API key to get started with mlrequest-python. You can obtain one for free that provides 5,000 monthly model transactions at https://mlrequest.com/signup.html. The free plan is limited to the deployment of a single online learning model or scikit-learn model file less than 1 MB in size. Scikit-learn model transactions are prioritized for paid accounts, and will generally receive up to 50ms faster response time than free accounts. Additionally, free accounts are restricted to a single data center (of your choosing) and will not benefit from latency routing.
For more transactions, larger scikit-learn model files (up to 100 MB), more models, and faster scikit-learn response times, see our paid plans at https://mlrequest.com/pricing.html. Check out our documentation for more information.
pip install mlrequest
fromsklearn.ensembleimportRandomForestClassifierfrommlrequestimportSKLearnclf=RandomForestClassifier()
clf.fit(X, y)
sklearn=SKLearn('your-api-key')
sklearn.deploy(clf, 'rf-model-name')
# Make predictionsfeatures= [[1, 2, 3]]
pred=sklearn.predict(features, 'rf-model-name')If you have a free or single region account, you will only be permitted to deploy to one data center (region) at a time. Your model will automatically failover to another region when loss of service is experienced in your deployed region. Below is an example of how to specify the region to deploy to.
fromsklearn.ensembleimportRandomForestClassifierfrommlrequestimportSKLearnfrommlrequestimportregionsclf=RandomForestClassifier()
clf.fit(X, y)
sklearn=SKLearn('your-api-key')
sklearn.deploy(clf, 'rf-model-name', regions.US_EAST)
# Make predictionsfeatures= [[1, 2, 3]]
pred=sklearn.predict(features, 'rf-model-name', regions.US_EAST)Use any of the following regions.
regions.US_WEST(N. California)regions.US_EAST(Ohio)regions.EU_CENTRAL(Frankfurt)regions.AP_SOUTH(Mumbai)regions.AP_NORTHEAST(Seoul)
Models are created automatically by calling one of the model endpoints below.
Currently classification is limited to logistic regression. Email support@mlrequest.com to request other online classifier models.
frommlrequestimportClassifierclassifier=Classifier('your-api-key')
# Learn singletraining_data= {'features': {'feature1': 23.1, 'feature2': 'some-value'}, 'label': 1}
# Learn batchtraining_data= [{'features': {'feature1': 23.1, 'feature2': 'some-value'}, 'label': 1}, ...]
r=classifier.learn(training_data=training_data, model_name='clf-model-name', class_count=2)
r.content# A single response or list of responses# Predict singlefeatures= {'feature1': 23.1, 'feature2': 'some-value'}
# Predict batchfeatures= [{'feature1': 23.1, 'feature2': 'some-value'}, ...]
r=classifier.predict(features=features, model_name='clf-model-name', class_count=2)
r.predict_result# A single predicted class or a list of predicted classesCurrently regression is limited to linear regression. Email support@mlrequest.com to request other online regression models.
frommlrequestimportRegressionregression=Regression('your-api-key')
# Learn singletraining_data= {'features': {'feature1': 23.1, 'feature2': 'some-value'}, 'label': 1.25}
# Learn batchtraining_data= [{'features': {'feature1': 23.1, 'feature2': 'some-value'}, 'label': 1.25}, ...]
r=regression.learn(training_data=training_data, model_name='reg-model-name')
r.content# A single response or list of responses# Predict singlefeatures= {'feature1': 23.1, 'feature2': 'some-value'}
# Predict batchfeatures= [{'feature1': 23.1, 'feature2': 'some-value'}, ...]
r=regression.predict(features=features, model_name='reg-model-name')
r.predict_result# A single predicted value or a list of predicted valuesfrommlrequestimportRLrl=RL('your-api-key')
# Predict# Note: epsilon, and action_list fields are optional - see the docs at https://docs.mlrequest.com for more informationfeatures= {'feature1': 23.1, 'feature2': 'some-value'}
r=rl.predict(features=features, model_name='rl-model-name', session_id='some-session-id', negative_reward=0, action_count=2)
r.predict_result# A list of actions, ordered by rank (choose r.predict_data[0] for the best action)# Reward - important note: only the first action from predict_data should be rewarded. Other actions can be used but should not be rewarded.r=rl.reward(reward=1, model_name='rl-model-name', session_id='some-session-id')
r.content# A single responsefrommlrequestimportAccountaccount=Account('your-api-key')
# Get account informationr=account.get_details()
r.content# Account info response# Delete a modelr=account.delete_model(model_name='some-model-name')
r.content# Delete success response