【发布时间】:2022-11-22 20:45:41
【问题描述】:
我的想法是应用线性回归在时间序列数据集上画一条线来近似它的演变方向(首先我画线,然后我计算斜率,然后我看看我的图是在增加还是减少,或者不变)。 为此,我依靠这段代码
def estimate_coef(x, y):
# number of observations/points
n = np.size(x)
# mean of x and y vector
m_x = np.mean(x)
m_y = np.mean(y)
# calculating cross-deviation and deviation about x
SS_xy = np.sum(y*x) - n*m_y*m_x
SS_xx = np.sum(x*x) - n*m_x*m_x
# calculating regression coefficients
b_1 = SS_xy / SS_xx
b_0 = m_y - b_1*m_x
return (b_0, b_1)
def plot_regression_line(x, y, b):
# plotting the actual points as scatter plot
plt.scatter(x, y, color = "m",
marker = "o", s = 30)
# predicted response vector
y_pred = b[0] + b[1]*x
# plotting the regression line
plt.plot(x, y_pred, color = "g")
# putting labels
plt.xlabel('x')
plt.ylabel('y')
# function to show plot
plt.show()
为此,我需要一个 X 和 Y 数组。 我提取的数据具有日期格式为“Y-M-D”的索引。 enter image description here
正如您可能知道的线性回归,将“日期”作为索引没有意义,因此我使用 A.reset_index() 来获取数字索引
enter image description here
现在我得到了我的数据,我需要提取索引以将它们放入数组“X”中,并将要绘制的数据放入数组“Y”中。 因此我的问题是如何提取这些新索引并将它们放入数组 X
【问题讨论】:
-
请不要使用图像来显示代码。