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

梦想与她 提交于 2021-02-11 00:11:08

问题


#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)

when I tried to run this code , I got this error

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

I would like that you help me solve this error. I want to train the svm to classify image pixels into two classes (edge and non-edges ), any suggestions will be helpful thanks in advance


回答1:


I don't know about problem domain. But this solves your error,

#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)

Output:

1.0



回答2:


model.fit needs 2D array but your X is 3D. Convert Your X into 2D using np.concatenate

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)


来源:https://stackoverflow.com/questions/54070437/valueerror-found-array-with-dim-3-estimator-expected-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!