如何在Julia中对DataFrame执行线性回归?

huangapple go评论74阅读模式
英文:

how do i perform linear regression on dataframe in julia

问题

M = Matrix(df[:, 2:3])
X = zeros(size(M[:, 1], 1), 2)
X[:, 1] = M[:, 1] # X is the reference data
X[:, 2] .= 1.0
Y = M[:, 2] # Y is the non reference
coeff_pred = X \ Y # this gives you the slope and the intercept

"MethodError: no method matching oneunit(::Type{Any})"

这是我的数据框:

enter image description here

我认为问题可能与矩阵的元素类型为Any有关,但当我尝试将列解析为int64或float64时,出现了不同的错误。

英文:

`

M = Matrix(df[:, 2:3])
X = zeros(size(M[:, 1], 1), 2)
X[:, 1] = M[:, 1] # X is the reference data
X[:, 2] .= 1.0
Y = M[:, 2] # Y is the non reference
coeff_pred = X \ Y # this gives you the slope and the intercept


MethodError: no method matching oneunit(::Type{Any})

this is my dataframe
enter image description here

I think it has to do with the matrix being of type any but when i try parse the columns as int64 or float64 i get a different error

答案1

得分: 2

如果出错,这意味着你的数据框包含非数字值,你需要首先清理它们。
英文:

Do:

M = Matrix{Float64}(df[:, 2:3])

If this errors it means that your data frame contains non-numeric values and you first need to clean them.

答案2

得分: 1

Bogumil的答案很可能是正确的,请考虑:

ERROR: MethodError: no method matching oneunit(::Type{Any})

2-element Vector{Float64}:
 -3.9999999999999987
  4.499999999999999

我会补充一点:当我看到人们的DataFrame仅包含Any类型的列,而没有明显的原因时,90%的情况是他们使用XLSX.jl读取Excel文件,并未使用infer_eltypes关键字。来自文档的说明:

> 使用 infer_eltypes=true 以将数据作为具有类型的向量的Vector{Any}获取。默认值为 infer_eltypes=false

英文:

Bogumil's answer is likely correct, consider:

julia> Any[1 2; 3 4] \ Any[5, 6]
ERROR: MethodError: no method matching oneunit(::Type{Any})

versus

julia> [1 2; 3 4] \ [5, 6]
2-element Vector{Float64}:
 -3.9999999999999987
  4.499999999999999

I will add one thing: when I see people with DataFrames consisting of only Any typed columns for no apparent reason, it turns out in 90% of all cases they have been reading in an Excel file using the XLSX.jl and not using the infer_eltypes keyword. From the docs:

> Use infer_eltypes=true to get data as a Vector{Any} of typed vectors. The default value is infer_eltypes=false.

huangapple
  • 本文由 发表于 2023年7月13日 19:04:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76678656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定