Module: Prophet::Plot
- Included in:
- Forecaster
- Defined in:
- lib/prophet/plot.rb
Class Method Summary collapse
- .plot_cross_validation_metric(df_cv, metric:, rolling_window: 0.1, ax: nil, figsize: [10, 6], color: "b", point_color: "gray") ⇒ Object
- .plt ⇒ Object
Instance Method Summary collapse
-
#add_changepoints_to_plot(ax, fcst, threshold: 0.01, cp_color: "r", cp_linestyle: "--", trend: true) ⇒ Object
in Python, this is a separate method.
- #plot(fcst, ax: nil, uncertainty: true, plot_cap: true, xlabel: "ds", ylabel: "y", figsize: [10, 6]) ⇒ Object
- #plot_components(fcst, uncertainty: true, plot_cap: true, weekly_start: 0, yearly_start: 0, figsize: nil) ⇒ Object
Class Method Details
.plot_cross_validation_metric(df_cv, metric:, rolling_window: 0.1, ax: nil, figsize: [10, 6], color: "b", point_color: "gray") ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/prophet/plot.rb', line 114 def self.plot_cross_validation_metric(df_cv, metric:, rolling_window: 0.1, ax: nil, figsize: [10, 6], color: "b", point_color: "gray") if ax.nil? fig = plt.figure(facecolor: "w", figsize: figsize) ax = fig.add_subplot(111) else fig = ax.get_figure end # Get the metric at the level of individual predictions, and with the rolling window. df_none = Diagnostics.performance_metrics(df_cv, metrics: [metric], rolling_window: -1) df_h = Diagnostics.performance_metrics(df_cv, metrics: [metric], rolling_window: rolling_window) # Some work because matplotlib does not handle timedelta # Target ~10 ticks. tick_w = df_none["horizon"].max * 1e9 / 10.0 # Find the largest time resolution that has <1 unit per bin. dts = ["D", "h", "m", "s", "ms", "us", "ns"] dt_names = ["days", "hours", "minutes", "seconds", "milliseconds", "microseconds", "nanoseconds"] dt_conversions = [ 24 * 60 * 60 * 10 ** 9, 60 * 60 * 10 ** 9, 60 * 10 ** 9, 10 ** 9, 10 ** 6, 10 ** 3, 1.0 ] # TODO update i = 0 # dts.each_with_index do |dt, i| # if np.timedelta64(1, dt) < np.timedelta64(tick_w, "ns") # break # end # end x_plt = df_none["horizon"] * 1e9 / dt_conversions[i].to_f x_plt_h = df_h["horizon"] * 1e9 / dt_conversions[i].to_f ax.plot(x_plt.to_a, df_none[metric].to_a, ".", alpha: 0.1, c: point_color) ax.plot(x_plt_h.to_a, df_h[metric].to_a, "-", c: color) ax.grid(true) ax.set_xlabel("Horizon (#{dt_names[i]})") ax.set_ylabel(metric) fig end |
.plt ⇒ Object
160 161 162 163 164 165 166 167 |
# File 'lib/prophet/plot.rb', line 160 def self.plt begin require "matplotlib/pyplot" rescue LoadError raise Error, "Install the matplotlib gem for plots" end Matplotlib::Pyplot end |
Instance Method Details
#add_changepoints_to_plot(ax, fcst, threshold: 0.01, cp_color: "r", cp_linestyle: "--", trend: true) ⇒ Object
in Python, this is a separate method
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/prophet/plot.rb', line 97 def add_changepoints_to_plot(ax, fcst, threshold: 0.01, cp_color: "r", cp_linestyle: "--", trend: true) artists = [] if trend artists << ax.plot(to_pydatetime(fcst["ds"]), fcst["trend"].to_a, c: cp_color) end signif_changepoints = if @changepoints.size > 0 (@params["delta"].mean(axis: 0, nan: true).abs >= threshold).mask(@changepoints.to_numo) else [] end to_pydatetime(signif_changepoints).each do |cp| artists << ax.axvline(x: cp, c: cp_color, ls: cp_linestyle) end artists end |
#plot(fcst, ax: nil, uncertainty: true, plot_cap: true, xlabel: "ds", ylabel: "y", figsize: [10, 6]) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/prophet/plot.rb', line 3 def plot(fcst, ax: nil, uncertainty: true, plot_cap: true, xlabel: "ds", ylabel: "y", figsize: [10, 6]) if ax.nil? fig = plt.figure(facecolor: "w", figsize: figsize) ax = fig.add_subplot(111) else fig = ax.get_figure end fcst_t = to_pydatetime(fcst["ds"]) ax.plot(to_pydatetime(@history["ds"]), @history["y"].to_a, "k.") ax.plot(fcst_t, fcst["yhat"].to_a, ls: "-", c: "#0072B2") if fcst.include?("cap") && plot_cap ax.plot(fcst_t, fcst["cap"].to_a, ls: "--", c: "k") end if @logistic_floor && fcst.include?("floor") && plot_cap ax.plot(fcst_t, fcst["floor"].to_a, ls: "--", c: "k") end if uncertainty && @uncertainty_samples ax.fill_between(fcst_t, fcst["yhat_lower"].to_a, fcst["yhat_upper"].to_a, color: "#0072B2", alpha: 0.2) end # Specify formatting to workaround matplotlib issue #12925 locator = dates.AutoDateLocator.new(interval_multiples: false) formatter = dates.AutoDateFormatter.new(locator) ax.xaxis.set_major_locator(locator) ax.xaxis.set_major_formatter(formatter) ax.grid(true, which: "major", c: "gray", ls: "-", lw: 1, alpha: 0.2) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) fig.tight_layout fig end |
#plot_components(fcst, uncertainty: true, plot_cap: true, weekly_start: 0, yearly_start: 0, figsize: nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/prophet/plot.rb', line 34 def plot_components(fcst, uncertainty: true, plot_cap: true, weekly_start: 0, yearly_start: 0, figsize: nil) components = ["trend"] if @train_holiday_names && fcst.include?("holidays") components << "holidays" end # Plot weekly seasonality, if present if @seasonalities["weekly"] && fcst.include?("weekly") components << "weekly" end # Yearly if present if @seasonalities["yearly"] && fcst.include?("yearly") components << "yearly" end # Other seasonalities components.concat(@seasonalities.keys.select { |name| fcst.include?(name) && !["weekly", "yearly"].include?(name) }.sort) regressors = {"additive" => false, "multiplicative" => false} @extra_regressors.each do |name, props| regressors[props[:mode]] = true end ["additive", "multiplicative"].each do |mode| if regressors[mode] && fcst.include?("extra_regressors_#{mode}") components << "extra_regressors_#{mode}" end end npanel = components.size figsize = figsize || [9, 3 * npanel] fig, axes = plt.subplots(npanel, 1, facecolor: "w", figsize: figsize) if npanel == 1 axes = [axes] end multiplicative_axes = [] axes.tolist.zip(components) do |ax, plot_name| if plot_name == "trend" plot_forecast_component(fcst, "trend", ax: ax, uncertainty: uncertainty, plot_cap: plot_cap) elsif @seasonalities[plot_name] if plot_name == "weekly" || @seasonalities[plot_name][:period] == 7 plot_weekly(name: plot_name, ax: ax, uncertainty: uncertainty, weekly_start: weekly_start) elsif plot_name == "yearly" || @seasonalities[plot_name][:period] == 365.25 plot_yearly(name: plot_name, ax: ax, uncertainty: uncertainty, yearly_start: yearly_start) else plot_seasonality(name: plot_name, ax: ax, uncertainty: uncertainty) end elsif ["holidays", "extra_regressors_additive", "extra_regressors_multiplicative"].include?(plot_name) plot_forecast_component(fcst, plot_name, ax: ax, uncertainty: uncertainty, plot_cap: false) end if @component_modes["multiplicative"].include?(plot_name) multiplicative_axes << ax end end fig.tight_layout # Reset multiplicative axes labels after tight_layout adjustment multiplicative_axes.each do |ax| ax = set_y_as_percent(ax) end fig end |