英文:
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})"
这是我的数据框:
我认为问题可能与矩阵的元素类型为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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论