ValueError: Found array with dim 3. Estimator expected <= 2. >>>

ValueError: Found array with dim 3. Estimator expected <= 2. >>>

#Import Library
from sklearn import svm
import numpy as np




X=np.array([
    [[25,25,25],[0,0,0],[0,0,0]],
    [[25,0,0],[25,0,0],[25,0,0]],
    [[75,75,75],[75,75,75],[75,75,75]]
           ])
y=np.array([-1,1,1]
           )


C=10

model = svm.SVC(kernel='rbf', C=10, gamma=0.6) 


model.fit(X, y)
model.score(X, y)

当我尝试 运行 这段代码时,我得到了这个错误

ValueError: Found array with dim 3. Estimator expected <= 2.

我希望你帮我解决这个错误。我想训练 svm 将图像像素分为两个 类(边缘和非边缘),任何建议都会有所帮助提前感谢

我不知道问题域。但这解决了你的错误,

#Import Library
from sklearn import svm
import numpy as np

X=np.array([
[[25,25,25],[0,0,0],[0,0,0]],
[[25,0,0],[25,0,0],[25,0,0]],
[[75,75,75],[75,75,75],[75,75,75]]
       ])
X = X.reshape(X.shape[0], -1)
y=np.array([-1,1,1])


C=10

model = svm.SVC(kernel='rbf', C=10, gamma=0.6) 


model.fit(X, y)
model.score(X, y)

输出:

1.0

model.fit 需要 2D 数组,但你的 X 是 3D。使用 np.concatenate

将您的 X 转换为 2D
from sklearn import svm
import numpy as np

X=np.array([
    [[25,25,25],[0,0,0],[0,0,0]],
    [[25,0,0],[25,0,0],[25,0,0]],
    [[75,75,75],[75,75,75],[75,75,75]]
           ])
y=np.array([-1,1,1]
           )


X = [np.concatenate(i) for i in X]
print(X)
model = svm.SVC(kernel='rbf', C=10, gamma=0.6) 


model.fit(X, y)
model.score(X, y)