请发布更多上下文以使您的问题可重现。如发布的那样,您的代码对我有用:
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]])