0
from sklearn import 
from matplotlib import image
import os
features=list()
##this loops are to load images as arrays in to features from the folders on my desktop
for f in os.listdir(r'C:\Users\Sriram\Desktop\300'):
    if f.endswith('.png'):
        try:

            pass
for f in os.listdir(r'C:\Users\Sriram\Desktop\300n'):
    if f.endswith('.png'):
        try:
            img=image.imread(r'C:\Users\Sriram\Desktop\300n'+f)
            features.append(img)
        except:
            pass
l=[0]
p=[1]
print("hello")
## there are onlt two classes if images each 20 hence i created targets ast list of 40 elements first
##for class 1 and other for 2
targets=l*20+p*20
##thi is where i get error
clf=tree.DecisionTreeClassifier()
clf=clf.fit(features,targets)

Traceback (most recent call last): File "C:/Users/Sriram/AppData/Local/Programs/Python/Python38-32/ML2.py", line 24, in clf=clf.fit(features,targets) File "C:\Users\Sriram\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\tree_classes.py", line 873, in fit super().fit( File "C:\Users\Sriram\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\tree_classes.py", line 149, in fit X = check_array(X, dtype=DTYPE, accept_sparse="csc") File "C:\Users\Sriram\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\utils\validation.py", line 552, in check_array raise ValueError( ValueError: Expected 2D array, got 1D array instead: array=[]. Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

I was trying to load some images from my desktop using matplotlib .those include only two classes of images and then i want to train a decisiontreeclassifier and when I try to access fit method using these features I got above error

Joshua Varghese
  • 5,082
  • 1
  • 13
  • 34
sriram anush
  • 77
  • 3
  • 13

1 Answers1

0

fit method expect to input shape to be (n_samples, n_features).If you have a 1D array you need to convert it to 2D array before fit to the model.You can use reshape method as suggested in the error.

clf=clf.fit(features.reshape(-1,1),targets)
Rajith Thennakoon
  • 3,975
  • 2
  • 14
  • 24