我已经创建了数据的稀疏表示,并希望将其转换为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通过这个dict,我创建了一个稀疏的表示形式如下:
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 (11x11),其中每一行i和列j代表一个单词,单元格指示单词i和j-共发生的频率。因此,一个开始是
cooc = np.zeros((len(w2i),len(w2i)), dtype=np.int16)然后,我想更新cooc,以便将与sparse中的单词组合相关联的行/列索引分配给相关的值。我该怎么做?
编辑:我知道我可以循环通过cooc并一个一个地分配每个单元格。但是,我的数据集很大,这将是时间密集型的.相反,我希望将cooc转换为一个Scipy稀疏矩阵,并使用toarray()方法。我该怎么做?
发布于 2022-01-12 21:47:19
我认为其他的答案是在重新发明一个已经存在的轮子。
import numpy as np
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)这个出现矩阵是一个稀疏矩阵,将它转化为共生矩阵只是一个简单的乘法(对角线是每个令牌出现的总次数)。
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}发布于 2022-01-12 18:24:26
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]])https://stackoverflow.com/questions/70683821
复制相似问题