【问题标题】:How to one hot encode the response variable in tfdatasets r?如何对 tfdatasets r 中的响应变量进行一次热编码?
【发布时间】:2020-04-09 10:03:50
【问题描述】:

我正在尝试在 R 中使用 tfdatasets 包,以生成一个管道,该管道采用 tibble/dataframe 并输出一个热编码的 Species 响应变量。如何使用 tfdatasets 转换响应变量 (y) 以便将 Species 输出为热编码?

期望的输出是:

杂色,setosa,弗吉尼亚

0, 1, 0 ...

【问题讨论】:

  • 嗨,@Allan A,你找到这个问题的答案了吗?我开始探索tfdstfdatasets 包,我一直很困惑......如果你分享你找到的任何答案,这将对其他用户很有用......我们观察python版本的文档.. . 但不适用于 r...
  • 嗨@hamagust,谢谢你的问题,我至少找到了一个足够的解决方法,我将在下面分享。我希望它可以帮助某人。最好的问候艾伦

标签: r tensorflow-datasets


【解决方案1】:

正如上面评论中所解释的,这是一种适用于我的目的的解决方法,但不一定是 100% 纯 tfdatasets 解决方案。

library(tidyverse)
library(lubridate)
library(rsample)
library(recipes)
library(reticulate)
library(tensorflow)
library(tfdatasets)
library(keras)

iris %>%
  recipe(Species ~ .) %>%
  step_dummy(Species,
             one_hot = T) %>%
  prep() %>%
  juice() %>%
  select(contains("Species")) %>%
  as.matrix() %>%
  tensor_slices_dataset()

该解决方案的纯 tfdatasets 管道较少,而下面的解决方法是一种更纯的方法。

iris %>%
  mutate(Species = Species %>%
           as.integer()) %>%
  select(Species) %>%
  tensor_slices_dataset() %>%
  dataset_map(function(iteration){
   
    iteration$Species <- tf$one_hot(iteration$Species,
                                    3L)
    iteration
   
  })

【讨论】:

    猜你喜欢
    • 2018-07-16
    • 1970-01-01
    • 2020-11-12
    • 2019-02-14
    • 2020-11-14
    • 2022-09-27
    • 2021-02-10
    • 2018-04-08
    相关资源
    最近更新 更多