我正在尝试翻译在不导入numpy的情况下,将X数组中的NaN行以及Y中的相应行从Python移至Julia 0.5.0.我可以复制删除NaN"部分:
I am trying to translate the first answer in Remove NaN row from X array and also the corresponding row in Y from Python to Julia 0.5.0 without importing numpy. I can replicate the "removing the NaNs" part with:
x1 = x[!isnan(x)]但是只能使用它将2D数组减小到1D,我不希望那样.在这种情况下,numpy.any的Julia等效值是什么?或者,如果没有等效项,该如何保持数组2D并删除包含NaN的整个行?
but only using that reduces the 2D array down to 1D, and I don't want that. What would be the Julia equivalent of numpy.any in this case? Or if there isn't an equivalent, how can I keep my array 2D and delete entire rows that contain NaNs?
推荐答案您可以使用 any :
You can find rows that contain a NaN entry with any:
julia> A = rand(5, 4) A[rand(1:end, 4)] = NaN A 5×4 Array{Float64,2}: 0.951717 0.0248771 0.903009 0.529702 0.702505 NaN 0.730396 0.785191 NaN 0.390453 0.838332 NaN 0.213665 NaN 0.178303 0.0100249 0.124465 0.363872 0.434887 0.305722 julia> nanrows = any(isnan(A), 2) # 2 means that we reduce over the second dimension 5×1 Array{Bool,2}: false true true true false然后,您可以将返回的逻辑数组用作第一维的掩码,但我们需要首先使其成为一维:
Then you can use the returned logical array as a mask into the first dimension, but we need to make it one-dimensional first:
julia> A[!vec(nanrows), :] 2×4 Array{Float64,2}: 0.951717 0.0248771 0.903009 0.529702 0.124465 0.363872 0.434887 0.305722更多推荐
从Julia的多维数组中删除带有NaN的整行?
发布评论