相关疑难解决方法(0)

HDF5中的稀疏阵列支持

我需要以某种方式在磁盘上存储512 ^ 3阵列,我目前正在使用HDF5.由于阵列稀疏,因此浪费了大量磁盘空间.

HDF5是否为稀疏阵列提供任何支持?

sparse-array hdf5 sparse-matrix

14
推荐指数
2
解决办法
5622
查看次数

使用pytables,效率更高:scipy.sparse还是numpy密集矩阵?

使用时pytables,对于scipy.sparse矩阵格式没有支持(据我所知),所以要存储矩阵我必须做一些转换,例如

def store_sparse_matrix(self):
    grp1 = self.getFileHandle().createGroup(self.getGroup(), 'M')
    self.getFileHandle().createArray(grp1, 'data', M.tocsr().data)
    self.getFileHandle().createArray(grp1, 'indptr', M.tocsr().indptr)
    self.getFileHandle().createArray(grp1, 'indices', M.tocsr().indices)

def get_sparse_matrix(self):
    return sparse.csr_matrix((self.getGroup().M.data, self.getGroup().M.indices, self.getGroup().M.indptr))
Run Code Online (Sandbox Code Playgroud)

麻烦的是该get_sparse函数需要一些时间(从磁盘读取),如果我理解正确也需要数据适合内存.

唯一的其他选择似乎是将矩阵转换为密集格式(numpy array)然后pytables正常使用.然而,这似乎是相当低效的,虽然我想也许pytables会处理压缩本身?

python numpy scipy sparse-matrix pytables

9
推荐指数
1
解决办法
1947
查看次数

将scipy稀疏矩阵存储为HDF5

我想以HDF5格式压缩和存储一个巨大的Scipy矩阵.我该怎么做呢?我试过以下代码:

a = csr_matrix((dat, (row, col)), shape=(947969, 36039))
f = h5py.File('foo.h5','w')    
dset = f.create_dataset("init", data=a, dtype = int, compression='gzip')
Run Code Online (Sandbox Code Playgroud)

我得到这样的错误,

TypeError: Scalar datasets don't support chunk/filter options
IOError: Can't prepare for writing data (No appropriate function for conversion path)
Run Code Online (Sandbox Code Playgroud)

我无法将其转换为numpy数组,因为会有内存溢出.什么是最好的方法?

python hdf5 scipy sparse-matrix h5py

9
推荐指数
2
解决办法
2768
查看次数

如何在Python中有效地计算巨大的矩阵乘法(tfidf特征)?

我目前想要使用余弦相似度和python中的Tfidf功能来计算所有对文档的相似度.我的基本方法如下:

from sklearn.feature_extraction.text import TfidfVectorizer
#c = [doc1, doc2, ..., docn]
vec = TfidfVectorizer()
X = vec.fit_transform(c)
del vec
Y = X * X.T
Run Code Online (Sandbox Code Playgroud)

工作得很好,但不幸的是,不是我的大数据集.X具有维度,(350363, 2526183)因此输出矩阵Y应该具有(350363, 350363).由于tfidf功能,X非常稀疏,因此很容易适合内存(仅约2GB).然而,在运行一段时间后,乘法会给我一个内存错误(即使内存不满但我想scipy是如此聪明以至于期望内存使用).

我已经尝试过使用dtypes而没有任何成功.我还确保numpy和scipy将他们的BLAS库链接起来 - 虽然这对csr_matrix点功能没有影响,因为它在C中实现.我想可能使用像memmap这样的东西,但我不确定那.

有没有人知道如何最好地接近这个?

python numpy matrix scipy scikit-learn

4
推荐指数
2
解决办法
1809
查看次数