Search code examples
pythonpandasscikit-learnregressionprediction

How to use .predict() method in python for linear regression?


i estimated multiple regression model from my dataframe. i have three indipendent variables: month(1 to 36), price, and ads day.

i would like to make prediction, changing the conditions:

-Forecast values for the next 10 month(37 to 47) with price = 85 and ads day=4

I estimate my model and tried:

Time1= np.arange(37,48)
Price1=85
Ads1=4
Lm.predict([Time1,Price1,Ads1])

But it doesn't work

thanks


Solution

  • First train the model using the train data of the past observations. In your case the train data constitutes 3 three independent variables and 1 dependent variable for each observation.

    Once a decent model is trained (using hyper parameter optimization) you can then use it to make forecasts.

    Sample code (documented inline)

    import numpy as np
    from sklearn.linear_model import LinearRegression
    
    # sample dummy data 
    
    # independent variables
    time = np.arange(1,36)
    price = np.random.randint(1,100,35)
    ads = np.random.randint(1,10,35)
    # dependent variable
    y = np.random.randn(35)
    
    # Reshape it into 35X3 where each row is an observation
    train_X = np.vstack([time, price, ads]).T
    
    # Fit the model
    model = LinearRegression().fit(train_X, y)
    
    # Sample observations for which 
    # forecast of dependent variable has to be made
    time1 = np.arange(37, 47)
    price1 = np.array([85]*len(time1))
    ads1 = np.array([4]*len(time1))
    
    # Reshape such that each row is an observation
    test_X = np.vstack([time1, price1, ads1]).T
    
    # make the predictions
    print (model.predict(test_X))'
    

    Output:

    array([0.22189608, 0.2269302 , 0.23196433, 0.23699845, 0.24203257,
           0.24706669, 0.25210081, 0.25713494, 0.26216906, 0.26720318])