Created
November 18, 2017 00:58
-
-
Save dyerrington/66e090a0fcb1d67f082d99049fb75e5b to your computer and use it in GitHub Desktop.
Flask route solution from a past student "Ian" that works with a loaded model based on a DecisionTreeClassifier instance.
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('/ian') | |
def ian(): | |
from sklearn.datasets import load_iris | |
import pandas as pd | |
data = load_iris() | |
df = pd.DataFrame(data['data'], columns=['sepal_len', 'sepal_width', 'petal_lengh', 'petal_width']) | |
y = data['target'] | |
from sklearn.tree import DecisionTreeClassifier | |
model = DecisionTreeClassifier(criterion='gini', random_state=42) | |
input_sepal_len = request.args.get("sepal_len") | |
input_sepal_width = request.args.get("sepal_width") | |
input_petal_lengh = request.args.get("petal_lengh") | |
input_petal_width = request.args.get("petal_width") | |
if input_sepal_len: | |
model.fit(df, y) | |
list_of_data_to_fit = [ | |
float(input_sepal_len), | |
float(input_sepal_width), | |
float(input_petal_lengh), | |
float(input_petal_width) | |
] | |
predicted = model.predict(list_of_data_to_fit).tolist() | |
probabilities = model.predict_proba(list_of_data_to_fit).tolist() | |
result = { | |
"response": "ok", | |
"predictions": predicted, | |
"score": model.score(df, y), | |
"probabilities": {flower: probabilities[0][index] for index, flower in enumerate(model.classes_.tolist())} | |
} | |
else: | |
return "Please pass an input" | |
return jsonify(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment