data在python_python中的data[:, :-1]和data[:, -1]什么意思?

  • Post author:
  • Post category:python


问题:在用python3使用knn.train(trainData,responses)的时候,可能会产生错误:TypeError:onlysize-1arrayscanbeconvertedtoPythonscalars

newcomer=np.random.randint(0,100,(1,2)).astype(np.float32)

plt.scatter(newcomer[:,0],newcomer[:,1],80,’g’,’o’)

knn=cv2.ml.KNearest_create()

knn.train(trainData,responses)

报错信息:

—————————————————————————

TypeErrorTraceback(mostrecentcalllast)

in()

2plt.scatter(newcomer[:,0],newcomer[:,1],80,’g’,’o’)

3knn=cv2.ml.KNearest_create()

—->4knn.train(trainData,responses)

5

TypeError:onlysize-1arrayscanbeconvertedtoPythonscalars

解决:将knn.train(trainData,responses)替换为:

knn.train(trainData,cv2.ml.ROW_SAMPLE,responses)

分析:

在python3中,knn.train需要传入layout参



版权声明:本文为weixin_29171087原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。