【问题标题】:Store indexes of a Serie into an array将 Serie 的索引存储到数组中
【发布时间】: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

【问题讨论】:

  • 请不要使用图像来显示代码。

标签: python numpy


【解决方案1】:

你可以做:

x=A.index + 1 # to make data x starts with 1 instead of 0
y=A['lift']

然后你将你的函数应用于那些xy

【讨论】:

  • x = A.index.values 也可以,但是你会得到一个 numpy 数组而不是 IndexRange
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 2013-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-01
相关资源
最近更新 更多