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

Python – Linear Regression Model by Group

I would like support trying to get a linear regression model built by ‘ClientID’ grouping.

I have managed to pull together the below functions but I am getting errors, that said I am very new to Python so would be open to any other ideas you may have.

The data below is from my dataframe dfTotal3 and the two variables I am using Steps (count of steps the client did in a day) and WeightDiff (change in weight since last input).

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

def model(dfTotal3, target):
    y = dfTotal3[['Steps']].values
    X = dfTotal3[['WeightDiff']].values
    return np.squeeze(LinearRegression().fit(X, y).predict(target))

def group_predictions(df, target):
    target = dfWeightComp['DTWDG']
    return dfTotal3.groupby('ClientID').apply(model, target)

group_predictions(dfTotal3, dfTotal3['DTWDG'])

Error Output:

ValueError: Expected 2D array, got 1D array instead:
array=[-0.03231707 -0.03780488 -0.04512195 -0.04615385].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

>Solution :

I think the error comes from:

return np.squeeze(LinearRegression().fit(X, y).predict(target))

Specifically I speculate there is an issue with this part of the code .predict(target)

This post is suggesting to change your target i.e.:

target = dfWeightComp['DTWDG']

to:

target = dfWeightComp[['DTWDG']]

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