How to resolve 'ValueError: Found array with dim 4. Estimator expected <= 2.'?
How to resolve 'ValueError: Found array with dim 4. Estimator expected <= 2.'?
我正在制作一个 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: Found array with dim 4. Estimator expected <= 2.
我有 4 张图像 类,我想要 SVM 分类器来训练模型,之前我是用 CNN 和迁移学习来训练的。我已经阅读了一些 post,在这里我可能不得不重塑它。你能帮我解决这个问题吗?感谢您的帮助。
目前,您的输入数据有 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)
希望对您有所帮助!
我正在制作一个 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: Found array with dim 4. Estimator expected <= 2.
我有 4 张图像 类,我想要 SVM 分类器来训练模型,之前我是用 CNN 和迁移学习来训练的。我已经阅读了一些 post,在这里我可能不得不重塑它。你能帮我解决这个问题吗?感谢您的帮助。
目前,您的输入数据有 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)
希望对您有所帮助!