#'
#' Transform factors from a data.frame into dummy variables (one hot encoding)
#'
#' This function will transform all factors into dummy variables with one column
#' for each level of the factor (unlike the contrasts matrices that will drop the first
#' level). The factors with only two levels will have only one column (0/1 on the second
#' level). The ordered factors and logicals are transformed into numeric.
#' The numeric and text vectors will remain untouched.
#'
下面是一个使用
model.matrix
的简短示例,可以让您开始:在
python3
中使用pandas
,可以执行以下操作:更多示例请参见documentation。在
在R中模型.矩阵@Maurits Evers提出的方法是,除第一个因素外,该函数降低每个因素的第一个水平。有时这是你想要的,但有时不是(取决于@Maurits Evers强调的问题)。在
有几个函数分散在不同的包中来实现这一点(例如package
caret
请参见here中的几个示例)。在我使用了以下受@Jaap的Stack Overflow answer启发的函数
^{pr2}$示例:
^{3}$于2018年3月19日由reprex package(v0.2.0)创建。在
相关问题 更多 >
编程相关推荐