【问题标题】:How to resolve 'ValueError: Found array with dim 4. Estimator expected <= 2.'?如何解决“ValueError: Found array with dim 4. Estimator expected <= 2.”?
【发布时间】:2019-12-02 21:19:36
【问题描述】:

我正在制作一个 SVM 分类器来对图像进行分类。该程序使用 Google Collab,文件上传到 Google Drive。图片的形状是torch.Size([32, 3, 224, 224])

这就是我拆分数据集的方式,

images = (images.numpy())
labels = (labels.numpy())
X_train, X_test, y_train, y_test = train_test_split(images, labels, test_size=0.3, random_state=42)

将训练数据和测试数据拆分后,X_train 和 X_test 的新形状分别为(22, 3, 224, 224)(10, 3, 224, 224)。 现在,当我尝试这样做时,就会出现问题

# Create a classifier: a support vector classifier
classifier = svm.SVC(gamma=0.001)
#fit to the trainin data
classifier.fit(X_train,y_train)

----> 3 classifier.fit(X_train,y_train)

   537         if not allow_nd and array.ndim >= 3:
    538             raise ValueError("Found array with dim %d. %s expected <= 2."
--> 539                              % (array.ndim, estimator_name))
    540         if force_all_finite:
    541             _assert_all_finite(array,

ValueError: 找到暗淡为 4 的数组。预计估计器

我有 4 个图像类,我希望 SVM 分类器来训练模型,之前我使用 CNN 和迁移学习来训练。我已经阅读了一些帖子,在这里我可能不得不重新塑造它。你能帮我解决这个问题吗?感谢您的帮助。

【问题讨论】:

    标签: image machine-learning scikit-learn svm


    【解决方案1】:

    目前,您的输入数据有 4 个维度 (batch size, channels, height, width),您需要将图像展平为二维 (number of images, channels* height* width)

    X_train = X_train.reshape(22,3*224*224)
    X_test = X_test.reshape(10,3*224*224)
    

    希望这会有所帮助!

    【讨论】:

    • 谢谢。我现在可以了。
    猜你喜欢
    • 2020-06-30
    • 2018-10-05
    • 2020-09-07
    • 2020-11-27
    • 2019-07-19
    • 2017-07-28
    • 1970-01-01
    • 2018-08-21
    • 2017-10-26
    相关资源
    最近更新 更多