Add plot_residuals
#539
Merged
Add plot_residuals
#539
Conversation
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
# Conflicts: # etna/analysis/plotters.py
Example of usage: import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from etna.pipeline import Pipeline
from etna.metrics import MAE
from etna.models import LinearPerSegmentModel
from etna.transforms import LagTransform
from etna.datasets import TSDataset
from etna.analysis import plot_residuals, plot_backtest
def multitrend_df() -> pd.DataFrame:
"""Generate one segment pd.DataFrame with multiple linear trend."""
df = pd.DataFrame({"timestamp": pd.date_range("2020-01-01", "2021-05-31")})
ns = [100, 150, 80, 187]
ks = [0.4, -0.3, 0.8, -0.6]
x = np.zeros(shape=(len(df)))
left = 0
right = 0
for i, (n, k) in enumerate(zip(ns, ks)):
right += n
x[left:right] = np.arange(0, n, 1) * k
for _n, _k in zip(ns[:i], ks[:i]):
x[left:right] += _n * _k
left = right
df["target"] = x
df["segment"] = "segment_1"
df = TSDataset.to_dataset(df=df)
return df
df = multitrend_df()
df.loc[:, pd.IndexSlice["segment_2", "target"]] = np.log(df.loc[:, pd.IndexSlice["segment_1", "target"]] + 100)
df.loc[:, pd.IndexSlice["segment_3", "target"]] = np.log(df.loc[:, pd.IndexSlice["segment_1", "target"]] + 1000)
ts = TSDataset(df=df, freq="D")
transforms = [LagTransform(in_column="target", out_column="lags", lags=[5, 6, 7, 8, 9, 10])]
pipeline = Pipeline(model=LinearPerSegmentModel(), transforms=transforms, horizon=5)
metrics, forecast_df, info = pipeline.backtest(ts=ts, metrics=[MAE()], n_folds=5)
plot_backtest(forecast_df=forecast_df, ts=ts)
plt.savefig("backtest.png")
plot_residuals(forecast_df=forecast_df, ts=ts, feature="timestamp")
plt.savefig("image_timestamp.png")
plot_residuals(forecast_df=forecast_df, ts=ts, transforms=transforms, feature="lags_5")
plt.savefig("image_lags.png") |
martins0n
previously approved these changes
Feb 18, 2022
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
martins0n
approved these changes
Feb 18, 2022
Codecov Report
@@ Coverage Diff @@
## master #539 +/- ##
===========================================
- Coverage 87.17% 54.30% -32.87%
===========================================
Files 118 118
Lines 5777 5797 +20
===========================================
- Hits 5036 3148 -1888
- Misses 741 2649 +1908
Continue to review full report at Codecov.
|
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
IMPORTANT: Please do not create a Pull Request without creating an issue first.
Before submitting (must do checklist)
Type of Change
Proposed Changes
Look #514.
Related Issue
#514.
Closing issues
Closes #514.