开始使用免费开始使用

Data encoding

Encoding of categorical data makes them useful for machine learning algorithms. R encodes factors internally, but encoding is necessary for the development of your own models.

In this exercise, you'll first build a linear model using lm() and then develop your own model step-by-step.

In one hot encoding, a separate column is created for each of the levels.

Note that one of the columns can be derived based on the others (e.g. 0's in the columns "B" and "C" imply 1 in the "A" column). So, you can drop the first column for the linear regression. We will review linear models in more detail in the next chapter.

For one hot encoding, you can use dummyVars() from the caret package.

To use it, first create the encoder and then transform the dataset:

encoder <- dummyVars(~ category, data = df)
predict(encoder, newdata = df)

The complete cases of the survey dataset from the MASS package are available as survey. The caret package has been preloaded.

本练习是课程的一部分

Practicing Statistics Interview Questions in R

查看课程

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Fit a linear model
lm(___ ~ Exer, data = ___)
编辑并运行代码