def readGammaGammaFitterModel(): gammaGammaFitterModel = GammaGammaFitter() gammaGammaFitterModel.load_model("GammaGammaFitterModel.pkl") return gammaGammaFitterModel
def predictSpending(customerId): # initialize the data dictionary that will be returned data = {"success": False, "result": {"customerId": "", "y": 0.0}} # ensure the customer ID was properly uploaded to our endpoint if customerId: print("* get data") data = pandas.read_csv("sample_transactions.csv") #data = pandas.read_json(baseURL + "/api/transactions") #data = data.drop(columns="_id") print("* prepare data") # prepare and shaping the data # columns - # customerId # frequency : number of repeat purchase transactions # recency: time (in days) between first purchase and latest purchase # T: time (in days) between first purchase and end of the period under study # monetary_value: average transactions amount today = pandas.to_datetime(datetime.date.today()) summaryData = summary_data_from_transaction_data( data, "customerId", "transactionDate", monetary_value_col="transactionAmount", observation_period_end=today) # filter the customer data that has no transaction analysisData = summaryData[summaryData["frequency"] > 0] # get the stat of the particular customer customer = analysisData.loc[customerId] # load model ggf_loaded = GammaGammaFitter() ggf_loaded.load_model('ggf.pkl') # estimate the average transaction amount predict = ggf_loaded.conditional_expected_average_profit( customer["frequency"], customer['monetary_value']) # add the input and predicted output to the return data data = { "success": True, "result": { "customerId": customerId, "y": predict } } # return the data dictionary as a JSON response return flask.jsonify(data)