### 在Ridge/Lasso回归中出现的h(simpleError(msg, call))错误

我在尝试使用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 sety_train <- train$SalePricey_test <- test$SalePrice# Define the x training and testx_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 testx_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.minbest.lambdamodel <- glmnet(x_train_matrix, y_train, alpha = 0, lambda = best.lambda)results_ridge <- predict(model,newx=x_test_matrix)

我知道我的数据是干净的,我的矩阵大小也是一致的,但每次尝试运行预测时都会遇到这个错误。

在评估选择函数’as.matrix’的参数’x’时发生错误:Cholmod错误’X和/或Y的维度错误’,文件../MatrixOps/cholmod_sdmult.c,第90行

我的教授还告诉我,在分割数据之前应该进行独热编码,但我觉得这没有意义。


回答:

很难调试这个特定的错误,因为不完全清楚你代码中的onehot函数是从哪里来的;它并不存在于基础Rglmnet包中。

尽管如此,我建议使用内置的传统函数model.matrix(如果你的数据集较大,可以使用其稀疏版本sparse.model.matrix)来创建glmnetx参数。model.matrix会自动为你对因子或分类变量进行独热编码。它需要一个模型公式作为输入,你可以从你的数据集中创建,如下所示。

# 创建模型公式y_variable <- "SalePrice"model_formula <- as.formula(paste(y_variable, "~",                                  paste(names(train)[names(train) != y_variable], collapse = "+"))) # 测试和训练矩阵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()函数,该函数基于你的测试/训练数据框构建矩阵。这可以直接作为cv.glmnetx参数输入,如下所示。

## 选项2:使用glmnet内置函数创建x矩阵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)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注