Search code examples
pythonmachine-learningregressiondata-sciencedata-modeling

Finding final regression equation in python


How to find final regression model equation including coefficients with all variables? is there any method?


Solution

  • Show you an Example

    I show you an example with OLS using boston house price data set.

    Code:

    # load a dataset and regression function
    from sklearn import linear_model,datasets
    import pandas as pd
    # I use boston dataset to show you 
    full_data = datasets.load_boston()
    
    # get a regressor, fit intercept
    reg = linear_model.LinearRegression(fit_intercept=True)
    # data is our explanatory, target is our response
    reg.fit(full_data['data'],full_data['target'])
    
    # we have 1 intercept and  11 variables' coef
    reg.intercept_,reg.coef_
    
    # get the name of features
    full_data.feature_names
    # append to get a new list
    coef = np.append(reg.intercept_,reg.coef_)
    feature_names = np.append(['Intercept'], full_data.feature_names)
    # output a dataframe contains coefficients you want
    pd.DataFrame({"feature_names":feature_names,"coef":coef})
    

    Output:

       feature_names       coef
    0      Intercept  36.459488
    1           CRIM  -0.108011
    2             ZN   0.046420
    3          INDUS   0.020559
    4           CHAS   2.686734
    5            NOX -17.766611
    6             RM   3.809865
    7            AGE   0.000692
    8            DIS  -1.475567
    9            RAD   0.306049
    10           TAX  -0.012335
    11       PTRATIO  -0.952747
    12             B   0.009312
    13         LSTAT  -0.524758
    

    Some Advice

    You can use dir(object) to see what's in your fitted model, like using dir(full_data) and dir(reg) to see atrributes and methods of an instance.

    As for sklearn, here is an official guide about it. You can find functions and datasets on the guide.