r"""Display scikit-learn feature importances. Parameters ---------- model : alphapy.Model The model object with plotting specifications. partition : alphapy.Partition Reference to the dataset. Returns ------- None : None References ----------
(model, partition)
| 365 | # |
| 366 | |
| 367 | def plot_importance(model, partition): |
| 368 | r"""Display scikit-learn feature importances. |
| 369 | |
| 370 | Parameters |
| 371 | ---------- |
| 372 | model : alphapy.Model |
| 373 | The model object with plotting specifications. |
| 374 | partition : alphapy.Partition |
| 375 | Reference to the dataset. |
| 376 | |
| 377 | Returns |
| 378 | ------- |
| 379 | None : None |
| 380 | |
| 381 | References |
| 382 | ---------- |
| 383 | |
| 384 | http://scikit-learn.org/stable/auto_examples/ensemble/plot_forest_importances.html |
| 385 | |
| 386 | """ |
| 387 | |
| 388 | logger.info("Generating Feature Importance Plots") |
| 389 | plot_dir = get_plot_directory(model) |
| 390 | pstring = datasets[partition] |
| 391 | |
| 392 | # For each algorithm that has importances, generate the plot. |
| 393 | |
| 394 | n_top = 20 |
| 395 | |
| 396 | for algo in model.algolist: |
| 397 | logger.info("Feature Importances for Algorithm: %s", algo) |
| 398 | try: |
| 399 | # get feature importances |
| 400 | importances = np.array(model.importances[algo]) |
| 401 | imp_flag = True |
| 402 | except: |
| 403 | imp_flag = False |
| 404 | if imp_flag: |
| 405 | # sort the importances by index |
| 406 | indices = np.argsort(importances)[::-1] |
| 407 | # get feature names |
| 408 | feature_names = np.array(model.fnames_algo[algo]) |
| 409 | n_features = len(feature_names) |
| 410 | # log the feature ranking |
| 411 | logger.info("Feature Ranking:") |
| 412 | n_min = min(n_top, n_features) |
| 413 | for i in range(n_min): |
| 414 | logger.info("%d. %s (%f)" % (i + 1, |
| 415 | feature_names[indices[i]], |
| 416 | importances[indices[i]])) |
| 417 | # plot the feature importances |
| 418 | title = BSEP.join([algo, "Feature Importances [", pstring, "]"]) |
| 419 | plt.figure() |
| 420 | plt.title(title) |
| 421 | plt.barh(range(n_min), importances[indices][:n_min][::-1]) |
| 422 | plt.yticks(range(n_min), feature_names[indices][:n_min][::-1]) |
| 423 | plt.ylim([-1, n_min]) |
| 424 | plt.xlabel('Relative Importance') |
no test coverage detected