gpt4 book ai didi

在数组向量算法中回收长度为 1 的数组已被弃用。平方误差的优化和

转载 作者:行者123 更新时间:2023-12-04 15:45:39 25 4
gpt4 key购买 nike

我需要找到最佳权重 (a) 以最小化误差平方和。我以此为例,因为我将有一个更复杂的问题需要解决,需要使用除 lm 之外的其他一些优化包。我收到 warnings() 说“在 * y 中。在数组向量算法中回收长度为 1 的数组已被弃用。改用 c() 或 as.vector()。”我尝试了 as.vector(a * y), a[1] * y, a * as.vector(y) 但没有任何效果。

我应该怎么做才能摆脱这条消息?

install.packages("NlcOptim")
library(NlcOptim)

x <- c(1:4)
y <- c(2,4,6,8)

objfun <- function(a) {
return(sum((x-a*y)^2))
}
x0 <- 1
solnl(x0,objfun = objfun)

这是我在键入 warnings() 后看到的错误消息:

1: In a * y :
Recycling array of length 1 in array-vector arithmetic is deprecated.
Use c() or as.vector() instead.

最佳答案

实际上我建议什么都不做。 R 根本不喜欢将一维数组添加到数值向量。

x <- array(1, dim = 1);
x + c(1,1)

[1] 2 2

Warning message:
In x + c(1, 1) :
Recycling array of length 1 in array-vector arithmetic is deprecated.
Use c() or as.vector() instead.

如您所见,结果是正确的,因此忽略该警告是非常安全的。

关于在数组向量算法中回收长度为 1 的数组已被弃用。平方误差的优化和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55884074/

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