import statsmodels.api as sm
x1 = sm.add_constant(tmp_data['smooth_h2']) # 线性回归增加常数项 y=kx+b
y1 = tmp_data['occur_time']
result1 = sm.OLS(y1, x1).fit()
print("第一:", result1.params)
========================================
第一: const 1603854807417446400.0
smooth_h2 -1049308423361152.0
dtype: object
from sklearn import linear_model
x3 = tmp_data['smooth_h2'].values.reshape(-1, 1) # 将其转化为列向量
y3 = tmp_data['occur_time'].values.reshape(-1, 1)
reg = linear_model.LinearRegression() # 建立一元线性模型
model = reg.fit(x3, y3) # 拟合数据
diabetes_y_pred = reg.predict(x3) # 模型预测
print("输出参数w:", reg.coef_) # 输出模型参数w
print("输出参数b:", reg.intercept_) # 输出参数b
===================================================
输出参数w: [[-1.04930842e+15]]
输出参数b: [1.60385481e+18]
# 由于上面的方法,将时间当成X轴带入报错,故改成这个方法
from sklearn import linear_model
x = tmp_data['occur_time'].values.reshape(-1, 1) # 将其转化为列向量
y = tmp_data['smooth_h2'].values.reshape(-1, 1)
reg = linear_model.LinearRegression() # 建立一元线性模型
model = reg.fit(x, y) # 拟合数据
diabetes_y_pred = reg.predict(y) # 模型预测
print("输出参数w:", reg.coef_[0][0]) # 输出模型参数w
print("输出参数b:", reg.intercept_[0]) # 输出参数b
==========================================================================
输出参数w: -2.2863873343073447e-16
输出参数b: 384.84968315098433