1
#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
  • Replacing X=np.array([...]) by X=np.array(...) should be good. Commented Jan 7, 2019 at 8:00

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

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)

1 Comment

This isn't the best way to convert to 2D array. It can be done with native numpy functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.