gpt4 book ai didi

r - 岭/套索回归中的 h(simpleError(msg, call)) 错误

转载 作者:行者123 更新时间:2023-12-04 03:38:56 28 4
gpt4 key购买 nike

我正在尝试使用 glmnetonehot 包运行 ridge/lasso,但出现错误。

library(glmnet)
library(onehot)
set.seed(123)

Sample <- HouseData[1:1460, ]
smp_size <- floor(0.5 * nrow(Sample))
train_ind <- sample(seq_len(nrow(Sample)), size = smp_size)
train <- Sample[train_ind, ]
test <- Sample[-train_ind, ]

############Ridge & Lasso Regressions ################

# Define the response for the training + test set
y_train <- train$SalePrice
y_test <- test$SalePrice

# Define the x training and test
x_train <- train[,!names(train)=="SalePrice"]
x_test <- test[,!names(train)=="SalePrice"]
str(y_train)

## encoding information for training set
x_train_encoded_data_info <- onehot(x_train,stringsAsFactors = TRUE, max_levels = 50)
x_train_matrix <- (predict(x_train_encoded_data_info,x_train))
x_train_matrix <- as.matrix(x_train_matrix)

# create encoding information for x test
x_test_encoded_data_info <- onehot(x_test,stringsAsFactors = TRUE, max_levels = 50)
x_test_matrix <- (predict(x_test_encoded_data_info,x_test))
str(x_train_matrix)

###Calculate best lambda
cv.out <- cv.glmnet(x_train_matrix, y_train,
alpha = 0, nlambda = 100,
lambda.min.ratio = 0.0001)

best.lambda <- cv.out$lambda.min
best.lambda
model <- glmnet(x_train_matrix, y_train, alpha = 0, lambda = best.lambda)
results_ridge <- predict(model,newx=x_test_matrix)

我知道我的数据是干净的并且我的矩阵大小相同,但是当我尝试运行我的预测时我总是收到这个错误。

Error in h(simpleError(msg, call)) : error in evaluating the argument 'x' in selecting a method for function 'as.matrix': Cholmod error 'X and/or Y have wrong dimensions' at file ../MatrixOps/cholmod_sdmult.c, line 90

我的教授还告诉我在拆分数据之前进行单热编码,但这对我来说毫无意义。

最佳答案

很难调试该特定错误,因为您的代码中的 onehot 函数从何而来并不完全清楚;它不存在于基础 Rglmnet 包中。

也就是说,我建议使用旧的内置备用函数 model.matrix(或者它的稀疏表亲 sparse.model.matrix,如果你有更大的数据集)用于为 glmnet 创建 x 参数。 model.matrix 会自动为您编码因子或分类变量。它需要一个模型公式作为输入,您可以从数据集中创建它,如下所示。

# create the model formula
y_variable <- "SalePrice"
model_formula <- as.formula(paste(y_variable, "~",
paste(names(train)[names(train) != y_variable], collapse = "+")))
# test & train matrices
x_train_matrix <- model.matrix(model_formula, data = train)[, -1]
x_test_matrix <- model.matrix(model_formula, data = test)[, -1]

###Calculate best lambda
cv.out <- cv.glmnet(x_train_matrix, y_train,
alpha = 0, nlambda = 100,
lambda.min.ratio = 0.0001)

第二种较新的选择是使用内置的 glmnet 函数 makeX(),它根据您的测试/训练数据帧构建矩阵。这可以作为 x 参数输入到 cv.glmnet 中,如下所示。

## option 2: use glmnet built in function to create x matrices
x_matrices <- glmnet::makeX(train = train[, !names(train) == "SalePrice"],
test = test[, !names(test) == "SalePrice"])

###Calculate best lambda
cv.out <- cv.glmnet(x_matrices$x, y_train,
alpha = 0, nlambda = 100,
lambda.min.ratio = 0.0001)

关于r - 岭/套索回归中的 h(simpleError(msg, call)) 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66415958/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com