Skip to content

Instantly share code, notes, and snippets.

@jeffesp
Created October 25, 2017 13:43
Show Gist options
  • Save jeffesp/6520900172cb748ad423aba1b67a6af9 to your computer and use it in GitHub Desktop.
Save jeffesp/6520900172cb748ad423aba1b67a6af9 to your computer and use it in GitHub Desktop.
def get_results(client, session_id):
# First wait on the results to be complete by checking periodically
while True:
current = client.sessions.get(session_id)
if current.status == session.Status.completed or current.status == session.Status.failed:
break
# waiting to not exhaust the call per minute limit on the API
time.sleep(5)
# Then get results and find the model_id that was generated
results = client.sessions.get(session_id)
return results.model_id
import json
import time
from nexosisapi import session
from nexosisapi import Client
if __name__ == '__main__':
# You must add a reference to the `nexosisapi` package: https://pypi.python.org/pypi/nexosisapi/
# $ pip install nexosisapi
client = Client('YOUR-API-KEY-GOES-HERE')
dataset_name = 'mpg-dataset'
upload_data(client, dataset_name)
session_id = request_model(client, dataset_name)
model_id = get_results(client, session_id)
features = {
'Cylinders': 8,
'Displacement': 307,
'Horsepower': 135,
'Weight': 3624,
'Acceleration': 13,
'ModelYear': 70,
'Origin': 1,
'Make': 'chevrolet'
}
prediction = client.models.predict(model_id, [features])
print(prediction.data[0]['MPG'])
def request_model(client, dataset_name):
# This call starts the model building process.
# Here we are looking to predict the mpg column
# on the dataset that was previously uploaded.
trainer = client.sessions.train_regression_model(dataset_name)
return trainer.session_id
def upload_data(client, dataset_name):
# auto-mpg.data.json is a json file that can be pulled from:
# https://s3.us-east-2.amazonaws.com/nexosis-api-sample-data/auto-mpg.data.json
with open('auto-mpg.data.json', 'r') as data:
ds = json.load(data)
client.datasets.create(dataset_name, ds['data'], ds['columns'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment