【问题标题】:IndexError: index 0 is out of bounds for axis 0 with size 0IndexError:索引 0 超出轴 0 的范围,大小为 0
【发布时间】:2016-04-03 19:58:58
【问题描述】:

我正在尝试编写一个代码来划分以二进制形式给出的两个多项式(分别为 newdata 和 proofin)。然而,当我运行代码时,我得到:

IndexError: index 0 is out of bounds for axis 0 with size 0

这是代码:

import numpy as np


def transformation_for_numpy_of_o():
    newdata = ("101001")
    freshdata = list(newdata)
    freshdatapoly = []

    for n in freshdata:
        if n == 1:
            freshdatapoly.append(1.0)
        if n == 0:
            freshdatapoly.append(0.0)

    freshdatapoly = np.array(freshdatapoly)

    return freshdatapoly


def transformation_for_numpy_of_proof():
    proofin = ("101001")
    proofing = list(proofin)
    proofpoly = []

    for n in proofing:
        if n == 1:
            proofpoly.append(1.0)
        if n == 0:
            proofpoly.append(0.0)

    proofpoly = np.array(proofpoly)

    return proofpoly


def total():
    # Based on http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.polydiv.html

    o_transformed = transformation_for_numpy_of_o()
    proof_transformed = transformation_for_numpy_of_proof()

    numer = np.array(o_transformed)
    denomin = np.array(proof_transformed)
    answer = np.polydiv(numer, denomin)

    print (answer)

total()

我是 numpy 的新手,不明白这个错误。我该如何解决这个问题?


*编辑:根据要求进行整个追溯:

/Users/M/anaconda/envs/Invictus/bin/python/Users/Max/PycharmProjects/1/Origin
Traceback (most recent call last):
  File "/Users/M/PycharmProjects/1/Origin", line 49, in <module>
    total()
  File "/Users/M/PycharmProjects/1/Origin", line 46, in total
    answer = np.polydiv(numer, denomin)
  File "/Users/M/anaconda/envs/Invictus/lib/python3.5/site-packages/numpy/lib/polynomial.py", line 895, in polydiv
    w = u[0] + v[0]
IndexError: index 0 is out of bounds for axis 0 with size 0
Process finished with exit code 1

【问题讨论】:

  • 是否可以发布整个回溯?

标签: python numpy


【解决方案1】:

您正在比较整数和字符串,因此您的 if 永远不会评估为 True,因此当您 np.array(proofpoly) 时,proofpoly 始终为空freshdatapoly 同样适用:

更改新鲜数据和校对:

 freshdata = map(int,"101001")
 proofing = list(map(int,"101001"))

既然您创建了它们,只需将它们都设为一个整数列表:

import numpy as np


def transformation_for_numpy_of_o():
    freshdata = [1,0,1,0,0,1]
    freshdatapoly = []
    for n in freshdata:
        if n == 1:
            freshdatapoly.append(1.0)
        if n == 0:
            freshdatapoly.append(0.0)
    return np.array(freshdatapoly)


def transformation_for_numpy_of_proof():
    proofing = [1, 0, 1, 0, 0, 1]
    proofpoly = []
    for n in proofing:
        if n == 1:
            proofpoly.append(1.0)
        if n == 0:
            proofpoly.append(0.0)
    return np.array(proofpoly)

现在当你运行它时,你会得到一个结果:

In [2]: total()
(array([ 1.]), array([ 0.]))

也许还有更多我们看不到但目前代码相当于:

def transformation_for_numpy_of_o():
    freshdata = [1,0,1,0,0,1]
    return np.array(freshdata)

def transformation_for_numpy_of_proof():
    proofing = [1, 0, 1, 0, 0, 1]
    return np.array(proofing)

如果还有其他可能的值,您仍然可以使用列表组合:

def transformation_for_numpy_of_proof():
    proofing = [1, 0, 4,5,1, 0, 0, 1, 4,3,5]
    return np.array([i for i in proofing if i in {1,0}])

【讨论】:

    猜你喜欢
    • 2020-04-24
    • 2022-01-13
    • 2019-04-01
    • 2017-02-16
    • 2021-11-03
    • 2016-07-29
    • 2021-07-17
    • 2021-05-24
    • 2018-07-23
    相关资源
    最近更新 更多