Progman
Progman

Reputation: 53

linear regression on time series in python

How to show dates on the chart for linear regression?

My data in csv file:

"date","bat"
"2020-05-13 00:00:00",84
"2020-05-14 00:00:00",83
"2020-05-15 00:00:00",81
"2020-05-16 00:00:00",81

I'm able to generate chart with linear reg. but don't know how to make x axis to show dates.

my code:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model

df = pd.read_csv('battery.csv', parse_dates=['date'])

x=np.array(pd.to_datetime(df['bat'].index.values, format='%Y-%m-%d'), dtype=float)
x=x.reshape(-1, 1)
y=np.array(df['bat'].values, dtype=float)

lm = linear_model.LinearRegression()
model = lm.fit(x,y)

predictions = lm.predict(x)

f, ax = plt.subplots(1, 1)
ax.plot(x, predictions,label='Linear fit', lw=3)
ax.scatter(x, y,label='value', marker='o', color='r')
plt.ylabel('bat')
ax.legend();

plt.show()

Upvotes: 5

Views: 4150

Answers (2)

Mike Olszak
Mike Olszak

Reputation: 403

try this:

df = pd.read_csv('battery.csv', parse_dates=['date'])

x=pd.to_datetime(df['date'], format='%Y-%m-%d')
y=df['bat'].values.reshape(-1, 1)

lm = linear_model.LinearRegression()
model = lm.fit(x.values.reshape(-1, 1),y)

predictions = lm.predict(x.values.astype(float).reshape(-1, 1))

f, ax = plt.subplots(1, 1)
ax.plot(x, predictions,label='Linear fit', lw=3)
ax.scatter(x, y,label='value', marker='o', color='r')
plt.ylabel('bat')
ax.legend();

plt.show()

Upvotes: 4

DYZ
DYZ

Reputation: 57033

Let Pandas handle all the plotting - but make sure the date is the index:

df['predictions'] = predictions
df.set_index('date').plot(style={'bat': 'or'})
plt.ylabel('bat')
plt.legend()

Upvotes: 3

Related Questions