Coder Social home page Coder Social logo

rootwoa202 / forecasting-energy-consumption-using-prophet Goto Github PK

View Code? Open in Web Editor NEW

This project forked from anandwigma/forecasting-energy-consumption-using-prophet

0.0 0.0 0.0 5.29 MB

Short tutorial on using prophet for forecasting

Jupyter Notebook 100.00%

forecasting-energy-consumption-using-prophet's Introduction

Forecasting-Energy-Consumption-Using-Prophet

Brief Introduction

Prophet is a forecasting for time series data method developed and released by Facebook's data science team that open source for everyone. What special about this method is it can plot the trend line of the data in yearly, monthly, weekly, and even daily. Additionally, it can add holiday and custom season effect to the forecast.

The Data

In this tutorial, we use hourly electricity usage/consumption from Kentucky state in USA from June 2013 to August 2018. This data obtained from kaggle here. There are 45334 rows data that consist of timestamp and energy usage in MegaWatt. Preview of the data

The Code

First load the data and named the column 'ds' for the timestamp/datetime and 'y' for the data in this case the electricity usage.

import pandas as pd

import numpy as np
from fbprophet import Prophet

%matplotlib inline
import matplotlib.pyplot as plt

from IPython.display import display

sample=pd.read_csv('EKPC_hourly.csv')

After that make the model and fit it with the electricity usage data.

from fbprophet.plot import add_changepoints_to_plot
m = Prophet(changepoint_prior_scale=0.01)
m.fit(sample)

Then make the forecast, specify the lenght and the type of time ('H' = hourly, 'Y' = yearly)

future = m.make_future_dataframe(periods=8760, freq='H')
fcst = m.predict(future)

Last is plot the forecast graph

fig = m.plot(fcst)

Forecast plot

To plot the daily, monthly, and yearly graph, use this code

fig = m.plot_components(fcst)

Forecast plot

Prophet has built-in holiday date to use. In my case, add the built-in holiday effect in US to the model before fitting the model to data.

m.add_country_holidays(country_name='US')

Prophet doesn't have built-in season in US like holiday, therefore we must add the season as custom season. We must specify which data is included in a season

def is_spring(ds):
    date = pd.to_datetime(ds)
    return (date.month >= 3 and date.month <= 5)

def is_summer(ds):
    date = pd.to_datetime(ds)
    return (date.month >= 6 and date.month <= 8)

def is_fall(ds):
    date = pd.to_datetime(ds)
    return (date.month >= 9 and date.month <= 11)

def is_winter(ds):
    date = pd.to_datetime(ds)
    return (date.month == 12 or date.month < 3)

sample['spring day'] = sample['ds'].apply(is_spring)
sample['summer day'] = sample['ds'].apply(is_summer)
sample['fall day'] = sample['ds'].apply(is_fall)
sample['winter day'] = sample['ds'].apply(is_winter)

Then add the seasonality before fitting the model

m.add_seasonality(name='Spring', period=7, fourier_order=3, condition_name='spring day')
m.add_seasonality(name='Summer', period=7, fourier_order=3, condition_name='summer day')
m.add_seasonality(name='Fall', period=7, fourier_order=3, condition_name='fall day')
m.add_seasonality(name='Winter', period=7, fourier_order=3, condition_name='winter day')

That is the simple tutorial for using prophet. Check the notebook for full code and also check prophet documentation for more information about prophet forecasting.

forecasting-energy-consumption-using-prophet's People

Contributors

anandwigma avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.