Created
April 14, 2019 17:43
-
-
Save linkerzx/43e9edcdb8d1f86393dec575553773b9 to your computer and use it in GitHub Desktop.
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
import logging | |
import azure.functions as func | |
import pandas as pd | |
from sklearn.linear_model import LogisticRegression | |
from sklearn.externals import joblib | |
def main(req: func.HttpRequest) -> func.HttpResponse: | |
logging.info('Python HTTP trigger function processed a request.') | |
if req.body: | |
try: | |
logging.info("Converting Request to DataFrame") | |
req_body = req.get_json() | |
df_body = pd.DataFrame([req_body]) | |
logging.info("Loadding the Prediction Model") | |
filename = "model.pckl" | |
loaded_model = joblib.load(filename) | |
# Features names need to have been added to the pickled model | |
feature_names = loaded_model.feature_names | |
# subselect only the feature names | |
logging.info("Subselecting the dataframe") | |
df_subselect = df_body[feature_names] | |
logging.info("Predicting the Probability") | |
result = loaded_model.predict_proba(df_subselect) | |
# We are looking at the probba prediction for class 1 | |
prediction = result[0][1] | |
return func.HttpResponse("{prediction}".format(prediction=prediction), status_code=200) | |
except ValueError: | |
pass | |
else: | |
return func.HttpResponse( | |
"Please pass a name on the query string or in the request body", | |
status_code=400 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment