如何在2D向量上正确使用reshape()函数

我已经对特征向量进行了重塑,但仍然遇到了以下错误:

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.

我在预测前使用了reshape,如下所示:

features = features.reshape(1, -1)

但仍然没有解决问题。

这是我的代码:

import cv2import numpy as npimport osimport globimport mahotas as mtfrom sklearn.svm import LinearSVC# function to extract haralick textures from an imagedef extract_features(image):    # calculate haralick texture features for 4 types of adjacency    textures = mt.features.haralick(image)    # take the mean of it and return it    ht_mean = textures.mean(axis = 0).reshape(1, -1)    return ht_mean# load the training datasettrain_path  = "C:/dataset/train"train_names = os.listdir(train_path)# empty list to hold feature vectors and train labelstrain_features = []train_labels   = []# loop over the training datasetprint ("[STATUS] Started extracting haralick textures..")for train_name in train_names:    cur_path = train_path + "/" + train_name    cur_label = train_name    i = 1    for file in glob.glob(cur_path + "/*.jpg"):        print ("Processing Image - {} in {}".format(i, cur_label))        # read the training image        image = cv2.imread(file)        # convert the image to grayscale        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)        # extract haralick texture from the image        features = extract_features(gray)        # append the feature vector and label        train_features.append(features.reshape(1, -1))[0]        train_labels.append(cur_label)        # show loop update        i += 1# have a look at the size of our feature vector and labelsprint ("Training features: {}".format(np.array(train_features).shape))print ("Training labels: {}".format(np.array(train_labels).shape))# create the classifierprint ("[STATUS] Creating the classifier..")clf_svm = LinearSVC(random_state = 9)# fit the training data and labelsprint ("[STATUS] Fitting data/label to model..")clf_svm.fit(train_features, train_labels)# loop over the test imagestest_path = "C:/dataset/test"for file in glob.glob(test_path + "/*.jpg"):     # read the input image    image = cv2.imread(file)    # convert to grayscale    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)    # extract haralick texture from the image    features = extract_features(gray)    # evaluate the model and predict label    prediction = clf_svm.predict(features)    # show the label    cv2.putText(image, prediction, (20,30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0,255,255), 3)    print ("Prediction - {}".format(prediction))    # display the output image    cv2.imshow("Test_Image", image)    cv2.waitKey(0)

我不知道我是否错误地使用了reshape()函数,还是遗漏了什么。

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.


回答:

请考虑以下几点:

  • 您遇到上述错误是因为train_featuresclf_svm.fit(train_features, train_labels)这一行是一个空列表([])。它应该至少包含1个数据。这是因为train_path指向一个只包含图像文件的文件夹,但上述代码假设train_path指向一个至少包含1个子文件夹(不包含文件)的文件夹。

    train    - class1_folder[class11.jpg, class12.jpg, ...]   - class2_folder[class21.jpg, class22.jpg, ...]   - and so on ...

    在这里,您的训练数据的类名将是[class1, class2, ...]

  • train_features.append(features.reshape(1, -1))[0]这一行改正为train_features.append(features.reshape(1, -1)[0])

  • clf_svm.predict(features)的输出是一个numpy数组。因此,在cv2.putText函数中,将prediction替换为str(prediction)。您也可以将其替换为prediction[0]

尝试以下代码:

(此处省略了代码内容,因为代码不需要翻译)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注