Created
November 18, 2017 01:02
-
-
Save dyerrington/712c961d0d1faf764f4d2caaf6796893 to your computer and use it in GitHub Desktop.
This particular solution fits a model inline upon each request to the endpoint. This is not ideal so one should load outside the Flask route method before prediction. More ideally, the model should be loaded through a serialization library like joblib.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route('/predict-iris') | |
def predict_iris(): | |
# Load data | |
iris = load_iris() | |
# print("Loaded iris", iris) | |
# Fit our model | |
logreg = LogisticRegression() | |
model = logreg.fit(iris['data'], iris['target']) | |
model.predict_proba(iris['data']) | |
# Parameters from GET request | |
sepal_length = request.args.get("sepal_length") | |
sepal_width = request.args.get("sepal_width") | |
petal_length = request.args.get("petal_length") | |
petal_width = request.args.get("petal_width") | |
# To predict | |
to_predict = np.array([ | |
float(sepal_length), | |
float(sepal_width), | |
float(petal_length), | |
float(petal_width) | |
]) | |
print( | |
"My input parameters are:", | |
sepal_length, sepal_width, | |
petal_length, petal_width | |
) | |
if all([sepal_length, sepal_width, petal_length, petal_width]): | |
result = { | |
"message": "OK", | |
"predict": model.predict(to_predict).tolist(), | |
"probas": model.predict_proba(to_predict).tolist() | |
} | |
else: | |
result = { | |
"message": "Please set input!" | |
} | |
return jsonify(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment