本文介绍了排列方形2向列联表(矩阵)的列以最大化其对角线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

进行聚类后,找到的标签毫无意义.可以计算一个列联表,以查看哪些标签与原始类别最相关.

After doing clustering, the labels found are meaningless. One can calculate a contingency table to see which labels are most related to the original classes if the ground-truth is available.

我想自动排列列联表的列以使其对角线最大化.例如:

I want to automatically permute columns of a contingency table to maximize its diagonal. For example:

# Ground-truth labels
c1 = c(1,1,1,1,1,2,2,2,3,3,3,3,3,3,3)
# Labels found
c2 = c(3,3,3,3,1,1,1,1,2,2,2,3,2,2,1)
# Labels found but renamed correctly
c3 = c(1,1,1,1,2,2,2,2,3,3,3,1,3,3,2)

# Current output
tab1 <- table(c1,c2)
#   c2
#c1  1 2 3
#  1 1 0 4
#  2 3 0 0
#  3 1 5 1

# Desired output
tab2 <- table(c1,c3)
#   c3
#c1  1 2 3
#  1 4 1 0
#  2 0 3 0
#  3 1 1 5

实际上, c3 不可用.是否有一种简单的方法可以从 c2 tab1 获得 c3 tab2 ?

In reality, c3 is not available. Is there an easy way to obtain c3, tab2 from c2, tab1?

推荐答案

c1 <- c(1,1,1,1,1,2,2,2,3,3,3,3,3,3,3)
c2 <- c(3,3,3,3,1,1,1,1,2,2,2,3,2,2,1)

## table works with factor variables internally
c1 <- as.factor(c1)
c2 <- as.factor(c2)

tab1 <- table(c1, c2)
#       c2
#    c1  1 2 3
#      1 1 0 4
#      2 3 0 0
#      3 1 5 1

您的问题本质上是:如何重新调整c2 的级别,以使一行中的最大值位于主对角线上.就矩阵运算而言,这是列排列.

Your question is essentially: how to re-level c2 so that the maximum value on a row sits on the main diagonal. In terms of matrix operation, this is a column permutation.

## find column permutation index
## this can potentially be buggy if there are multiple maxima on a row
## because `sig` may then not be a permutation index vector
## A simple example is:
## tab1 <- matrix(5, 3, 3); max.col(tab1, "first")
sig <- max.col(tab1, "first")
#[1] 3 1 2

## re-level `c2` (create `c3`)
c3 <- factor(c2, levels = levels(c2)[sig])

## create new contingency table
table(c1, c3)
#   c3
#c1  3 1 2
#  1 4 1 0
#  2 0 3 0
#  3 1 1 5

## if creation of `c3` is not necessary, just do
tab1[, sig]
#   c3
#c1  3 1 2
#  1 4 1 0
#  2 0 3 0
#  3 1 1 5

这篇关于排列方形2向列联表(矩阵)的列以最大化其对角线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 16:23