吴恩达深度学习课后编程作业IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and i

吴恩达深度学习课后编程作业出现的错误 IndexError: only integers, slices (" : “), ellipsis (”…"), numpy.newaxis (“None”) and integer or boolean arrays are valid indices

出现的这个错误是数据类型错误,错误如下图所示

吴恩达深度学习课后编程作业IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and i_第1张图片

找到所在错误行所用的数据

吴恩达深度学习课后编程作业IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and i_第2张图片

然后查看是哪个数据错误

注释错误的哪一行,并将所有的单个数据输出
运行,找出错误数据

吴恩达深度学习课后编程作业IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and i_第3张图片

这个数据中还含有数据,对其进行分离,并输出其类型。

吴恩达深度学习课后编程作业IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and i_第4张图片

发现这个数据为numpy.float64类型,但是Python中的list[i],其中的i只能是int型,所以错误在这里,所以只需要将其强换为int。

吴恩达深度学习课后编程作业IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and i_第5张图片

只需加int()。

# Example of a picture that was wrongly classified.
index = 1
plt.imshow(test_set_x[:,index].reshape((num_px, num_px, 3)))
#print(str(test_set_y[0,index]))
#print(classes[d["Y_prediction_test"][0,index]].decode("utf-8"))

#print(d["Y_prediction_test"][0,index])

#type(d["Y_prediction_test"][0,index])
print ("y = " + str(test_set_y[0,index]) + ", you predicted that it is a \"" + classes[int(d["Y_prediction_test"][0,index])].decode("utf-8") +  "\" picture.")

此文章只提供一种找错误的思路

你可能感兴趣的:(机器学习及深度学习,python,机器学习,深度学习,神经网络)