Guest User

Untitled

a guest
Nov 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. @app.route('/ian')
  2. def ian():
  3.  
  4. from sklearn.datasets import load_iris
  5. import pandas as pd
  6.  
  7. data = load_iris()
  8. df = pd.DataFrame(data['data'], columns=['sepal_len', 'sepal_width', 'petal_lengh', 'petal_width'])
  9. y = data['target']
  10.  
  11. from sklearn.tree import DecisionTreeClassifier
  12. model = DecisionTreeClassifier(criterion='gini', random_state=42)
  13.  
  14. input_sepal_len = request.args.get("sepal_len")
  15. input_sepal_width = request.args.get("sepal_width")
  16. input_petal_lengh = request.args.get("petal_lengh")
  17. input_petal_width = request.args.get("petal_width")
  18.  
  19. if input_sepal_len:
  20. model.fit(df, y)
  21. list_of_data_to_fit = [
  22. float(input_sepal_len),
  23. float(input_sepal_width),
  24. float(input_petal_lengh),
  25. float(input_petal_width)
  26. ]
  27.  
  28. predicted = model.predict(list_of_data_to_fit).tolist()
  29. probabilities = model.predict_proba(list_of_data_to_fit).tolist()
  30.  
  31. result = {
  32. "response": "ok",
  33. "predictions": predicted,
  34. "score": model.score(df, y),
  35. "probabilities": {flower: probabilities[0][index] for index, flower in enumerate(model.classes_.tolist())}
  36. }
  37.  
  38. else:
  39. return "Please pass an input"
  40.  
  41. return jsonify(result)
Add Comment
Please, Sign In to add comment