索引4超出大小为4的轴1的界限

2024-06-27 09:45:41 发布

您现在位置:Python中文网/ 问答频道 /正文

import numpy as np

survey1 = [[0, 1, 0, 1],
           [0, 1, 0, 0],
           [1, 0, 0, 1],
           [0, 1, 0, 1],
           [1, 1, 0, 1]]
survey2 = [[1, 0, 1, 1, 0, 1, 1, 1, 1, 1],
           [0, 0, 0, 0, 1, 1, 1, 0, 0, 1],
           [1, 0, 1, 1, 1, 0, 1, 0, 1, 1],
           [1, 1, 0, 1, 1, 0, 0, 1, 1, 1],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
           [1, 0, 1, 1, 1, 1, 1, 0, 1, 1],
           [1, 0, 0, 0, 0, 1, 0, 1, 1, 0],
           [0, 1, 1, 1, 1, 1, 1, 0, 1, 1],
           [0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
           [1, 0, 0, 1, 1, 1, 1, 0, 1, 1],
           [0, 0, 1, 1, 1, 1, 0, 1, 1, 1],
           [0, 0, 1, 1, 1, 1, 1, 0, 1, 1],
           [0, 0, 1, 1, 0, 1, 0, 1, 1, 0],
           [1, 1, 0, 1, 1, 0, 1, 1, 1, 0],
           [0, 0, 0, 1, 0, 1, 0, 0, 0, 1],
           [0, 1, 0, 1, 0, 0, 1, 0, 1, 0],
           [0, 1, 1, 1, 0, 1, 0, 0, 0, 1],
           [0, 1, 0, 1, 0, 1, 1, 1, 1, 1],
           [1, 0, 1, 1, 1, 1, 1, 0, 0, 0],
           [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]]


def makeArray(surveyAnswers):
    array = np.array(surveyAnswers)
    return array

def calculatePercentage(surveyAnswers):
    shapeSurvey = np.shape(makeArray(surveyAnswers))
    index_counter = 0
    answerArray = makeArray(surveyAnswers)
    while index_counter <= shapeSurvey[0]:
        currentBrand = answerArray[:,index_counter]
        print(currentBrand)
        index_counter += 1


calculatePercentage(survey1)

嗨,我必须遍历数组中的每一列,我使用一个计数器,这样我就可以在接下来的每一列中进行循环。但我收到一条错误消息,它指出,对于大小为4的轴1,索引超出了范围。我是新手请帮忙。谢谢您。在


Tags: importnumpyindexdefasnpcounterarray