我有下面的功能代码和示例数据,即抛出以下错误。该代码用于创建图表以评估三种不同类型模型的训练时间,准确度和fscore。我正在尝试修改vs_evaluate函数,因此它只显示训练时间和准确度的图,但我得到的索引超出界限误差。我已经注释了功能的几个部分,将子图从2,3更改为2,2并从枚举中删除了与fscore相关的条目。谁能看到导致问题的原因并建议如何解决?
数据:
print(results)
{'LinearSVC': {0: {'train_time': 0.0013530254364013672, 'acc_train': 0.69999999999999996, 'pred_time': 0.0007898807525634766, 'acc_test': 0.73076923076923073}, 1: {'train_time': 0.001964092254638672, 'acc_train': 0.90000000000000002, 'pred_time': 0.0012090206146240234, 'acc_test': 0.65384615384615385}, 2: {'train_time': 0.005677938461303711, 'acc_train': 0.69999999999999996, 'pred_time': 0.000701904296875, 'acc_test': 0.66153846153846152}}, 'LogisticRegression': {0: {'train_time': 0.0014522075653076172, 'acc_train': 0.69999999999999996, 'pred_time': 0.0009410381317138672, 'acc_test': 0.73076923076923073}, 1: {'train_time': 0.0015349388122558594, 'acc_train': 0.80000000000000004, 'pred_time': 0.0006539821624755859, 'acc_test': 0.69230769230769229}, 2: {'train_time': 0.0023081302642822266, 'acc_train': 0.69999999999999996, 'pred_time': 0.0006170272827148438, 'acc_test': 0.68461538461538463}}, 'AdaBoostClassifier': {0: {'train_time': 0.005421876907348633, 'acc_train': 0.59999999999999998, 'pred_time': 0.0018930435180664062, 'acc_test': 0.69230769230769229}, 1: {'train_time': 0.17421698570251465, 'acc_train': 0.90000000000000002, 'pred_time': 0.014720916748046875, 'acc_test': 0.61538461538461542}, 2: {'train_time': 0.14342188835144043, 'acc_train': 0.59999999999999998, 'pred_time': 0.012198925018310547, 'acc_test': 0.67692307692307696}}}
错误:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-113-76d9833e28b9> in <module>()
25
26 # Run metrics visualization for the three supervised learning models chosen
---> 27 vs_evaluate(results, accuracy) #, fscore)
<ipython-input-112-11d513962c2a> in vs_evaluate(results, accuracy)
45
46 # Creative plot code
---> 47 ax[j/3, j%3].bar(i+k*bar_width, results[learner][i][metric], width = bar_width, color = colors[k])
48 ax[j/3, j%3].set_xticks([0.45, 1.45, 2.45])
49 ax[j/3, j%3].set_xticklabels(["1%", "10%", "100%"])
IndexError: index 2 is out of bounds for axis 1 with size 2
通话功能:
vs_evaluate(results, accuracy)
代码:
# Function for evaluating model performance
###########################################
# Suppress matplotlib user warnings
# Necessary for newer version of matplotlib
import warnings
warnings.filterwarnings("ignore", category = UserWarning, module = "matplotlib")
#
# Display inline matplotlib plots with IPython
from IPython import get_ipython
get_ipython().run_line_magic('matplotlib', 'inline')
###########################################
import matplotlib.pyplot as pl
import matplotlib.patches as mpatches
import numpy as np
import pandas as pd
from time import time
from sklearn.metrics import accuracy_score
def vs_evaluate(results, accuracy):
"""
Visualization code to display results of various learners.
inputs:
- learners: a list of supervised learners
- stats: a list of dictionaries of the statistic results from 'train_predict()'
- accuracy: The score for the naive predictor
- f1: The score for the naive predictor
"""
# Create figure
fig, ax = pl.subplots(2, 2, figsize = (11,7))
# Constants
bar_width = 0.3
colors = ['#A00000','#00A0A0','#00A000']
# Super loop to plot four panels of data
for k, learner in enumerate(results.keys()):
for j, metric in enumerate(['train_time', 'acc_train', 'pred_time', 'acc_test']):
for i in np.arange(3):
# Creative plot code
ax[j/3, j%3].bar(i+k*bar_width, results[learner][i][metric], width = bar_width, color = colors[k])
ax[j/3, j%3].set_xticks([0.45, 1.45, 2.45])
ax[j/3, j%3].set_xticklabels(["1%", "10%", "100%"])
ax[j/3, j%3].set_xlabel("Training Set Size")
ax[j/3, j%3].set_xlim((-0.1, 3.0))
# Add unique y-labels
ax[0, 0].set_ylabel("Time (in seconds)")
ax[0, 1].set_ylabel("Accuracy Score")
#ax[0, 2].set_ylabel("F-score")
ax[1, 0].set_ylabel("Time (in seconds)")
ax[1, 1].set_ylabel("Accuracy Score")
#ax[1, 2].set_ylabel("F-score")
# Add titles
ax[0, 0].set_title("Model Training")
ax[0, 1].set_title("Accuracy Score on Training Subset")
#ax[0, 2].set_title("F-score on Training Subset")
ax[1, 0].set_title("Model Predicting")
ax[1, 1].set_title("Accuracy Score on Testing Set")
#ax[1, 2].set_title("F-score on Testing Set")
# Add horizontal lines for naive predictors
ax[0, 1].axhline(y = accuracy, xmin = -0.1, xmax = 3.0, linewidth = 1, color = 'k', linestyle = 'dashed')
ax[1, 1].axhline(y = accuracy, xmin = -0.1, xmax = 3.0, linewidth = 1, color = 'k', linestyle = 'dashed')
#ax[0, 2].axhline(y = f1, xmin = -0.1, xmax = 3.0, linewidth = 1, color = 'k', linestyle = 'dashed')
#ax[1, 2].axhline(y = f1, xmin = -0.1, xmax = 3.0, linewidth = 1, color = 'k', linestyle = 'dashed')
# Set y-limits for score panels
ax[0, 1].set_ylim((0, 1))
#ax[0, 2].set_ylim((0, 1))
ax[1, 1].set_ylim((0, 1))
#ax[1, 2].set_ylim((0, 1))
# Create patches for the legend
patches = []
for i, learner in enumerate(results.keys()):
patches.append(mpatches.Patch(color = colors[i], label = learner))
pl.legend(handles = patches, bbox_to_anchor = (-.80, 2.53), \
loc = 'upper center', borderaxespad = 0., ncol = 3, fontsize = 'x-large')
# Aesthetics
pl.suptitle("Performance Metrics for Three Supervised Learning Models", fontsize = 16, y = 1.10)
pl.tight_layout()
pl.show()
答案 0 :(得分:0)
更改3到2解决了错误。例如ax [j / 2,j%2]
答案 1 :(得分:0)
这是你的错误: 你有2个2子图。当j从0循环到3时,您将获得以下索引,
ax[j/3,j%3] =
ax[0,0] #j = 0
ax[0,1] #j = 1
ax[0,2] #j = 2 #Problem is here
ax[1,0] #j = 3
相反,你可以这样做:
ax[0 if j in [0,1] else 1,0 if j in [0,2] else 1] =
ax[0,0] #j = 0
ax[0,1] #j = 1
ax[1,0] #j = 2
ax[1,1] #j = 3