如何将 one-hot 编码数据传递给 nnet 模型以执行预测?
How can I pass one-hot encoded data to a nnet model to perform prediction?
我是数据科学的新手,想在 R 中构建一个神经网络模型。我读过有关在训练前一次性编码分类数据的信息。我试图实现这一点,但是,我在尝试训练模型时收到以下错误:
Error in model.frame.default(formula = nndf$class ~ ., data = train) :
invalid type (list) for variable 'nndf$class'
我已阅读 nnet 文档,其中解释了公式应按以下形式传递:
class ~ x1 + x2
但我仍然不确定如何正确传递数据。
代码如下:
nndf$al <- one_hot(as.data.table(nndf$al))
nndf$su <- one_hot(as.data.table(nndf$su))
nndf$rbc <- one_hot(as.data.table(nndf$rbc))
nndf$pc <- one_hot(as.data.table(nndf$pc))
nndf$pcc <- one_hot(as.data.table(nndf$pcc))
nndf$ba <- one_hot(as.data.table(nndf$ba))
nndf$htn <- one_hot(as.data.table(nndf$htn))
nndf$dm <- one_hot(as.data.table(nndf$dm))
nndf$cad <- one_hot(as.data.table(nndf$cad))
nndf$appet <- one_hot(as.data.table(nndf$appet))
nndf$pe <- one_hot(as.data.table(nndf$pe))
nndf$ane <- one_hot(as.data.table(nndf$ane))
nndf$class <- one_hot(as.data.table(nndf$class))
class(nndf$class)
# view the dataframe to ensure one hot encoding is correct
summary(nndf)
# randomly sample rows for tt split
train_idx <- sample(1:nrow(nndf), 0.8 * nrow(nndf))
test_idx <- setdiff(1:nrow(nndf), train_idx)
# prepare training set and corresponding labels
train <- nndf[train_idx,]
# prepare testing set and corresponding labels
X_test <- nndf[test_idx,]
y_test <- nndf[test_idx, "class"]
# create model with a single hidden layer containing 500 neurons
model <- nnet(nndf$class~., train, maxit=150, size=10)
# prediction
X_pred <- predict(train, type="raw")
假设
数据集中的所有变量 (nndf) 都是分类变量。
步骤
- 将除响应变量(即class)之外的所有变量转换为单热编码(即0,1格式)
one_hot方法
one_hot_df <- one_hot(nndf[, -13]) # 13 is the index of `class` variable.
model.matrix方法
model_mat_df <- model.matrix( ~ . - 1, nndf[, -13])
将class
转换为因子并将其添加到上述任一dfs中。
class <- as.factor(nndf$class)
final_df <- cbind(model_mat_df, class)
将 final_df
拆分为训练并测试并在模型中使用它。
nnet(class~., train, maxit=150, size=10)
我是数据科学的新手,想在 R 中构建一个神经网络模型。我读过有关在训练前一次性编码分类数据的信息。我试图实现这一点,但是,我在尝试训练模型时收到以下错误:
Error in model.frame.default(formula = nndf$class ~ ., data = train) :
invalid type (list) for variable 'nndf$class'
我已阅读 nnet 文档,其中解释了公式应按以下形式传递:
class ~ x1 + x2
但我仍然不确定如何正确传递数据。
代码如下:
nndf$al <- one_hot(as.data.table(nndf$al))
nndf$su <- one_hot(as.data.table(nndf$su))
nndf$rbc <- one_hot(as.data.table(nndf$rbc))
nndf$pc <- one_hot(as.data.table(nndf$pc))
nndf$pcc <- one_hot(as.data.table(nndf$pcc))
nndf$ba <- one_hot(as.data.table(nndf$ba))
nndf$htn <- one_hot(as.data.table(nndf$htn))
nndf$dm <- one_hot(as.data.table(nndf$dm))
nndf$cad <- one_hot(as.data.table(nndf$cad))
nndf$appet <- one_hot(as.data.table(nndf$appet))
nndf$pe <- one_hot(as.data.table(nndf$pe))
nndf$ane <- one_hot(as.data.table(nndf$ane))
nndf$class <- one_hot(as.data.table(nndf$class))
class(nndf$class)
# view the dataframe to ensure one hot encoding is correct
summary(nndf)
# randomly sample rows for tt split
train_idx <- sample(1:nrow(nndf), 0.8 * nrow(nndf))
test_idx <- setdiff(1:nrow(nndf), train_idx)
# prepare training set and corresponding labels
train <- nndf[train_idx,]
# prepare testing set and corresponding labels
X_test <- nndf[test_idx,]
y_test <- nndf[test_idx, "class"]
# create model with a single hidden layer containing 500 neurons
model <- nnet(nndf$class~., train, maxit=150, size=10)
# prediction
X_pred <- predict(train, type="raw")
假设
数据集中的所有变量 (nndf) 都是分类变量。
步骤
- 将除响应变量(即class)之外的所有变量转换为单热编码(即0,1格式)
one_hot方法
one_hot_df <- one_hot(nndf[, -13]) # 13 is the index of `class` variable.
model.matrix方法
model_mat_df <- model.matrix( ~ . - 1, nndf[, -13])
将
class
转换为因子并将其添加到上述任一dfs中。class <- as.factor(nndf$class)
final_df <- cbind(model_mat_df, class)
将
final_df
拆分为训练并测试并在模型中使用它。nnet(class~., train, maxit=150, size=10)