gpt4 book ai didi

python - 索引错误: index 0 is out of bounds for axis 0 with size 0

转载 作者:行者123 更新时间:2023-12-01 04:01:39 27 4
gpt4 key购买 nike

我正在尝试编写一个代码来除以二进制形式给出的两个多项式(分别是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

最佳答案

您正在比较整数和字符串,因此您的 if 永远不会计算为 True,因此当您 np.array(proofpoly) 时,proofpoly 始终为空,这同样适用于 新鲜数据聚:

更改新鲜数据和校对:

 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}])

关于python - 索引错误: index 0 is out of bounds for axis 0 with size 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36390458/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com