1

I have a pandas dataframe with the heat production of 300 devices mapped on the outside tempearture which looks like this:

Dataframe

I now want to do a linear regression (y= ß0+ ß1*x1) on all 300 heatig_devices for the temperature range 2 to 3.5. So that x is the outside temperature and y is the heating_device output And at the end I would like to have for every heating device a regression cefficient ß1. Whats the best way to do so ?

2
  • Try numpy.polyfit() link Commented Sep 5, 2022 at 14:12
  • Does any of the answers answer your question? Please mark as is :D Commented Sep 6, 2022 at 7:39

2 Answers 2

2

Simply compute the coefficient for every column using LinearRegression from sklearn.linear_model.

for i in range(300):
    t = LinearRegression().fit(df[['outside temperature']], df[['heating_device'+str(i+1)]])
    print(i + 1, t.coef_[0], t.intercept_[0])

Now it will print the coefficient for every column

Sign up to request clarification or add additional context in comments.

6 Comments

Hey, thank you very much for your answer ! It gives a error back: print(i + 1, t.coef_[0][0]) IndexError: invalid index to scalar variable. I think with t.coef_[0] its fine. But do you know how to get the other coefficient ß0 ?
Run it for one column and see what t.coef_ returns, perhaps you just need t.coef_[0]
For the intercept you can use t.intercept_, see documentation
@ocram113 does it work for you?
I think you just mixed up x and y in .fit(...) , but apart from this it works now fine ! Thank you very much :)
|
0

You should provide some workable code but from the numpy documentation:

x = np.array([0.0, 1.0, 2.0, 3.0,  4.0,  5.0])
y = np.array([0.0, 0.8, 0.9, 0.1, -0.8, -1.0])
z = np.polyfit(x, y, 1) #1 is the fitting order i.e. degree of polynomial
z

For the linear case you would get two parameters slope and intercept.

If you read the docs then y can be a 2D numpy array. In your case y would be the heating values and x would be the temperature.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.