Home > OS >  How to access prediction from another python module?
How to access prediction from another python module?

Time:07-12

I have a file file_calling_class.py that needs to access the prediction value from another python module in file_with_class.py. However, I do not know how to access the prediction. The function alone works fine if it is the only script but if I want to pass the budget value from file_calling_class.py to file_with_class.py by using self I do not know how to access the prediction result of calculate_sales(self).

This is my file_calling_class.py

 import file_with_class

 budget = 100

 sales = file_with_class.CalcSales(budget=budget).__str__()
 print('Your sales are: '  sales)

This is my file_with_class.py

 import pickle
 import pandas as pd




class CalcSales():

def __init__(self, budget: int):
    self.budget = budget
    self.sales = 0
    self.prediction = 0


def calculate_sales(self): 
    #budget = request.args.get('budget')
    print(self.budget) 
    budget = [int(self.budget)] 

    df = pd.DataFrame(budget, columns=['Marketing Budget']) 

    model = pickle.load(open('simple_linear_regression.pkl', 'rb'))

    prediction = model.predict(df) 

    self.prediction = int(prediction[0]) 

    # return(self.prediction)


def __str__ (self):
    return (str(self.prediction))

Output

  Your sales are: 0

which is just the value with which I initialized self.prediction

CodePudding user response:

You are not invoking the .calculate_sales() method in your call. Try changing sales = file_with_class.CalcSales(budget=budget).__str__() in file_calling_class.py to:

sales = file_with_class.CalcSales(budget=budget).calculate_sales().__str__()
  • Related