Phil
Phil

Reputation: 185

"ValueError: Expected 2D array, got 1D array instead" when fitting data into model

I've viewed some questions regarding the same issue but none of them could help me. The problem is as it says, I'm unable to fit data into learning model.

This is the main file, which calls out the class regarding the data i use to fit in the model:

def main():
    action = input(
        "Choose an action:\n A - Create LinearSVC classifier\n B - Create Random Forest Classifier\n C - Create K Nearest Neighbor classifier\n -> ").upper()

    loader = ImageLoader()
    if action == "A":
        lsvc = LinearSVC(random_state=0, tol=1e-5)
        lsvc.fit(loader.hogArray(), loader.labelArray())
        joblib.dump(lsvc, './LSVCmodel.pkl')
    elif action == "B":
        rfc = RandomForestClassifier(n_estimators=100)
        rfc.fit(loader.hogArray(), loader.labelArray())
        joblib.dump(rfc, './RFmodel.pkl')
    elif action == "C":
        knc = KNeighborsClassifier(n_neighbors=3)
        knc.fit(loader.hogArray(), loader.labelArray())
        joblib.dump(knc, './KNCmodel.pkl')
    else:
        print("That's not a valid answer")
        main()

The same error occurs with all 3 models. The class that retrieves the data is written as following:

class ImageProcess:

    def __init__(self, image, hog_data=None):
        self.hog_data = hog_data
        self.image = image

    def hog_data_extractor(self):
        self.hog_data = feature.hog(self.image) / 255.0
        return self.hog_data

    def normalize(self):
        imageRead = cv2.resize(cv2.imread(self.image), (150, 150))
        gaussImage = cv2.fastNlMeansDenoisingColored(imageRead, None, 10, 10, 7, 21)
        self.image = cv2.Canny(gaussImage, 100, 200)
        self.image = cv2.cvtColor(self.image, cv2.COLOR_GRAY2RGB)
        self.image *= np.array((0, 0, 1), np.uint8)
        return self.image


class ImageLoader:

    def __init__(self):
        self.sourcePath = "dataset/seg_train/"
        self.labels = ['Buildings', 'Forest', 'Glacier', 'Mountain', 'Sea', 'Street']
        self.x_train = []
        self.y_train = []

    def fillArray(self):
        label_train = []
        le = LabelEncoder()

        run_time = time.time()
        for scene in self.labels:
            scene_path = os.path.join(self.sourcePath, scene.lower())
            fileNumber = 0

            scene_length = len([image for image in os.listdir(scene_path)])
            for img in os.listdir(scene_path):
                per = (file_number / scene_length)
                arrow = '-' * int(round(per * 100) - 1) + '>'
                spaces = ' ' * (100 - len(arrow))
                sys.stdout.write(
                    "\rProgress: [{0}] {1}% -Ellapsed time: {2}".format(arrow + spaces, int(round(per * 100, 2)),
                                                                        (int(time.time() - run_time))))

                file_number += 1
                img_path = os.path.join(scene_path, img)
                process = ImageProcess(img_path)
                self.x_train.append(process.hog_data_extractor())
                label_train.append(str(scene_type))

        self.y_train = le.fit_transform(label_train)

    def hogArray(self):
        return self.x_train

    def labelArray(self):
        return self.y_train

A side note: Previously I didn't have this ImageLoader class, and simply had the method fillArray() under main() on the previous code, and it didn't give back this error, all was working well. But due to some restrictions I have to follow I tried to transferred it into a class to be use in more other files.

    Traceback (most recent call last):
  File "main.py", line 35, in <module>
    main()
  File "main.py", line 19, in main
    lsvc.fit(loader.hogArray(), loader.labelArray())
  File "/home/filipe/Documents/NovaPasta/2019_20/LP_recuperacao/Trabalho_recuperacao/venv/lib/python3.6/site-packages/sklearn/svm/classes.py", line 229, in fit
    accept_large_sparse=False)
  File "/home/filipe/Documents/NovaPasta/2019_20/LP_recuperacao/Trabalho_recuperacao/venv/lib/python3.6/site-packages/sklearn/utils/validation.py", line 756, in check_X_y
    estimator=estimator)
  File "/home/filipe/Documents/NovaPasta/2019_20/LP_recuperacao/Trabalho_recuperacao/venv/lib/python3.6/site-packages/sklearn/utils/validation.py", line 552, in check_array
    "if it contains a single sample.".format(array))
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've tried reshaping as its recommended in the error but it retrieves("AttributeError: 'list' object has no attribute 'reshape'") and since I didn't needed this reshape before I assumed this wasn't the solution.

Sorry if its poor coding but I'm not that much of an expert(not even close) and the time period I had to do this was very short so I just focused on getting it to work properly.

Upvotes: 0

Views: 204

Answers (1)

DHANANJAY RAUT
DHANANJAY RAUT

Reputation: 190

You are not calling fillArray. so the lists are empty. Try doing it at end of init function. array=[] in error shows this.

Upvotes: 1

Related Questions