且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

类型错误:只能将长度为1的数组转换为Python标量

更新时间:2022-06-08 18:37:33

您收到的错误消息:

TypeError: Only length-1 arrays can be converted to Python Scalars

从字面上看是指:您在单个值单个元素数组的地方提供了一个多个元素的数组>是预期的.

literally means: you have provided an array of more than one element in a place where a single value or an array of a single element was expected.

所以传递给调用model.train(samples, responses)的参数之一是标量和标量...但是,哪个?

So one of the arguments passed to call model.train(samples, responses) requires and scalar... but which?

看看 KNearest类的最新文档,请允许我们查看 StatsModel.train方法:

A look at the latest documentation of KNearest class, allow us to see the signature of the StatsModel.train method:

虚拟布尔cv :: ml :: StatModel :: train(InputArray示例,int布局,InputArray响应)

virtual bool cv::ml::StatModel::train ( InputArray samples, int layout, InputArray responses )

显然,添加了一个新的 layout 参数.但这含义有点晦涩从文档中获取.

Apparently, a new layout argument was added. But it's meaning is a little obscure from the documentation.

在不了解文件内容的情况下,我无法确定您是否需要传递ROW_SAMPLECOL_SAMPLE,但是有了这些信息,我可以找到

Without knowledge of the contents of you file, I can't tell if you need to pass ROW_SAMPLE or COL_SAMPLE, but armed with that information, I could find a similar question, whose solution was to add cv2.ml.ROW_SAMPLE as second argument to the train method:

model.train(samples, cv2.ml.ROW_SAMPLE, responses)