1

Trying to train my DecisionTreeClassifier with fit method:

from sklearn import tree
import skimage

features = []
labels = []

for i in range(5):
    img = skimage.io.imread("circle" + str(i+1) + ".jpg")
    img = skimage.img_as_float(img)
    features.append(img)
    labels.append(0)

    img = skimage.io.imread("square" + str(i+1) + ".jpg")
    img = skimage.img_as_float(img)
    features.append(img)
    labels.append(1)

clf = tree.DecisionTreeClassifier()
clf = clf.fit(features, labels)

Receiving error:

ValueError: setting an array element with a sequence.

Dinko Pehar
  • 5,454
  • 4
  • 23
  • 57
  • Perhaps related problem [here](https://stackoverflow.com/questions/25485503/valueerror-setting-an-array-element-with-a-sequence-while-using-svm-in-scikit), [here](https://stackoverflow.com/questions/51511432/valueerror-setting-an-array-element-with-a-sequence-in-scikit-learn-sklearn-u), [here](https://stackoverflow.com/questions/36115472/sklearn-svm-fit-valueerror-setting-an-array-element-with-a-sequence). Did you already have a look at all of them? Perhaps not – Sheldore Dec 23 '18 at 19:34
  • More [here](https://stackoverflow.com/questions/37548189/valueerror-setting-an-array-element-with-a-sequence-with-decision-tree-where-al) and [here](https://stackoverflow.com/questions/40514019/setting-an-array-element-with-a-sequence-error-while-training-svm-to-classify-im?rq=1) and several more – Sheldore Dec 23 '18 at 19:36
  • @Bazingaa I've seen this topic but the number of elements in both my lists are equal (10). – Igor Krutilin Dec 23 '18 at 19:36
  • Perhaps there is some issue with the shape of the features? Try printing the shape by converting to numpy array – Sheldore Dec 23 '18 at 19:37
  • Bazingaa is probably right: `img` is likely a two-dimensional array. You can't use a two-dimensional array as a feature; try making it one-dimensional. – 9769953 Dec 23 '18 at 19:39

2 Answers2

2

You will use only the first pixel value alone, if you do the following.

features.append(img[0][0])

Try this!

import numpy as np
features.append(np.array(img).flatten())

please check the dimension of the data, which you are appending to know whats actually happening.

print(np.array(img).flatten().shape)
Venkatachalam
  • 16,288
  • 9
  • 49
  • 77
0

Thanks a lot, Bazinga and 9769953.

Solved my problem with replacing

features.append(img)

with

features.append(img[0][0])