小编blm*_*ore的帖子

提取大矩阵的非对角切片

我有一个很大的nxn矩阵,想要拍摄不同大小的非对角切片.例如:

1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
Run Code Online (Sandbox Code Playgroud)

我想要一个R函数,当给定矩阵和"对角线切片的宽度"时,它将返回仅具有这些值的nxn矩阵.所以对于上面的矩阵,比方说3,我得到:

1 x x x x x
1 2 x x x x
1 2 3 x x x
x 2 3 4 x x
x x 3 4 5 x
x x x 4 5 6
Run Code Online (Sandbox Code Playgroud)

目前我正在使用(原谅我)一个非常慢的for循环:

getDiags<-function(ndiags, cormat){
  resmat=matrix(ncol=ncol(cormat),nrow=nrow(cormat))
  dimnames(resmat)<-dimnames(cormat)
  for(j …
Run Code Online (Sandbox Code Playgroud)

r matrix slice diagonal

13
推荐指数
1
解决办法
6007
查看次数

更快速地计算大矩阵中的非对角线平均值

我需要计算n×n矩阵中每个非对角线元素的平均值.下三角和上三角是多余的.这是我目前使用的代码:

A <- replicate(500, rnorm(500))
sapply(1:(nrow(A)-1), function(x) mean(A[row(A) == (col(A) - x)]))
Run Code Online (Sandbox Code Playgroud)

这似乎有效,但不适用于较大的矩阵.我拥有的不是很大,大约2-5000 ^ 2,但即使有1000 ^ 2,它也比我想要的时间更长:

A <- replicate(1000, rnorm(1000)) 
system.time(sapply(1:(nrow(A)-1), function(x) mean(A[row(A) == (col(A) - x)])))
>   user  system elapsed 
> 26.662   4.846  31.494  
Run Code Online (Sandbox Code Playgroud)

有更聪明的方法吗?

编辑为了澄清,我想独立地对待每个对角线的平均值,例如:

 1 2 3 4
 1 2 3 4
 1 2 3 4
 1 2 3 4
Run Code Online (Sandbox Code Playgroud)

我想要:

 mean(c(1,2,3))
 mean(c(1,2))
 mean(1)
Run Code Online (Sandbox Code Playgroud)

average r matrix

10
推荐指数
2
解决办法
1052
查看次数

使用缺少值的Python嵌套字典构建表

这是我当前的数据结构:

{'bin1': {'A': 14545,
          'B': 18579,
          'C': 5880,
          'D': 20771,
          'E': 404396},
 'bin2': {'A': 13200,
          'B': 12279,
          'C': 5000,
          'D': 16766,
          'E': 200344},
 [...] }
Run Code Online (Sandbox Code Playgroud)

我想以以下形式写表:

        A     B     C    D     E    
bin1  14545 18579 5880 20771 494396
bin2  13200 12279 5000 16766 200344
...
Run Code Online (Sandbox Code Playgroud)

目前,我正在使用原始打印循环(其中d ==上述字典):

# print the table header
labs = [i for i in d[d.keys()[0]]]
print "bin" + "\t" + "\t".join(labs)

# loop and print the values
for j in d:
    print j + "\t" + "\t".join(map(str, …
Run Code Online (Sandbox Code Playgroud)

python dictionary tabular

6
推荐指数
1
解决办法
2553
查看次数

将因子组合转换为R中存在/不存在的宽格式表

首先我很确定之前已经回答了这个问题,但搜索条件似乎很难找到,如果有重复的话,我会道歉.

说我有一系列因素:

all <- factor(letters)
Run Code Online (Sandbox Code Playgroud)

我继续使用这些因子级别的所有组合作为建模管道的一部分:

combos <- t(combn(as.character(all), 5))
head(combos)
#     [,1] [,2] [,3] [,4] [,5]
# [1,] "a"  "b"  "c"  "d"  "e" 
# [2,] "a"  "b"  "c"  "d"  "f" 
# [3,] "a"  "b"  "c"  "d"  "g" 
# ...
Run Code Online (Sandbox Code Playgroud)

我的问题是:如何将第二个矩阵转换为显示所有级别存在/不存在的矩阵,例如:

      a   b   c   d   e   f   g  ...
[1,]  1   1   1   1   1   0   0  ...
[2,]  1   1   1   1   0   1   0  ...
[3,]  1   1   1   1   0   0   1  ...
...
Run Code Online (Sandbox Code Playgroud)

鉴于我已经试过方面,我首先想到的是的,在行应用程序ifelse使用 …

combinations r

5
推荐指数
1
解决办法
347
查看次数

R中的连续独立方式(不使用滑动窗口)

我想计算一些类似于滚动平均值或移动平均值的东西但是没有通过滑动窗口这样做.例如,对于下面的一组数字,我希望下面显示的平均数为5组:

 1,2,3,4,5,1,2,4,5,6,7,8,1,2,3,1,1,3,2,1    
|    3    |   3.6   |   4.2   |   1.6   |  //mean of every 5 numbers
Run Code Online (Sandbox Code Playgroud)

我知道movingAveragesTTR lib 中的可用rollmean功能,以及使用滑动窗口的功能,所以这样做是相当简单的:

d <- c(1,2,3,4,5,1,2,4,5,6,7,8,1,2,3,1,1,3,2,1)
m <- rollmean(d,5)
m[seq(1,length(m),5)]
> [1] 3.0 3.6 4.2 1.6
Run Code Online (Sandbox Code Playgroud)

但我有一个大型数据集,必须有一个更有效的方法来计算这个...任何想法?我假设有一个函数可以做到这一点,但我不能想到这种类型的平均值被调用.

r moving-average

3
推荐指数
1
解决办法
395
查看次数