After pre-processing the data, we can create a co-occurrence matrix by iterating through each document and its words, and incrementing the count for each pair of words that appear within a certain window size. We can then convert this co-occurrence matrix into a word embedding using techniques such as Singular Value Decomposition (SVD) or Principal Component Analysis (PCA).

Here is some sample code in Python using the NumPy library to create and plot co-occurrence word embeddings:

import numpy as np
import matplotlib.pyplot as plt

# define some sample documents
docs = [
  "The quick brown fox jumps over the lazy dog",
  "The quick brown fox jumps over the lazy cat",
  "The lazy cat sleeps all day"
]

# define vocabulary, including start and end tokens
vocab = ["<START>", "<END>"]
for doc in docs:
  for word in doc.lower().split():
    if word not in vocab:
      vocab.append(word)

# define co-occurrence matrix
co_occurrence = np.zeros((len(vocab), len(vocab)))
window_size = 2

# iterate through each document and update co-occurrence matrix
for doc in docs:
  words = ["<START>"] + doc.lower().split() + ["<END>"]
  for i, word in enumerate(words):
    for j in range(max(0, i - window_size), min(len(words), i + window_size + 1)):
      if i != j:
        co_occurrence[vocab.index(word), vocab.index(words[j])] += 1

# perform SVD on co-occurrence matrix to get word embeddings
U, _, _ = np.linalg.svd(co_occurrence)
word_embeddings = U[:, :2]

# plot word embeddings
fig, ax = plt.subplots()
ax.scatter(word_embeddings[:, 0], word_embeddings[:, 1])
for i, word in enumerate(vocab):
  ax.annotate(word, (word_embeddings[i, 0], word_embeddings[i, 1]))
plt.show()

This code creates a co-occurrence matrix for the sample documents using a window size of 2, performs SVD on the matrix to get 2-dimensional word embeddings, and plots the embeddings using Matplotlib. The resulting plot should show the relative positions of each word in the embedding space


原文地址: https://www.cveoy.top/t/topic/eDin 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录