在Confusion矩阵中获取列表索引超出范围错误

时间:2016-05-11 06:37:09

标签: python-2.7 pygame confusion-matrix

import pygame
import numpy
import pickle
import pdb
import sys


def printConfMatrix(grid,num):
    # Define some colors
    BLACK    = (   0,   0,   0)
    WHITE    = ( 255, 255, 255)
    GREEN    = (   0, 255,   0)
    RED      = ( 255,   0,   0)

    # This sets the width and height of each grid location
    width  = 20
    height = 20

    # This sets the margin between each cell
    margin = 5

    # Initialize pygame
    pygame.init()

    # Set the height and width of the screen
    size = [255, 255]
    screen = pygame.display.set_mode(size)

    # Set title of screen
    pygame.display.set_caption("Array Backed Grid")

    # Set the screen background
    screen.fill(WHITE)

    # Draw the grid
    for row in range(10):
        for column in range(10):
            color = (WHITE[0]-grid[row,column]*10,WHITE[1]-grid[row,column]*10,WHITE[2]-grid[row,column]*10)
            pygame.draw.rect(screen,
                             color,
                             [(margin+width)*column+margin,
                              (margin+height)*row+margin,
                              width,
                              height])


    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()
    pygame.image.save(screen, "confusionMatrix" + num + ".jpg")
    #pdb.set_trace()
    # Be IDLE friendly. If you forget this line, the program will 'hang'
    # on exit.
    #pygame.quit()


f = open("confusion" + sys.argv[1] + ".pickle",'r')
grid = pickle.load(f)

printConfMatrix(grid,sys.argv[1])

此文件出现如下错误,

c:\Python27\Scripts>python D:\pr\final_project\code\confusion.py
Traceback (most recent call last):
  File "D:\pr\final_project\code\confusion.py", line 68, in <module>
    f = open("confusion" + sys.argv[1] + ".pickle",'r')
IndexError: list index out of range

1 个答案:

答案 0 :(得分:0)

IndexError表示您正在尝试访问不在列表中的索引。在您的情况下,列表是sys.argv ---命令行参数列表。

要消除错误,请使用单个参数调用脚本,该参数是pickle confusion矩阵的文件名后缀。例如。如果矩阵位于名为confusion_final.pickle的文件中,则应运行

python D:\pr\final_project\code\confusion.py final_