在 python 中使用 window 大小计算的缩放共现矩阵
Scaled Co-occurrence matrix with window size calculation in python
比如说,我有一个 CSV 格式的数据集,其中包含 sentences/paragraphs 行。
假设,它看起来像这样:
df = ['A B X B', 'X B B']
现在,我可以生成如下所示的共现矩阵
A B X
A 0 2 1
B 2 0 4
X 1 4 0
这里,(A,B,X)是单词。它说 B 出现在 X 出现的地方 = 4 次
我使用的代码
def co_occurrence(sentences, window_size):
d = defaultdict(int)
vocab = set()
for text in sentences:
# preprocessing (use tokenizer instead)
text = text.lower().split()
# iterate over sentences
for i in range(len(text)):
token = text[i]
vocab.add(token) # add to vocab
next_token = text[i+1 : i+1+window_size]
for t in next_token:
key = tuple( sorted([t, token]) )
d[key] += 1
# formulate the dictionary into dataframe
vocab = sorted(vocab) # sort vocab
df = pd.DataFrame(data=np.zeros((len(vocab), len(vocab)), dtype=np.int16),
index=vocab,
columns=vocab)
for key, value in d.items():
df.at[key[0], key[1]] = value
df.at[key[1], key[0]] = value
return df
这段代码的美妙之处在于它允许我选择 windows 大小。这意味着如果一个特定的单词没有出现在总句子大小的固定范围内,那么它就会被忽略。但我想缩放它。
所以这意味着如果一个词与目标词“to”相距很远,那么它将被赋予较小的值。不幸的是,我找不到合适的解决方案。 scikit-learn 之类的软件包是否可行?或者除了原始编码还有其他方法吗?
Here 的实现可以根据输入句子中单词标记之间的距离选择性地缩放累积的同现值:
In [11]: sentences = ['from swerve of shore to bend of bay , brings'.split()]
In [12]: index, matrix = co_occurence_matrix(sentences, window=3, scale=True)
In [13]: cell = index['bend'], index['of']
In [14]: matrix[cell]
Out[14]: 1.3333333333333333
In [15]: index, matrix = co_occurence_matrix(sentences, window=3, scale=False)
In [16]: matrix[cell]
Out[16]: 2.0
In [17]: {w: matrix[index['to']][i] for w, i in index.items()}
Out[17]:
{',': 0.0,
'bend': 1.0,
'of': 1.0,
'bay': 0.3333333333333333,
'brings': 0.0,
'to': 0.0,
'from': 0.0,
'shore': 1.0,
'swerve': 0.3333333333333333}
比如说,我有一个 CSV 格式的数据集,其中包含 sentences/paragraphs 行。 假设,它看起来像这样:
df = ['A B X B', 'X B B']
现在,我可以生成如下所示的共现矩阵
A B X
A 0 2 1
B 2 0 4
X 1 4 0
这里,(A,B,X)是单词。它说 B 出现在 X 出现的地方 = 4 次 我使用的代码
def co_occurrence(sentences, window_size):
d = defaultdict(int)
vocab = set()
for text in sentences:
# preprocessing (use tokenizer instead)
text = text.lower().split()
# iterate over sentences
for i in range(len(text)):
token = text[i]
vocab.add(token) # add to vocab
next_token = text[i+1 : i+1+window_size]
for t in next_token:
key = tuple( sorted([t, token]) )
d[key] += 1
# formulate the dictionary into dataframe
vocab = sorted(vocab) # sort vocab
df = pd.DataFrame(data=np.zeros((len(vocab), len(vocab)), dtype=np.int16),
index=vocab,
columns=vocab)
for key, value in d.items():
df.at[key[0], key[1]] = value
df.at[key[1], key[0]] = value
return df
这段代码的美妙之处在于它允许我选择 windows 大小。这意味着如果一个特定的单词没有出现在总句子大小的固定范围内,那么它就会被忽略。但我想缩放它。
所以这意味着如果一个词与目标词“to”相距很远,那么它将被赋予较小的值。不幸的是,我找不到合适的解决方案。 scikit-learn 之类的软件包是否可行?或者除了原始编码还有其他方法吗?
Here 的实现可以根据输入句子中单词标记之间的距离选择性地缩放累积的同现值:
In [11]: sentences = ['from swerve of shore to bend of bay , brings'.split()]
In [12]: index, matrix = co_occurence_matrix(sentences, window=3, scale=True)
In [13]: cell = index['bend'], index['of']
In [14]: matrix[cell]
Out[14]: 1.3333333333333333
In [15]: index, matrix = co_occurence_matrix(sentences, window=3, scale=False)
In [16]: matrix[cell]
Out[16]: 2.0
In [17]: {w: matrix[index['to']][i] for w, i in index.items()}
Out[17]:
{',': 0.0,
'bend': 1.0,
'of': 1.0,
'bay': 0.3333333333333333,
'brings': 0.0,
'to': 0.0,
'from': 0.0,
'shore': 1.0,
'swerve': 0.3333333333333333}