Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to access prediction from another python module?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 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

>Solution :

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__()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading