plot_feature_importances with examples#
An example showing the plot_feature_importances
function
used by a scikit-learn classifier.
9 # Authors: The scikit-plots developers
10 # SPDX-License-Identifier: BSD-3-Clause
Import scikit-plots#
16 from sklearn.datasets import (
17 load_iris as data_3_classes,
18 )
19 from sklearn.ensemble import RandomForestClassifier
20 from sklearn.model_selection import train_test_split
21
22 import numpy as np
23
24 np.random.seed(0) # reproducibility
25 # importing pylab or pyplot
26 import matplotlib.pyplot as plt
27
28 # Import scikit-plot
29 import scikitplot as sp
Loading the dataset#
35 # Load the data
36 X, y = data_3_classes(return_X_y=True, as_frame=False)
37 X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.5, random_state=0)
Model Training#
43 # Create an instance of the LogisticRegression
44 model = RandomForestClassifier(random_state=0).fit(X_train, y_train)
Plot!#
50 # Plot!
51 ax, features = sp.estimators.plot_feature_importances(
52 model,
53 feature_names=["petal length", "petal width", "sepal length", "sepal width"],
54 save_fig=True,
55 save_fig_filename="",
56 # overwrite=True,
57 add_timestamp=True,
58 # verbose=True,
59 )

References
The use of the following functions, methods, classes and modules is shown in this example:
Total running time of the script: (0 minutes 0.398 seconds)
Related examples