运行此代码时出现错误
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
答案 0 :(得分:1)
对于使用numpy进行机器学习编码时遇到此错误的人。当您尝试打印预测private IsValid(List<Expenditure> ExpnTrxs)
{
var pptCds = Enum.GetNames(typeof(ReferenceEnums.pptCds));
var validpptCdsTrxs = AAExpnTrxs.FindAll(x => x.pptCd.);
var inValidPpptCdsTrxs = AAExpnTrxs.FindAll(x => x.pptCd);
....................
....................
....................
}
时,您将得到相同的错误。请注意,classes[d["Y_prediction_test"][0,index]]
返回一个d["Y_prediction_test"][0,index]
,例如1.0。因此您应该将其转换为float
int
我一直在寻找这个答案。但是却找不到它。解决这个问题花了一些时间,希望对您有所帮助。我正在自学ML / AI。因此,如果我们在同一个团队中,让我们联系并互相帮助。谢谢。
答案 1 :(得分:0)
您的代码中有很多问题,但是要解决当前的错误,您需要按照错误中所述的整数而不是char索引数组,可以执行#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
string str;
char *tokenPtr;
cout << "Enter a sentence : " << endl;
getline(cin, str);
tokenPtr = strtok( str, " " ); // ERROR: No matching function for call to 'strtok'
while( tokenPtr != NULL ) {
cout << tokenPtr << endl;
tokenPtr = strtok( NULL, " " );
}
return 0;
}
,然后返回结果。
int(char)