在python中堆叠不等矩阵

时间:2013-09-16 03:51:13

标签: python arrays numpy matrix scipy

有人能告诉我如何连接两个不等的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))

1 个答案:

答案 0 :(得分: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]])
相关问题