-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
65 lines (61 loc) · 2.21 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
This file runs the Flask application we are using as an API endpoint.
"""
import pickle
from flask import Flask
from flask import request
from flask import jsonify
import json
from sklearn.linear_model import LogisticRegression
import joblib
from sklearn import preprocessing
import numpy as np
import pandas as pd
# Create a flask
app = Flask(__name__)
class NumpyEncoder(json.JSONEncoder):
""" Special json encoder for numpy types """
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
return json.JSONEncoder.default(self, obj)
# Load pickled model file
model = joblib.load('chrunlog_r.pkl')
y=np.array([0,1])
@app.route("/")
def hello():
print("Handling request to home page.")
return "Hello, Azure!"
# Create an API end point
@app.route('/api/v1.0/predict', methods=['GET'])
def get_prediction(trained_model=model):
# ['tenure', 'age', 'address', 'income', 'ed', 'employ', 'equip']
# tenure
tenure = float(request.args.get('tenure'))
# age
age = float(request.args.get('age'))
# address
address = float(request.args.get('address'))
#income
income = float(request.args.get('income'))
#ed
ed = float(request.args.get('ed'))
#employ
employ = float(request.args.get('employ'))
#equip
equip = float(request.args.get('equip'))
# The features of the observation to predict
data_features=pd.DataFrame([tenure, age, address, income, ed, employ, equip])
features = np.asarray(data_features).reshape(1, -1)
features = preprocessing.StandardScaler().fit(features).transform(features)
# Predict the class using the model
predicted_class = int(trained_model.predict(features.reshape(1, -1) ))
# Return a json object containing the features and prediction
return jsonify(features=json.dumps(data_features.to_json(), cls=NumpyEncoder), predicted_class=json.dumps( predicted_class, cls=NumpyEncoder))
if __name__ == '__main__':
# Run the app at 0.0.0.0:3333
app.run(port=80,host='0.0.0.0')