Last active
February 11, 2024 21:25
-
-
Save se7enack/fa5db1322f40b9ee5b319518c985ca17 to your computer and use it in GitHub Desktop.
Example of using Plotly to predict trends
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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