- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 glmnet
和 onehot
包运行 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
函数从何而来并不完全清楚;它不存在于基础 R
或 glmnet
包中。
也就是说,我建议使用旧的内置备用函数 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/
我正在尝试使用 glmnet 和 onehot 包运行 ridge/lasso,但出现错误。 library(glmnet) library(onehot) set.seed(123) Sample
我正在应用与 luxor-demo 中类似的 add.distribution 规则,而我的策略只有多头头寸。 整个策略有效,但在应用参数集时出现以下错误: TakeProfitLONG 47 0.0
我是一名优秀的程序员,十分优秀!