在 R 中创建包含表的列表

发布于 2025-01-17 13:16:41 字数 440 浏览 0 评论 0原文

我在r中有以下数据集,我想计算香农的熵。为了做到这一点,因为数据是连续的,我必须离散它们。 软件包的函数,可以计算$ x_1 $和$ x_2 $之间的熵

set.seed(1234)
data <- matrix(rnorm(150 * 11, mean = 0, sd = 1), 150, 11)

library(entropy)
dis <- discretize2d(data[,1],data[,2], numBins1 = 10, numBins2 = 10)

entropy(dis)

使用ivatize2d >包含所有iptize2d data之间的结果熵(DIS)。有人可以帮我编码吗?

I have the following dataset in R and I want to calculate Shannon's entropy. In order to do that since the data are continuous, I have to discretise them. Using the discretize2d function of Entropy package, the entropy between $X_1$ and $X_2$ can be calculated as follows:

set.seed(1234)
data <- matrix(rnorm(150 * 11, mean = 0, sd = 1), 150, 11)

library(entropy)
dis <- discretize2d(data[,1],data[,2], numBins1 = 10, numBins2 = 10)

entropy(dis)

I want to create a list containing all the discretize2d results between between the variables of data so i can later just use entropy(dis$1.2) and getting the same result as entropy(dis). Can someone help me code it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千纸鹤 2025-01-24 13:16:41

这是一个全基R解决方案。我们使用combn(x,m)函数来生成x的元素的所有组合,并带有size m。在这里,我们需要对m = 2。这将创建2 x 55矩阵。然后将应用()在该矩阵的列上迭代应用distize2d()apply()的第二个参数为2,意思是在列上应用。我们还指定Simplify = false,以便结果将留下列表,而不是被胁迫到数组。

combs <- combn(1:ncol(data), 2)
dis <- apply(combs, 2, function(x) discretize2d(dat[, x[1]], dat[, x[2]], numBins1 = 10, numBins2 = 10), simplify = FALSE)

如果您需要像指定的元素的名称,例如dis $ 1.2,您可以执行此操作:

names(dis) <- apply(combs, 2, paste, collapse = '.')

最后,您还可以使用lapply()一次计算所有元素的熵:

lapply(dis, entropy)

Here is an all-base R solution. We use the combn(x, m) function to generate all combinations of the elements of x with size m. Here we want pairs so m = 2. This creates a 2 by 55 matrix. Then use apply() to iteratively apply discretize2d() over columns of that matrix. The second argument of apply() is 2, meaning to apply over columns. We also specify simplify = FALSE so that the result will stay a list rather than being coerced to an array.

combs <- combn(1:ncol(data), 2)
dis <- apply(combs, 2, function(x) discretize2d(dat[, x[1]], dat[, x[2]], numBins1 = 10, numBins2 = 10), simplify = FALSE)

If you want names for the elements like you specified, such as dis$1.2, you can do this:

names(dis) <- apply(combs, 2, paste, collapse = '.')

Finally you could also calculate entropy for all elements at once with lapply():

lapply(dis, entropy)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文