在Julia

时间:2018-11-29 02:17:28

标签: julia least-squares

我是Julia的新手,所以如果这太简单了,请原谅我。我正在尝试运行以下脚本。

using Pkg
Pkg.add("DataFrames")
using DataFrames

function LS(x,y,a)
    T = size(x,1)
    N = size(x,2)
    pred = fill(0.0,T)
    w= fill(0.0,N)
    for t = 1:T
        x1 = x[t,:]
        pred[t] = transpose(w) * x1
        err = a*(y[t]-pred[t])
        w = w + (err * x1)
    end
    return pred
end

input = readtable("input.csv")
output = readtable("label.csv")
en = convert(Array, input)
out = convert(Array, output)
a = 0.5
prediction = LS(en,out,a)
print(prediction)

我跑步时出现以下错误:

  

错误:LoadError:MethodError:没有方法匹配   *(:: Array {Float64,1},:: Array {Union {Missing,Float64},1})最近的候选者是:*(:: Any,:: Any,!Matched :: Any,!Matched :: Any ...) 在   operator.jl:502 *(!Matched :: LinearAlgebra.Adjoint {#s571,#s570}   其中#s570 <:Union {DenseArray {T <:Union {Complex {Float32},   复杂{Float64},Float32,Float64},2},   ReinterpretArray {T <:Union {Complex {Float32},Complex {Float64},Float32,   Float64},2,S,A}其中S其中A <:Union {SubArray {T,N,A,I,true}其中   I <:Tuple {AbstractUnitRange,Vararg {Any,N},其中N},其中A <:DenseArray   其中N,其中T,DenseArray},ReshapedArray {T <:Union {Complex {Float32},   Complex {Float64},Float32,Float64},2,A,MI}其中   MI <:Tuple {Vararg {SignedMultiplicativeInverse {Int64},N}其中N}   A <:Union {ReinterpretArray {T,N,S,A}其中S在哪里   A <:Union {SubArray {T,N,A,I,true}其中   I <:Tuple {AbstractUnitRange,Vararg {Any,N},其中N},其中A <:DenseArray   其中N其中T,DenseArray}其中N其中T,SubArray {T,N,A,I,true}   其中I <:Tuple {AbstractUnitRange,Vararg {Any,N}其中N}   A <:DenseArray,其中N,其中T,DenseArray},   SubArray {T <:Union {Complex {Float32},Complex {Float64},Float32,   Float64},2,A,I,L}其中L其中I <:Tuple {Vararg {Union {Int64,   AbstractRange {Int64},AbstractCartesianIndex},N}其中N}其中   A <:Union {ReinterpretArray {T,N,S,A}其中S在哪里   A <:Union {SubArray {T,N,A,I,true}其中   I <:Tuple {AbstractUnitRange,Vararg {Any,N},其中N},其中A <:DenseArray   其中N其中T,DenseArray}其中N其中T,ReshapedArray {T,N,A,MI}   其中MI <:Tuple {Vararg {SignedMultiplicativeInverse {Int64},N},其中N}   其中A <:Union {ReinterpretArray {T,N,S,A}其中S   A <:Union {SubArray {T,N,A,I,true}其中   I <:Tuple {AbstractUnitRange,Vararg {Any,N},其中N},其中A <:DenseArray   其中N其中T,DenseArray}其中N其中T,SubArray {T,N,A,I,true}   其中I <:Tuple {AbstractUnitRange,Vararg {Any,N}其中N}   A <:DenseArray,其中N在T,​​DenseArray}其中N在T,   DenseArray}},其中#s571,:: Union {DenseArray {S,1},   ReinterpretArray {S,1,S,A}其中S在哪里   A <:Union {SubArray {T,N,A,I,true}其中   I <:Tuple {AbstractUnitRange,Vararg {Any,N},其中N},其中A <:DenseArray   哪里N哪里T,DenseArray},ReshapedArray {S,1,A,MI}哪里   MI <:Tuple {Vararg {SignedMultiplicativeInverse {Int64},N}其中N}   A <:Union {ReinterpretArray {T,N,S,A}其中S在哪里   A <:Union {SubArray {T,N,A,I,true}其中   I <:Tuple {AbstractUnitRange,Vararg {Any,N},其中N},其中A <:DenseArray   其中N其中T,DenseArray}其中N其中T,SubArray {T,N,A,I,true}   其中I <:Tuple {AbstractUnitRange,Vararg {Any,N}其中N}   A <:DenseArray,其中N,其中T,DenseArray},SubArray {S,1,A,I,L}其中   L其中I <:Tuple {Vararg {Union {Int64,AbstractRange {Int64},   AbstractCartesianIndex},N},其中N},其中   A <:Union {ReinterpretArray {T,N,S,A}其中S在哪里   A <:Union {SubArray {T,N,A,I,true}其中   I <:Tuple {AbstractUnitRange,Vararg {Any,N},其中N},其中A <:DenseArray   其中N其中T,DenseArray}其中N其中T,ReshapedArray {T,N,A,MI}   其中MI <:Tuple {Vararg {SignedMultiplicativeInverse {Int64},N},其中N}   其中A <:Union {ReinterpretArray {T,N,S,A}其中S   A <:Union {SubArray {T,N,A,I,true}其中   I <:Tuple {AbstractUnitRange,Vararg {Any,N},其中N},其中A <:DenseArray   其中N其中T,DenseArray}其中N其中T,SubArray {T,N,A,I,true}   其中I <:Tuple {AbstractUnitRange,Vararg {Any,N}其中N}   A <:DenseArray,其中N在T,​​DenseArray}其中N在T,   DenseArray}}),其中{T <:Union {Complex {Float32},Complex {Float64},   Float32,Float64},S}在   /用户/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/LinearAlgebra/src/matmul.jl:98   *(!Matched :: LinearAlgebra.Adjoint {#s571,#s570}其中#s570 <:LinearAlgebra.AbstractTriangular其中#s571,:: AbstractArray {T,1}其中T)在   /用户/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/LinearAlgebra/src/triangular.jl:1805   ...

     

Stacktrace:2 LS(:: Array {Union {Missing,Float64},2},   :: Array {Union {Missing,Float64},2},:: Float64)在   /Users/wj/Desktop/Julia/NLSR.jl:16

     

2顶级范围,无:0

     

[3]包含于./boot.jl:317 [内联]

     

[4] include.relative(:: Module,:: String)在./loading.jl:1044

     

[5]在./sysimg.jl:29处包含(:: Module,:: String)

     

[6] exec_options(:: Base.JLOptions),位于./client.jl:231

     

[7] _start()在./client.jl:425

如何成功运行此脚本?

备注: 算法伪代码为follows enter image description here

沃伦发表评论后更新了脚本

代码现在可以使用!

1 个答案:

答案 0 :(得分:0)

我不确定您要在这里做什么,但是对我来说,它看起来至少不是最小平方。

话虽如此,脚本中的问题在此行中:

w = w + (err * x1)

x1err都是向量,并且两个向量没有乘法运算符方法。例如,rand(2) * rand(2)也将出错。这是因为在这种情况下*的使用是不明确的。

如果要点积,请使用dot(x1, err)

如果要使用按元素的乘积,请使用x1 .* err,它会在输入的元素之间广播*

您的代码还有其他几个问题,但是我犹豫要尝试纠正它们,因为如上所述,我不确定您要做什么。