线性回归与熊猫数据框架的一部分300列



我有一个熊猫数据帧,300个设备的热产量映射在外部温度上,看起来像这样:

数据帧

我现在想对温度范围为2到3.5的所有300个热设备进行线性回归(y=ß0+坙1*x1(。因此,x是外部温度,y是加热装置的输出最后,我希望每个加热设备都有一个回归系数ß1。最好的方法是什么?

只需使用LinearRegressionsklearn.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])

现在它将打印每列的系数

您应该提供一些可行的代码,但来自numpy文档:

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

对于线性情况,您将得到两个参数slopeintercept

如果你阅读了文档,那么y可以是一个2D numpy数组。在您的情况下,y将是热值,x将是温度。

最新更新