Home > Blockchain >  Store indexes of a Series into an array
Store indexes of a Series into an array

Time:11-23

My idea is to apply linear regression to draw a line on a time series dataset to approximate the direction it is evolving in (first I draw the line, then I calculate the slope and I see if my plot is increasing decreasing, or constant). For that, I relied on this code

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()

For that I need an X and Y array. The data I extracted had an index in the format of a date "Y-M-D". enter image description here

As you may know for linear regression it does not make sense to have the "date" as index, hence I used the A.reset_index() to get numeric indexes enter image description here

Now that I got my data I need to extract the indexes to put them in an array "X" and the data to be plotted in an array "Y". Therefore my question would be how to extract these new indexes and put them in the array X.

CodePudding user response:

You can do:

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

And you apply your functions on those x and y

  • Related