def rossmann_predict(): test_json = request.get_json() if test_json: if isinstance(test_json, dict): test_row = pd.DataFrame(test_json, index=[0]) else: test_row = pd.DataFrame(test_json, columns=test_json[0].keys()) pipeline = Rossmann() # data cleaning df1 = pipeline.data_cleaning(test_row) # feature engineering df2 = pipeline.feature_engineering(df1) # data preparation df3 = pipeline.data_preparation(df2) # prediction df_response = pipeline.get_prediction(model, test_row, df3) return df_response else: return Response('{}', status=200, mimetype='application/json')
def rossmann_predict( ): #function that is executed when an endpoint receives a POST request. This function works on the data received. test_json = request.get_json() # retrieve the json data if test_json: # test whether the data is there or not if isinstance( test_json, dict): # if data is a dict, then we have only one line of data test_raw = pd.DataFrame(test_json, index=[0]) else: test_raw = pd.DataFrame( test_json, columns=test_json[0].keys() ) # if data is not a dict, then it has multiple data. We need to name the columns. # Instantiate Rossmann Class ("copy"/call Rossmann class) pipeline = Rossmann() # run Data Cleaning on raw data df1 = pipeline.data_cleaning(test_raw) # run feature engineering on df1 df2 = pipeline.feature_engineering(df1) # run data preprocessing on df2 df3 = pipeline.data_preparation(df2) # prediction df_response = pipeline.get_prediction( model, test_raw, df3 ) #generate predictions with xgboost model, the data that the user sent, and the data to generate predictions on. return df_response else: return Response( '{}', status=200, mimetype='application/json' ) # if there's no data, return a response answer 200 (request was correct but execution failed)
def rossmann_predict(): # input data for prediction print('===> getting the data from request') test_json = request.get_json() if test_json: # convert json to DataFrame if isinstance(test_json, dict): # an unique example of test test_raw = pd.DataFrame(test_json, index=[0]) else: # multiple examples test_raw = pd.DataFrame(test_json, columns=list(test_json[0].keys())) # class to prediction pipeline = Rossmann() # pre-process the test data to prediction print('===> pre-process test data to prediction') test_transformed = pipeline.transform(test_raw.copy()) # feature engineering print('===> feature engineering') data = pipeline.feature_engineering(test_transformed) # make a prediction print('===> prediction') response_json = pipeline.get_prediction(model=model_rossmann, test_original=test_raw, test_data=data) return response_json else: return Response("{}", status=201, mimetype='application/json')
def rossmann_predict(): test_json = request.get_json() if test_json: # there is data if isinstance(test_json, dict): # unique example test_raw = pd.DataFrame(test_json, index=[0]) else: # multiple example test_raw = pd.DataFrame(test_json, columns=test_json[0].keys()) # Instantiate Rossmann class pipeline = Rossmann() # data cleaning df1 = pipeline.data_cleaning(test_raw) # feature engineering df2 = pipeline.feature_engineering(df1) # data preparation df3 = pipeline.data_preparation(df2) # prediction df_response = pipeline.get_prediction(model, test_raw, df3) return df_response else: return Response("{}", status=200, mimetype="application/json")
def rossmann_predict(): try: test_json = request.get_json() if test_json: #there is data if isinstance(test_json, dict): #unique Example test_raw = pd.DataFrame(test_json, index[0]) else: # multiple examples test_raw = pd.DataFrame(test_json, columns=test_json[0].keys()) # Instantiate Rossmann class pipeline = Rossmann() #data cleaning df1 = pipeline.data_cleaning(test_raw) #feature engineering df2 = pipeline.feature_engineering(df1) #data preparation df3 = pipeline.data_preparation(df2) #prediction df_response = pipeline.get_prediction(model, test_raw, df3) return df_response else: return Response('{}', status=404, mimetype='application/json') except Exception as e: print("/rossamann/predict/ error: ", e)
def rossmann_predcit(): # gets json that comes from API test_json = request.get_json() # checks if json exists if test_json: # unique example if isinstance(test_json, dict): test_raw = pd.DataFrame(test_json, index=[0]) #multiple examples else: test_raw = pd.DataFrame(test_json, columns=test_json[0].keys()) # instantiates Rossmann class pipeline = Rossmann() # data cleaning df1 = pipeline.data_cleaning(test_raw) # feature engineering df2 = pipeline.feature_engineering(df1) # data preparation df3 = pipeline.data_preparation(df2) # prediction df_response = pipeline.get_prediction(model, test_raw, df3) return df_response else: return Response('{}', status=200, mimetype='application/json')
def rossmanPredict(): test_JSON = request.get_json() if test_JSON: #there is data if isinstance(test_JSON, dict): teste_raw = pd.DataFrame(test_JSON, index=[0]) #unique example else: teste_raw = pd.DataFrame( test_JSON, columns=test_JSON[0].keys()) #multiple examples # Instantiate pipeline = Rossmann() # Data Cleaning df1 = pipeline.data_cleaning(teste_raw) # Feature Engineering df2 = pipeline.feature_engineering(df1) # Data Preparation df3 = pipeline.data_preparation(df2) # Prediction df_response = pipeline.get_prediction(model, teste_raw, df3) return df_response else: return Response('{}', status=200, mimetype='application/json')
def rossmann_predict(): json_request = request.get_json() if json_request: # there is data if isinstance( json_request, dict ): # unique example df_raw = pd.DataFrame( json_request, index=[0] ) else: # multiple example df_raw = pd.DataFrame( json_request, columns=json_request[0].keys() ) pipeline = Rossmann() df1 = pipeline.data_cleaning( df_raw ) df2 = pipeline.feature_engineering( df1 ) df3 = pipeline.data_preparation( df2 ) df_response = pipeline.get_prediction( model, df_raw, df3 ) return df_response else: return Reponse( '{}', status=200, mimetype='application/json' )