Meta-features from a model

In this example, we will show you how to extract meta-features from a pre-fitted model.

# Load a dataset
import sklearn.tree
from sklearn.datasets import load_iris
from pymfe.mfe import MFE

iris = load_iris()

If you want to extract metafeatures from a pre-fitted machine learning model (from sklearn package), you can use the extract_from_model method without needing to use the training data:

# Extract from model

model = sklearn.tree.DecisionTreeClassifier().fit(iris.data, iris.target)
extractor = MFE()
ft = extractor.extract_from_model(model)
print("\n".join("{:50} {:30}".format(x, y) for x, y in zip(ft[0], ft[1])))

# Extract specific metafeatures from model
extractor = MFE(features=["tree_shape", "nodes_repeated"], summary="histogram")

ft = extractor.extract_from_model(
    model,
    arguments_fit={"verbose": 1},
    arguments_extract={"verbose": 1, "histogram": {"bins": 5}})

print("\n".join("{:50} {:30}".format(x, y) for x, y in zip(ft[0], ft[1])))
leaves                                                                          9
leaves_branch.mean                                             3.7777777777777777
leaves_branch.sd                                               1.2018504251546631
leaves_corrob.mean                                             0.1111111111111111
leaves_corrob.sd                                              0.15051762539834182
leaves_homo.mean                                                37.46666666666667
leaves_homo.sd                                                 13.142298124757328
leaves_per_class.mean                                          0.3333333333333333
leaves_per_class.sd                                           0.22222222222222224
nodes                                                                           8
nodes_per_attr                                                                2.0
nodes_per_inst                                                0.05333333333333334
nodes_per_level.mean                                                          1.6
nodes_per_level.sd                                             0.8944271909999159
nodes_repeated.mean                                                           2.0
nodes_repeated.sd                                              1.1547005383792515
tree_depth.mean                                                3.0588235294117645
tree_depth.sd                                                  1.4348601079588785
tree_imbalance.mean                                           0.19491705385114738
tree_imbalance.sd                                             0.13300709991513865
tree_shape.mean                                                0.2708333333333333
tree_shape.sd                                                 0.10711960313126631
var_importance.mean                                                          0.25
var_importance.sd                                             0.27823897162264016

  0%|          | 0/1 [00:00<?, ?it/s]
100%|##########| 1/1 [00:00<00:00, 6017.65it/s]

Process of precomputation finished.

  0%|          | 0/2 [00:00<?, ?it/s]
100%|##########| 2/2 [00:00<00:00, 3238.84it/s]

Process of metafeature extraction finished.
nodes_repeated.histogram.0                                                    0.5
nodes_repeated.histogram.1                                                    0.0
nodes_repeated.histogram.2                                                    0.0
nodes_repeated.histogram.3                                                    0.0
nodes_repeated.histogram.4                                                    0.5
tree_shape.histogram.0                                         0.2222222222222222
tree_shape.histogram.1                                         0.5555555555555556
tree_shape.histogram.2                                                        0.0
tree_shape.histogram.3                                         0.1111111111111111
tree_shape.histogram.4                                         0.1111111111111111

Total running time of the script: ( 0 minutes 0.013 seconds)

Gallery generated by Sphinx-Gallery