私はそれがcoo
形式の行列の上三角形を取得するのが最も簡単だと思う:coo
に変換し、かつ低三角形データを設定し
In [876]: A = sparse.random(5,5,.3,'csr')
In [877]: A = A+A.T
In [878]: A
Out[878]:
<5x5 sparse matrix of type '<class 'numpy.float64'>'
with 11 stored elements in Compressed Sparse Row format>
In [879]: A.A
Out[879]:
array([[ 0. , 0. , 0.81388978, 0. , 0. ],
[ 0. , 0. , 0.73944395, 0.20736975, 0.98968617],
[ 0.81388978, 0.73944395, 0. , 0. , 0. ],
[ 0. , 0.20736975, 0. , 0.05581152, 0.04448881],
[ 0. , 0.98968617, 0. , 0.04448881, 0. ]])
:
まず小さな対称行列を作りますバック0~0
In [880]: Ao = A.tocoo()
In [881]: mask = (Ao.row>Ao.col)
In [882]: mask
Out[882]:
array([False, False, False, False, True, True, True, False, False,
True, True], dtype=bool)
In [883]: Ao.data[mask]=0
変換する値、およびマトリックスを整理するeliminate_zeros
を使用します。
In [890]: A1 = Ao.tocsr()
In [891]: A1
Out[891]:
<5x5 sparse matrix of type '<class 'numpy.float64'>'
with 11 stored elements in Compressed Sparse Row format>
In [892]: A1.eliminate_zeros()
In [893]: A1
Out[893]:
<5x5 sparse matrix of type '<class 'numpy.float64'>'
with 6 stored elements in Compressed Sparse Row format>
In [894]: A1.A
Out[894]:
array([[ 0. , 0. , 0.81388978, 0. , 0. ],
[ 0. , 0. , 0.73944395, 0.20736975, 0.98968617],
[ 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0.05581152, 0.04448881],
[ 0. , 0. , 0. , 0. , 0. ]])
coo
とcsr
形式の両方がインプレースeliminate_zeros
方法を有しています。代わりに、あなただけのlower_triangle値を排除するためのモデルとして、このコードをAo.data[mask]=0
を使用して可能性の
def eliminate_zeros(self):
"""Remove zero entries from the matrix
This is an *in place* operation
"""
mask = self.data != 0
self.data = self.data[mask]
self.row = self.row[mask]
self.col = self.col[mask]
。
'vectorize_words'と' cosine_similarity'はどこから来ますか? 'cos_similarity'を生成している間に' duplicates 'を削除する方が、後で行列から削除する方が簡単でしょう。'疎な(sparse)'行列は個々の要素操作のために設計されていません。 – hpaulj
'scipy.spatial.distance.squareform'は、重複を排除するコンパクトなupper_triangle形式に/から変換します。疎な行列で動作するバージョンがあるかどうかわかりません。 – hpaulj
@hpaulj cosine_similarityはsklearnから来て、単語をベクトル化することは、各単語ベクトルを取得する私の関数です – nitheism