Exemple #1
0
 def getKM(self, X):
     """Returns the kernel matrix between the basis vectors and X.
     
     Parameters
     ----------
     X : {array-like, sparse matrix}, shape = [n_samples, n_features]
     
     Returns
     -------
     K : array, shape = [n_samples, n_bvectors]
         kernel matrix
     """
     X = array_tools.as_2d_array(X, True)
     test_X = X 
     if sp.issparse(test_X):
         test_X = array_tools.spmat_resize(test_X, self.train_X.shape[1])
     else:
         test_X = array_tools.as_dense_matrix(test_X)
     gamma = self.gamma
     m = self.train_X.shape[0]
     n = test_X.shape[0]
     #The Gaussian kernel matrix is constructed from a linear kernel matrix
     linkm = self.train_X * test_X.T
     linkm = array_tools.as_dense_matrix(linkm)
     if sp.issparse(test_X):
         test_norms = ((test_X.T.multiply(test_X.T)).sum(axis=0)).T
     else:
         test_norms = (np.multiply(test_X.T, test_X.T).sum(axis=0)).T
     K = mat(np.ones((m, 1), dtype = float64)) * test_norms.T
     K = K + self.train_norms * mat(np.ones((1, n), dtype = float64))
     K = K - 2 * linkm
     K = - gamma * K
     K = np.exp(K)
     return K.A.T
Exemple #2
0
 def getKM(self, X):
     """Returns the kernel matrix between the basis vectors and X.
     
     Parameters
     ----------
     X: {array-like, sparse matrix}, shape = [n_samples, n_features]
     
     Returns
     -------
     K : array, shape = [n_samples, n_bvectors]
         kernel matrix
     """
     test_X = X
     degree, coef0, gamma = self.degree, self.coef0, self.gamma
     if sp.issparse(test_X):
         test_X = array_tools.spmat_resize(test_X, self.train_X.shape[1])
     else:
         test_X = array_tools.as_dense_matrix(test_X)
     train_X = self.train_X
     K = array_tools.as_array(train_X * test_X.T)
     K *= gamma
     K += coef0
     K = K**degree
     if self.bias != 0:
         K += self.bias
     return K.T
Exemple #3
0
 def getKM(self, X):
     """Returns the kernel matrix between the basis vectors and X.
     
     Parameters
     ----------
     X : {array-like, sparse matrix}, shape = [n_samples, n_features]
     
     Returns
     -------
     K : array, shape = [n_samples, n_bvectors]
         kernel matrix
     """
     X = array_tools.as_2d_array(X, True)
     test_X = X
     degree, coef0, gamma = self.degree, self.coef0, self.gamma
     if sp.issparse(test_X):
         test_X = array_tools.spmat_resize(test_X, self.train_X.shape[1])
     else:
         test_X = array_tools.as_dense_matrix(test_X)
     train_X = self.train_X
     K = array_tools.as_array(train_X * test_X.T)
     K *= gamma
     K += coef0
     K = K ** degree
     return K.T
Exemple #4
0
 def getKM(self, X):
     """Returns the kernel matrix between the basis vectors and X.
     
     Parameters
     ----------
     X : {array-like, sparse matrix}, shape = [n_samples, n_features]
     
     Returns
     -------
     K : array, shape = [n_samples, n_bvectors]
         kernel matrix
     """
     X = array_tools.as_2d_array(X, True)
     test_X = X
     if sp.issparse(test_X):
         test_X = array_tools.spmat_resize(test_X, self.train_X.shape[1])
     else:
         test_X = array_tools.as_dense_matrix(test_X)
     gamma = self.gamma
     m = self.train_X.shape[0]
     n = test_X.shape[0]
     #The Gaussian kernel matrix is constructed from a linear kernel matrix
     linkm = self.train_X * test_X.T
     linkm = array_tools.as_dense_matrix(linkm)
     if sp.issparse(test_X):
         test_norms = ((test_X.T.multiply(test_X.T)).sum(axis=0)).T
     else:
         test_norms = (np.multiply(test_X.T, test_X.T).sum(axis=0)).T
     K = mat(np.ones((m, 1), dtype=float64)) * test_norms.T
     K = K + self.train_norms * mat(np.ones((1, n), dtype=float64))
     K = K - 2 * linkm
     K = -gamma * K
     K = np.exp(K)
     return K.A.T
Exemple #5
0
 def getKM(self, X):
     """Returns the kernel matrix between the basis vectors and X.
     
     Parameters
     ----------
     X: {array-like, sparse matrix}, shape = [n_samples, n_features]
     
     Returns
     -------
     K : array, shape = [n_samples, n_bvectors]
         kernel matrix
     """
     test_X = X
     if sp.issparse(test_X):
         test_X = array_tools.spmat_resize(test_X, self.train_X.shape[1])
     else:
         test_X = array_tools.as_dense_matrix(test_X)
     train_X = self.train_X
     K = train_X * test_X.T
     K = array_tools.as_array(K)
     if self.bias != 0:
         K += self.bias
     return K.T
Exemple #6
0
 def getKM(self, X):
     """Returns the kernel matrix between the basis vectors and X.
     
     Parameters
     ----------
     X : {array-like, sparse matrix}, shape = [n_samples, n_features]
     
     Returns
     -------
     K : array, shape = [n_samples, n_bvectors]
         kernel matrix
     """
     X = array_tools.as_2d_array(X, True)
     test_X = X
     if sp.issparse(test_X):
         test_X = array_tools.spmat_resize(test_X, self.train_X.shape[1])
     else:
         test_X = array_tools.as_dense_matrix(test_X)
     train_X = self.train_X
     K = train_X * test_X.T
     K = array_tools.as_array(K)
     if self.bias != 0:
         K += self.bias
     return K.T