0

有人能告诉我如何加入两个不相等的 numpy 数组(一个稀疏数组,一个密集数组)。我尝试使用 hstack/vstack 但不断收到维度错误。

from scipy import sparse 
from scipy.sparse import coo_matrix 
m=(coo_matrix(X_new)) # is a(7395,50000) sparse array 
a=(other) # is a (7395,20) dense array 
new_tr=scipy.sparse.hstack((m,a))
4

1 回答 1

0

请发布更多上下文以使您的问题可重现。如发布的那样,您的代码对我有用:

X_new = np.zeros((10,5), int)
X_new[(np.random.randint(0,10,5),np.random.randint(0,5,5))] = np.random.randint(0,10,5)
X_new
#array([[ 0, 0, 7, 0, 0],
#       [ 0, 0, 0, 0, 0],
#       [ 0, 0, 8, 3, 0],
#       [ 0, 0, 0, 0, 0],
#       [ 0, 0, 0, 0, 0],
#       [ 0, 0, 0, 0, 0],
#       [ 0, 0, 8, 0, 0],
#       [ 1, 0, 0, 0, 0],
#       [ 0, 0, 0, 0, 0],
#       [ 0, 0, 0, 0, 0]])

m = coo_matrix(X_new)
m
#<10x5 sparse matrix of type '<type 'numpy.int64'>'
#    with 5 stored elements in COOrdinate format>


a = np.matrix(np.random.randint(0,10,(10,2)))
a
#matrix([[2, 1],
#        [5, 2],
#        [4, 1],
#        [1, 4],
#        [5, 2],
#        [7, 2],
#        [6, 3],
#        [8, 4],
#        [5, 5],
#        [7, 4]])

new_tr = sparse.hstack([m,a])
new_tr
#<10x7 sparse matrix of type '<type 'numpy.int64'>'
#   with 25 stored elements in COOrdinate format>

new_tr.todense()
#matrix([[ 0, 0, 7, 0, 0, 2, 1],
#        [ 0, 0, 0, 0, 0, 5, 2],
#        [ 0, 0, 8, 3, 0, 4, 1],
#        [ 0, 0, 0, 0, 0, 1, 4],
#        [ 0, 0, 0, 0, 0, 5, 2],
#        [ 0, 0, 0, 0, 0, 7, 2],
#        [ 0, 0, 8, 0, 0, 6, 3],
#        [ 1, 0, 0, 0, 0, 8, 4],
#        [ 0, 0, 0, 0, 0, 5, 5],
#        [ 0, 0, 0, 0, 0, 7, 4]])
于 2013-09-16T12:52:25.233 回答