将julia中的位数组{2}转换为数组{Bool,1}
因此,正如标题所解释的: 有没有办法将将julia中的位数组{2}转换为数组{Bool,1},julia,Julia,因此,正如标题所解释的: 有没有办法将位数组{2}类型数组转换为数组{Bool,1}:类型数组 PD:问题的核心是,我试图用位数组{2}对数组{float64,2}求值,我不断得到以下错误: 错误:边界错误:尝试访问索引[Base.Slice(Base.OneTo(20))、Base.LogicalIndex(Bool[0;0;…;0;0])、Base.Slice(Base.OneTo(1))]处的20×1数组{Float64,2} 提前谢谢 @石匠 编辑: 因此,我一直在尝试分离数组{Floa
位数组{2}
类型数组转换为数组{Bool,1}:
类型数组
PD:问题的核心是,我试图用位数组{2}
对数组{float64,2}
求值,我不断得到以下错误:
错误:边界错误:尝试访问索引[Base.Slice(Base.OneTo(20))、Base.LogicalIndex(Bool[0;0;…;0;0])、Base.Slice(Base.OneTo(1))]处的20×1数组{Float64,2}
提前谢谢
@石匠
编辑:
因此,我一直在尝试分离数组{Float64,2}
中存在的异常值。我在网上找到了这个,并对它们进行了编辑,以允许Array{float64,2}
数组作为输入
function idealf(x::Array{Float64,2})
y = sort(x,dims=1);
n = length(x);
j = floor(Int64, n/4+5/12) # 25%ile is in [y[j], y[j+1]]
k = n-j+1 # 75%ile is in [y[k],y[k-1]]
g = n/4+5/12 - j # weighting for the two data surrounding quartiles.
(1-g).*y[j]+g.*y[j+1], (1-g).*y[k]+g.*y[k-1]
end
function outbox(x::Array{Float64,2}; mbox::Bool=false, gval::Real=NaN, method::Bool=true)
n = length(x);
lower_quartile, upper_quartile = idealf(x)
IQR = upper_quartile-lower_quartile
cl = cu = 0.0
if mbox
if isnan(gval)
gval=(17.63*n-23.64)/(7.74*n-3.71)
end
cl = median(x) - gval*IQR
cu = median(x) + gval*IQR
elseif !mbox
if isnan(gval)
gval=1.5
end
cl = lower_quartile - gval*IQR
cu = upper_quartile + gval*IQR
end
flag = (x.<cl) .| (x.>cu)
flag=collect(flag) # tunring my BitArray{2} to a Array{float64,2}
vec = collect(1:n);
outid = vec[flag,:] # TROUBLE HERE
keepid = vec[.!flag] # TROUBLE HERE
outval = x[flag]
nout = length(outid)
if method && !mbox
METHOD = "Outlier detection method using \nthe ideal-fourths based boxplot rule\n"
elseif method && mbox
METHOD = "Outlier detection method using \nthe ideal-fourths based boxplot rule\n(using the modification suggested by Carling (2000))\n"
else
METHOD = nothing
end
return outid, keepid, outval, nout;
end
a=rand(20,1)*10; a[10]=1000.0;a[2]=2000.0;
outbox(a)
要回答您的问题,您可以使用
collect
将BitArray
转换为Array
,并使用vec
将矩阵转换为向量:
julia> BA = BitArray(rand(Bool, 3, 3))
3×3 BitArray{2}:
0 1 0
1 0 1
1 0 0
julia> collect(BA)
3×3 Array{Bool,2}:
0 1 0
1 0 1
1 0 0
julia> (vec ∘ collect)(BA)
9-element Array{Bool,1}:
0
1
1
1
0
0
0
1
0
但是看看你的“PD”,我怀疑这并不能解决你的根本问题,但你没有提供足够的信息让我来帮助你
编辑以响应OP的编辑: 问题是,您试图用大小为
(20,1)
的位矩阵对向量
进行逻辑索引。与Matlab不同,Julia不会自动将单列矩阵视为向量。这有很好的理由,但我知道这可能是令人惊讶的
您应该使用dropdims(flag,dims=2)
将flag
转换为对逻辑索引有效的位向量,而不是对位矩阵使用collect
这是您的原始代码,但带有建议的修复程序:
#+BEGIN_SRC julia
function idealf(x::Array{Float64,2})
y = sort(x,dims=1);
n = length(x);
j = floor(Int64, n/4+5/12) # 25%ile is in [y[j], y[j+1]]
k = n-j+1 # 75%ile is in [y[k],y[k-1]]
g = n/4+5/12 - j # weighting for the two data surrounding quartiles.
(1-g).*y[j]+g.*y[j+1], (1-g).*y[k]+g.*y[k-1]
end
function outbox(x::Array{Float64,2}; mbox::Bool=false, gval::Real=NaN, method::Bool=true)
n = length(x);
lower_quartile, upper_quartile = idealf(x)
IQR = upper_quartile-lower_quartile
cl = cu = 0.0
if mbox
if isnan(gval)
gval=(17.63*n-23.64)/(7.74*n-3.71)
end
cl = median(x) - gval*IQR
cu = median(x) + gval*IQR
elseif !mbox
if isnan(gval)
gval=1.5
end
cl = lower_quartile - gval*IQR
cu = upper_quartile + gval*IQR
end
flag = dropdims((x.<cl) .| (x.>cu), dims=2)
vec = collect(1:n)
outid = vec[flag]
keepid = vec[.!flag]
outval = x[flag]
nout = length(outid)
if method && !mbox
METHOD = "Outlier detection method using \nthe ideal-fourths based boxplot rule\n"
elseif method && mbox
METHOD = "Outlier detection method using \nthe ideal-fourths based boxplot rule\n(using the modification suggested by Carling (2000))\n"
else
METHOD = nothing
end
return outid, keepid, outval, nout;
end
a=rand(20,1)*10; a[10]=1000.0;a[2]=2000.0;
outbox(a)
#+END_SRC
#+RESULTS:
: ([2, 10], [1, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], [2000.0, 1000.0], 2)
#+开始
函数idealf(x::数组{Float64,2})
y=排序(x,dims=1);
n=长度(x);
j=楼层(Int64,n/4+5/12)#25%的ile位于[y[j],y[j+1]]
k=n-j+1#75%的ile在[y[k],y[k-1]]
g=n/4+5/12——四分位数周围两个数据的加权。
(1-g)。*y[j]+g.*y[j+1],(1-g)。*y[k]+g.*y[k-1]
结束
函数发件箱(x::数组{Float64,2};mbox::Bool=false,gval::Real=NaN,method::Bool=true)
n=长度(x);
下四分位数,上四分位数=idealf(x)
IQR=上四分位数-下四分位数
cl=cu=0.0
如果mbox
如果是isnan(gval)
gval=(17.63*n-23.64)/(7.74*n-3.71)
结束
cl=中值(x)-gval*IQR
cu=中值(x)+gval*IQR
埃尔塞夫!mbox
如果是isnan(gval)
gval=1.5
结束
cl=下四分位-gval*IQR
cu=上四分位数+gval*IQR
结束
标志=落差dims((x.cu),dims=2)
vec=收集(1:n)
outid=vec[标志]
keepid=vec[.!flag]
outval=x[标志]
nout=长度(outid)
如果方法&!mbox
METHOD=“使用基于理想四分之一的箱线图规则的异常值检测方法\n”
elseif方法和mbox
METHOD=“使用基于理想四分之一的箱线图规则的异常值检测方法\n(使用Carling(2000)建议的修改)\n”
其他的
方法=无
结束
返回outid、keepid、outval、nout;
结束
a=兰特(20,1)*10;a[10]=1000.0;a[2]=2000.0;
发件箱(a)
#+结束(SRC)
#+结果:
: ([2, 10], [1, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], [2000.0, 1000.0], 2)
,谢谢你的回答。请注意,我已经编辑了这篇文章,以提供更多关于我的问题的信息!美好的正是我需要的。太多了!请尝试编辑此文件,以生成一个最小的工作示例,而不是转储整个生产代码。这样,这些问题及其答案也可以对其他人有用@PrzemyslawSzufel好的,所以我编辑了代码块,这样它就可以粘贴到终端(包括运行它的玩具数据)中,并且它会重现错误。不需要任何包(只需在终端中打开Julia并粘贴此包)。希望这足够了!谢谢
#+BEGIN_SRC julia
function idealf(x::Array{Float64,2})
y = sort(x,dims=1);
n = length(x);
j = floor(Int64, n/4+5/12) # 25%ile is in [y[j], y[j+1]]
k = n-j+1 # 75%ile is in [y[k],y[k-1]]
g = n/4+5/12 - j # weighting for the two data surrounding quartiles.
(1-g).*y[j]+g.*y[j+1], (1-g).*y[k]+g.*y[k-1]
end
function outbox(x::Array{Float64,2}; mbox::Bool=false, gval::Real=NaN, method::Bool=true)
n = length(x);
lower_quartile, upper_quartile = idealf(x)
IQR = upper_quartile-lower_quartile
cl = cu = 0.0
if mbox
if isnan(gval)
gval=(17.63*n-23.64)/(7.74*n-3.71)
end
cl = median(x) - gval*IQR
cu = median(x) + gval*IQR
elseif !mbox
if isnan(gval)
gval=1.5
end
cl = lower_quartile - gval*IQR
cu = upper_quartile + gval*IQR
end
flag = dropdims((x.<cl) .| (x.>cu), dims=2)
vec = collect(1:n)
outid = vec[flag]
keepid = vec[.!flag]
outval = x[flag]
nout = length(outid)
if method && !mbox
METHOD = "Outlier detection method using \nthe ideal-fourths based boxplot rule\n"
elseif method && mbox
METHOD = "Outlier detection method using \nthe ideal-fourths based boxplot rule\n(using the modification suggested by Carling (2000))\n"
else
METHOD = nothing
end
return outid, keepid, outval, nout;
end
a=rand(20,1)*10; a[10]=1000.0;a[2]=2000.0;
outbox(a)
#+END_SRC
#+RESULTS:
: ([2, 10], [1, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], [2000.0, 1000.0], 2)