def _csr_normalize_columns(m): factor = m.sum(0) nnzeros = np.where(factor > 0) factor[nnzeros] = 1.0 / factor[nnzeros] factor = np.array(factor)[0] spt.csr_scale_columns(m.shape[0], m.shape[1], m.indptr, m.indices, m.data, factor) return m
def normalize_sets_sparse (train, test): norm = train.copy() norm.data **= 2 # Square every value norm = norm.sum(axis=0) # Sum every column n_nonzeros = np.where(norm > 0) norm[n_nonzeros] = 1.0 / np.sqrt(norm[n_nonzeros]) norm = np.array(norm).T[0] sparsetools.csr_scale_columns(train.shape[0], train.shape[1], train.indptr, train.indices, train.data, norm) sparsetools.csr_scale_columns(test.shape[0], test.shape[1], test.indptr, test.indices, test.data, norm) return train, test
def normalize_sets_sparse(train, test): norm = train.copy() norm.data **= 2 # Square every value norm = norm.sum(axis=0) # Sum every column n_nonzeros = np.where(norm > 0) norm[n_nonzeros] = 1.0 / np.sqrt(norm[n_nonzeros]) norm = np.array(norm).T[0] sparsetools.csr_scale_columns(train.shape[0], train.shape[1], train.indptr, train.indices, train.data, norm) sparsetools.csr_scale_columns(test.shape[0], test.shape[1], test.indptr, test.indices, test.data, norm) return train, test
def test_scale_rows_and_cols(self): D = matrix([[1,0,0,2,3], [0,4,0,5,0], [0,0,6,7,0]]) #TODO expose through function S = csr_matrix(D) v = array([1,2,3]) csr_scale_rows(3,5,S.indptr,S.indices,S.data,v) assert_equal(S.todense(), diag(v)*D ) S = csr_matrix(D) v = array([1,2,3,4,5]) csr_scale_columns(3,5,S.indptr,S.indices,S.data,v) assert_equal(S.todense(), D*diag(v) ) # blocks E = kron(D,[[1,2],[3,4]]) S = bsr_matrix(E,blocksize=(2,2)) v = array([1,2,3,4,5,6]) bsr_scale_rows(3,5,2,2,S.indptr,S.indices,S.data,v) assert_equal(S.todense(), diag(v)*E ) S = bsr_matrix(E,blocksize=(2,2)) v = array([1,2,3,4,5,6,7,8,9,10]) bsr_scale_columns(3,5,2,2,S.indptr,S.indices,S.data,v) assert_equal(S.todense(), E*diag(v) ) E = kron(D,[[1,2,3],[4,5,6]]) S = bsr_matrix(E,blocksize=(2,3)) v = array([1,2,3,4,5,6]) bsr_scale_rows(3,5,2,3,S.indptr,S.indices,S.data,v) assert_equal(S.todense(), diag(v)*E ) S = bsr_matrix(E,blocksize=(2,3)) v = array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]) bsr_scale_columns(3,5,2,3,S.indptr,S.indices,S.data,v) assert_equal(S.todense(), E*diag(v) )
def csr_scale_columns(A, x): sparsetools.csr_scale_columns(A.shape[0], A.shape[1], A.indptr, A.indices, A.data, x)
def _csr_scale_columns(m, scale): spt.csr_scale_columns(m.shape[0], m.shape[1], m.indptr, m.indices, m.data, scale)