Skip to content

Instantly share code, notes, and snippets.

@se7enack
Last active February 11, 2024 21:25
Show Gist options
  • Save se7enack/fa5db1322f40b9ee5b319518c985ca17 to your computer and use it in GitHub Desktop.
Save se7enack/fa5db1322f40b9ee5b319518c985ca17 to your computer and use it in GitHub Desktop.
Example of using Plotly to predict trends
#!/usr/local/bin/env python3
import pandas as pd
from prophet import Prophet
from prophet.plot import plot_plotly, plot_components_plotly
import ssl
ssl._create_default_https_context = ssl._create_stdlib_context
# Load the dataset
df = pd.read_csv('https://raw.githubusercontent.com/se7enack/randomfiles/main/btc.csv')
df = df.rename(columns={'Date': 'ds', 'Price': 'y'})
df['ds'] = pd.to_datetime(df['ds'])
# Initialize and fit the Prophet model
m = Prophet()
m.fit(df)
# Make predictions
future = m.make_future_dataframe(periods=120, freq='M')
forecast = m.predict(future)
# # Plot the forecast components
plot1 = plot_plotly(m, forecast)
plot1.show()
plot2 = plot_components_plotly(m, forecast)
plot2.show()
#!/usr/local/bin/env python3
import pandas as pd
from prophet import Prophet
from prophet.plot import plot_plotly
import ssl
ssl._create_default_https_context = ssl._create_stdlib_context
df = pd.read_csv('https://raw.githubusercontent.com/se7enack/randomfiles/main/br.csv')
df = df.rename(columns={'Year': 'ds', 'Birthrate Per 1000 People': 'y'})
df['ds'] = pd.to_datetime(df['ds'], format='%Y')
m = Prophet()
m.fit(df)
# Make some predictions out 100 years
future = m.make_future_dataframe(periods=100, freq='YE')
forecast = m.predict(future)
fig1 = plot_plotly(m, forecast)
fig1.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment