Label Encoder functionality in R?

前端 未结 9 924
你的背包
你的背包 2021-02-06 08:56

In python, scikit has a great function called LabelEncoder that maps categorical levels (strings) to integer representation.

Is there anything in R to do this?

9条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-06 09:10

    Try CatEncoders package. It replicates the Python sklearn.preprocessing functionality.

    # variable to encode values
    colors = c("red", "red", "blue", "green")
    lab_enc = LabelEncoder.fit(colors)
    
    # new values are transformed to NA
    values = transform(lab_enc, c('red', 'red', 'yellow'))
    values
    
    # [1]  3  3 NA
    
    
    # doing the inverse: given the encoded numbers return the labels
    inverse.transform(lab_enc, values)
    # [1] "red" "red" NA   
    

    I would add the functionality of reporting the non-matching labels with a warning.

    PS: It also has the OneHotEncoder function.

提交回复
热议问题