在self._compute_kernel(X)中引发ValueError(" X.shape [0]应该等于X.shape [1]")

时间:2017-11-30 01:30:27

标签: python machine-learning scikit-learn svm

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

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:元组索引超出范围

这是我写的定制高斯内核:

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示例匹配时无法解决问题。

3 个答案:

答案 0 :(得分:1)

请确保自定义内核的输出是方阵。

目前,gauss_kernel的实现将返回一个数字,而不是数组。所以调用shape [0]或shape [1]会使&#34;元组索引超出范围错误&#34;。

所以解决这个问题:

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的错误是否返回单个数字。

答案 1 :(得分:0)

from sklearn import svm

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



# from @lejlot http://stackoverflow.com/a/26962861/583834
def gaussianKernelGramMatrix(X1, X2, K_function=gauss_kernel, gamma=0.1):
    """(Pre)calculates Gram Matrix K"""

    gram_matrix = np.zeros((X1.shape[0], X2.shape[0]))
    for i, x1 in enumerate(X1):
        for j, x2 in enumerate(X2):
            gram_matrix[i, j] = K_function(x1, x2, gamma)
    return gram_matrix

gamma=0.1
y = y.flatten()

clf = svm.SVC(kernel="precomputed", verbose=2, C=2.0, probability=True)
clf.fit(gaussianKernelGramMatrix(X,X, gauss_kernel, gamma=gamma), y)

答案 2 :(得分:0)

今天我正在做课程家庭作业ex6,我也遇到同样的问题。现在我解决了。 sklearn使用自定义内核请求内核函数返回新的[m * m]矩阵,其代码如下:

def _compute_kernel(self, X):
        """Return the data transformed by a callable kernel"""
        if callable(self.kernel):
            # in the case of precomputed kernel given as a function, we
            # have to compute explicitly the kernel matrix
            kernel = self.kernel(X, self.__Xfit)
            if sp.issparse(kernel):
                kernel = kernel.toarray()
            X = np.asarray(kernel, dtype=np.float64, order='C')
        return X

因此我定义了内核函数返回矩阵,它可以计算x1 = [m,n]和x2 = [h,n]欧几里得距离,然后使用exp计算返回值。

def gaussianKernel(x1: ndarray, x2: ndarray, sigma):
    # RBFKERNEL returns a radial basis function kernel between x1 and x2
    # sim = gaussianKernel(x1, x2) returns a gaussian kernel between x1 and x2
    # and returns the value in sim

    # Ensure that x1 and x2 are column vectors
    m = size(x1, 0)
    n = size(x2, 0)

    # You need to return the following variables correctly.
    sim = 0

    # ====================== YOUR CODE HERE ======================
    # Instructions: Fill in this function to return the similarity between x1
    #               and x2 computed using a Gaussian kernel with bandwidth
    #               sigma
    #
    # Note: use the matrix compute the distence
    M = x1@x2.T
    H1 = sum(square(mat(x1)), 1)  # [m,1]
    H2 = sum(square(mat(x2)), 1)  # [n,1]
    D = H1+H2.T-2*M

    sim = exp(-D/(2*sigma*sigma))
    # =============================================================
    return sim

现在在主函数中添加以下行代码:

def mykernel(x1, x2): return gaussianKernel(x1, x2, sigma)
model = svm.SVC(C, kernel=mykernel)  # type:SVC
model.fit(X, y.ravel())
visualizeBoundary(X, y, model)

完成剧情: visualizeBoundary