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
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
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
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
def __init__(self, W, b): """Initializes a primal model @param W: coefficients of the linear model, one column per task @type W: numpy matrix @param b: bias of the model, one column per task @type b: numpy matrix """ self.W = array_tools.as_dense_matrix(W) self.b = b
def getPrimalDataMatrix(X, bias): """ Constructs the feature representation of the data. If bias is defined, a bias feature with value sqrt(bias) is added to each example. This function should be used when making predictions, or training the primal formulation of the learner. @param X: matrix containing the data @type X: scipy.sparse.base.spmatrix @param dimensionality: dimensionality of the feature space (by default the number of rows in the data matrix) @type dimensionality: integer @return: data matrix @rtype: scipy sparse matrix in csc format """ #if sp.issparse(X): # X = X.todense() X = array_tools.as_dense_matrix(X) if bias!=0: bias_slice = sqrt(bias)*ones((X.shape[0],1),dtype=float64) X = np.hstack([X,bias_slice]) return X
def getPrimalDataMatrix(X, bias): """ Constructs the feature representation of the data. If bias is defined, a bias feature with value sqrt(bias) is added to each example. This function should be used when making predictions, or training the primal formulation of the learner. @param X: matrix containing the data @type X: scipy.sparse.base.spmatrix @param dimensionality: dimensionality of the feature space (by default the number of rows in the data matrix) @type dimensionality: integer @return: data matrix @rtype: scipy sparse matrix in csc format """ #if sp.issparse(X): # X = X.todense() X = array_tools.as_dense_matrix(X) if bias != 0: bias_slice = sqrt(bias) * mat(ones((X.shape[0], 1), dtype=float64)) X = np.hstack([X, bias_slice]) return X
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
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