R似乎很奇怪,Python似乎也很顺利

2024-10-01 22:39:44 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的代码运行结果出乎意料,我觉得有点奇怪,首先定义featfun():

featfun <- function(yi_1, yi, i) {
    all_fea <- list(c(1, 2, 2),
                    c(1, 2, 3),
                    c(1, 1, 2),
                    c(2, 1, 3),
                    c(2, 1, 2),
                    c(2, 2, 3),
                    c(   1, 1),
                    c(   2, 1),
                    c(   2, 2),
                    c(   1, 2),
                    c(   1, 3),
                    c(   2, 3))
    weights <- c(1,1,0.6,1,1,0.2,1,0.5,0.5,0.8,0.8,0.5)

    idx1 <- 0; idx2 <- 0
    if (list(c(yi_1, yi, i)) %in% all_fea) {
      idx1 <- which(all_fea %in% list(c(yi_1, yi, i)))
    }
    if (list(c(yi, i)) %in% all_fea) {
      idx2 <- which(all_fea %in% list(c(yi, i)))
    }

    if (idx1 != 0 & idx2 != 0) {
      return(list(c(1, weights[idx1]), c(1, weights[idx2])))
    } else if (idx1 != 0 & idx2 == 0) {
      return(list(c(1, weights[idx1])))
    } else if (idx1 == 0 & idx2 != 0) {
      return(list(c(1, weights[idx2])))
    } else {
      return(NA)
    }
  }

 > featfun(1,1,2)
    [[1]]
    [1] 1.0 0.6

    [[2]]
    [1] 1.0 0.8

我将featfun()与for循环结合起来:

> for (k in seq(2,3)) {
  +   cat("k=",k,"\n")
  +   for (i in seq(1, 2)) {
    +     cat("i=", i,"\n")
    +     print(featfun(1, i, k))
    +   }
  + }
k= 2 
i= 1 
[[1]]
[1] 1.0 0.6

i= 2 
[[1]]
[1] 1 1

[[2]]
[1] 1.0 0.5

k= 3 
i= 1 
[[1]]
[1] 1.0 0.8

i= 2 
[[1]]
[1] 1 1

我们可以看到,当k=2,i=1时,它只返回第一个元素“[1]1.0.6”,第二个元素丢失,这与featfun(1,1,2)的结果不一样。你知道吗

此外,我还使用python重写了代码。下面是python代码:

def featfun(yi_1, yi, i):
  all_fea = [
    [1,2,2],
    [1,2,3],
    [1,1,2],
    [2,1,3],
    [2,1,2],
    [2,2,3],
    [  1,1],
    [  2,1],
    [  2,2],
    [  1,2],
    [  1,3],
    [  2,3]]
  weights = [1,1,0.6,1,1,0.2,1,0.5,0.5,0.8,0.8,0.5]
  idx1 = 999
  idx2 = 999
  if [yi_1,yi,i] in all_fea:
    idx1 = all_fea.index([yi_1, yi, i])
  if [yi, i] in all_fea:
    idx2 = all_fea.index([yi, i])

  if (idx1!=999)&(idx2!=999):
    return [[1,weights[idx1]],[1,weights[idx2]]]
  elif (idx1!=999)&(idx2==999):
    return [1,weights[idx1]]
  elif (idx1==999)&(idx2!=999):
    return [1,weights[idx2]]
  else:
    return None

featfun(1,1,2)返回[[1,0.6],[1,0.8]]。你知道吗

然后,我再次将基于python的featfun与for循环结合起来:

for k in [2,3]:
  for i in [1,2]:
      return featfun(1,i,k)

以下是返回结果,正确的结果,即与教科书中的答案相同。你知道吗

[[1, 0.6], [1, 0.8]]
[[1, 1], [1, 0.5]]
[1, 0.8]
[[1, 1], [1, 0.5]]

我的R码怎么了?或者看起来R出了什么问题? 我希望有人能帮我!谢谢!你知道吗


Tags: inwhichforreturnifallelseseq
1条回答
网友
1楼 · 发布于 2024-10-01 22:39:44

好吧,我不太清楚为什么会出现这个问题,但这是一个数值精度问题。当您使用seq(1,2)seq(2,3)时,它们是整数,all_fea列表是数字的,并且由于某种原因(这是不寻常的)匹配不起作用。如果将all_fea列表项设为整数,则它可以工作:

all_fea <- list(c(1L, 2L, 2L),
                c(1L, 2L, 3L),
                c(1L, 1L, 2L),
                c(2L, 1L, 3L),
                c(2L, 1L, 2L),
                c(2L, 2L, 3L),
                c(   1L, 1L),
                c(   2L, 1L),
                c(   2L, 2L),
                c(   1L, 2L),
                c(   1L, 3L),
                c(   2L, 3L))

以上为手动方式。或者您可以保持原样并添加行all_fea = lapply(all_fea, as.integer)。不管怎样,在那次改变之后,你的循环工作正常。你知道吗

相关问题 更多 >

    热门问题