私は一連のテキストコーパスを使用しています。そのため、共起行列を構築する必要があります。私は現在、コードをテストしていますので、実行するたびに別の行列が得られます(list(set())
は順序付けされていません)。scipy.sparse.coo_matrix()
を使用して疎行列を構築しました。工事の種類。私は、これはそれを行うための最速かつ最もメモリeffictient方法だろうと想像する。私はそれらの値にアクセスしようとしたとき、私は私が得る疎行列をprint
た瞬間に、私はスパース行列からアイテムを抽出する
[<1x16 sparse matrix of type '<class 'numpy.float32'>'
with 10 stored elements in Compressed Sparse Row format>, <1x16 sparse matrix of type '<class 'numpy.float32'>'
with 4 stored elements in Compressed Sparse Row format>, <1x16 sparse matrix of type '<class 'numpy.float32'>'
with 4 stored elements in Compressed Sparse Row format>, <1x16 sparse matrix of type '<class 'numpy.float32'>'
with 7 stored elements in Compressed Sparse Row format>, <1x16 sparse matrix of type '<class 'numpy.float32'>'
を提示しています次のようになります。
(0, 1) 0.5
(0, 4) 1.0
(0, 6) 0.5
(1, 7) 1.0
(1, 11) 1.0
(1, 12) 1.0
(1, 13) 0.5
(2, 14) 0.5
...
(15, 6) 1.0
(15, 9) 0.5
(15, 15) 3.0
(15, 0) 2.0
(15, 1) 0.5
(15, 6) 0.5
(15, 14) 1.5
私は、それらが現れるときの値は可能です。上記の例の場合
私は、次のインスタンスを抽出します。私はsps_array.toarray
を使用してsps_array
を変換することができるよ瞬間
row = [0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4,
4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13,
13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15]
column = [1, 4, 6, 7, 11, 12, 13, 14, 15, 0, 4, 9, 12, 13, 14, 15, 4, 5, 12, 13,
4, 9, 13, 14, 0, 1, 2, 3, 5, 8, 10, 12, 13, 14, 2, 4, 12, 13, 0, 14,
15, 0, 8, 11, 13, 4, 7, 10, 11, 1, 3, 12, 14, 4, 8, 11, 13, 0, 7, 8,
10, 0, 1, 2, 4, 5, 9, 13, 0, 1, 2, 3, 4, 5, 7, 10, 12, 0, 1, 3, 4, 6,
9, 15, 0, 1, 6, 14]
values = [0.5, 1.0, 0.5, 1.0, 1.0, 1.0, 0.5, 0.5, 1.0, 1.0, 0.5, 0.5, 1.0, 0.5,
1.0, 0.5, 1.0, 0.5, 1.0, 0.5, 0.5, 1.0, 0.5, 1.0, 1.0, 1.0, 1.0, 0.5,
0.5, 1.0, 0.5, 0.5, 1.0, 1.0, 1.5, 2.0, 1.0, 2.5, 1.0, 3.0, 1.0, 0.5,
1.5, 2.0, 1.0, 1.0, 2.0, 0.5, 1.0, 0.5, 2.0, 2.0, 0.5, 4.0, 0.5, 0.5,
0.5, 1.0, 1.0, 0.5, 0.5, 1.0, 0.5, 1.0, 1.0, 0.5, 0.5, 0.5, 2.5, 1.0,
4.0, 1.0, 1.0, 1.5, 1.0, 1.0, 1.0, 0.5, 1.0, 0.5, 1.0, 1.0, 0.5, 3.0,
2.0, 0.5, 0.5, 1.5]
sps_array = sparse.coo_matrix((values, (row, column)), shape=(16, 16))
はその後
list1 = list(np.nonzero(sps_array > 0)[0])
list2 = list(np.nonzero(sps_array > 0)[1])
し、次のように作成するリストを作成し、その後for
座標を再構成するループ
index = 0
sps_coordinates = []
for i in range(token_size):
for j in range(list1_count[i]):
sps_coordinates.append((list1[index+j], list2[index+j]))
index += list1_count[i]
私は
list(sps_array[sps_array > 0]
で値を取得するには、私がやっていることに関連し、それらの座標と値を取得するためのより効率的な方法はありますか?コピー-N-ペーストを
しようとしたとき、私は奇妙な行動を観察しています私が作成した疎行列で '.row'または' .col'を使用してください。私は 'AttributeError:row not found'と表示されていますが、上記の例でも同じことをしようとすると、リストを出力します。 '.data'を使うときはbevahiourは存在せず、すべての場合に値が出力されます。私は@hpauljによって提案された 'np.transpose(np.nonzero(sps_array))'を使ってこの問題を解決し、適切な列と行のリストを生成するためにスライスしました。 – Lukasz
あなたのスパース行列が 'coo'形式ではないと思われます。そのフォーマットだけが 'row'と' col'属性を持っています。 'nonzero'は' tocoo'を使って入力をcooに変換します(必要な場合)。もっとシンプルであれば自由に「非ゼロ」に固執してください。 – hpaulj