【问题标题】:How to make prediction based on a set of rules in R [closed]如何根据 R 中的一组规则进行预测 [关闭]
【发布时间】:2014-01-18 21:28:33
【问题描述】:

我有一个列表,其中包含一组规则(或键或字典;不管你怎么称呼它)。

> list.prob[c(1,2)] [[1]] x 不 是 阴天 0.07692308 0.42857143 下雨 0.38461538 0.33333333 晴天 0.53846154 0.23809524

因此,给定“否”的阴天的值为 -0.08,给定“是”的值为 0.43。

[[2]] 是的 x 不 是 酷0.2307692 0.3333333 热 0.3846154 0.2380952 轻度 0.3846154 0.4285714

同理,热给定“否”的值为 0.38,给定“是”为 0.24。

一旦建立了规则,我就有了一个字符矩阵

> 垫子[c(1:4),] 展望 温度 湿度 有风 [1、]“晴”“热”“高”“无” [2、]“晴”“热”“高”“是” [3、]“阴”“热”“高”“无” [4、]“下雨”“温和”“高”“无”

问题是“否”或“是”,如何使用先前的规则将矩阵中的单元格(存储为字符)转换为相应的数值。

【问题讨论】:

    标签: r key rule


    【解决方案1】:

    这是你想要的吗?我不确定是/否在哪里起作用,所以我只是查找了“是”的概率。

    a  <- matrix(runif(6), nrow = 3)
    weather <- c("sunny", "rainy", "overcast")
    temp <- c("cool", "hot", "mild")
    yn <- c("yes", "no")
    rownames(a) <- weather
    colnames(a) <- yn
    b  <- matrix(runif(6), nrow = 3)
    rownames(b) <- temp
    colnames(b) <- yn
    c <- data.frame(weather = sample(weather, 10, replace = T), 
         temp = sample(temp, 10, replace = T))
    d <- data.frame(weather = a[c$weather, "yes"], temp = b[c$temp, "yes"])
    a
    b
    c
    d
    

    【讨论】:

      【解决方案2】:

      你的例子是不可重现的,所以我试着这样做:

      list.prob <-
      list(structure(c(0.07692308, 0.38461538, 0.53846154, 0.42857143, 
      0.33333333, 0.23809524), .Dim = c(3L, 2L), .Dimnames = structure(list(
          x = c("overcast", "rainy", "sunny"), y = c("no", "yes")), .Names = c("x", 
      "y"))), structure(c(0.2307692, 0.3846154, 0.3846154, 0.3333333, 
      0.2380952, 0.4285714), .Dim = c(3L, 2L), .Dimnames = structure(list(
          x = c("cool", "hot", "mild"), y = c("no", "yes")), .Names = c("x", 
      "y"))))
      mat <-
      structure(c("sunny", "sunny", "overcast", "rainy", "hot", "hot", 
      "hot", "mild"), .Dim = c(4L, 2L), .Dimnames = list(NULL, c("outlook", 
      "temperature")))
      

      这提供了与您开始时非常相似的内容(除了有限的 mat 仅限于那些似乎与 list.prob 中的条目相对应的列:

      > list.prob
      [[1]]
                y
      x                  no       yes
        overcast 0.07692308 0.4285714
        rainy    0.38461538 0.3333333
        sunny    0.53846154 0.2380952
      
      [[2]]
            y
      x             no       yes
        cool 0.2307692 0.3333333
        hot  0.3846154 0.2380952
        mild 0.3846154 0.4285714
      
      > mat
           outlook    temperature
      [1,] "sunny"    "hot"      
      [2,] "sunny"    "hot"      
      [3,] "overcast" "hot"      
      [4,] "rainy"    "mild"     
      

      然后您的问题是根据list.prob 中的表翻译mat 中的每一列,但使用哪一列取决于另一个变量的值(我将其称为yesorno)。

      # setup
      res <- matrix(0,nrow=nrow(mat),ncol=ncol(mat))
      yesorno <- "yes"
      # actual computation
      for (col in seq_len(ncol(mat))) {
        res[,col] <- list.prob[[col]][,yesorno][mat[,col]]
      }
      

      给予

      > res
                [,1]      [,2]
      [1,] 0.2380952 0.2380952
      [2,] 0.2380952 0.2380952
      [3,] 0.4285714 0.2380952
      [4,] 0.3333333 0.4285714
      

      这里假设mat的第一列对应list.prob的第一个元素,以此类推;如果映射不是那么简单,那么您需要某种方式来关联它们(也许根据mat 的列名命名list.prob 的元素并循环列名以在list.prob 中进行查找) .

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-18
        • 1970-01-01
        • 2018-11-27
        • 1970-01-01
        • 1970-01-01
        • 2020-12-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多