问题:在用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参