类型错误:只能将长度为1的数组转换为Python标量 [英] Type Error: Only length-1 arrays can be converted to Python Scalars
问题描述
我是openCV的初学者,正在尝试分析数独求解器的现有代码.代码的这一部分会引发错误.
I am a beginner to openCV and am trying to analyse an existing code for a sudoku solver. There is this section of code which throws an error.
samples = np.float32(np.loadtxt('feature_vector_pixels.data'))
responses = np.float32(np.loadtxt('samples_pixels.data'))
model = cv2.ml.KNearest_create()
model.train(samples, responses)
错误如下Type Error: Only length-1 arrays can be converted to Python Scalars
.
完整的回溯如下:
C:\Study stuff\FinalProject>c:\Python27\python.exe Sudoku.py
Traceback (most recent call last):
File "Sudoku.py", line 15, in <module>
model.train(samples, responses)
TypeError: only length-1 arrays can be converted to Python scalars
对这个问题有什么想法吗?
Any idea as to what the issue is?
推荐答案
您收到的错误消息:
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_SAMPLE
或COL_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)
这篇关于类型错误:只能将长度为1的数组转换为Python标量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!