#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:找到的数组为暗3。估计值应为<= 2。
我希望您能帮助我解决此错误。我想训练svm将图像像素分为两类(edge和non-edges),任何建议都会有所帮助
答案 0 :(得分:0)
我不知道问题域。但这可以解决您的错误,
#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
答案 1 :(得分:0)
model.fit
需要2D数组,但是X是3D。使用X
np.concatenate
转换为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)