如何将两个具有重叠但不相同词汇表的共现矩阵合并在一起?
How can I merge together two co-occurrence matrices with overlapping but not identical vocabularies?
我正在查看多个文档中的单词共现情况。对于每组文档,我找到了 N 个最常用词的词汇表。然后我为每个文档制作一个 NxN 矩阵,表示这些词是否在同一上下文中一起出现 window(k 个词的序列)。这是一个稀疏矩阵,所以如果我有 M 个文档,我就有一个 NxNxM 稀疏矩阵。因为 Matlab 不能存储超过 2 维的稀疏矩阵,所以我将这个矩阵展平为 (NxN)xM 稀疏矩阵。
我面临的问题是我为不同的文档集生成了 2 个这些共现矩阵。因为套路不同,词汇也不同。我不想将文档集合并在一起并重新计算共现矩阵,而是想将两个现有矩阵合并在一起。
例如,
N = 5; % Size of vocabulary
M = 5; % Number of documents
A = ones(N*N, M); % A is a flattened (N, N, M) matrix
B = 2*ones(N*N, M); % B is a flattened (N, N, M) matrix
A_ind = {'A', 'B', 'C', 'D', 'E'}; % The vocabulary labels for A
B_ind = {'A', 'F', 'B', 'C', 'G'}; % The vocabulary labels for B
应该合并生成一个 (49, 5) 矩阵,其中每个 (49, 1) 切片都可以重新整形为具有以下结构的 (7,7) 矩阵。
A B C D E F G
__________________________________________
A| 3 3 3 1 1 2 2
B| 3 3 3 1 1 2 2
C| 3 3 3 1 1 2 2
D| 1 1 1 1 1 0 0
E| 1 1 1 1 1 0 0
F| 2 2 2 0 0 2 2
G| 2 2 2 0 0 2 2
在 A 和 B 重叠的地方,应将同现计数相加。否则,元素应该是 A 中的计数或 B 中的计数。会有一些元素(示例中为 0)我没有计数统计信息,因为有些词汇只在 A 中,有些则只在 B 中.
关键是利用逻辑索引的扁平化能力。
A = ones(25, 5);
B = 2*ones(25,5);
A_ind = {'A', 'B', 'C', 'D', 'E'};
B_ind = {'A', 'F', 'B', 'C', 'G'};
new_ind = [A_ind, B_ind(~ismember(B_ind, A_ind))];
new_size = length(new_ind)^2;
new_array = zeros(new_size, 5);
% Find the indices that correspond to elements of A
A_overlap = double(ismember(new_ind, A_ind));
A_mask = (A_overlap'*A_overlap)==1;
% Find the indices that correspond to elements of B
B_overlap = double(ismember(new_ind, B_ind));
B_mask = (B_overlap'*B_overlap)==1;
% Flatten the logical indices to assign the elements to the new array
new_array(A_mask(:), :) = A;
new_array(B_mask(:), :) = new_array(B_mask(:), :) + B;
我正在查看多个文档中的单词共现情况。对于每组文档,我找到了 N 个最常用词的词汇表。然后我为每个文档制作一个 NxN 矩阵,表示这些词是否在同一上下文中一起出现 window(k 个词的序列)。这是一个稀疏矩阵,所以如果我有 M 个文档,我就有一个 NxNxM 稀疏矩阵。因为 Matlab 不能存储超过 2 维的稀疏矩阵,所以我将这个矩阵展平为 (NxN)xM 稀疏矩阵。
我面临的问题是我为不同的文档集生成了 2 个这些共现矩阵。因为套路不同,词汇也不同。我不想将文档集合并在一起并重新计算共现矩阵,而是想将两个现有矩阵合并在一起。
例如,
N = 5; % Size of vocabulary
M = 5; % Number of documents
A = ones(N*N, M); % A is a flattened (N, N, M) matrix
B = 2*ones(N*N, M); % B is a flattened (N, N, M) matrix
A_ind = {'A', 'B', 'C', 'D', 'E'}; % The vocabulary labels for A
B_ind = {'A', 'F', 'B', 'C', 'G'}; % The vocabulary labels for B
应该合并生成一个 (49, 5) 矩阵,其中每个 (49, 1) 切片都可以重新整形为具有以下结构的 (7,7) 矩阵。
A B C D E F G
__________________________________________
A| 3 3 3 1 1 2 2
B| 3 3 3 1 1 2 2
C| 3 3 3 1 1 2 2
D| 1 1 1 1 1 0 0
E| 1 1 1 1 1 0 0
F| 2 2 2 0 0 2 2
G| 2 2 2 0 0 2 2
在 A 和 B 重叠的地方,应将同现计数相加。否则,元素应该是 A 中的计数或 B 中的计数。会有一些元素(示例中为 0)我没有计数统计信息,因为有些词汇只在 A 中,有些则只在 B 中.
关键是利用逻辑索引的扁平化能力。
A = ones(25, 5);
B = 2*ones(25,5);
A_ind = {'A', 'B', 'C', 'D', 'E'};
B_ind = {'A', 'F', 'B', 'C', 'G'};
new_ind = [A_ind, B_ind(~ismember(B_ind, A_ind))];
new_size = length(new_ind)^2;
new_array = zeros(new_size, 5);
% Find the indices that correspond to elements of A
A_overlap = double(ismember(new_ind, A_ind));
A_mask = (A_overlap'*A_overlap)==1;
% Find the indices that correspond to elements of B
B_overlap = double(ismember(new_ind, B_ind));
B_mask = (B_overlap'*B_overlap)==1;
% Flatten the logical indices to assign the elements to the new array
new_array(A_mask(:), :) = A;
new_array(B_mask(:), :) = new_array(B_mask(:), :) + B;