Demo entry 3549941



Submitted by anonymous on Jan 22, 2016 at 03:58
Language: Python 3. Code size: 1.1 kB.

import cPickle as pickle
from flask import Flask, abort, jsonify, request
import numpy as np

# load random forest model which has been pickled
random_forest_model = pickle.load(open("random_forest_model.pkl", "rb"))

app = Flask(__name__)

@app.route('/api', methods=['Post'])s
def make_predict():
	# error checking will go here
	data = request.get_json(force=True)
	predict_request = [data['time'], data['location'], data['event'], data['user_agent'], data['userID']]
	# convert json to numpy array
	predict_request = np.array(predict_request)
	# based upon input data, random forest model will return probability whether a location is home or work
	# test could be more explicit; given userID and if it is unique, then predict home and work. 
	# or given a new userID and data frame of times, locations, events, user_agent, then predict home and work.
	y_predict = random_forest_model(predict_request)
	# return our prediction
	output = [y_predict[0]]
	return jsonify(results=output)

if __name__ == '__main__': = 9000, debug=True)

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).