如何有效地从稀疏矩阵中删除列? [英] How can I remove a column from a sparse matrix efficiently?
问题描述
如果我使用sparse.lil_matrix格式,如何才能轻松,高效地从矩阵中删除列?
If I am using the sparse.lil_matrix format, how can I remove a column from the matrix easily and efficiently?
推荐答案
我一直想要这个,实际上还没有很好的内置方法.这是一种方法.我选择制作lil_matrix的子类并添加remove_col函数.如果需要,可以改为将removecol函数添加到lib/site-packages/scipy/sparse/lil.py
文件中的lil_matrix类中.这是代码:
I've been wanting this myself and in truth there isn't a great built-in way to do it yet. Here's a way to do it. I chose to make a subclass of lil_matrix and add the remove_col function. If you want, you can instead add the removecol function to the lil_matrix class in your lib/site-packages/scipy/sparse/lil.py
file. Here's the code:
from scipy import sparse
from bisect import bisect_left
class lil2(sparse.lil_matrix):
def removecol(self,j):
if j < 0:
j += self.shape[1]
if j < 0 or j >= self.shape[1]:
raise IndexError('column index out of bounds')
rows = self.rows
data = self.data
for i in xrange(self.shape[0]):
pos = bisect_left(rows[i], j)
if pos == len(rows[i]):
continue
elif rows[i][pos] == j:
rows[i].pop(pos)
data[i].pop(pos)
if pos == len(rows[i]):
continue
for pos2 in xrange(pos,len(rows[i])):
rows[i][pos2] -= 1
self._shape = (self._shape[0],self._shape[1]-1)
我已经尝试过了,没有发现任何错误.我当然认为,这比将列切成薄片要好,据我所知,这只会创建一个新的矩阵.
I have tried it out and don't see any bugs. I certainly think that it is better than slicing the column out, which just creates a new matrix as far as I know.
我决定也要创建一个removerow函数,但是我认为它不如removecol好.我受到限制,因为无法以自己想要的方式从ndarray中删除一行.这是可以添加到上述类中的removerow
I decided to make a removerow function as well, but I don't think that it is as good as removecol. I'm limited by not being able to remove one row from an ndarray in the way that I would like. Here is removerow which can be added to the above class
def removerow(self,i):
if i < 0:
i += self.shape[0]
if i < 0 or i >= self.shape[0]:
raise IndexError('row index out of bounds')
self.rows = numpy.delete(self.rows,i,0)
self.data = numpy.delete(self.data,i,0)
self._shape = (self._shape[0]-1,self.shape[1])
也许我应该将这些功能提交到Scipy存储库.
Perhaps I should submit these functions to the Scipy repository.
这篇关于如何有效地从稀疏矩阵中删除列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!