在 self._compute_kernel(X) 中引发 ValueError(“X.shape[0] 应该等于 X.shape[1]") [英] in self._compute_kernel(X) raise ValueError("X.shape[0] should be equal to X.shape[1]")

查看:77
本文介绍了在 self._compute_kernel(X) 中引发 ValueError(“X.shape[0] 应该等于 X.shape[1]")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,X 和 y 是训练数据:

In my code in which X and y are the training data:

from sklearn.svm import SVC
clf = SVC(kernel=lambda x,y:gauss_kernel(x, y, 100) )
print(X.shape[0])
print(X.shape[1])
print(X.shape)

clf.fit(X, y)

我收到以下错误:

211
2
(211, 2)
/Users/mona/anaconda/lib/python3.6/site-packages/sklearn/utils/validation.py:547: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-23-1f163ab380a5> in <module>()
      8 print(X.shape)
      9 
---> 10 clf.fit(X, y)
     11 plot_data()
     12 plot_boundary(svm,-.5,.3,-.8,.6)

~/anaconda/lib/python3.6/site-packages/sklearn/svm/base.py in fit(self, X, y, sample_weight)
    185 
    186         seed = rnd.randint(np.iinfo('i').max)
--> 187         fit(X, y, sample_weight, solver_type, kernel, random_seed=seed)
    188         # see comment on the other call to np.iinfo in this file
    189 

~/anaconda/lib/python3.6/site-packages/sklearn/svm/base.py in _dense_fit(self, X, y, sample_weight, solver_type, kernel, random_seed)
    226             X = self._compute_kernel(X)
    227 
--> 228             if X.shape[0] != X.shape[1]:
    229                 raise ValueError("X.shape[0] should be equal to X.shape[1]")
    230 

IndexError:元组索引超出范围

IndexError: tuple index out of range

这是我写的自定义高斯核:

Here's the customized Gaussian Kernel I wrote:

import math
def gauss_kernel(x1, x2, gamma):
    sigma = math.sqrt(gamma) 
    return np.exp(-np.sum((x1-x2)**2)/(2*sigma**2))

我该如何解决?当我看sklearn中的SVM示例时,它们基本上会做同样的事情.我相信我忽略了一些小问题,但在与sklearn示例匹配时无法解决问题.

How should I fix this? When I look at SVM examples in sklearn, they basically do the same thing. I believe I am neglecting something small but can't pin down the problem when matching with sklearn examples.

推荐答案

请确保自定义内核的输出为方矩阵.

Please make sure that the output of your custom kernel is a square matrix.

当前,您对 gauss_kernel 的实现将返回一个数字,而不是一个数组.因此,调用shape [0]或shape [1]会引发元组索引超出范围错误".

Currently your implementation of gauss_kernel will return a number, not an array. So calling shape[0] or shape[1] throws the "tuple index out of range error".

因此,请解决此问题:

import math
def gauss_kernel(x1, x2):
    sigma = math.sqrt(100) 
    return np.array([np.exp(-np.sum((x1-x2)**2)/(2*sigma**2))])

然后使用您的代码.

注意:这只是将单个数字包装到数组中的一种解决方法.您应该检查原始的 gauss_kernel 出了什么问题,该错误返回单个数字.

Note: This is just a workaround for wrapping a single number to an array. You should check whats wrong with your original gauss_kernel that it returns the single number.

这篇关于在 self._compute_kernel(X) 中引发 ValueError(“X.shape[0] 应该等于 X.shape[1]")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆