我創建了資料的稀疏表示,并希望將其轉換為 Numpy 陣列。
假設,我有以下資料(實際上data包含更多串列,每個串列更長):
data = [['this','is','my','first','dataset','here'],['but','here', 'is', 'another','one'],['and','yet', 'another', 'one']]
我有兩個dict專案將每個單詞映射到一個唯一的整數值,反之亦然:
w2i = {'this':0, 'is':1, 'my':2, 'first':3, 'dataset':4, 'here':5, 'but':6, 'another':7, 'one':8, 'and':9, 'yet':10}
此外,我有一個dict得到每個單詞組合的計數:
comb_dict = dict()
for text in data:
sorted_set_text = sorted(list(set(text)))
for i in range(len(sorted_set_text)-1):
for j in range(i 1, len(sorted_set_text)):
if (sorted_set_text[i],sorted_set_text[j]) in comb_dict:
comb_dict[(sorted_set_text[i],sorted_set_text[j])] = 1
else:
comb_dict[(sorted_set_text[i],sorted_set_text[j])] = 1
從這個字典中,我創建了一個稀疏表示,如下所示:
sparse = [(w2i[k[0]],w2i[k[1]],v) for k,v in comb_dict.items()]
此串列由元組組成,其中第一個值表示 x 軸的位置,第二個值表示 y 軸的位置,第三個值表示共現的數量:
[(4, 3, 1),
(4, 5, 1),
(4, 1, 1),
(4, 2, 1),
(4, 0, 1),
(3, 5, 1),
(3, 1, 1),
(3, 2, 1),
(3, 0, 1),
(5, 1, 2),
(5, 2, 1),
(5, 0, 1),
(1, 2, 1),
(1, 0, 1),
(2, 0, 1),
(7, 6, 1),
(7, 5, 1),
(7, 1, 1),
(7, 8, 2),
(6, 5, 1),
(6, 1, 1),
(6, 8, 1),
(5, 8, 1),
(1, 8, 1),
(9, 7, 1),
(9, 8, 1),
(9, 10, 1),
(7, 10, 1),
(8, 10, 1)]
現在,我想得到一個Numpy array(11 x 11) ,其中 i 行和 j 列的每一行代表一個單詞,單元格表示單詞 i 和 j 共同出現的頻率。因此,一個開始將是
cooc = np.zeros((len(w2i),len(w2i)), dtype=np.int16)
然后,我想更新cooc,以便為與單詞組合關聯的行/列索引sparse分配關聯的值。我怎樣才能做到這一點?
編輯:我知道我可以遍歷cooc并一個一個地分配每個單元格。但是,我的資料集很大,這將非常耗時。相反,我想轉換cooc為 Scipy 稀疏矩陣并使用該toarray()方法。我怎樣才能做到這一點?
uj5u.com熱心網友回復:
我認為這些其他答案有點重新發明已經存在的輪子。
import numpy as np
from scipy.sparse import vstack
from sklearn.feature_extraction.text import CountVectorizer
data = [['this','is','my','first','dataset','here'],['but','here', 'is', 'another','one'],['and','yet', 'another', 'one']]
我要把這些重新組合起來,然后使用 sklearn 的 CountVectorizer
data = [" ".join(x) for x in data]
encoder = CountVectorizer()
occurrence = encoder.fit_transform(data)
這個出現矩陣是一個稀疏矩陣,把它變成一個共現矩陣只是一個簡單的乘法(對角線是每個token出現的總次數)。
co_occurrence = occurrence.T @ occurrence
>>> co_occurrence.A
array([[1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1],
[1, 2, 1, 0, 0, 1, 1, 0, 2, 0, 1],
[0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0],
[0, 1, 1, 1, 1, 2, 2, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 2, 2, 1, 1, 1, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0],
[1, 2, 1, 0, 0, 1, 1, 0, 2, 0, 1],
[0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0],
[1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1]])
并且可以從編碼器中恢復行/列標簽:
encoder.vocabulary_
{'this': 9,
'is': 6,
'my': 7,
'first': 4,
'dataset': 3,
'here': 5,
'but': 2,
'another': 1,
'one': 8,
'and': 0,
'yet': 10}
uj5u.com熱心網友回復:
In [268]: alist = [(4, 3, 1),
...: (4, 5, 1),
...: (4, 1, 1),
...: (4, 2, 1),
...
...: (9, 10, 1),
...: (7, 10, 1),
...: (8, 10, 1)]
從串列中創建一個陣列:
In [269]: arr = np.array(alist)
In [270]: arr.shape
Out[270]: (29, 3)
并使用陣列的列填充定義的 (11,11) 陣列中的插槽:
In [271]: res = np.zeros((11,11),int)
In [272]: res[arr[:,0],arr[:,1]]=arr[:,2]
In [273]: res
Out[273]:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0],
[1, 2, 1, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0],
[0, 1, 0, 0, 0, 1, 1, 0, 2, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
您可以使用相同的列來創建scipy.sparse矩陣。
In [274]: from scipy import sparse
In [276]: M = sparse.coo_matrix((arr[:,2],(arr[:,0],arr[:,1])), shape=(11,11))
In [277]: M
Out[277]:
<11x11 sparse matrix of type '<class 'numpy.int64'>'
with 29 stored elements in COOrdinate format>
In [278]: M.A
Out[278]:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0],
[1, 2, 1, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0],
[0, 1, 0, 0, 0, 1, 1, 0, 2, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
uj5u.com熱心網友回復:
在不使用幼稚回圈的附加約束后編輯
您可以使用numbaJIT 裝飾器在執行前編譯回圈。這將比天真的回圈快得多。如果您使用dtype=np.uint16,您將使用默認 RAM 的 1/4 int64(最大值為65535,因此您應該可以使用 60k 字)。
我用 3 600 000 000 (num_words 2 ) 組合嘗試了這個,它在我的 c2016 筆記本電腦上運行了 43 秒。
完整代碼:
from numba import jit
# Create array of each word with dtype uint16 (max value 65535)
n_unique_words = 11
start_words = np.array([row[0] for row in sparse], dtype = np.uint16)
neighbour_words = np.array([row[1] for row in sparse], dtype = np.uint16)
frequences = np.array([row[2] for row in sparse], dtype = np.uint16)
# Numba loop method
@jit(nopython=True) # Set "nopython" mode for best performance, equivalent to @njit
def make_cooc_fast(start_words, neighbour_words, frequencies, n_unique_words): # Function is compiled to machine code when called the first time
big_cooc = np.zeros((n_unique_words, n_unique_words), dtype=np.int16)
for i in range(0, len(start_words)):
big_cooc[start_words[i], neighbour_words[i]] = frequencies[i]
big_cooc[neighbour_words[i], start_words[i]] = frequencies[i]
return big_cooc
cooc = make_cooc_fast(
start_words=start_words,
neighbour_words=neighbour_words,
frequencies=frequences,
n_unique_words=n_unique_words)
原始回復
如果我正確理解了這個問題,那么您已經完成了所有艱苦的作業。從這里開始,它應該是:
for row in sparse:
cooc[row[0], row[1]] = row[2]
cooc[row[1], row[0]] = row[2]
這是兩行,因為如果單詞a出現在單詞b n時間旁邊,那么單詞b也出現在單詞時間旁邊a n。
這個領域是我感興趣的領域 - 如果您想進一步討論,請隨時 PM 我。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/410485.html
標籤:
下一篇:將列內插到目標x坐標后的意外結果
