【问题标题】:Linear Model in JuliaJulia 中的线性模型
【发布时间】:2019-08-01 09:41:33
【问题描述】:

我对使用 Julia 进行数据分析非常陌生。如果我问的是非常琐碎的事情,请耐心等待。我有一个二维数组X,显示为

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]   

我的 Y 显示为

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   

现在如果我尝试将它传递给 sklearn 线性模型,它会给我一个错误

ValueError('Found input variables with inconsistent numbers of samples: [6, 365]',)

搜索错误表明它可能是重塑的问题。建议转置可以正常工作。

当尝试transpose(X)时,错误是这样的,

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`

我什至尝试过 GLM 包,但有一些荒谬的错误

MethodError: no method matching fit(::Type{LinearModel}, ::Array{Array{T,1} where T,1}, ::Array{Union{Missing, Float64},1}, ::Bool)

但是我会得到如图所示的 X 和 Y,我怎样才能成功地拟合回归呢?任何帮助表示赞赏。提前致谢。

【问题讨论】:

    标签: scikit-learn julia


    【解决方案1】:

    您的X不是二维数组或Matrix。正如类型所说,它是一个Array{Array{T,1} where T,1},在其他语言中,例如,它被称为“锯齿状数组”。要将其转换为Matrix,有multiple options,但最短的是使用hcat 和splatting:

    hcat(X...)
    

    尽管如果可能的话,应该避免以这种方式溅射大数组。尝试将X 构造为一个矩阵。

    除此之外,在 Julia 中做线性回归也很短

    hcat(X...) \ Y
    

    没有任何外部库。

    根据@Milan 的评论,reduce(hcat, X) 也很短,并且通过节省编译时间会更快。

    【讨论】:

    • 建议使用 reduce(hcat, X) 而不是 hcat(X...) 以避免为大量参数编译 hcat
    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 2014-11-25
    • 1970-01-01
    • 2016-10-05
    • 1970-01-01
    相关资源
    最近更新 更多