Type Error: Only length-1 arrays can be converted to Python Scalars
Type Error: Only length-1 arrays can be converted to Python Scalars
我是 openCV 的初学者,正在尝试分析数独求解器的现有代码。有一段代码会抛出错误。
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
知道问题出在哪里吗?
您收到的错误消息:
TypeError: Only length-1 arrays can be converted to Python Scalars
字面意思是:您在 单个值 或 需要单个元素的数组。
所以传递给调用 model.train(samples, responses)
的参数之一需要和标量...但是哪个?
查看KNearest class, allow us to see the signature of the StatsModel.train method的最新文档:
virtual bool cv::ml::StatModel::train ( InputArray samples, int layout, InputArray responses )
显然,添加了一个新的 layout
参数。但是 meaning 在文档中有点模糊。
不知道你文件的内容,我无法判断你是否需要传递 ROW_SAMPLE
或 COL_SAMPLE
,但有了这些信息,我可以找到一个 ,其解决方案是添加 cv2.ml.ROW_SAMPLE
作为训练方法的第二个参数:
model.train(samples, cv2.ml.ROW_SAMPLE, responses)
由于在最新版本中进行了一系列重构操作而出现错误。
Error fix
我是 openCV 的初学者,正在尝试分析数独求解器的现有代码。有一段代码会抛出错误。
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
知道问题出在哪里吗?
您收到的错误消息:
TypeError: Only length-1 arrays can be converted to Python Scalars
字面意思是:您在 单个值 或 需要单个元素的数组。
所以传递给调用 model.train(samples, responses)
的参数之一需要和标量...但是哪个?
查看KNearest class, allow us to see the signature of the StatsModel.train method的最新文档:
virtual bool cv::ml::StatModel::train ( InputArray samples, int layout, InputArray responses )
显然,添加了一个新的 layout
参数。但是 meaning 在文档中有点模糊。
不知道你文件的内容,我无法判断你是否需要传递 ROW_SAMPLE
或 COL_SAMPLE
,但有了这些信息,我可以找到一个 cv2.ml.ROW_SAMPLE
作为训练方法的第二个参数:
model.train(samples, cv2.ml.ROW_SAMPLE, responses)
由于在最新版本中进行了一系列重构操作而出现错误。 Error fix