问题
#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