【发布时间】:2016-11-24 16:42:13
【问题描述】:
我有以下矩阵:
A.toarray()
array([[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
...,
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0],
[0, 0, 0, ..., 0, 0, 0]], dtype=int64)
type(A)
scipy.sparse.csr.csr_matrix
A.shape
(878049, 942)
还有矩阵B:
B
array([2248, 2248, 2248, ..., 0, 0, 0])
type(B)
numpy.ndarray
B.shape
(878049,)
我想在 C 中列堆栈 A 和 B,我尝试了以下操作:
C = sparse.column_stack([A,B])
然后:
/usr/local/lib/python3.5/site-packages/numpy/lib/shape_base.py in column_stack(tup)
315 arr = array(arr, copy=False, subok=True, ndmin=2).T
316 arrays.append(arr)
--> 317 return _nx.concatenate(arrays, 1)
318
319 def dstack(tup):
ValueError: all the input array dimensions except for the concatenation axis must match exactly
我的问题是如何保存尺寸。因此,您知道如何对它们进行列堆叠吗?
更新
我尝试了以下方法:
#Sorry for the name
C = np.vstack(( A.A.T, B)).T
我得到了:
array([[ 0, 0, 0, ..., 0, 6],
[ 0, 0, 0, ..., 0, 6],
[ 0, 0, 0, ..., 0, 6],
...,
[ 0, 0, 0, ..., 0, 1],
[ 0, 0, 0, ..., 0, 1],
[ 0, 0, 0, ..., 0, 1]], dtype=int64)
这是对它们进行列堆叠的正确方法吗?
【问题讨论】:
-
你在哪里找到
sparse.column_stack?有np.column_stack,但没有稀疏版本。 -
下面的所有答案都是稀疏的。看看这个答案stackoverflow.com/a/33259578/2988730。它看起来正是您正在寻找的东西。我投票决定以骗子的身份结束。
标签: python python-2.7 python-3.x numpy scipy