0
votes

I am new to Julia, so please forgive me if this is too basic. I am trying to run the following script.

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)

When I run I get the following error:

ERROR: LoadError: MethodError: no method matching *(::Array{Float64,1}, ::Array{Union{Missing, Float64},1}) Closest candidates are: *(::Any, ::Any, !Matched::Any, !Matched::Any...) at operators.jl:502 *(!Matched::LinearAlgebra.Adjoint{#s571,#s570} where #s570<:Union{DenseArray{T<:Union{Complex{Float32}, Complex{Float64}, Float32, Float64},2}, ReinterpretArray{T<:Union{Complex{Float32}, Complex{Float64}, Float32, Float64},2,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Tuple{AbstractUnitRange,Vararg{Any,N} where N} where A<:DenseArray where N where T, DenseArray}, ReshapedArray{T<:Union{Complex{Float32}, Complex{Float64}, Float32, Float64},2,A,MI} where MI<:Tuple{Vararg{SignedMultiplicativeInverse{Int64},N} where N} where A<:Union{ReinterpretArray{T,N,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Tuple{AbstractUnitRange,Vararg{Any,N} where N} where A<:DenseArray where N where T, DenseArray} where N where T, SubArray{T,N,A,I,true} where I<:Tuple{AbstractUnitRange,Vararg{Any,N} where N} where A<:DenseArray where N where T, DenseArray}, SubArray{T<:Union{Complex{Float32}, Complex{Float64}, Float32, Float64},2,A,I,L} where L where I<:Tuple{Vararg{Union{Int64, AbstractRange{Int64}, AbstractCartesianIndex},N} where N} where A<:Union{ReinterpretArray{T,N,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Tuple{AbstractUnitRange,Vararg{Any,N} where N} where A<:DenseArray where N where T, DenseArray} where N where T, ReshapedArray{T,N,A,MI} where MI<:Tuple{Vararg{SignedMultiplicativeInverse{Int64},N} where N} where A<:Union{ReinterpretArray{T,N,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Tuple{AbstractUnitRange,Vararg{Any,N} where N} where A<:DenseArray where N where T, DenseArray} where N where T, SubArray{T,N,A,I,true} where I<:Tuple{AbstractUnitRange,Vararg{Any,N} where N} where A<:DenseArray where N where T, DenseArray} where N where T, DenseArray}} where #s571, ::Union{DenseArray{S,1}, ReinterpretArray{S,1,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Tuple{AbstractUnitRange,Vararg{Any,N} where N} where A<:DenseArray where N where T, DenseArray}, ReshapedArray{S,1,A,MI} where MI<:Tuple{Vararg{SignedMultiplicativeInverse{Int64},N} where N} where A<:Union{ReinterpretArray{T,N,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Tuple{AbstractUnitRange,Vararg{Any,N} where N} where A<:DenseArray where N where T, DenseArray} where N where T, SubArray{T,N,A,I,true} where I<:Tuple{AbstractUnitRange,Vararg{Any,N} where N} where A<:DenseArray where N where T, DenseArray}, SubArray{S,1,A,I,L} where L where I<:Tuple{Vararg{Union{Int64, AbstractRange{Int64}, AbstractCartesianIndex},N} where N} where A<:Union{ReinterpretArray{T,N,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Tuple{AbstractUnitRange,Vararg{Any,N} where N} where A<:DenseArray where N where T, DenseArray} where N where T, ReshapedArray{T,N,A,MI} where MI<:Tuple{Vararg{SignedMultiplicativeInverse{Int64},N} where N} where A<:Union{ReinterpretArray{T,N,S,A} where S where A<:Union{SubArray{T,N,A,I,true} where I<:Tuple{AbstractUnitRange,Vararg{Any,N} where N} where A<:DenseArray where N where T, DenseArray} where N where T, SubArray{T,N,A,I,true} where I<:Tuple{AbstractUnitRange,Vararg{Any,N} where N} where A<:DenseArray where N where T, DenseArray} where N where T, DenseArray}}) where {T<:Union{Complex{Float32}, Complex{Float64}, Float32, Float64}, S} at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.0/LinearAlgebra/src/matmul.jl:98 *(!Matched::LinearAlgebra.Adjoint{#s571,#s570} where #s570<:LinearAlgebra.AbstractTriangular where #s571, ::AbstractArray{T,1} where T) at /Users/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) at /Users/wj/Desktop/Julia/NLSR.jl:16

2 top-level scope at none:0

[3] include at ./boot.jl:317 [inlined]

[4] include_relative(::Module, ::String) at ./loading.jl:1044

[5] include(::Module, ::String) at ./sysimg.jl:29

[6] exec_options(::Base.JLOptions) at ./client.jl:231

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

How can I successfully run this script?

Remark: The algorithm pseudo code is as followsenter image description here:

Script updated after the comment by Warren

The code works now!

1

1 Answers

1
votes

I'm not sure what you're trying to do here, but it doesn't look anything like least squares to me.

Having said that, the problem in your script is in this line here:

w = w + (err * x1)

x1 and err will both be vectors, and there is no multiplication operator method for two vectors. For example, rand(2) * rand(2) will also error. This is because usage of * is ambiguous in this situation.

If you want the dot product, use dot(x1, err).

If you want the element-wise product, use x1 .* err, which broadcasts * across the elements of the inputs.

There are several other issues with your code, but I'm hesitant to try and correct them, since, as stated, I'm not really sure what you're trying to do.