def get_feature_importances(self, relative: bool = True) -> AxPlotConfig: """ Get a bar chart showing feature_importances for a metric. A drop-down controls the metric for which the importances are displayed. Args: relative: Whether the values are displayed as percentiles or as raw importance metrics. """ if not self.experiment.trials: raise ValueError("Cannot generate plot as there are no trials.") cur_model = self.generation_strategy.model if cur_model is not None: try: return plot_feature_importance_by_feature(cur_model, relative=relative) except NotImplementedError: logger.info( f"Model {self.generation_strategy.model} does not implement " "`feature_importances`, so it cannot be used to generate " "this plot. Only certain models, specifically GPEI, implement " "feature importances." ) raise ValueError( "Could not obtain feature_importances for any metrics " " as a model that can produce feature importances, such as a " "Gaussian Process, has not yet been trained in the course " "of this optimization." )
def testFeatureImportances(self): model = get_modelbridge() # Assert that each type of plot can be constructed successfully plot = plot_feature_importance_by_feature(model) self.assertIsInstance(plot, AxPlotConfig) plot = plot_feature_importance_by_metric(model) self.assertIsInstance(plot, AxPlotConfig) plot = plot_relative_feature_importance(model) self.assertIsInstance(plot, AxPlotConfig)
def testFeatureImportances(self): model = get_modelbridge() # Assert that each type of plot can be constructed successfully plot = plot_feature_importance_by_feature_plotly(model=model) self.assertIsInstance(plot, go.Figure) plot = plot_feature_importance_by_feature_plotly(model=model, caption=DUMMY_CAPTION) self.assertIsInstance(plot, go.Figure) self.assertEqual(len(plot.layout.annotations), 1) self.assertEqual(plot.layout.annotations[0].text, DUMMY_CAPTION) plot = plot_feature_importance_by_feature(model=model) self.assertIsInstance(plot, AxPlotConfig) plot = plot_feature_importance_by_metric_plotly(model=model) self.assertIsInstance(plot, go.Figure) plot = plot_feature_importance_by_metric(model=model) self.assertIsInstance(plot, AxPlotConfig) plot = plot_relative_feature_importance_plotly(model=model) self.assertIsInstance(plot, go.Figure) plot = plot_relative_feature_importance(model=model) self.assertIsInstance(plot, AxPlotConfig)