Skip to content Skip to sidebar Skip to footer

Type Error: Only Length-1 Arrays Can Be Converted To Python Scalars

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('feat

Solution 1:

The error message that you are getting:

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.

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

A look at the latest documentation of 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 )

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

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) 

Solution 2:

Error occurs due to a series of refactoring operations made in the latest versions. Error fix

Post a Comment for "Type Error: Only Length-1 Arrays Can Be Converted To Python Scalars"