【发布时间】:2019-11-21 08:30:36
【问题描述】:
我有一个问题,我必须堆叠一个 numpy.ndarray(其中包含字符串值) 和一个 csr 矩阵(其中包含浮点值)
我尝试了以下操作
1)
from scipy.sparse import hstack
from scipy import sparse
temp = hstack((image_features,sparse.csr_matrix(feature_names)))
print(temp.shape)
print(type(temp))
这给了我以下错误
TypeError: no supported conversion for types: (dtype('O'),)
2)
from scipy.sparse import hstack
from scipy import sparse
temp = hstack((image_features.astype(object),feature_names))
print(temp.shape)
print(type(temp))
由于两个矩阵的大小,这给了我一个内存错误
print(type(image_features))
--> <class 'scipy.sparse.csr.csr_matrix'>
print(type(feature_names))
--> <class 'numpy.ndarray'>
print(image_features.shape)
--> (140047, 34464)
print(feature_names.shape)
--> (140047, 2)
两个矩阵的第一行供参考
print(image_features[0].toarray())
--> array([[0. , 0. , 0. , ..., 0. , 0.6384238,
0. ]])
print(feature_names[0])
--> array(['00007787805e474ea3f33c722178f550', 'Men'], dtype=object)
更新:
- 将 image_feature 转换为数组会出现内存错误
- 执行 image_feature.astype('O') 会出现内存错误
输出:
- 我希望输出是稀疏矩阵。
【问题讨论】:
-
不可能。
image_features太大而致密。feature_names不能做成浮点稀疏矩阵。 -
你为什么要用 numpy 来做这个?如果您使用 pandas,这可以很容易地处理。我也会在解决方案中添加它。
标签: python numpy scipy concatenation