时间序列分析 - 线性回归和多项式回归
#data-science #时间序列分析 4

本节涉及的概念我们大多熟悉,故不阐述原理,只记录使用sklearn进行线性回归和多项式回归的操作

一元线性回归

from sklearn.linear_model import LinearRegression

x = np.array([1, 2, 3, 4])
y = np.array([2, 4, 7, 9])
X = x[:, np.newaxis]

model = LinearRegression().fit(X, y)
yhat = model.predict(X)

plt.scatter(x, y)
plt.plot(x, yhat)
plt.show()

print(model.coef_[0], model.intercept_)

Pasted image 20250503193020.png

多元线性回归

df = ...

X = df.iloc[, :-1].values # 转换为 ndarray
y = df.iloc[, -1].values

model = LinearRegression().fit(X, y)

print(model.coef_[0], model.coef_[1], ...)
print(model.intercept_)

多项式回归

from sklearn.preprocessing import PolynomialFeatures as pf

trans = pf(degree=2, include_bias=False)
X2 = trans.fit_transform(X)

model = LinearRegression().fit(X2, y)
...
时间序列分析 - 线性回归和多项式回归
http://localhost:8090/archives/bRlSRg3S
作者
酱紫瑞
发布于
更新于
许可协议