Bar
model_feature_importances(X_test, model, title='Feature importance', horizontal=True, size=None, show=True, write_html_path=None)
¶
Creates a bar chart with the feature importance of a model.
Example: ```python from blitzly.plots.bar import model_feature_importance import pandas as pd from sklearn.ensemble import RandomForestClassifier from sklearn.datasets import make_classification from sklearn.model_selection import train_test_split
X, y = make_classification( n_samples=100, n_features=4, n_informative=2, n_redundant=0, random_state=42, shuffle=False, )
X = pd.DataFrame(X, columns=["foo", "bar", "blitz", "licht"]) y = pd.Series(y)
X_train, X_test, y_train, _ = train_test_split(X, y)
model = RandomForestClassifier() model.fit(X_train, y_train)
model_feature_importances(X_test, model)
Args:
X_test (pd.DataFrame): The test data for the model. You can also use the train
data but it is recommend to use test
.
model (Any): The model to get the feature importance from. The model must have a feature_importances_
attribute!
title (Optional[str]): The title of the plot. Defaults to "Feature importance".
horizontal (bool): Whether to plot the bar chart horizontally or vertically.
size (Optional[Tuple[int, int]]): The size of the plot.
show (bool): Whether to show the plot.
write_html_path (Optional[str]): The path to write the plot as HTML.
Raises:
AttributeError: If the model does not have a feature_importances_
attribute.
Returns: BaseFigure: The plotly figure.
Source code in blitzly/plots/bar.py
multi_bar(data, group_labels, x_labels, mark_x_labels=None, mark_x_label_color='crimson', title='Bar chart', stack=False, text_position='none', hover_texts=None, errors=None, size=None, show_legend=True, show=True, write_html_path=None)
¶
Creates a bar chart with multiple groups. Each group is represented by a
bar. The bars are grouped by the x-axis. The number of group_labels
must
be equal to the number of rows in the data. The number of x_labels
must
be equal to the number of columns in the data.
Example:
from blitzly.plots.bar import multi_bar
import numpy as np
data=np.array([[1, 2, 3], [4, 5, 6]])
errors=np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]),
multi_bar(
data,
x_labels=["X1", "X2", "X3"],
group_labels=["Z1", "Z2"],
hover_texts=["foo", "bar", "blitzly"],
errors=errors
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Union[pd.DataFrame, pd.Series, NDArray]
|
The data to plot. |
required |
group_labels |
List[str]
|
The labels for the groups. |
required |
x_labels |
List[str]
|
The labels for the x-axis. |
required |
mark_x_labels |
Optional[List[str]]
|
The bars of |
None
|
mark_x_label_color |
str
|
The color of the marked bars. |
'crimson'
|
title |
Optional[str]
|
The title of the bar chart. |
'Bar chart'
|
stack |
Optional[bool]
|
Whether to stack the bars. Values are summed up by columns. By default, the bars are grouped. Stacked bars don't support errors. If provided, they will be ignored. |
False
|
text_position |
Optional[str]
|
The position of the text. Can be "auto", "inside", "outside", "none". |
'none'
|
hover_texts |
Optional[List[str]]
|
The hover texts for the data. |
None
|
errors |
Optional[Union[pd.DataFrame, pd.Series, NDArray]]
|
The errors for the data. |
None
|
size |
Optional[Tuple[int, int]
|
Size of the plot. |
None
|
show_legend |
Optional[bool]
|
Whether to show the legend. |
True
|
show |
bool
|
Whether to show the figure. |
True
|
write_html_path |
Optional[str]
|
The path to which the histogram should be written as an HTML file. If None, the histogram will not be saved. |
None
|
Returns:
Name | Type | Description |
---|---|---|
BaseFigure |
BaseFigure
|
The figure. |
Source code in blitzly/plots/bar.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
|