基于Scikit-learn的字符串核支持向量机



我是scikit-learn的新手,我在Stackoverflow上的scikit-learn字符串内核的其他问题中看到了一个示例解决方案。所以我试了一下,但我得到这个错误信息:

>>> X = np.arange(len(data)).reshape(-1, 1)
>>> X
   array([[0],
   [1],
   [2]])
    def string_kernel(X, Y):
    ... R = np.zeros((len(x), len(y)))
    ... for x in X:
    ...     for y in Y:
    ...         i = int(x[0])
    ...         j = int(y[0])
    ...         R[i, j] = data[i][0] == data[j][0]
    ... return R
>>> clf = SVC(kernel=string_kernel)
>>> clf.fit(X, ['no', 'yes', 'yes'])

这是我得到的错误信息:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/sklearn/svm/base.py", line 178, in   fit
  fit(X, y, sample_weight, solver_type, kernel, random_seed=seed)
  File "/Library/Python/2.7/site-packages/sklearn/svm/base.py", line 217, in    _dense_fit
   X = self._compute_kernel(X)
  File "/Library/Python/2.7/site-packages/sklearn/svm/base.py", line 345, in _compute_kernel
  kernel = self.kernel(X, self.__Xfit)
  File "<stdin>", line 2, in string_kernel
  UnboundLocalError: local variable 'x' referenced before assignment

您看到的特定错误实际上与SVM无关。在string_kernel函数的这一行:

R = np.zeros((len(x), len(y)))

小写x(和y)目前未定义,因此是UnboundLocalErrorYou larsmans可能是指len(X)len(Y)

相关内容

  • 没有找到相关文章

最新更新