Skip to content

Instantly share code, notes, and snippets.

@aficionado
Created March 13, 2013 01:06
Show Gist options
  • Save aficionado/5148561 to your computer and use it in GitHub Desktop.
Save aficionado/5148561 to your computer and use it in GitHub Desktop.
A simple example to create local predictions using a pre-built model
# import BigML class
from bigml.api import BigML
# import local Model class
from bigml.model import Model
# Instantiate the API with your credentials. You can avoid this if you set up your username and API key in your environment
api = BigML('yourusername', '3ff25044b4f4582903d90000a5fab240442e734c')
# Get the model. Assuming that you already created in BigML
model = api.get_model('model/513fbf8a035d07361e000509')
# Instantiate the local model
local_model = Model(model)
# Assuming that you have the data that you want to create predictions from in a csv file
file = open("your_file.csv" "U")
# File where you want to store the predictions
predictions = open("predictions.csv', "w")
# Assuming that your csv file has a header that matches the one that you use to create the model
field_names = file.readline().strip().split(',')
# Assuming that there's no missing values and all the values in your input are float.
for line in file:
# parse each line in the file
values = line.strip().split(',')
# convert string to values. Adapt it as you need it
values = [float(value) for value in values]
# creates a dictionary for the input data
input_data = dict(zip(field_names, values))
# creates a local prediction
prediction = local_model.predict(input_data)
# saves the prediction to a file
predictions.write("%s\n" % prediction)
predictions.flush()
# Close files
predictions.close()
file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment