Neural Market Trends |||

Forecasting Medium Subscriber Count With Facebook Prophet

I’m going to share some code that I stole and cobbled together to forecast my Medium subscriber growth using ARIMA and Facebook’s Prophet python package.

I did ARIMA for shits and giggles and wanted to see where how far I’ve deviated from my upward trend. For the actual forecasting, I used FB Prophet and liked it a lot.

FB Prophet requires some specific setup for your data with a column called ds for your timestamp and y for your numerical value.

It runs your data and automatically adjusts for things like seasonality and holidays, which I like a lot. The package does a lot of heavy lifting if you do time series for forecasting a lot.

Plus, it produces some nice graphs too. Here’s what my forecasted growth looks like.

My Medium subscriber growth is estimated to close around 700 by the end of the year. What’s interesting is the seasonality” of my subscriber growth. It looks like there’s a big bump between February and April with a late summer bump in August. That leads me to wonder if I should post more often in those time frames than at other times of the year.

Anyway, here’s the code.

Medium subscriber forecast python code

#!/usr/bin/env python
# coding: utf-8

# In[1]:


#Stole from https://www.alldatascience.com/time-series/forecasting-time-series-with-auto-arima/
import pandas as pd
from pandas import to_datetime
import matplotlib.pyplot as plt
import numpy as np

from pmdarima.arima import auto_arima

from fbprophet import Prophet


# In[2]:


data = pd.read_csv('data/thomasottio-subscriber-stats.csv')
data.head()

# convert data type
data['period_end'] = pd.to_datetime(data['period_end'])

df = data[['period_end', 'followers_total']]


# In[3]:


# convert data type
data['period_end'] = pd.to_datetime(data['period_end'])
# set period_end as index
data.set_index('period_end', drop = True, inplace = True)
# select relevant column
data = data['followers_total']
# set the frequency of index to monthly
data = data.asfreq('M')

# check data index after all changes
data.index


# In[4]:


data.tail()


# In[5]:


data.to_csv('data/subscriber-out.csv')


# In[6]:


data.plot(figsize=(15, 6))
plt.show()


# In[7]:


TEST_SIZE = 6
train, test = data.iloc[:-TEST_SIZE], data.iloc[-TEST_SIZE:]
x_train, x_test = np.array(range(train.shape[0])), np.array(range(train.shape[0], data.shape[0]))
train.shape, x_train.shape, test.shape, x_test.shape


# In[8]:


fig, ax = plt.subplots(1, 1, figsize=(15, 5))
ax.plot(x_train, train)
ax.plot(x_test, test)


# In[9]:


model = auto_arima(train, start_p=1, start_q=1,
                      test='adf',
                      max_p=5, max_q=5,
                      m=1,             
                      d=1,          
                      seasonal=False,   
                      start_P=0, 
                      D=None, 
                      trace=True,
                      error_action='ignore',  
                      suppress_warnings=True, 
                      stepwise=True)


# In[10]:


model.summary()


# In[11]:


# Forecast

prediction, confint = model.predict(n_periods=TEST_SIZE, return_conf_int=True)

prediction


# In[12]:


cf= pd.DataFrame(confint)


# In[13]:

prediction_series = pd.Series(prediction,index=test.index)
fig, ax = plt.subplots(1, 1, figsize=(15, 5))
ax.plot(data)
ax.plot(prediction_series)
ax.fill_between(prediction_series.index,
                cf[0],
                cf[1],color='grey',alpha=.3)


# In[16]:


#!conda install -c conda-forge fbprophet -y


# In[17]:


df.head()


# In[18]:


df = df.rename(columns={"period_end": "ds", "followers_total": "y"})


# In[19]:


#df["ds"]= to_datetime(df["ds"])


# In[20]:


df.dtypes


# In[21]:


# define the model
model = Prophet(interval_width=0.95)


# In[22]:


#stole code from https://machinelearningmastery.com/time-series-forecasting-with-prophet-in-python/


# In[23]:


# fit the model
model.fit(df)


# In[24]:


#stole code from https://www.kaggle.com/code/prashant111/tutorial-time-series-forecasting-with-prophet

future_dates = model.make_future_dataframe(periods=18, freq='MS')
future_dates.tail()


# In[25]:


forecast = model.predict(future_dates)
forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()


# In[26]:


model.plot(forecast, uncertainty=True)


# In[27]:


model.plot_components(forecast)


# In[28]:


fig1 = model.plot_components(forecast)
Up next NVDA I’m going to sue all of you
Latest posts GBPUSD Flash Crash to 1.04 In the End, Climate Change Is the Only Story That Matters The Colorado River Is Dying. Lawns Are Dumb SPX Inflation Check - Energy Prices Keep Going Lower Inflation Check - Soft Commodity Prices Starting To Break Inflation Check - Meat Prices Starting To Break Inflation Check - Grain Prices Starting To Break It’s either to the moon or wendys More Bosses Are Spying on Quiet Quitters TensorStore for High-Performance, Scalable Array Storage recent sale 1875s united states trade dollar recent sales 187477 counterfeit united states CEO Pichai fields questions on cost cuts at all hands meeting" /> CO2 than the entire Western hemisphere" /> #Design Patterns for Deep Learning Architectures Quantitative Easing and Rates meme Selling AI KB Homes and Real Estate Reckoning GME - What A Hunk Of Crap GILT Weekly Chart Heating Oil Keeps Dropping - Thank Goodness! SPY This is why I don't sell Great Wall of China Locations FMS, an agent based Financial Market Simulator — FMS 0. The Fed forecasts hiking rates as high as 4pt6pct before ending inflation fight JPOW must be house shopping" /> I’m going to sue all of you Forecasting Medium Subscriber Count With Facebook Prophet