小智 6
更新:
dot在对数组执行标量加法时,必须在 Julia 1.0 中使用运算符。IEy = m*x .+ b
您还可以使用简单的线性代数进行线性回归。下面是一个例子:
# Linear Algebra style
# For single linear regresion y= mx .+ b
m = 3.3; b = 2; x = rand(100,1)
y = m * x .+ b
# add noise
yn= y + randn(size(y)) * 0.5
# regression
X = zeros(100,2); X[:,1] = x; X[:,2] = 1.0
coeff_pred = X\yn
slope = round(coeff_pred[1], 2)
intercept = round(coeff_pred[2], 2)
println("The real slope is $m, and the predicted slope is $slope")
println("The real intercept is $b, and the predicted slope is $intercept")
Run Code Online (Sandbox Code Playgroud)