【发布时间】:2019-12-15 19:45:34
【问题描述】:
运行此代码时出现错误
IndexError: 只有整数、切片 (
:)、省略号 (...)、 numpy.newaxis (None) 和整数或布尔数组是有效的索引
python 代码
import random
def getsys():
row = ''
for i in range(0 , 8):
randintt = str(random.randint(0 , 4))
row += randintt
return row
def getx():
x = []
for i in range(0,14):
mysys = getsys()
x.append(mysys)
return x
y = getx()
print (y)
import initialsys
import numpy as np
R = np.array([[0.90 , 0.93,0.91 , 0.95],
[0.95 , 0.94, 0.93, 0],
[0.85 , 0.90 , 0.87 , 0.92],
[0.83 , 0.87 , 0.85 , 0 ],
[0.94 , 0.93 , 0.95 , 0],
[0.99 , 0.98 , 0.97 , 0.96],
[0.91 , 0.92 , 0.94 , 0],
[0.81 , 0.90 , 0.91 , 0],
[0.97 , 0.99 , 0.96 , 0.91],
[0.83 , 0.85 , 0.90 , 0],
[0.94 , 0.95 , 0.96 , 0],
[0.79 , 0.82 , 0.85 , 0.90],
[0.98 , 0.99 , 0.97 , 0],
[0.85 , 0.92 , 0.95 , 0.99]
])
def expression(r ,possition , char ):
exp = 1-r[possition , char]
x = initialsys.getx()
possition = 1
Total = 1
char = ""
for row in x :
for char in row :
if char!= 0 :
exp = expression(R , possition , char)
Total = Total*exp
Total = 1-Total
possition = possition + 1
【问题讨论】:
-
仅供参考,这不是重点,但您应该始终使用
np.array()而不是np.matrix()。矩阵已弃用。此外,即使工作正常,您也总是会得到 0;int()在 0 和 1 之间的任何值都将被截断为 0。您希望如何使用 int(位置)和字符(charr)来索引矩阵? -
抱歉,已编辑代码但当前出现该错误
标签: python numpy data-science