【问题标题】:scikit-learn: Expected 2D array, got 1D array insteadscikit-learn:预期的 2D 数组,得到 1D 数组
【发布时间】:2019-06-06 05:14:44
【问题描述】:

我目前正在尝试训练我的数据集,并且目前正在尝试按照链接中的步骤进行操作。 Link here

我不断收到类似的错误

ValueError: Expected 2D array, got 1D array instead: array=[0. 0. 0. and so on..]. 

这是我尝试使用 sci-kit learn 测试和训练的代码:

import numpy as np
from sklearn.svm import LinearSVC
import os
import cv2
import joblib

# Generate training set
TRAIN_PATH = 'Try/'
list_folder = os.listdir(TRAIN_PATH)
trainset = []
for folder in list_folder:
    flist = os.listdir(os.path.join(TRAIN_PATH, folder))
    for f in flist:
        im = cv2.imread(os.path.join(TRAIN_PATH, folder, f))
        im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY )
        im = cv2.resize(im, (36,36))
        trainset.append(im)
# Labeling for trainset
train_label = []
for i in range(0,1): #in the dataset i currently have a 0 folder and a 1 folder
    temp = 400*[i] #400 images in 1 folder
    train_label += temp

# Generate testing set
TEST_PATH = 'Test/'
list_folder = os.listdir(TEST_PATH)
testset = []
test_label = []
for folder in list_folder:
    flist = os.listdir(os.path.join(TEST_PATH, folder))
    for f in flist:
        im = cv2.imread(os.path.join(TEST_PATH, folder, f))
        im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY )
        im = cv2.resize(im, (36,36))
        testset.append(im)
        test_label.append(folder)
trainset = np.reshape(trainset, (800, -1)) #800 because total number of images

# Create an linear SVM object
clf = LinearSVC()

# Perform the training
clf.fit(train_label, testset)
print("Training finished successfully")

# Testing
testset = np.reshape(testset, (len(testset), -1))
y = clf.predict(testset)
print("Testing accuracy: " + str(clf.score(testset, test_label)))

这是错误信息:

 Traceback (most recent call last):   File
 "C:\Users\hp\AppData\Local\Programs\Python\Python37-32\Thesis\Test.py",
 line 43, in <module>
     clf.fit(train_label, testset)   File "C:\Users\hp\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\svm\classes.py",
 line 229, in fit
     accept_large_sparse=False)   File "C:\Users\hp\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\utils\validation.py",
 line 756, in check_X_y
     estimator=estimator)   File "C:\Users\hp\AppData\Local\Programs\Python\Python37-32\lib\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=[0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]. 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.

【问题讨论】:

    标签: python machine-learning scikit-learn


    【解决方案1】:

    很难用这么少的信息来判断,但这可能与clf.fit() 正在接收的参数有关。看起来您在实际训练数据X 之前为其提供了标签train_label

    如果你看一下文档,顺序如下:

    fit(X, y[, sample_weight]) 根据给定的训练数据拟合SVM模型。

    改用:

    clf.fit(testset, train_label)
    

    【讨论】:

    • 谢谢。它不显示错误,但现在有一个新错误,即 Found array with dim 3. Estimator expected
    猜你喜欢
    • 2021-05-14
    • 2018-09-16
    • 2020-12-18
    • 2018-12-11
    • 2021-12-28
    • 2020-11-02
    • 2020-03-19
    • 1970-01-01
    相关资源
    最近更新 更多