I have a 2D array X
, shown as
6-element Array{Array{T,1} where T,1}:
[0.962, 0.282, 0.19, 0.533, 2.032, 2.482, 0.863, 1.24, 0.819, 0.927 … 2.161, 0.967, 0.809, 1.22, 1.3, 1.307, 0.945, 1.02, 0.519, 0.837]
[11.0, 8.5625, 6.65, 6.68, 17.0, 11.75, 8.5625, 6.65, 7.54, 8.0 … 6.315, 5.661, 6.189, 6.455, 7.297, 6.7, 7.3, 6.475, 65.601, 6.506]
[59, 59, 59, 61, 52, 59, 61, 60, 66, 68 … 2, 2, 4, 1, 3, 2, 2, 4, 2, 0]
[1, 1, 0, -1, 1, 1, -1, 0, 0, 1 … 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[115.725, -1.0, 111.515, -1.0, 119.467, 111.515, 110.111, 115.725, -1.0, -1.0 … 12.933, 12.933, 12.933, 12.933, 12.933, 12.933, 12.933, 12.933, 12.933, 12.933]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0 … 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
I have a Y shown as
365-element Array{Union{Missing, Float64},1}:
1.33
1.1995
1.029
1.15
3.15
4.0
1.725
1.845
1.445
1.8
1.525
1.17
1.32
⋮
1.32
1.7495
1.9045
1.6999
1.45
1.98
2.08
1.6199
1.36188
1.55
1.28
1.35
Now if I try to pass it to sklearn Linear Model, it gives me an error
ValueError('Found input variables with inconsistent numbers of samples: [6, 365]',)
Searching the error shows it might be a problem of reshaping. It is suggested that transpose can work fine.
When trying to transpose(X)
, the error is like,
Element type mismatch. Tried to create a `Transpose{LinearAlgebra.Transpose}` from an object with eltype `Array{T,1} where T`, but the element type of the transpose of an object with eltype `Array{T,1} where T` must be `LinearAlgebra.Transpose{_1,_2} where _2 where _1`
I even tried the GLM package but there are some absurd errors
MethodError: no method matching fit(::Type{LinearModel}, ::Array{Array{T,1} where T,1}, ::Array{Union{Missing, Float64},1}, ::Bool)
But I will have the X and Y as shown, how can I successfully fit a regression on it?
Your X
is not a 2D array or Matrix
. It is, as the type says, an Array{Array{T,1} where T,1}
, which in other languages is, e.g., called a "jagged array". To convert this into a Matrix
, there are multiple options, but the shortest one is to use hcat
and splatting:
hcat(X...)
Although splatting large arrays that way should be avoided, if possible. Try to construct X
already as a matrix.
Aside from that, just doing linear regression in Julia is as short as
hcat(X...) \ Y
without any external libraries.
As per @Milan's comment, reduce(hcat, X)
is short as well and will be faster by saving compile time.