Simple linear regression fit and prediction on time series data with visualization in python

Ishan Mehta
2 min readMay 16, 2020

It is a simple article in which we will go through how to create a simple linear regression line for time-series data, visualize it, and get the slope and intercept values in python and how can it be used to predict future values.

We are using Alphabet Inc closing stock price as a sample time series data

Import the libraries

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

We will initialize a data frame with ‘date’ and corresponding column values. I am importing the stocks closing price data from my local PostgreSQL DB, but it can be from CSV or any other database.

database = "test"
postgresql_user = "test"
postgresql_password = "****"
conn = psycopg2.connect(database=database, user=user, password=password, host="127.0.0.1", port="5432")
ticker = 'GOOG'
close_price_data_query = "SELECT date,adjusted_close from stocks.eod_data where ticker = '" + ticker + "' order by date asc"
df = pd.read_sql(close_price_data_query, con=conn)

Printing the data:

print (df.head)
<bound method NDFrame.head of date adjusted_close
0 2014-03-27 556.9310
1 2014-03-28 558.4568
2 2014-03-31 555.4451
3 2014-04-01 565.6072
4 2014-04-02 565.4471
... ... ...
1541 2020-05-11 1403.2600
1542 2020-05-12 1375.7400
1543 2020-05-13 1349.3300
1544 2020-05-14 1356.1300
1545 2020-05-15 1373.1899

We will create a NumPy array starting from 0…df[‘date’].size -1 to fit the x-axis values in the linear regression model.

x = np.arange(df['date'].size)

Now we will fit the linear regression using np.polyfit and get slope and intercept values. As it is linear regression we will have deg (degree) parameter as 1.

fit = np.polyfit(x, df['adjusted_close'], deg=1)
print ("Slope : " + str(fit[0]))
print ("Intercept : " + str(fit[1]))

Now we will plot this fit initializing it to a fit function

#Fit function : y = mx + c [linear regression ]
fit_function = np.poly1d(fit)

#Linear regression plot
plt.plot(df['date'], fit_function(x))
#Time series data plot
plt.plot(df['date'], df['adjusted_close'])

plt.xlabel('Date')
plt.ylabel('Adjusted Closing Price')
plt.title('GOOG Close Price')
plt.show()

Also in order to predict future values using the model, we can use the fit function. To predict the future values 15 days ahead

prediction = fit_function(df['date'].size + 14)
print(prediction)

--

--