R语言 one_hot函数在mltools包中不起作用

baubqpgj  于 2023-06-03  发布在  其他
关注(0)|答案(1)|浏览(299)

我需要一个热编码为我的分类数据在R。我跟踪了this answer,但似乎one_hot函数不起作用。甚至,我试图重现的例子提到的答案,但不工作。

install.packages(c("mltools"))
library(mltools)
library(data.table)

给予以下日志:

Installing package into ‘C:/Users/falamiw/AppData/Local/R/win-library/4.2’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/mltools_0.3.5.zip'
Content type 'application/zip' length 111375 bytes (108 KB)
downloaded 108 KB

package ‘mltools’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
    C:\Users\falamiw\AppData\Local\Temp\RtmpaSBOFc\downloaded_packages
Warning: package ‘mltools’ was built under R version 4.2.3Registered S3 method overwritten by 'data.table':
  method           from
  print.data.table     
data.table 1.14.8 using 2 threads (see ?getDTthreads).  Latest news: r-datatable.com

这个虚拟的例子也不起作用:

我不知道问题出在哪里。是从我这边还是包裹那边?

qyzbxkaa

qyzbxkaa1#

您可以考虑以下不需要任何包的方法:

Y <- c(1, 2, 3, 2, 3, 1, 0, 1, 0)
Y <- as.factor(Y)
df <- data.frame(Y)
stats::model.matrix(~ 0 + Y, df)

  Y0 Y1 Y2 Y3
1  0  1  0  0
2  0  0  1  0
3  0  0  0  1
4  0  0  1  0
5  0  0  0  1
6  0  1  0  0
7  1  0  0  0
8  0  1  0  0
9  1  0  0  0

相关问题